Skip to content

Commit 840cd5d

Browse files
committed
fix: delegated account + gasIncluded7702=false
1 parent 6e76ea2 commit 840cd5d

3 files changed

Lines changed: 68 additions & 47 deletions

File tree

packages/bridge-status-controller/src/bridge-status-controller.test.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ import type {
2626
MockAnyNamespace,
2727
} from '@metamask/messenger';
2828
import type { Provider } from '@metamask/network-controller';
29-
import { CHAIN_IDS } from '@metamask/transaction-controller';
29+
import {
30+
CHAIN_IDS,
31+
GasFeeEstimateType,
32+
} from '@metamask/transaction-controller';
3033
import {
3134
TransactionType,
3235
TransactionStatus,
@@ -2833,6 +2836,7 @@ describe('BridgeStatusController', () => {
28332836

28342837
const mockEstimateGasFeeResult = {
28352838
estimates: {
2839+
type: GasFeeEstimateType.FeeMarket,
28362840
high: {
28372841
suggestedMaxFeePerGas: '0x1234',
28382842
suggestedMaxPriorityFeePerGas: '0x5678',
@@ -3636,6 +3640,7 @@ describe('BridgeStatusController', () => {
36363640
mockMessengerCall.mockReturnValueOnce('arbitrum-client-id');
36373641
mockMessengerCall.mockResolvedValueOnce({
36383642
estimates: {
3643+
type: GasFeeEstimateType.FeeMarket,
36393644
high: {
36403645
suggestedMaxFeePerGas: '0x1234',
36413646
suggestedMaxPriorityFeePerGas: '0x5678',
@@ -3806,6 +3811,7 @@ describe('BridgeStatusController', () => {
38063811

38073812
const mockEstimateGasFeeResult = {
38083813
estimates: {
3814+
type: GasFeeEstimateType.FeeMarket,
38093815
high: {
38103816
suggestedMaxFeePerGas: '0x1234',
38113817
suggestedMaxPriorityFeePerGas: '0x5678',

packages/bridge-status-controller/src/utils/transaction.test.ts

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import type {
1111
TxData,
1212
} from '@metamask/bridge-controller';
1313
import {
14+
GasFeeEstimateType,
1415
TransactionStatus,
1516
TransactionType,
1617
} from '@metamask/transaction-controller';
@@ -1709,10 +1710,14 @@ describe('Bridge Status Controller Transaction Utils', () => {
17091710
[FeeType.METABRIDGE]: {
17101711
amount: '100000000000000000',
17111712
},
1712-
txFee: {
1713-
maxFeePerGas: '50000000000000000',
1714-
maxPriorityFeePerGas: '50000000000000000',
1715-
},
1713+
...(overrides.gasIncluded7702 || overrides.gasIncluded
1714+
? {
1715+
txFee: {
1716+
maxFeePerGas: '50000000000000000',
1717+
maxPriorityFeePerGas: '50000000000000000',
1718+
},
1719+
}
1720+
: {}),
17161721
},
17171722
gasIncluded: overrides.gasIncluded ?? false,
17181723
gasIncluded7702: overrides.gasIncluded7702 ?? false,
@@ -1961,6 +1966,7 @@ describe('Bridge Status Controller Transaction Utils', () => {
19611966

19621967
const mockMessenger = createMockMessagingSystem({
19631968
estimates: {
1969+
type: GasFeeEstimateType.FeeMarket,
19641970
medium: {
19651971
maxFeePerGas: '0xabc',
19661972
maxPriorityFeePerGas: '0xdef',
@@ -2012,11 +2018,18 @@ describe('Bridge Status Controller Transaction Utils', () => {
20122018
`);
20132019
// Transaction params should include gas fields
20142020
expect(result.transactions).toHaveLength(1);
2015-
expect(result.transactions[0].params).toHaveProperty('gas');
2016-
expect(result.transactions[0].params).toHaveProperty('maxFeePerGas');
2017-
expect(result.transactions[0].params).toHaveProperty(
2018-
'maxPriorityFeePerGas',
2019-
);
2021+
// TxFee values from the estimateGasFee call
2022+
expect(result.transactions[0].params).toMatchInlineSnapshot(`
2023+
{
2024+
"data": "0xbridgeData",
2025+
"from": "0xUserAddress",
2026+
"gas": "0x5208",
2027+
"maxFeePerGas": "0xabc",
2028+
"maxPriorityFeePerGas": "0xdef",
2029+
"to": "0xBridgeContract",
2030+
"value": "0x1000",
2031+
}
2032+
`);
20202033
});
20212034

20222035
it('should enable 7702 and omit gas fields when isDelegatedAccount is true and gasIncluded7702 is true', async () => {
@@ -2050,11 +2063,18 @@ describe('Bridge Status Controller Transaction Utils', () => {
20502063
).toHaveLength(0);
20512064
// Transaction params should NOT include gas fields
20522065
expect(result.transactions).toHaveLength(1);
2053-
expect(result.transactions[0].params).not.toHaveProperty('gas');
2054-
expect(result.transactions[0].params).not.toHaveProperty('maxFeePerGas');
2055-
expect(result.transactions[0].params).not.toHaveProperty(
2056-
'maxPriorityFeePerGas',
2057-
);
2066+
// These are the txFee values from the quote response
2067+
expect(result.transactions[0].params).toMatchInlineSnapshot(`
2068+
{
2069+
"data": "0xbridgeData",
2070+
"from": "0xUserAddress",
2071+
"gas": "0x5208",
2072+
"maxFeePerGas": "0xb1a2bc2ec50000",
2073+
"maxPriorityFeePerGas": "0xb1a2bc2ec50000",
2074+
"to": "0xBridgeContract",
2075+
"value": "0x1000",
2076+
}
2077+
`);
20582078
});
20592079
});
20602080

packages/bridge-status-controller/src/utils/transaction.ts

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import type {
1616
} from '@metamask/bridge-controller';
1717
import { toHex } from '@metamask/controller-utils';
1818
import {
19+
GasFeeEstimateType,
1920
TransactionStatus,
2021
TransactionType,
2122
} from '@metamask/transaction-controller';
@@ -58,6 +59,22 @@ const is7702Tx = (tx: TransactionMeta) => {
5859
);
5960
};
6061

62+
const getGasFeeEstimates = async (
63+
messenger: BridgeStatusControllerMessenger,
64+
args: Parameters<TransactionController['estimateGasFee']>[0],
65+
) => {
66+
const { estimates } = await messenger.call(
67+
'TransactionController:estimateGasFee',
68+
args,
69+
);
70+
71+
if (estimates?.type === GasFeeEstimateType.FeeMarket) {
72+
return estimates[BRIDGE_PREFERRED_GAS_ESTIMATE];
73+
}
74+
75+
return undefined;
76+
};
77+
6178
export const getTransactions = (messenger: BridgeStatusControllerMessenger) => {
6279
return messenger.call('TransactionController:getState').transactions ?? [];
6380
};
@@ -298,7 +315,6 @@ export const toQuoteAndTxMetadata = ({
298315
* @param trade.gasLimit - the gas limit to use for the gas fee estimates
299316
* @param networkClientId - the network client ID to use for the gas fee estimates
300317
* @param chainId - the chain ID to use for the gas fee estimates
301-
* @param isGasIncluded7702 - whether the gas is included via 7702
302318
* @param simulatedGasFeeLimits - either the txFee from the quote or the simulated gas fee limits for the batch sell
303319
* @returns The gas fee estimates for the transaction
304320
*/
@@ -307,7 +323,6 @@ export const toTransactionParams = async (
307323
{ chainId: tradeChainId, gasLimit, ...trade }: TxData,
308324
networkClientId: string,
309325
chainId: Hex,
310-
isGasIncluded7702: boolean,
311326
simulatedGasFeeLimits?: SimulatedGasFeeLimits | TxFeeGasLimits,
312327
): Promise<BatchTransactionParams> => {
313328
const normalizedTrade = {
@@ -317,16 +332,14 @@ export const toTransactionParams = async (
317332
from: trade.from,
318333
value: trade.value,
319334
};
320-
if (isGasIncluded7702 && !simulatedGasFeeLimits) {
321-
return normalizedTrade;
322-
}
323335
const transactionParams = {
324336
...trade,
325337
// Only add gasLimit and gas if they're truthy
326338
gas: gasLimit ? toHex(gasLimit) : undefined,
327339
...normalizedTrade,
328340
};
329341

342+
// Use bridge-api's provided gas fee estimates
330343
if (simulatedGasFeeLimits) {
331344
return {
332345
...transactionParams,
@@ -336,32 +349,16 @@ export const toTransactionParams = async (
336349
}
337350

338351
// Get transaction's 1559 gas fee estimates
339-
const { estimates } = await messenger.call(
340-
'TransactionController:estimateGasFee',
341-
{
342-
transactionParams,
343-
networkClientId,
344-
chainId,
345-
},
346-
);
347-
348-
let gasFeeEstimates: Partial<Record<keyof SimulatedGasFeeLimits, Hex>> = {
349-
maxFeePerGas: undefined,
350-
maxPriorityFeePerGas: undefined,
351-
};
352-
if (
353-
estimates &&
354-
BRIDGE_PREFERRED_GAS_ESTIMATE in estimates &&
355-
typeof estimates[BRIDGE_PREFERRED_GAS_ESTIMATE] === 'object' &&
356-
'maxFeePerGas' in estimates[BRIDGE_PREFERRED_GAS_ESTIMATE]
357-
) {
358-
gasFeeEstimates = estimates[BRIDGE_PREFERRED_GAS_ESTIMATE];
359-
}
352+
const gasFeeEstimates = await getGasFeeEstimates(messenger, {
353+
transactionParams,
354+
networkClientId,
355+
chainId,
356+
});
360357

361358
return {
362359
...transactionParams,
363-
maxFeePerGas: gasFeeEstimates.maxFeePerGas,
364-
maxPriorityFeePerGas: gasFeeEstimates.maxPriorityFeePerGas,
360+
maxFeePerGas: gasFeeEstimates?.maxFeePerGas,
361+
maxPriorityFeePerGas: gasFeeEstimates?.maxPriorityFeePerGas,
365362
};
366363
};
367364

@@ -390,14 +387,13 @@ export const getAddTransactionBatchParams = async ({
390387
const networkClientId = getNetworkClientIdByChainId(messenger, hexChainId);
391388

392389
const transactions: TransactionBatchSingleRequest[] = await Promise.all(
393-
tradeData.map(async ({ tx, txFee, quoteResponse: { quote }, ...rest }) => ({
390+
tradeData.map(async ({ tx, txFee, quoteResponse: _, ...rest }) => ({
394391
params: await toTransactionParams(
395392
messenger,
396393
tx,
397394
networkClientId,
398395
hexChainId,
399-
Boolean(quote.gasIncluded7702),
400-
isDelegatedAccount ? undefined : txFee,
396+
txFee,
401397
),
402398
...rest,
403399
})),
@@ -635,7 +631,6 @@ export const submitEvmTransaction = async ({
635631
trade,
636632
networkClientId,
637633
hexChainId,
638-
false,
639634
txFee,
640635
);
641636

0 commit comments

Comments
 (0)