diff --git a/packages/sentinel-api-service/CHANGELOG.md b/packages/sentinel-api-service/CHANGELOG.md index f7ea8b2377b..11991fa8afd 100644 --- a/packages/sentinel-api-service/CHANGELOG.md +++ b/packages/sentinel-api-service/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- **BREAKING:** `SentinelSimulationResponseTransaction.gasCost` is now typed `string` instead of `number`, matching the value the Sentinel API actually returns on the wire (a quoted decimal string, not a JSON number) ([#10050](https://github.com/MetaMask/core/pull/10050)) + - Consumers reading `gasCost` and treating it as a `number` (e.g. doing arithmetic directly on it) must parse it first, e.g. via `new BN(gasCost)` or `BigInt(gasCost)`. + ## [1.0.1] ### Changed diff --git a/packages/sentinel-api-service/src/types.ts b/packages/sentinel-api-service/src/types.ts index 63658e6e4b8..13aef1c9697 100644 --- a/packages/sentinel-api-service/src/types.ts +++ b/packages/sentinel-api-service/src/types.ts @@ -449,10 +449,10 @@ export type SentinelSimulationResponseTransaction = { fees?: SentinelSimulationFeeLevel[]; /** - * Estimated total gas cost of the transaction, in wei as a decimal number. + * Estimated total gas cost of the transaction, in wei as a decimal string. * Included in the `stateDiff` when `withGas` is true. */ - gasCost?: number; + gasCost?: string; /** Required `gasLimit` for the transaction. */ gasLimit?: Hex; diff --git a/packages/transaction-controller/src/api/simulation-api.ts b/packages/transaction-controller/src/api/simulation-api.ts index 4d080a3055e..cfc1494d0bc 100644 --- a/packages/transaction-controller/src/api/simulation-api.ts +++ b/packages/transaction-controller/src/api/simulation-api.ts @@ -225,10 +225,10 @@ export type SimulationResponseTransaction = { }[]; /** - * Estimated total gas cost of the transaction. + * Estimated total gas cost of the transaction, in wei as a decimal string. * Included in the stateDiff if `withGas` is true. */ - gasCost?: number; + gasCost?: string; /** Required `gasLimit` for the transaction. */ gasLimit?: Hex; diff --git a/packages/transaction-controller/src/utils/balance-changes.test.ts b/packages/transaction-controller/src/utils/balance-changes.test.ts index 91948e6eadf..69a19c0627f 100644 --- a/packages/transaction-controller/src/utils/balance-changes.test.ts +++ b/packages/transaction-controller/src/utils/balance-changes.test.ts @@ -198,7 +198,7 @@ function createEventResponseMock( function createNativeBalanceResponse( previousBalance: string, newBalance: string, - gasCost: number = 0, + gasCost: string = '0', ): SimulationResponse { return { transactions: [ @@ -343,7 +343,7 @@ describe('Balance Change Utils', () => { it('ignoring gas cost', async () => { simulateTransactionsMock.mockResolvedValueOnce( - createNativeBalanceResponse('0x3', '0x8', 2), + createNativeBalanceResponse('0x3', '0x8', '2'), ); const result = await getBalanceChanges(REQUEST_MOCK); @@ -363,6 +363,33 @@ describe('Balance Change Utils', () => { simulationRevert: undefined, }); }); + + it('supports a gas cost above Number.MAX_SAFE_INTEGER', async () => { + // A wei-scale gas cost this large is realistic on chains such as + // Polygon, and would previously throw when the wire value was + // mistakenly parsed as a JS `number` via `new BN(offset)`. + const largeGasCost = '10500000000000000'; // 2^53 (9007199254740992) < this + simulateTransactionsMock.mockResolvedValueOnce( + createNativeBalanceResponse('0x0', '0x0', largeGasCost), + ); + + const result = await getBalanceChanges(REQUEST_MOCK); + + expect(result).toStrictEqual({ + simulationData: { + callTraceErrors: [], + nativeBalanceChange: { + difference: '0x254db1c2244000', + isDecrease: false, + newBalance: '0x254db1c2244000', + previousBalance: '0x0', + }, + tokenBalanceChanges: [], + }, + gasUsed: undefined, + simulationRevert: undefined, + }); + }); }); describe('returns token balance changes', () => { diff --git a/packages/transaction-controller/src/utils/balance-changes.ts b/packages/transaction-controller/src/utils/balance-changes.ts index b366a75ba53..ca30759a1c6 100644 --- a/packages/transaction-controller/src/utils/balance-changes.ts +++ b/packages/transaction-controller/src/utils/balance-changes.ts @@ -699,7 +699,7 @@ function extractRootRevert( function getSimulationBalanceChange( previousBalance: Hex, newBalance: Hex, - offset: number = 0, + offset: string = '0', ): SimulationBalanceChange | undefined { const newBalanceBN = hexToBN(newBalance).add(new BN(offset)); const previousBalanceBN = hexToBN(previousBalance);