Skip to content

Commit

Permalink
Add "fees" and "liveness" into the smartTransactionsState, update ver…
Browse files Browse the repository at this point in the history
…sion (#41)

* Add "fees" and "liveness" into the smartTransactionsState, update version

* Fix UTs
  • Loading branch information
dan437 authored Jan 19, 2022
1 parent 4471915 commit af6d69a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@metamask/smart-transactions-controller",
"version": "1.4.0",
"version": "1.4.1",
"description": "MetaMask controller for Smart Transactions.",
"repository": {
"type": "git",
Expand Down
6 changes: 6 additions & 0 deletions src/SmartTransactionsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ describe('SmartTransactionsController', () => {
[CHAIN_IDS.ETHEREUM]: [],
},
userOptIn: undefined,
fees: undefined,
liveness: true,
},
});
});
Expand Down Expand Up @@ -439,6 +441,8 @@ describe('SmartTransactionsController', () => {
[CHAIN_IDS.ETHEREUM]: [pendingTransaction],
},
userOptIn: undefined,
fees: undefined,
liveness: true,
},
});
});
Expand Down Expand Up @@ -470,6 +474,8 @@ describe('SmartTransactionsController', () => {
],
},
userOptIn: undefined,
fees: undefined,
liveness: true,
},
});
});
Expand Down
43 changes: 29 additions & 14 deletions src/SmartTransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
UnsignedTransaction,
SmartTransactionsStatus,
SmartTransactionStatuses,
Fee,
Fees,
} from './types';
import {
getAPIRequestURL,
Expand Down Expand Up @@ -51,6 +51,8 @@ export interface SmartTransactionsControllerState extends BaseState {
smartTransactionsState: {
smartTransactions: Record<string, SmartTransaction[]>;
userOptIn: boolean | undefined;
liveness: boolean | undefined;
fees: Fees | undefined;
};
}

Expand Down Expand Up @@ -118,6 +120,8 @@ export default class SmartTransactionsController extends BaseController<
smartTransactionsState: {
smartTransactions: {},
userOptIn: undefined,
fees: undefined,
liveness: true,
},
};

Expand Down Expand Up @@ -446,15 +450,7 @@ export default class SmartTransactionsController extends BaseController<
};
}

async getFees(
unsignedTransaction: UnsignedTransaction,
): Promise<{
fees: Fee[];
cancelFees: Fee[];
feeEstimate: number;
gasLimit: number;
gasUsed: number;
}> {
async getFees(unsignedTransaction: UnsignedTransaction): Promise<Fees> {
const { chainId } = this.config;

const unsignedTransactionWithNonce = await this.addNonceToTransaction(
Expand All @@ -464,6 +460,12 @@ export default class SmartTransactionsController extends BaseController<
method: 'POST',
body: JSON.stringify({ tx: unsignedTransactionWithNonce }),
});
this.update({
smartTransactionsState: {
...this.state.smartTransactionsState,
fees: data,
},
});

return data;
}
Expand Down Expand Up @@ -549,10 +551,23 @@ export default class SmartTransactionsController extends BaseController<

async fetchLiveness(): Promise<boolean> {
const { chainId } = this.config;
const response = await this.fetch(
getAPIRequestURL(APIType.LIVENESS, chainId),
);
return Boolean(response.lastBlock);
let liveness = false;
try {
const response = await this.fetch(
getAPIRequestURL(APIType.LIVENESS, chainId),
);
liveness = Boolean(response.lastBlock);
} catch (e) {
console.log('"fetchLiveness" API call failed');
}

this.update({
smartTransactionsState: {
...this.state.smartTransactionsState,
liveness,
},
});
return liveness;
}

async setStatusRefreshInterval(interval: number): Promise<void> {
Expand Down
8 changes: 8 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,14 @@ export interface Fee {
maxPriorityFeePerGas: number;
}

export interface Fees {
fees: Fee[];
cancelFees: Fee[];
feeEstimate: number;
gasLimit: number;
gasUsed: number;
}

// TODO: maybe grab the type from transactions controller?
export type UnsignedTransaction = any;

Expand Down

0 comments on commit af6d69a

Please sign in to comment.