Skip to content

Commit

Permalink
fix: send tokens with decimals > 20
Browse files Browse the repository at this point in the history
  • Loading branch information
salimtb committed Oct 2, 2023
1 parent b7ad9f6 commit 9642e20
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/scripts/controllers/swaps.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -735,7 +735,7 @@ describe('SwapsController', function () {
fee: '-0.061067',
metaMaskFee: '0.5050505050505050505',
performance: '6',
total: '5.4338824949494949495',
total: '5.4338824949494949494949494949495',
medianMetaMaskFee: '0.44444444444444444444',
},
ethFee: '0.113536',
Expand Down Expand Up @@ -800,7 +800,7 @@ describe('SwapsController', function () {
fee: '-0.061067',
metaMaskFee: '0.5050505050505050505',
performance: '6',
total: '5.4338824949494949495',
total: '5.4338824949494949494949494949495',
medianMetaMaskFee: '0.44444444444444444444',
},
ethFee: '0.113822',
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.00019818832554181190762092402392');

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

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

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.00019818832554181190762092402392');

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

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

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.0213447171824973319103521878335');
});

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.0213447171824973319103521878335');
});
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 = 32;
BigNumber.config({ DECIMAL_PLACES: MAX_DECIMALS_FOR_TOKENS });
export type NumericValue = string | number | BN | BigNumber;
export type NumericBase = 10 | 16;

Expand Down

0 comments on commit 9642e20

Please sign in to comment.