Skip to content

Commit

Permalink
Fix bug with improper parsing of more than single digit NEAR amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
vgrichina committed Dec 20, 2019
1 parent d9da124 commit 9c6fc44
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/utils/format.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src.ts/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function parseNearAmount(amt?: string): string | null {
amt = amt.trim();
const split = amt.split('.');
if (split.length === 1) {
return `${amt.padEnd(NEAR_NOMINATION_EXP + 1, '0')}`;
return amt + '0'.repeat(NEAR_NOMINATION_EXP);
}
if (split.length > 2 || split[1].length > NEAR_NOMINATION_EXP) {
throw new Error(`Cannot parse '${amt}' as NEAR amount`);
Expand Down
3 changes: 2 additions & 1 deletion test/utils/format.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ test('formatting yoctonear amounts', async() => {
expect(nearlib.utils.format.formatNearAmount('910000000000000000000000', 0)).toEqual('1');
});

// TODO: Renable after moving to yoctonear
test('converting near to account balance units', async() => {
expect(nearlib.utils.format.parseNearAmount(null)).toEqual(null);
expect(nearlib.utils.format.parseNearAmount('5.3')).toEqual('5300000000000000000000000');
expect(nearlib.utils.format.parseNearAmount('5')).toEqual('5000000000000000000000000');
expect(nearlib.utils.format.parseNearAmount('1')).toEqual('1000000000000000000000000');
expect(nearlib.utils.format.parseNearAmount('10')).toEqual('10000000000000000000000000');
expect(nearlib.utils.format.parseNearAmount('0.000008999999999837087887')).toEqual('8999999999837087887');
expect(nearlib.utils.format.parseNearAmount('0.000008099099999837087887')).toEqual('8099099999837087887');
expect(nearlib.utils.format.parseNearAmount('0.000008099099999837087887')).not.toEqual('8099099999837087888');
Expand Down

0 comments on commit 9c6fc44

Please sign in to comment.