-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
139 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './numbers' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export const MAS_DECIMALS = 9 | ||
|
||
/** | ||
* Convert Mas float amount to 9 decimals bigint | ||
* | ||
* @param amount MAS amount in floating point representation | ||
Check warning on line 6 in packages/massa-web3/src/experimental/utils/numbers.ts GitHub Actions / build
|
||
* @returns amount in nanoMAS | ||
*/ | ||
export const fromMAS = (amount: string | number): bigint => { | ||
if (typeof amount === 'number') { | ||
amount = amount.toString() | ||
} | ||
let [integerPart, decimalPart] = amount.split('.') | ||
decimalPart = decimalPart ?? '' | ||
return BigInt(integerPart + decimalPart.padEnd(MAS_DECIMALS, '0')) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/massa-web3/test/experimental/unit/numbers.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { fromMAS } from '../../../src/experimental/utils' | ||
|
||
describe('amount format conversion tests', () => { | ||
it('MAS(string) => nMAS', () => { | ||
let amount = '0' | ||
expect(fromMAS(amount)).toStrictEqual(BigInt(0)) | ||
|
||
amount = '1.5234' | ||
expect(fromMAS(amount)).toStrictEqual(BigInt('1523400000')) | ||
|
||
amount = '0.123456789' | ||
expect(fromMAS(amount)).toStrictEqual(BigInt('123456789')) | ||
|
||
amount = '123456789' | ||
expect(fromMAS(amount)).toStrictEqual(BigInt('123456789000000000')) | ||
|
||
amount = '123456789.123456789' | ||
expect(fromMAS(amount)).toStrictEqual(BigInt('123456789123456789')) | ||
}) | ||
|
||
it('MAS(number) => nMAS', () => { | ||
let amount = 0 | ||
expect(fromMAS(amount)).toStrictEqual(BigInt(0)) | ||
|
||
amount = 1.5234 | ||
expect(fromMAS(amount)).toStrictEqual(BigInt('1523400000')) | ||
|
||
amount = 0.123456789 | ||
expect(fromMAS(amount)).toStrictEqual(BigInt('123456789')) | ||
|
||
amount = 123456789 | ||
expect(fromMAS(amount)).toStrictEqual(BigInt('123456789000000000')) | ||
|
||
// max safe floating point number | ||
// see https://stackoverflow.com/questions/45929493/node-js-maximum-safe-floating-point-number | ||
amount = 8388607.123456789 | ||
expect(fromMAS(amount)).toStrictEqual(BigInt('8388607123456789')) | ||
}) | ||
}) |