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

fix: send tokens with decimals > 20 #21147

Merged
merged 7 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 8 additions & 8 deletions app/scripts/controllers/swaps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,14 +733,14 @@ describe('SwapsController', function () {
gasEstimateWithRefund: '0xb8cae',
savings: {
fee: '-0.061067',
metaMaskFee: '0.5050505050505050505',
metaMaskFee: '0.50505050505050505050505050505050505',
performance: '6',
total: '5.4338824949494949495',
medianMetaMaskFee: '0.44444444444444444444',
total: '5.43388249494949494949494949494949495',
medianMetaMaskFee: '0.444444444444444444444444444444444444',
},
ethFee: '0.113536',
overallValueOfQuote: '49.886464',
metaMaskFeeInEth: '0.5050505050505050505',
metaMaskFeeInEth: '0.50505050505050505050505050505050505',
ethValueOfTokens: '50',
});
assert.strictEqual(
Expand Down Expand Up @@ -798,15 +798,15 @@ describe('SwapsController', function () {
gasEstimateWithRefund: '0xb8cae',
savings: {
fee: '-0.061067',
metaMaskFee: '0.5050505050505050505',
metaMaskFee: '0.50505050505050505050505050505050505',
performance: '6',
total: '5.4338824949494949495',
medianMetaMaskFee: '0.44444444444444444444',
total: '5.43388249494949494949494949494949495',
medianMetaMaskFee: '0.444444444444444444444444444444444444',
},
ethFee: '0.113822',
multiLayerL1TradeFeeTotal: '0x0103c18816d4e8',
overallValueOfQuote: '49.886178',
metaMaskFeeInEth: '0.5050505050505050505',
metaMaskFeeInEth: '0.50505050505050505050505050505050505',
ethValueOfTokens: '50',
});
assert.strictEqual(
Expand Down
31 changes: 23 additions & 8 deletions shared/modules/Numeric.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,37 +225,37 @@ describe('Numeric', () => {
it('should compute correct results for division of big numbers', () => {
expect(
new Numeric('175671432', 10).divide('686216', 10).toString(),
).toStrictEqual('256.00019818832554181191');
).toStrictEqual('256.000198188325541811907620924023922497');

expect(
new Numeric('1756714320', 10)
.divide(new Numeric('686216', 10))
.toString(),
).toStrictEqual('2560.00198188325541811908');
).toStrictEqual('2560.001981883255418119076209240239224967');

expect(
new Numeric('41756714320', 10)
.divide(new Numeric('6862160', 10))
.toString(),
).toStrictEqual('6085.06859647691106007438');
).toStrictEqual('6085.068596476911060074378912762162351213');
});

it('should compute correct results for division of negative big numbers', () => {
expect(
new Numeric('175671432', 10).divide('-686216', 10).toString(),
).toStrictEqual('-256.00019818832554181191');
).toStrictEqual('-256.000198188325541811907620924023922497');

expect(
new Numeric('1756714320', 10)
.divide(new Numeric('-686216', 10))
.toString(),
).toStrictEqual('-2560.00198188325541811908');
).toStrictEqual('-2560.001981883255418119076209240239224967');

expect(
new Numeric('-41756714320', 10)
.divide(new Numeric('-6862160', 10))
.toString(),
).toStrictEqual('6085.06859647691106007438');
).toStrictEqual('6085.068596476911060074378912762162351213');
});
});

Expand Down Expand Up @@ -377,15 +377,30 @@ describe('Numeric', () => {
it('should multiply the value by the inverse of conversionRate supplied when second parameter is true', () => {
expect(
new Numeric(10, 10).applyConversionRate(468.5, true).toString(),
).toStrictEqual('0.0213447171824973319');
).toStrictEqual('0.02134471718249733191035218783351121');
});

it('should multiply the value by the inverse of the BigNumber conversionRate supplied when second parameter is true', () => {
expect(
new Numeric(10, 10)
.applyConversionRate(new BigNumber(468.5, 10), true)
.toString(),
).toStrictEqual('0.0213447171824973319');
).toStrictEqual('0.02134471718249733191035218783351121');
});
it('should not return 0 if decimals is greater than 20', () => {
expect(
new Numeric(10, 10)
.applyConversionRate(new BigNumber(1e27, 10), true)
.toString(),
).toStrictEqual('0.00000000000000000000000001');
});

it('should return 0 if decimals is greater than 32', () => {
expect(
new Numeric(10, 10)
.applyConversionRate(new BigNumber(1e40, 10), true)
.toString(),
).toStrictEqual('0');
});
});
});
Expand Down
2 changes: 2 additions & 0 deletions shared/modules/Numeric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { addHexPrefix } from 'ethereumjs-util';
import { EtherDenomination } from '../constants/common';
import { stripHexPrefix } from './hexstring-utils';

const MAX_DECIMALS_FOR_TOKENS = 36;
BigNumber.config({ DECIMAL_PLACES: MAX_DECIMALS_FOR_TOKENS });
export type NumericValue = string | number | BN | BigNumber;
export type NumericBase = 10 | 16;

Expand Down