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: sending an ERC20 token with an amount with more decimals than the token decimal, results in nothing #6754

Merged
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
3 changes: 3 additions & 0 deletions app/components/Views/SendFlow/Amount/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
fromTokenMinimalUnitString,
toHexadecimal,
hexToBN,
formatValueToMatchTokenDecimals,
} from '../../../../util/number';
import {
getTicker,
Expand Down Expand Up @@ -610,6 +611,8 @@ class Amount extends PureComponent {
if (value && value.includes(',')) {
value = inputValue.replace(',', '.');
}

value = formatValueToMatchTokenDecimals(value, selectedAsset.decimals);
blackdevelopa marked this conversation as resolved.
Show resolved Hide resolved
if (
!selectedAsset.tokenId &&
this.validateAmount(value, internalPrimaryCurrencyIsCrypto)
Expand Down
14 changes: 14 additions & 0 deletions app/util/number/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -840,3 +840,17 @@ export const isZeroValue = (value) => {
}
return value === '0x0' || (isBN(value) && value.isZero()) || isZero(value);
};

export const formatValueToMatchTokenDecimals = (value, decimal) => {
if (value === null || value === undefined) {
return value;
}
const decimalIndex = value.indexOf('.');
if (decimalIndex !== -1) {
const fractionalLength = value.substring(decimalIndex + 1).length;
if (fractionalLength > decimal) {
value = parseFloat(value).toFixed(decimal);
}
}
return value;
};
25 changes: 25 additions & 0 deletions app/util/number/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
fastSplit,
fiatNumberToTokenMinimalUnit,
fiatNumberToWei,
formatValueToMatchTokenDecimals,
fromTokenMinimalUnit,
fromTokenMinimalUnitString,
fromWei,
Expand Down Expand Up @@ -819,3 +820,27 @@ describe('Number utils :: isZeroValue', () => {
expect(isZeroValue(toBN('0'))).toBe(true);
});
});

describe('Number utils :: formatValueToMatchTokenDecimals', () => {
it('should return a formatted value if the submitted decimals is 0', () => {
expect(formatValueToMatchTokenDecimals('1.0', 0)).toBe('1');
});
it('should return the value if value is null', () => {
expect(formatValueToMatchTokenDecimals(null, 18)).toBe(null);
});
it('should return the value if the decimal is undefined', () => {
expect(formatValueToMatchTokenDecimals('1', undefined)).toBe('1');
});
it('should return a formatted value if the decimal is null', () => {
expect(formatValueToMatchTokenDecimals('1', null)).toBe('1');
});
it('should return the value if the decimal is not a number', () => {
expect(formatValueToMatchTokenDecimals('1', 'a')).toBe('1');
});
it('should return the value if the value decimal is equal to or less than the submitted decimal', () => {
expect(formatValueToMatchTokenDecimals('1.2348', 4)).toBe('1.2348');
});
it('should return a formatted value if the value decimal is greater than the submitted decimal', () => {
expect(formatValueToMatchTokenDecimals('1.234567', 4)).toBe('1.2346');
});
});