-
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
141 additions
and
30 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 / test / build
Check warning on line 6 in packages/massa-web3/src/experimental/utils/numbers.ts GitHub Actions / build
|
||
* @returns amount in nanoMAS | ||
*/ | ||
export const toNanoMas = (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 { toNanoMas } from '../../../src/experimental/utils' | ||
|
||
describe('amount format conversion tests', () => { | ||
it('MAS(string) => nMAS', () => { | ||
let amount = '0' | ||
expect(toNanoMas(amount)).toStrictEqual(BigInt(0)) | ||
|
||
amount = '1.5234' | ||
expect(toNanoMas(amount)).toStrictEqual(BigInt('1523400000')) | ||
|
||
amount = '0.123456789' | ||
expect(toNanoMas(amount)).toStrictEqual(BigInt('123456789')) | ||
|
||
amount = '123456789' | ||
expect(toNanoMas(amount)).toStrictEqual(BigInt('123456789000000000')) | ||
|
||
amount = '123456789.123456789' | ||
expect(toNanoMas(amount)).toStrictEqual(BigInt('123456789123456789')) | ||
}) | ||
|
||
it('MAS(number) => nMAS', () => { | ||
let amount = 0 | ||
expect(toNanoMas(amount)).toStrictEqual(BigInt(0)) | ||
|
||
amount = 1.5234 | ||
expect(toNanoMas(amount)).toStrictEqual(BigInt('1523400000')) | ||
|
||
amount = 0.123456789 | ||
expect(toNanoMas(amount)).toStrictEqual(BigInt('123456789')) | ||
|
||
amount = 123456789 | ||
expect(toNanoMas(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(toNanoMas(amount)).toStrictEqual(BigInt('8388607123456789')) | ||
}) | ||
}) |
1d47a35
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage report for experimental massa-web3
Test suite run success
10 tests passing in 4 suites.
Report generated by 🧪jest coverage report action from 1d47a35