Skip to content

Commit af6d69a

Browse files
authored
Add "fees" and "liveness" into the smartTransactionsState, update version (#41)
* Add "fees" and "liveness" into the smartTransactionsState, update version * Fix UTs
1 parent 4471915 commit af6d69a

File tree

4 files changed

+44
-15
lines changed

4 files changed

+44
-15
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@metamask/smart-transactions-controller",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"description": "MetaMask controller for Smart Transactions.",
55
"repository": {
66
"type": "git",

src/SmartTransactionsController.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ describe('SmartTransactionsController', () => {
249249
[CHAIN_IDS.ETHEREUM]: [],
250250
},
251251
userOptIn: undefined,
252+
fees: undefined,
253+
liveness: true,
252254
},
253255
});
254256
});
@@ -439,6 +441,8 @@ describe('SmartTransactionsController', () => {
439441
[CHAIN_IDS.ETHEREUM]: [pendingTransaction],
440442
},
441443
userOptIn: undefined,
444+
fees: undefined,
445+
liveness: true,
442446
},
443447
});
444448
});
@@ -470,6 +474,8 @@ describe('SmartTransactionsController', () => {
470474
],
471475
},
472476
userOptIn: undefined,
477+
fees: undefined,
478+
liveness: true,
473479
},
474480
});
475481
});

src/SmartTransactionsController.ts

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
UnsignedTransaction,
1818
SmartTransactionsStatus,
1919
SmartTransactionStatuses,
20-
Fee,
20+
Fees,
2121
} from './types';
2222
import {
2323
getAPIRequestURL,
@@ -51,6 +51,8 @@ export interface SmartTransactionsControllerState extends BaseState {
5151
smartTransactionsState: {
5252
smartTransactions: Record<string, SmartTransaction[]>;
5353
userOptIn: boolean | undefined;
54+
liveness: boolean | undefined;
55+
fees: Fees | undefined;
5456
};
5557
}
5658

@@ -118,6 +120,8 @@ export default class SmartTransactionsController extends BaseController<
118120
smartTransactionsState: {
119121
smartTransactions: {},
120122
userOptIn: undefined,
123+
fees: undefined,
124+
liveness: true,
121125
},
122126
};
123127

@@ -446,15 +450,7 @@ export default class SmartTransactionsController extends BaseController<
446450
};
447451
}
448452

449-
async getFees(
450-
unsignedTransaction: UnsignedTransaction,
451-
): Promise<{
452-
fees: Fee[];
453-
cancelFees: Fee[];
454-
feeEstimate: number;
455-
gasLimit: number;
456-
gasUsed: number;
457-
}> {
453+
async getFees(unsignedTransaction: UnsignedTransaction): Promise<Fees> {
458454
const { chainId } = this.config;
459455

460456
const unsignedTransactionWithNonce = await this.addNonceToTransaction(
@@ -464,6 +460,12 @@ export default class SmartTransactionsController extends BaseController<
464460
method: 'POST',
465461
body: JSON.stringify({ tx: unsignedTransactionWithNonce }),
466462
});
463+
this.update({
464+
smartTransactionsState: {
465+
...this.state.smartTransactionsState,
466+
fees: data,
467+
},
468+
});
467469

468470
return data;
469471
}
@@ -549,10 +551,23 @@ export default class SmartTransactionsController extends BaseController<
549551

550552
async fetchLiveness(): Promise<boolean> {
551553
const { chainId } = this.config;
552-
const response = await this.fetch(
553-
getAPIRequestURL(APIType.LIVENESS, chainId),
554-
);
555-
return Boolean(response.lastBlock);
554+
let liveness = false;
555+
try {
556+
const response = await this.fetch(
557+
getAPIRequestURL(APIType.LIVENESS, chainId),
558+
);
559+
liveness = Boolean(response.lastBlock);
560+
} catch (e) {
561+
console.log('"fetchLiveness" API call failed');
562+
}
563+
564+
this.update({
565+
smartTransactionsState: {
566+
...this.state.smartTransactionsState,
567+
liveness,
568+
},
569+
});
570+
return liveness;
556571
}
557572

558573
async setStatusRefreshInterval(interval: number): Promise<void> {

src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ export interface Fee {
9090
maxPriorityFeePerGas: number;
9191
}
9292

93+
export interface Fees {
94+
fees: Fee[];
95+
cancelFees: Fee[];
96+
feeEstimate: number;
97+
gasLimit: number;
98+
gasUsed: number;
99+
}
100+
93101
// TODO: maybe grab the type from transactions controller?
94102
export type UnsignedTransaction = any;
95103

0 commit comments

Comments
 (0)