Skip to content

Commit 8a24e34

Browse files
committed
Subtract gas cost
Add gas fee token fee.
1 parent 5640b42 commit 8a24e34

File tree

7 files changed

+53
-3
lines changed

7 files changed

+53
-3
lines changed

packages/transaction-controller/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
### Changed
2828

2929
- Include gas limit and gas fees in simulation requests ([#5754](https://github.com/MetaMask/core/pull/5754))
30+
- Add optional `fee` property to `GasFeeToken`.
3031

3132
### Fixed
3233

packages/transaction-controller/src/api/simulation-api.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,9 @@ export type SimulationResponseTokenFee = {
173173
/** Conversation rate of 1 token to native WEI. */
174174
rateWei: Hex;
175175

176+
/** Portion of `balanceNeededToken` that is the fee paid to MetaMask. */
177+
serviceFee?: Hex;
178+
176179
/** Estimated gas limit required for fee transfer. */
177180
transferEstimate: Hex;
178181
};
@@ -200,6 +203,12 @@ export type SimulationResponseTransaction = {
200203
tokenFees: SimulationResponseTokenFee[];
201204
}[];
202205

206+
/**
207+
* Estimated total gas cost of the transaction.
208+
* Included in the stateDiff if `withGas` is true.
209+
*/
210+
gasCost?: number;
211+
203212
/** Required `gasLimit` for the transaction. */
204213
gasLimit?: Hex;
205214

packages/transaction-controller/src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,9 @@ export type GasFeeToken = {
17531753
/** Decimals of the token. */
17541754
decimals: number;
17551755

1756+
/** Portion of the amount that is the fee paid to MetaMask. */
1757+
fee?: Hex;
1758+
17561759
/** Estimated gas limit required for original transaction. */
17571760
gas: Hex;
17581761

packages/transaction-controller/src/utils/balance-changes.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,17 +182,20 @@ function createEventResponseMock(
182182
*
183183
* @param previousBalance - The previous balance.
184184
* @param newBalance - The new balance.
185+
* @param gasCost - Gas cost of the transaction.
185186
* @returns Mock API response.
186187
*/
187188
function createNativeBalanceResponse(
188189
previousBalance: string,
189190
newBalance: string,
191+
gasCost: number = 0,
190192
) {
191193
return {
192194
transactions: [
193195
{
194196
...defaultResponseTx,
195197
return: encodeTo32ByteHex(previousBalance),
198+
gasCost,
196199
stateDiff: {
197200
pre: {
198201
[USER_ADDRESS_MOCK]: { balance: previousBalance },
@@ -317,6 +320,24 @@ describe('Simulation Utils', () => {
317320
tokenBalanceChanges: [],
318321
});
319322
});
323+
324+
it('ignoring gas cost', async () => {
325+
simulateTransactionsMock.mockResolvedValueOnce(
326+
createNativeBalanceResponse('0x3', '0x8', 2),
327+
);
328+
329+
const result = await getBalanceChanges(REQUEST_MOCK);
330+
331+
expect(result).toStrictEqual({
332+
nativeBalanceChange: {
333+
difference: '0x7',
334+
isDecrease: false,
335+
newBalance: '0xa',
336+
previousBalance: '0x3',
337+
},
338+
tokenBalanceChanges: [],
339+
});
340+
});
320341
});
321342

322343
describe('returns token balance changes', () => {

packages/transaction-controller/src/utils/balance-changes.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,11 @@ function getNativeBalanceChange(
195195
return undefined;
196196
}
197197

198-
return getSimulationBalanceChange(previousBalance, newBalance);
198+
return getSimulationBalanceChange(
199+
previousBalance,
200+
newBalance,
201+
transactionResponse.gasCost,
202+
);
199203
}
200204

201205
/**
@@ -622,13 +626,17 @@ function extractLogs(
622626
*
623627
* @param previousBalance - The previous balance.
624628
* @param newBalance - The new balance.
629+
* @param offset - Optional offset to apply to the new balance.
625630
* @returns The balance change data or undefined if unchanged.
626631
*/
627632
function getSimulationBalanceChange(
628633
previousBalance: Hex,
629634
newBalance: Hex,
635+
offset: number = 0,
630636
): SimulationBalanceChange | undefined {
631-
const differenceBN = hexToBN(newBalance).sub(hexToBN(previousBalance));
637+
const newBalanceBN = hexToBN(newBalance).add(new BN(offset));
638+
const previousBalanceBN = hexToBN(previousBalance);
639+
const differenceBN = newBalanceBN.sub(previousBalanceBN);
632640
const isDecrease = differenceBN.isNeg();
633641
const difference = toHex(differenceBN.abs());
634642

@@ -639,7 +647,7 @@ function getSimulationBalanceChange(
639647

640648
return {
641649
previousBalance,
642-
newBalance,
650+
newBalance: toHex(newBalanceBN),
643651
difference,
644652
isDecrease,
645653
};

packages/transaction-controller/src/utils/gas-fee-tokens.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ describe('Gas Fee Tokens Utils', () => {
6969
feeRecipient: '0x6',
7070
rateWei: '0x7',
7171
transferEstimate: '0x7a',
72+
serviceFee: '0x7b',
7273
},
7374
{
7475
token: {
@@ -81,6 +82,7 @@ describe('Gas Fee Tokens Utils', () => {
8182
feeRecipient: '0xa',
8283
rateWei: '0xb',
8384
transferEstimate: '0xba',
85+
serviceFee: '0xbb',
8486
},
8587
],
8688
},
@@ -97,6 +99,7 @@ describe('Gas Fee Tokens Utils', () => {
9799
amount: '0x4',
98100
balance: '0x5',
99101
decimals: 3,
102+
fee: '0x7b',
100103
gas: '0x1',
101104
gasTransfer: '0x7a',
102105
maxFeePerGas: '0x2',
@@ -110,6 +113,7 @@ describe('Gas Fee Tokens Utils', () => {
110113
amount: '0x8',
111114
balance: '0x9',
112115
decimals: 4,
116+
fee: '0xbb',
113117
gas: '0x1',
114118
gasTransfer: '0xba',
115119
maxFeePerGas: '0x2',
@@ -143,6 +147,7 @@ describe('Gas Fee Tokens Utils', () => {
143147
feeRecipient: '0x6',
144148
rateWei: '0x7',
145149
transferEstimate: '0x7a',
150+
serviceFee: '0x7b',
146151
},
147152
],
148153
},
@@ -162,6 +167,7 @@ describe('Gas Fee Tokens Utils', () => {
162167
feeRecipient: '0xd',
163168
rateWei: '0xe',
164169
transferEstimate: '0xee',
170+
serviceFee: '0xef',
165171
},
166172
],
167173
},
@@ -178,6 +184,7 @@ describe('Gas Fee Tokens Utils', () => {
178184
amount: '0x4',
179185
balance: '0x5',
180186
decimals: 3,
187+
fee: '0x7b',
181188
gas: '0x1',
182189
gasTransfer: '0x7a',
183190
maxFeePerGas: '0x2',

packages/transaction-controller/src/utils/gas-fee-tokens.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ function parseGasFeeTokens(response: SimulationResponse): GasFeeToken[] {
126126
amount: tokenFee.balanceNeededToken,
127127
balance: tokenFee.currentBalanceToken,
128128
decimals: tokenFee.token.decimals,
129+
fee: tokenFee.serviceFee,
129130
gas: feeLevel.gas,
130131
gasTransfer: tokenFee.transferEstimate,
131132
maxFeePerGas: feeLevel.maxFeePerGas,

0 commit comments

Comments
 (0)