Skip to content

Commit

Permalink
Add Mas type
Browse files Browse the repository at this point in the history
  • Loading branch information
gregLibert committed May 3, 2024
1 parent 6516fc8 commit 9128666
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
47 changes: 47 additions & 0 deletions packages/massa-web3/src/experimental/basicElements/mas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const NB_DECIMALS = 9

export type Mas = bigint

export function fromMas(value: number): Mas {
if (!Number.isSafeInteger(value)) {
throw new Error('value is not a safe integer.')
}

return BigInt(value * 10 ** NB_DECIMALS)
}

export function fromMilliMas(value: number): Mas {
if (!Number.isSafeInteger(value)) {
throw new Error('value is not a safe integer.')
}

return BigInt(value * 10 ** (NB_DECIMALS - 3))
}

export function fromMicroMas(value: number): Mas {
if (!Number.isSafeInteger(value)) {
throw new Error('value is not a safe integer.')
}

return BigInt(value * 10 ** (NB_DECIMALS - 6))
}

export function fromNanoMas(value: number): Mas {
if (!Number.isSafeInteger(value)) {
throw new Error('value is not a safe integer.')
}

return BigInt(value * 10 ** (NB_DECIMALS - 9))
}

export function fromString(value: string): Mas {
const [integerPart, decimalPart = ''] = value.split('.')
return BigInt(integerPart + decimalPart.padEnd(NB_DECIMALS, '0'))
}

export function toString(value: Mas): string {
const valueString = value.toString()
const integerPart = valueString.slice(0, -NB_DECIMALS) || '0'
const decimalPart = valueString.slice(-NB_DECIMALS).replace(/0+$/, '')
return `${integerPart}${decimalPart.length ? '.' + decimalPart : ''}`
}
34 changes: 34 additions & 0 deletions packages/massa-web3/test/experimental/unit/mas.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as Mas from '../../../src/experimental/basicElements/mas'

describe('amount conversion', () => {
it('converts from integer', () => {
expect(Mas.fromMas(1)).toStrictEqual(1_000_000_000n)
expect(Mas.fromMas(1234)).toStrictEqual(1_234_000_000_000n)
expect(() => Mas.fromMas(1.1)).toThrow('value is not a safe integer.')

expect(Mas.fromMilliMas(1)).toStrictEqual(1_000_000n)
expect(Mas.fromMilliMas(1234)).toStrictEqual(1_234_000_000n)
expect(() => Mas.fromMilliMas(1.1)).toThrow('value is not a safe integer.')

expect(Mas.fromMicroMas(1)).toStrictEqual(1_000n)
expect(Mas.fromMicroMas(1234)).toStrictEqual(1_234_000n)
expect(() => Mas.fromMicroMas(1.1)).toThrow('value is not a safe integer.')

expect(Mas.fromNanoMas(1)).toStrictEqual(1n)
expect(Mas.fromNanoMas(1234)).toStrictEqual(1_234n)
expect(() => Mas.fromNanoMas(1.1)).toThrow('value is not a safe integer.')
})

it('converts from string', () => {
expect(Mas.fromString('1')).toStrictEqual(1_000_000_000n)
expect(Mas.fromString('1.1')).toStrictEqual(1_100_000_000n)
expect(Mas.fromString('01234.56789')).toStrictEqual(1_234_567_890_000n)
expect(Mas.fromString('01234.567890000')).toStrictEqual(1_234_567_890_000n)
})

it('converts to string', () => {
expect(Mas.toString(1_000_000_000n)).toStrictEqual('1')
expect(Mas.toString(1_100_000_000n)).toStrictEqual('1.1')
expect(Mas.toString(1_234_567_890_000n)).toStrictEqual('1234.56789')
})
})

1 comment on commit 9128666

@github-actions
Copy link

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

St.
Category Percentage Covered / Total
🔴 Statements 41.93% 829/1977
🔴 Branches 18.86% 93/493
🔴 Functions 32.03% 123/384
🔴 Lines 41.77% 817/1956

Test suite run success

50 tests passing in 8 suites.

Report generated by 🧪jest coverage report action from 9128666

Please sign in to comment.