1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 03:12:42 +02:00

adds test and fix for conversion to use numeric (#17758)

This commit is contained in:
Brad Decker 2023-02-15 10:26:32 -06:00 committed by GitHub
parent 9e9f9126cc
commit 52b3d58cc3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 233 additions and 8 deletions

View File

@ -1,8 +1,233 @@
import { calcGasTotal } from './transactions-controller-utils';
import { EtherDenomination } from '../constants/common';
import { CHAIN_IDS } from '../constants/network';
import { TransactionEnvelopeType } from '../constants/transaction';
import { Numeric } from '../modules/Numeric';
import {
calcGasTotal,
calcTokenAmount,
getSwapsTokensReceivedFromTxMeta,
TOKEN_TRANSFER_LOG_TOPIC_HASH,
} from './transactions-controller-utils';
describe('calcGasTotal()', () => {
it('should correctly compute gasTotal', () => {
const result = calcGasTotal(12, 15);
expect(result).toStrictEqual('17a');
describe('transaction controller utils', () => {
describe('calcGasTotal()', () => {
it('should correctly compute gasTotal', () => {
const result = calcGasTotal(12, 15);
expect(result).toStrictEqual('17a');
});
});
describe('getSwapsTokensReceivedFromTxMeta', () => {
it('returns null if txMeta is not well formed', () => {
expect(getSwapsTokensReceivedFromTxMeta('ETH', {}, '0x00')).toBe(null);
});
it('returns null if tokenSymbol is the default for network but txMeta does not contain a receipt', () => {
expect(
getSwapsTokensReceivedFromTxMeta(
'ETH',
{},
'0x00',
'8',
{},
CHAIN_IDS.MAINNET,
),
).toBe(null);
});
it('returns null if tokenSymbol is the default for network but txMeta does not contain a preTxBalance', () => {
expect(
getSwapsTokensReceivedFromTxMeta(
'ETH',
{ txReceipt: {}, postTxBalance: '0xe' },
'0x00',
'8',
{},
CHAIN_IDS.MAINNET,
),
).toBe(null);
});
it('returns null if tokenSymbol is the default for network but txMeta does not contain a postTxBalance', () => {
expect(
getSwapsTokensReceivedFromTxMeta(
'ETH',
{ txReceipt: {}, preTxBalance: '0xe' },
'0x00',
'8',
{},
CHAIN_IDS.MAINNET,
),
).toBe(null);
});
it('returns estimated receiving amount if post and pre tx balances are the same', () => {
expect(
getSwapsTokensReceivedFromTxMeta(
'ETH',
{
txReceipt: {},
preTxBalance: '0xe',
postTxBalance: '0xe',
swapMetaData: { token_to_amount: '0x1' },
},
'0x00',
'0x00',
'8',
{},
CHAIN_IDS.MAINNET,
),
).toBe('0x1');
});
it('returns postTxBalance minus preTxBalance less the cost of gas of the transaction', () => {
const preTxBalance = new Numeric('5000000', 10, EtherDenomination.WEI);
const postTxBalance = new Numeric('5500000', 10, EtherDenomination.WEI);
const gasUsed = new Numeric('28000', 10).toPrefixedHexString();
const effectiveGasPrice = new Numeric('21', 10).toPrefixedHexString();
const gasCost = calcGasTotal(gasUsed, effectiveGasPrice);
const ethReceived = postTxBalance
.minus(preTxBalance.minus(gasCost, 16))
.toDenomination(EtherDenomination.ETH)
.round(6);
expect(
getSwapsTokensReceivedFromTxMeta(
'ETH',
{
txReceipt: {
gasUsed,
effectiveGasPrice,
type: TransactionEnvelopeType.feeMarket,
},
preTxBalance: preTxBalance.toPrefixedHexString(),
postTxBalance: postTxBalance.toPrefixedHexString(),
swapMetaData: { token_to_amount: '0x1' },
},
'0x00',
'0x00',
'8',
{},
CHAIN_IDS.MAINNET,
),
).toBe(ethReceived.toString());
});
it('returns postTxBalance minus preTxBalance less the cost of gas of the transaction and the approval tx', () => {
const preTxBalance = new Numeric('5000000', 10, EtherDenomination.WEI);
const postTxBalance = new Numeric('5500000', 10, EtherDenomination.WEI);
const gasUsed = new Numeric('28000', 10).toPrefixedHexString();
const gasUsedApproval = new Numeric('75000', 10).toPrefixedHexString();
const effectiveGasPrice = new Numeric('21', 10).toPrefixedHexString();
const effectiveGasPriceApproval = new Numeric(
'15',
10,
).toPrefixedHexString();
const gasCost = new Numeric(
calcGasTotal(gasUsed, effectiveGasPrice),
16,
).add(calcGasTotal(gasUsedApproval, effectiveGasPriceApproval), 16);
const ethReceived = postTxBalance
.minus(preTxBalance.minus(gasCost))
.toDenomination(EtherDenomination.ETH)
.round(6);
expect(
getSwapsTokensReceivedFromTxMeta(
'ETH',
{
txReceipt: {
gasUsed,
effectiveGasPrice,
type: TransactionEnvelopeType.feeMarket,
},
preTxBalance: preTxBalance.toPrefixedHexString(),
postTxBalance: postTxBalance.toPrefixedHexString(),
swapMetaData: { token_to_amount: '0x1' },
},
'0x00',
'0x00',
'8',
{
txReceipt: {
gasUsed: gasUsedApproval,
effectiveGasPrice: effectiveGasPriceApproval,
type: TransactionEnvelopeType.feeMarket,
},
},
CHAIN_IDS.MAINNET,
),
).toBe(ethReceived.toString());
});
it('returns postTxBalance minus preTxBalance less the cost of gas of the transaction and the approval tx for legacy txs', () => {
const preTxBalance = new Numeric('5000000', 10, EtherDenomination.WEI);
const postTxBalance = new Numeric('5500000', 10, EtherDenomination.WEI);
const gasUsed = new Numeric('28000', 10).toPrefixedHexString();
const gasUsedApproval = new Numeric('75000', 10).toPrefixedHexString();
const gasPrice = new Numeric('21', 10).toPrefixedHexString();
const gasPriceApproval = new Numeric('15', 10).toPrefixedHexString();
const gasCost = new Numeric(calcGasTotal(gasUsed, gasPrice), 16).add(
calcGasTotal(gasUsedApproval, gasPriceApproval),
16,
);
const ethReceived = postTxBalance
.minus(preTxBalance.minus(gasCost))
.toDenomination(EtherDenomination.ETH)
.round(6);
expect(
getSwapsTokensReceivedFromTxMeta(
'ETH',
{
txReceipt: {
gasUsed,
type: TransactionEnvelopeType.legacy,
},
txParams: {
gasPrice,
},
preTxBalance: preTxBalance.toPrefixedHexString(),
postTxBalance: postTxBalance.toPrefixedHexString(),
swapMetaData: { token_to_amount: '0x1' },
},
'0x00',
'0x00',
'8',
{
txReceipt: {
gasUsed: gasUsedApproval,
type: TransactionEnvelopeType.feeMarket,
},
txParams: {
gasPrice: gasPriceApproval,
},
},
CHAIN_IDS.MAINNET,
),
).toBe(ethReceived.toString());
});
it('returns value from tokenTransferLogs if not default token for network', () => {
const logs = [
{
topics: [TOKEN_TRANSFER_LOG_TOPIC_HASH, '', '0x00'],
address: '0x00',
data: new Numeric('10000', 10).toPrefixedHexString(),
},
];
expect(
getSwapsTokensReceivedFromTxMeta(
'USDC',
{
txReceipt: { logs, status: '0x1' },
},
'0x00',
'0x00',
'8',
{
txReceipt: {},
},
CHAIN_IDS.MAINNET,
),
).toBe(calcTokenAmount(logs[0].data, 8).toString(10), 6);
});
});
});

View File

@ -4,7 +4,7 @@ import { TransactionEnvelopeType } from '../constants/transaction';
import { Numeric } from '../modules/Numeric';
import { isSwapsDefaultTokenSymbol } from '../modules/swaps.utils';
const TOKEN_TRANSFER_LOG_TOPIC_HASH =
export const TOKEN_TRANSFER_LOG_TOPIC_HASH =
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef';
export const TRANSACTION_NO_CONTRACT_ERROR_KEY = 'transactionErrorNoContract';
@ -82,7 +82,7 @@ export function getSwapsTokensReceivedFromTxMeta(
? txReceipt.effectiveGasPrice
: txMeta.txParams.gasPrice,
);
const totalGasCost = new Numeric(gasCost, 16).plus(approvalTxGasCost);
const totalGasCost = new Numeric(gasCost, 16).add(approvalTxGasCost);
const preTxBalanceLessGasCost = new Numeric(txMeta.preTxBalance, 16).minus(
totalGasCost,
@ -93,7 +93,7 @@ export function getSwapsTokensReceivedFromTxMeta(
16,
EtherDenomination.WEI,
)
.subtract(preTxBalanceLessGasCost)
.minus(preTxBalanceLessGasCost)
.toDenomination(EtherDenomination.ETH)
.toBase(10)
.round(6);