Skip to content

Commit

Permalink
handle formatted string case
Browse files Browse the repository at this point in the history
  • Loading branch information
jmzwar committed Jul 7, 2022
1 parent 737bc30 commit 5d60c44
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/wei/__tests__/wei.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ describe('Wei numeric type', () => {
expect(b.toBN()).toEqual(BigNumber.from('14'));
});

it('constructs from a large formatted string', () => {
const a = new Wei('1,214,114.3');
expect(a.toBN()).toEqual(BigNumber.from('1214114300000000000000000'));
});

it('constructs from a smaller formatted string', () => {
const a = new Wei('114.3');
expect(a.toBN()).toEqual(BigNumber.from('114300000000000000000'));
});

it('constructs from a number', () => {
const a = new Wei('14.3');
expect(a.toBN()).toEqual(BigNumber.from('14300000000000000000'));
Expand Down
4 changes: 4 additions & 0 deletions packages/wei/src/wei.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export default class Wei {
} else {
this.bn = BigNumber.from(n);
}
} else if (typeof n === 'string') {
this.bn = BigNumber.from(
new Big(n.replaceAll(',', '') as string).mul(new Big(10).pow(this.p)).toFixed(0)
);
} else {
// not wei, scale it
// TODO: avoid use of Big.js, but this is a really easy way to do the conversion for now
Expand Down

0 comments on commit 5d60c44

Please sign in to comment.