-
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
134 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
34 changes: 34 additions & 0 deletions
34
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,34 @@ | ||
import { fromMAS } from '../../../src/experimental/utils' | ||
|
||
describe('amount format conversion tests', () => { | ||
it('MAS(string) 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' | ||
expect(fromMAS(amount)).toStrictEqual(BigInt('123456789000000000')) | ||
}) | ||
|
||
it('MAS(number) 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')) | ||
}) | ||
}) |