Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"estimateGas" -> "getFees", support a new cancellation reason, refactoring #69

Merged
merged 6 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 116 additions & 84 deletions src/SmartTransactionsController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,59 +47,104 @@ const createUnsignedTransaction = () => {

const createGetFeesApiResponse = () => {
return {
cancelFees: [
{ maxFeePerGas: 2100001000, maxPriorityFeePerGas: 466503987 },
{ maxFeePerGas: 2310003200, maxPriorityFeePerGas: 513154852 },
{ maxFeePerGas: 2541005830, maxPriorityFeePerGas: 564470851 },
{ maxFeePerGas: 2795108954, maxPriorityFeePerGas: 620918500 },
{ maxFeePerGas: 3074622644, maxPriorityFeePerGas: 683010971 },
{ maxFeePerGas: 3382087983, maxPriorityFeePerGas: 751312751 },
{ maxFeePerGas: 3720300164, maxPriorityFeePerGas: 826444778 },
{ maxFeePerGas: 4092333900, maxPriorityFeePerGas: 909090082 },
{ maxFeePerGas: 4501571383, maxPriorityFeePerGas: 1000000000 },
{ maxFeePerGas: 4951733023, maxPriorityFeePerGas: 1100001000 },
{ maxFeePerGas: 5446911277, maxPriorityFeePerGas: 1210002200 },
{ maxFeePerGas: 5991607851, maxPriorityFeePerGas: 1331003630 },
{ maxFeePerGas: 6590774628, maxPriorityFeePerGas: 1464105324 },
{ maxFeePerGas: 7249858682, maxPriorityFeePerGas: 1610517320 },
{ maxFeePerGas: 7974851800, maxPriorityFeePerGas: 1771570663 },
{ maxFeePerGas: 8772344955, maxPriorityFeePerGas: 1948729500 },
{ maxFeePerGas: 9649588222, maxPriorityFeePerGas: 2143604399 },
{ maxFeePerGas: 10614556694, maxPriorityFeePerGas: 2357966983 },
{ maxFeePerGas: 11676022978, maxPriorityFeePerGas: 2593766039 },
],
feeEstimate: 42000000000000,
fees: [
{ maxFeePerGas: 2310003200, maxPriorityFeePerGas: 513154852 },
{ maxFeePerGas: 2541005830, maxPriorityFeePerGas: 564470850 },
{ maxFeePerGas: 2795108954, maxPriorityFeePerGas: 620918500 },
{ maxFeePerGas: 3074622644, maxPriorityFeePerGas: 683010970 },
{ maxFeePerGas: 3382087983, maxPriorityFeePerGas: 751312751 },
{ maxFeePerGas: 3720300163, maxPriorityFeePerGas: 826444777 },
{ maxFeePerGas: 4092333900, maxPriorityFeePerGas: 909090082 },
{ maxFeePerGas: 4501571382, maxPriorityFeePerGas: 999999999 },
{ maxFeePerGas: 4951733022, maxPriorityFeePerGas: 1100001000 },
{ maxFeePerGas: 5446911277, maxPriorityFeePerGas: 1210002200 },
{ maxFeePerGas: 5991607851, maxPriorityFeePerGas: 1331003630 },
{ maxFeePerGas: 6590774627, maxPriorityFeePerGas: 1464105324 },
{ maxFeePerGas: 7249858681, maxPriorityFeePerGas: 1610517320 },
{ maxFeePerGas: 7974851800, maxPriorityFeePerGas: 1771570662 },
{ maxFeePerGas: 8772344954, maxPriorityFeePerGas: 1948729500 },
{ maxFeePerGas: 9649588222, maxPriorityFeePerGas: 2143604398 },
{ maxFeePerGas: 10614556693, maxPriorityFeePerGas: 2357966982 },
{ maxFeePerGas: 11676022977, maxPriorityFeePerGas: 2593766039 },
{ maxFeePerGas: 12843636951, maxPriorityFeePerGas: 2853145236 },
txs: [
{
// Approval tx.
cancelFees: [
{ maxFeePerGas: 2100001000, maxPriorityFeePerGas: 466503987 },
{ maxFeePerGas: 2310003200, maxPriorityFeePerGas: 513154852 },
{ maxFeePerGas: 2541005830, maxPriorityFeePerGas: 564470851 },
{ maxFeePerGas: 2795108954, maxPriorityFeePerGas: 620918500 },
{ maxFeePerGas: 3074622644, maxPriorityFeePerGas: 683010971 },
{ maxFeePerGas: 3382087983, maxPriorityFeePerGas: 751312751 },
{ maxFeePerGas: 3720300164, maxPriorityFeePerGas: 826444778 },
{ maxFeePerGas: 4092333900, maxPriorityFeePerGas: 909090082 },
{ maxFeePerGas: 4501571383, maxPriorityFeePerGas: 1000000000 },
{ maxFeePerGas: 4951733023, maxPriorityFeePerGas: 1100001000 },
{ maxFeePerGas: 5446911277, maxPriorityFeePerGas: 1210002200 },
{ maxFeePerGas: 5991607851, maxPriorityFeePerGas: 1331003630 },
{ maxFeePerGas: 6590774628, maxPriorityFeePerGas: 1464105324 },
{ maxFeePerGas: 7249858682, maxPriorityFeePerGas: 1610517320 },
{ maxFeePerGas: 7974851800, maxPriorityFeePerGas: 1771570663 },
{ maxFeePerGas: 8772344955, maxPriorityFeePerGas: 1948729500 },
{ maxFeePerGas: 9649588222, maxPriorityFeePerGas: 2143604399 },
{ maxFeePerGas: 10614556694, maxPriorityFeePerGas: 2357966983 },
{ maxFeePerGas: 11676022978, maxPriorityFeePerGas: 2593766039 },
],
feeEstimate: 42000000000000,
fees: [
{ maxFeePerGas: 2310003200, maxPriorityFeePerGas: 513154852 },
{ maxFeePerGas: 2541005830, maxPriorityFeePerGas: 564470850 },
{ maxFeePerGas: 2795108954, maxPriorityFeePerGas: 620918500 },
{ maxFeePerGas: 3074622644, maxPriorityFeePerGas: 683010970 },
{ maxFeePerGas: 3382087983, maxPriorityFeePerGas: 751312751 },
{ maxFeePerGas: 3720300163, maxPriorityFeePerGas: 826444777 },
{ maxFeePerGas: 4092333900, maxPriorityFeePerGas: 909090082 },
{ maxFeePerGas: 4501571382, maxPriorityFeePerGas: 999999999 },
{ maxFeePerGas: 4951733022, maxPriorityFeePerGas: 1100001000 },
{ maxFeePerGas: 5446911277, maxPriorityFeePerGas: 1210002200 },
{ maxFeePerGas: 5991607851, maxPriorityFeePerGas: 1331003630 },
{ maxFeePerGas: 6590774627, maxPriorityFeePerGas: 1464105324 },
{ maxFeePerGas: 7249858681, maxPriorityFeePerGas: 1610517320 },
{ maxFeePerGas: 7974851800, maxPriorityFeePerGas: 1771570662 },
{ maxFeePerGas: 8772344954, maxPriorityFeePerGas: 1948729500 },
{ maxFeePerGas: 9649588222, maxPriorityFeePerGas: 2143604398 },
{ maxFeePerGas: 10614556693, maxPriorityFeePerGas: 2357966982 },
{ maxFeePerGas: 11676022977, maxPriorityFeePerGas: 2593766039 },
{ maxFeePerGas: 12843636951, maxPriorityFeePerGas: 2853145236 },
],
gasLimit: 21000,
gasUsed: 21000,
},
{
// Trade tx.
cancelFees: [
{ maxFeePerGas: 2100001000, maxPriorityFeePerGas: 466503987 },
{ maxFeePerGas: 2310003200, maxPriorityFeePerGas: 513154852 },
{ maxFeePerGas: 2541005830, maxPriorityFeePerGas: 564470851 },
{ maxFeePerGas: 2795108954, maxPriorityFeePerGas: 620918500 },
{ maxFeePerGas: 3074622644, maxPriorityFeePerGas: 683010971 },
{ maxFeePerGas: 3382087983, maxPriorityFeePerGas: 751312751 },
{ maxFeePerGas: 3720300164, maxPriorityFeePerGas: 826444778 },
{ maxFeePerGas: 4092333900, maxPriorityFeePerGas: 909090082 },
{ maxFeePerGas: 4501571383, maxPriorityFeePerGas: 1000000000 },
{ maxFeePerGas: 4951733023, maxPriorityFeePerGas: 1100001000 },
{ maxFeePerGas: 5446911277, maxPriorityFeePerGas: 1210002200 },
{ maxFeePerGas: 5991607851, maxPriorityFeePerGas: 1331003630 },
{ maxFeePerGas: 6590774628, maxPriorityFeePerGas: 1464105324 },
{ maxFeePerGas: 7249858682, maxPriorityFeePerGas: 1610517320 },
{ maxFeePerGas: 7974851800, maxPriorityFeePerGas: 1771570663 },
{ maxFeePerGas: 8772344955, maxPriorityFeePerGas: 1948729500 },
{ maxFeePerGas: 9649588222, maxPriorityFeePerGas: 2143604399 },
{ maxFeePerGas: 10614556694, maxPriorityFeePerGas: 2357966983 },
{ maxFeePerGas: 11676022978, maxPriorityFeePerGas: 2593766039 },
],
feeEstimate: 42000000000000,
fees: [
{ maxFeePerGas: 2310003200, maxPriorityFeePerGas: 513154852 },
{ maxFeePerGas: 2541005830, maxPriorityFeePerGas: 564470850 },
{ maxFeePerGas: 2795108954, maxPriorityFeePerGas: 620918500 },
{ maxFeePerGas: 3074622644, maxPriorityFeePerGas: 683010970 },
{ maxFeePerGas: 3382087983, maxPriorityFeePerGas: 751312751 },
{ maxFeePerGas: 3720300163, maxPriorityFeePerGas: 826444777 },
{ maxFeePerGas: 4092333900, maxPriorityFeePerGas: 909090082 },
{ maxFeePerGas: 4501571382, maxPriorityFeePerGas: 999999999 },
{ maxFeePerGas: 4951733022, maxPriorityFeePerGas: 1100001000 },
{ maxFeePerGas: 5446911277, maxPriorityFeePerGas: 1210002200 },
{ maxFeePerGas: 5991607851, maxPriorityFeePerGas: 1331003630 },
{ maxFeePerGas: 6590774627, maxPriorityFeePerGas: 1464105324 },
{ maxFeePerGas: 7249858681, maxPriorityFeePerGas: 1610517320 },
{ maxFeePerGas: 7974851800, maxPriorityFeePerGas: 1771570662 },
{ maxFeePerGas: 8772344954, maxPriorityFeePerGas: 1948729500 },
{ maxFeePerGas: 9649588222, maxPriorityFeePerGas: 2143604398 },
{ maxFeePerGas: 10614556693, maxPriorityFeePerGas: 2357966982 },
{ maxFeePerGas: 11676022977, maxPriorityFeePerGas: 2593766039 },
{ maxFeePerGas: 12843636951, maxPriorityFeePerGas: 2853145236 },
],
gasLimit: 21000,
gasUsed: 21000,
},
],
gasLimit: 21000,
gasUsed: 21000,
};
};

const createEstimateGasApiResponse = () => {
return {
feeEstimate: 42000000000000,
gasLimit: 21000,
gasUsed: 21000,
};
};

Expand Down Expand Up @@ -255,12 +300,11 @@ describe('SmartTransactionsController', () => {
[CHAIN_IDS.ETHEREUM]: [],
},
userOptIn: undefined,
fees: undefined,
liveness: true,
estimatedGas: {
approvalTxData: undefined,
txData: undefined,
fees: {
approvalTxFees: undefined,
tradeTxFees: undefined,
},
liveness: true,
},
});
});
Expand Down Expand Up @@ -391,15 +435,20 @@ describe('SmartTransactionsController', () => {

describe('getFees', () => {
it('gets unsigned transactions and estimates based on an unsigned transaction', async () => {
const unsignedTransaction = createUnsignedTransaction();
const tradeTx = createUnsignedTransaction();
const approvalTx = createUnsignedTransaction();
const getFeesApiResponse = createGetFeesApiResponse();
nock(API_BASE_URL)
.post(`/networks/${ethereumChainIdDec}/getFees`)
.reply(200, getFeesApiResponse);
const fees = await smartTransactionsController.getFees(
unsignedTransaction,
tradeTx,
approvalTx,
);
expect(fees).toStrictEqual(getFeesApiResponse);
expect(fees).toMatchObject({
approvalTxFees: getFeesApiResponse.txs[0],
tradeTxFees: getFeesApiResponse.txs[1],
});
});
});

Expand Down Expand Up @@ -451,12 +500,11 @@ describe('SmartTransactionsController', () => {
[CHAIN_IDS.ETHEREUM]: [pendingTransaction],
},
userOptIn: undefined,
fees: undefined,
liveness: true,
estimatedGas: {
approvalTxData: undefined,
txData: undefined,
fees: {
approvalTxFees: undefined,
tradeTxFees: undefined,
},
liveness: true,
},
});
});
Expand Down Expand Up @@ -488,10 +536,9 @@ describe('SmartTransactionsController', () => {
],
},
userOptIn: undefined,
fees: undefined,
estimatedGas: {
approvalTxData: undefined,
txData: undefined,
fees: {
approvalTxFees: undefined,
tradeTxFees: undefined,
},
liveness: true,
},
Expand Down Expand Up @@ -688,19 +735,4 @@ describe('SmartTransactionsController', () => {
expect(actual).toBe(false);
});
});

describe('estimateGas', () => {
it('gets estimated gas for a transaction', async () => {
const unsignedTransaction = createUnsignedTransaction();
const estimateGasApiResponse = createEstimateGasApiResponse();
nock(API_BASE_URL)
.post(`/networks/${ethereumChainIdDec}/estimateGas`)
.reply(200, estimateGasApiResponse);
const estimatedGas = await smartTransactionsController.estimateGas(
unsignedTransaction,
null,
);
expect(estimatedGas).toStrictEqual(estimateGasApiResponse);
});
});
});
100 changes: 38 additions & 62 deletions src/SmartTransactionsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
SmartTransactionsStatus,
SmartTransactionStatuses,
Fees,
EstimatedGas,
IndividualTxFees,
} from './types';
import {
getAPIRequestURL,
Expand Down Expand Up @@ -50,10 +50,9 @@ export interface SmartTransactionsControllerState extends BaseState {
smartTransactions: Record<string, SmartTransaction[]>;
userOptIn: boolean | undefined;
liveness: boolean | undefined;
fees: Fees | undefined;
estimatedGas: {
txData: EstimatedGas | undefined;
approvalTxData: EstimatedGas | undefined;
fees: {
approvalTxFees: IndividualTxFees | undefined;
tradeTxFees: IndividualTxFees | undefined;
};
};
}
Expand Down Expand Up @@ -122,12 +121,11 @@ export default class SmartTransactionsController extends BaseController<
smartTransactionsState: {
smartTransactions: {},
userOptIn: undefined,
fees: undefined,
liveness: true,
estimatedGas: {
txData: undefined,
approvalTxData: undefined,
fees: {
approvalTxFees: undefined,
tradeTxFees: undefined,
},
liveness: true,
},
};

Expand Down Expand Up @@ -469,72 +467,50 @@ export default class SmartTransactionsController extends BaseController<
};
}

async getFees(unsignedTransaction: UnsignedTransaction): Promise<Fees> {
async getFees(
tradeTx: UnsignedTransaction,
approvalTx: UnsignedTransaction,
): Promise<Fees> {
const { chainId } = this.config;

const unsignedTransactionWithNonce = await this.addNonceToTransaction(
unsignedTransaction,
const transactions = [];
if (approvalTx) {
const unsignedApprovalTransactionWithNonce = await this.addNonceToTransaction(
approvalTx,
);
transactions.push(unsignedApprovalTransactionWithNonce);
}
const unsignedTradeTransactionWithNonce = await this.addNonceToTransaction(
tradeTx,
);
transactions.push(unsignedTradeTransactionWithNonce);
const data = await this.fetch(getAPIRequestURL(APIType.GET_FEES, chainId), {
method: 'POST',
body: JSON.stringify({
tx: unsignedTransactionWithNonce,
txs: transactions,
}),
});
this.update({
smartTransactionsState: {
...this.state.smartTransactionsState,
fees: data,
},
});
return data;
}

async estimateGas(
unsignedTransaction: UnsignedTransaction,
approveTxParams: UnsignedTransaction,
): Promise<EstimatedGas> {
const { chainId } = this.config;

let approvalTxData;
if (approveTxParams) {
const unsignedApprovalTransactionWithNonce = await this.addNonceToTransaction(
approveTxParams,
);
approvalTxData = await this.fetch(
getAPIRequestURL(APIType.ESTIMATE_GAS, chainId),
{
method: 'POST',
body: JSON.stringify({
tx: unsignedApprovalTransactionWithNonce,
}),
},
);
let approvalTxFees;
let tradeTxFees;
if (approvalTx) {
approvalTxFees = data?.txs[0];
tradeTxFees = data?.txs[1];
} else {
tradeTxFees = data?.txs[0];
}
const unsignedTransactionWithNonce = await this.addNonceToTransaction(
unsignedTransaction,
);
const data = await this.fetch(
getAPIRequestURL(APIType.ESTIMATE_GAS, chainId),
{
method: 'POST',
body: JSON.stringify({
tx: unsignedTransactionWithNonce,
...(approveTxParams && { pending_txs: [approveTxParams] }),
}),
},
);

this.update({
smartTransactionsState: {
...this.state.smartTransactionsState,
estimatedGas: {
txData: data,
approvalTxData,
fees: {
approvalTxFees,
tradeTxFees,
},
},
});

return data;
return {
approvalTxFees,
tradeTxFees,
};
}

// * After this successful call client must add a nonce representative to
Expand Down
Loading