Skip to content

Commit ee73b8d

Browse files
authored
"estimateGas" -> "getFees", support a new cancellation reason, refactoring (#69)
* Add support for a new cancellation reason: PREVIOUS_TX_CANCELLED * Get approval and trade fees within one API call * Remove code related to "estimatedGas", update UTs for "getFees", refactoring * Renaming * If there is an approval tx, the trade tx's nonce is increased by 1
1 parent 62dae0d commit ee73b8d

5 files changed

+198
-153
lines changed

src/SmartTransactionsController.test.ts

Lines changed: 116 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -47,59 +47,104 @@ const createUnsignedTransaction = () => {
4747

4848
const createGetFeesApiResponse = () => {
4949
return {
50-
cancelFees: [
51-
{ maxFeePerGas: 2100001000, maxPriorityFeePerGas: 466503987 },
52-
{ maxFeePerGas: 2310003200, maxPriorityFeePerGas: 513154852 },
53-
{ maxFeePerGas: 2541005830, maxPriorityFeePerGas: 564470851 },
54-
{ maxFeePerGas: 2795108954, maxPriorityFeePerGas: 620918500 },
55-
{ maxFeePerGas: 3074622644, maxPriorityFeePerGas: 683010971 },
56-
{ maxFeePerGas: 3382087983, maxPriorityFeePerGas: 751312751 },
57-
{ maxFeePerGas: 3720300164, maxPriorityFeePerGas: 826444778 },
58-
{ maxFeePerGas: 4092333900, maxPriorityFeePerGas: 909090082 },
59-
{ maxFeePerGas: 4501571383, maxPriorityFeePerGas: 1000000000 },
60-
{ maxFeePerGas: 4951733023, maxPriorityFeePerGas: 1100001000 },
61-
{ maxFeePerGas: 5446911277, maxPriorityFeePerGas: 1210002200 },
62-
{ maxFeePerGas: 5991607851, maxPriorityFeePerGas: 1331003630 },
63-
{ maxFeePerGas: 6590774628, maxPriorityFeePerGas: 1464105324 },
64-
{ maxFeePerGas: 7249858682, maxPriorityFeePerGas: 1610517320 },
65-
{ maxFeePerGas: 7974851800, maxPriorityFeePerGas: 1771570663 },
66-
{ maxFeePerGas: 8772344955, maxPriorityFeePerGas: 1948729500 },
67-
{ maxFeePerGas: 9649588222, maxPriorityFeePerGas: 2143604399 },
68-
{ maxFeePerGas: 10614556694, maxPriorityFeePerGas: 2357966983 },
69-
{ maxFeePerGas: 11676022978, maxPriorityFeePerGas: 2593766039 },
70-
],
71-
feeEstimate: 42000000000000,
72-
fees: [
73-
{ maxFeePerGas: 2310003200, maxPriorityFeePerGas: 513154852 },
74-
{ maxFeePerGas: 2541005830, maxPriorityFeePerGas: 564470850 },
75-
{ maxFeePerGas: 2795108954, maxPriorityFeePerGas: 620918500 },
76-
{ maxFeePerGas: 3074622644, maxPriorityFeePerGas: 683010970 },
77-
{ maxFeePerGas: 3382087983, maxPriorityFeePerGas: 751312751 },
78-
{ maxFeePerGas: 3720300163, maxPriorityFeePerGas: 826444777 },
79-
{ maxFeePerGas: 4092333900, maxPriorityFeePerGas: 909090082 },
80-
{ maxFeePerGas: 4501571382, maxPriorityFeePerGas: 999999999 },
81-
{ maxFeePerGas: 4951733022, maxPriorityFeePerGas: 1100001000 },
82-
{ maxFeePerGas: 5446911277, maxPriorityFeePerGas: 1210002200 },
83-
{ maxFeePerGas: 5991607851, maxPriorityFeePerGas: 1331003630 },
84-
{ maxFeePerGas: 6590774627, maxPriorityFeePerGas: 1464105324 },
85-
{ maxFeePerGas: 7249858681, maxPriorityFeePerGas: 1610517320 },
86-
{ maxFeePerGas: 7974851800, maxPriorityFeePerGas: 1771570662 },
87-
{ maxFeePerGas: 8772344954, maxPriorityFeePerGas: 1948729500 },
88-
{ maxFeePerGas: 9649588222, maxPriorityFeePerGas: 2143604398 },
89-
{ maxFeePerGas: 10614556693, maxPriorityFeePerGas: 2357966982 },
90-
{ maxFeePerGas: 11676022977, maxPriorityFeePerGas: 2593766039 },
91-
{ maxFeePerGas: 12843636951, maxPriorityFeePerGas: 2853145236 },
50+
txs: [
51+
{
52+
// Approval tx.
53+
cancelFees: [
54+
{ maxFeePerGas: 2100001000, maxPriorityFeePerGas: 466503987 },
55+
{ maxFeePerGas: 2310003200, maxPriorityFeePerGas: 513154852 },
56+
{ maxFeePerGas: 2541005830, maxPriorityFeePerGas: 564470851 },
57+
{ maxFeePerGas: 2795108954, maxPriorityFeePerGas: 620918500 },
58+
{ maxFeePerGas: 3074622644, maxPriorityFeePerGas: 683010971 },
59+
{ maxFeePerGas: 3382087983, maxPriorityFeePerGas: 751312751 },
60+
{ maxFeePerGas: 3720300164, maxPriorityFeePerGas: 826444778 },
61+
{ maxFeePerGas: 4092333900, maxPriorityFeePerGas: 909090082 },
62+
{ maxFeePerGas: 4501571383, maxPriorityFeePerGas: 1000000000 },
63+
{ maxFeePerGas: 4951733023, maxPriorityFeePerGas: 1100001000 },
64+
{ maxFeePerGas: 5446911277, maxPriorityFeePerGas: 1210002200 },
65+
{ maxFeePerGas: 5991607851, maxPriorityFeePerGas: 1331003630 },
66+
{ maxFeePerGas: 6590774628, maxPriorityFeePerGas: 1464105324 },
67+
{ maxFeePerGas: 7249858682, maxPriorityFeePerGas: 1610517320 },
68+
{ maxFeePerGas: 7974851800, maxPriorityFeePerGas: 1771570663 },
69+
{ maxFeePerGas: 8772344955, maxPriorityFeePerGas: 1948729500 },
70+
{ maxFeePerGas: 9649588222, maxPriorityFeePerGas: 2143604399 },
71+
{ maxFeePerGas: 10614556694, maxPriorityFeePerGas: 2357966983 },
72+
{ maxFeePerGas: 11676022978, maxPriorityFeePerGas: 2593766039 },
73+
],
74+
feeEstimate: 42000000000000,
75+
fees: [
76+
{ maxFeePerGas: 2310003200, maxPriorityFeePerGas: 513154852 },
77+
{ maxFeePerGas: 2541005830, maxPriorityFeePerGas: 564470850 },
78+
{ maxFeePerGas: 2795108954, maxPriorityFeePerGas: 620918500 },
79+
{ maxFeePerGas: 3074622644, maxPriorityFeePerGas: 683010970 },
80+
{ maxFeePerGas: 3382087983, maxPriorityFeePerGas: 751312751 },
81+
{ maxFeePerGas: 3720300163, maxPriorityFeePerGas: 826444777 },
82+
{ maxFeePerGas: 4092333900, maxPriorityFeePerGas: 909090082 },
83+
{ maxFeePerGas: 4501571382, maxPriorityFeePerGas: 999999999 },
84+
{ maxFeePerGas: 4951733022, maxPriorityFeePerGas: 1100001000 },
85+
{ maxFeePerGas: 5446911277, maxPriorityFeePerGas: 1210002200 },
86+
{ maxFeePerGas: 5991607851, maxPriorityFeePerGas: 1331003630 },
87+
{ maxFeePerGas: 6590774627, maxPriorityFeePerGas: 1464105324 },
88+
{ maxFeePerGas: 7249858681, maxPriorityFeePerGas: 1610517320 },
89+
{ maxFeePerGas: 7974851800, maxPriorityFeePerGas: 1771570662 },
90+
{ maxFeePerGas: 8772344954, maxPriorityFeePerGas: 1948729500 },
91+
{ maxFeePerGas: 9649588222, maxPriorityFeePerGas: 2143604398 },
92+
{ maxFeePerGas: 10614556693, maxPriorityFeePerGas: 2357966982 },
93+
{ maxFeePerGas: 11676022977, maxPriorityFeePerGas: 2593766039 },
94+
{ maxFeePerGas: 12843636951, maxPriorityFeePerGas: 2853145236 },
95+
],
96+
gasLimit: 21000,
97+
gasUsed: 21000,
98+
},
99+
{
100+
// Trade tx.
101+
cancelFees: [
102+
{ maxFeePerGas: 2100001000, maxPriorityFeePerGas: 466503987 },
103+
{ maxFeePerGas: 2310003200, maxPriorityFeePerGas: 513154852 },
104+
{ maxFeePerGas: 2541005830, maxPriorityFeePerGas: 564470851 },
105+
{ maxFeePerGas: 2795108954, maxPriorityFeePerGas: 620918500 },
106+
{ maxFeePerGas: 3074622644, maxPriorityFeePerGas: 683010971 },
107+
{ maxFeePerGas: 3382087983, maxPriorityFeePerGas: 751312751 },
108+
{ maxFeePerGas: 3720300164, maxPriorityFeePerGas: 826444778 },
109+
{ maxFeePerGas: 4092333900, maxPriorityFeePerGas: 909090082 },
110+
{ maxFeePerGas: 4501571383, maxPriorityFeePerGas: 1000000000 },
111+
{ maxFeePerGas: 4951733023, maxPriorityFeePerGas: 1100001000 },
112+
{ maxFeePerGas: 5446911277, maxPriorityFeePerGas: 1210002200 },
113+
{ maxFeePerGas: 5991607851, maxPriorityFeePerGas: 1331003630 },
114+
{ maxFeePerGas: 6590774628, maxPriorityFeePerGas: 1464105324 },
115+
{ maxFeePerGas: 7249858682, maxPriorityFeePerGas: 1610517320 },
116+
{ maxFeePerGas: 7974851800, maxPriorityFeePerGas: 1771570663 },
117+
{ maxFeePerGas: 8772344955, maxPriorityFeePerGas: 1948729500 },
118+
{ maxFeePerGas: 9649588222, maxPriorityFeePerGas: 2143604399 },
119+
{ maxFeePerGas: 10614556694, maxPriorityFeePerGas: 2357966983 },
120+
{ maxFeePerGas: 11676022978, maxPriorityFeePerGas: 2593766039 },
121+
],
122+
feeEstimate: 42000000000000,
123+
fees: [
124+
{ maxFeePerGas: 2310003200, maxPriorityFeePerGas: 513154852 },
125+
{ maxFeePerGas: 2541005830, maxPriorityFeePerGas: 564470850 },
126+
{ maxFeePerGas: 2795108954, maxPriorityFeePerGas: 620918500 },
127+
{ maxFeePerGas: 3074622644, maxPriorityFeePerGas: 683010970 },
128+
{ maxFeePerGas: 3382087983, maxPriorityFeePerGas: 751312751 },
129+
{ maxFeePerGas: 3720300163, maxPriorityFeePerGas: 826444777 },
130+
{ maxFeePerGas: 4092333900, maxPriorityFeePerGas: 909090082 },
131+
{ maxFeePerGas: 4501571382, maxPriorityFeePerGas: 999999999 },
132+
{ maxFeePerGas: 4951733022, maxPriorityFeePerGas: 1100001000 },
133+
{ maxFeePerGas: 5446911277, maxPriorityFeePerGas: 1210002200 },
134+
{ maxFeePerGas: 5991607851, maxPriorityFeePerGas: 1331003630 },
135+
{ maxFeePerGas: 6590774627, maxPriorityFeePerGas: 1464105324 },
136+
{ maxFeePerGas: 7249858681, maxPriorityFeePerGas: 1610517320 },
137+
{ maxFeePerGas: 7974851800, maxPriorityFeePerGas: 1771570662 },
138+
{ maxFeePerGas: 8772344954, maxPriorityFeePerGas: 1948729500 },
139+
{ maxFeePerGas: 9649588222, maxPriorityFeePerGas: 2143604398 },
140+
{ maxFeePerGas: 10614556693, maxPriorityFeePerGas: 2357966982 },
141+
{ maxFeePerGas: 11676022977, maxPriorityFeePerGas: 2593766039 },
142+
{ maxFeePerGas: 12843636951, maxPriorityFeePerGas: 2853145236 },
143+
],
144+
gasLimit: 21000,
145+
gasUsed: 21000,
146+
},
92147
],
93-
gasLimit: 21000,
94-
gasUsed: 21000,
95-
};
96-
};
97-
98-
const createEstimateGasApiResponse = () => {
99-
return {
100-
feeEstimate: 42000000000000,
101-
gasLimit: 21000,
102-
gasUsed: 21000,
103148
};
104149
};
105150

@@ -255,12 +300,11 @@ describe('SmartTransactionsController', () => {
255300
[CHAIN_IDS.ETHEREUM]: [],
256301
},
257302
userOptIn: undefined,
258-
fees: undefined,
259-
liveness: true,
260-
estimatedGas: {
261-
approvalTxData: undefined,
262-
txData: undefined,
303+
fees: {
304+
approvalTxFees: undefined,
305+
tradeTxFees: undefined,
263306
},
307+
liveness: true,
264308
},
265309
});
266310
});
@@ -391,15 +435,20 @@ describe('SmartTransactionsController', () => {
391435

392436
describe('getFees', () => {
393437
it('gets unsigned transactions and estimates based on an unsigned transaction', async () => {
394-
const unsignedTransaction = createUnsignedTransaction();
438+
const tradeTx = createUnsignedTransaction();
439+
const approvalTx = createUnsignedTransaction();
395440
const getFeesApiResponse = createGetFeesApiResponse();
396441
nock(API_BASE_URL)
397442
.post(`/networks/${ethereumChainIdDec}/getFees`)
398443
.reply(200, getFeesApiResponse);
399444
const fees = await smartTransactionsController.getFees(
400-
unsignedTransaction,
445+
tradeTx,
446+
approvalTx,
401447
);
402-
expect(fees).toStrictEqual(getFeesApiResponse);
448+
expect(fees).toMatchObject({
449+
approvalTxFees: getFeesApiResponse.txs[0],
450+
tradeTxFees: getFeesApiResponse.txs[1],
451+
});
403452
});
404453
});
405454

@@ -451,12 +500,11 @@ describe('SmartTransactionsController', () => {
451500
[CHAIN_IDS.ETHEREUM]: [pendingTransaction],
452501
},
453502
userOptIn: undefined,
454-
fees: undefined,
455-
liveness: true,
456-
estimatedGas: {
457-
approvalTxData: undefined,
458-
txData: undefined,
503+
fees: {
504+
approvalTxFees: undefined,
505+
tradeTxFees: undefined,
459506
},
507+
liveness: true,
460508
},
461509
});
462510
});
@@ -488,10 +536,9 @@ describe('SmartTransactionsController', () => {
488536
],
489537
},
490538
userOptIn: undefined,
491-
fees: undefined,
492-
estimatedGas: {
493-
approvalTxData: undefined,
494-
txData: undefined,
539+
fees: {
540+
approvalTxFees: undefined,
541+
tradeTxFees: undefined,
495542
},
496543
liveness: true,
497544
},
@@ -688,19 +735,4 @@ describe('SmartTransactionsController', () => {
688735
expect(actual).toBe(false);
689736
});
690737
});
691-
692-
describe('estimateGas', () => {
693-
it('gets estimated gas for a transaction', async () => {
694-
const unsignedTransaction = createUnsignedTransaction();
695-
const estimateGasApiResponse = createEstimateGasApiResponse();
696-
nock(API_BASE_URL)
697-
.post(`/networks/${ethereumChainIdDec}/estimateGas`)
698-
.reply(200, estimateGasApiResponse);
699-
const estimatedGas = await smartTransactionsController.estimateGas(
700-
unsignedTransaction,
701-
null,
702-
);
703-
expect(estimatedGas).toStrictEqual(estimateGasApiResponse);
704-
});
705-
});
706738
});

0 commit comments

Comments
 (0)