825772cf73
* refactor: added toBZZbaseUnit function * feat: added utility for attesting value is BZZ convertible to base units * fix: conversion from 15 to 16 decimal places, added unsafe versions * refactor: withdraw modal uses the safe conversion from BZZ * refactor: added BigNumber and Token class to handle BZZ digits correctly * refactor: extract deposit and withdraw functionality into single modal * test: added tests for Token * chore: removed unused component * chore: addressed PR review, token decimal is now integer 0-18 * chore: added comment to clarify the value restriction on token amount
40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import BigNumber from 'bignumber.js'
|
|
import { Token } from './Token'
|
|
|
|
describe('models/Token', () => {
|
|
describe('Token.fromDecimal', () => {
|
|
const values = [
|
|
{ bzz: '0', baseUnits: '0' },
|
|
{ bzz: '0.1', baseUnits: BigInt('1000000000000000') },
|
|
{ bzz: '9.9', baseUnits: BigInt('99000000000000000') },
|
|
]
|
|
|
|
// Test with default 16 decimal places
|
|
values.forEach(({ bzz, baseUnits }) => {
|
|
test(`converting ${bzz} => ${baseUnits}`, () => {
|
|
expect(Token.fromDecimal(bzz).toBigNumber.eq(baseUnits.toString())).toBe(true)
|
|
})
|
|
})
|
|
|
|
// Test with 0 decimal places
|
|
values.forEach(({ baseUnits }) => {
|
|
test(`converting ${baseUnits} => ${baseUnits} with 0 decimals`, () => {
|
|
expect(Token.fromDecimal(baseUnits, 0).toBigNumber.eq(baseUnits.toString())).toBe(true)
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('new Token', () => {
|
|
const cs = ['0', '1234567890', '99000000000000000']
|
|
const correctValues = [...cs, ...cs.map(BigInt), ...cs.map(v => new BigNumber(v))]
|
|
|
|
correctValues.forEach(v => {
|
|
test(`New Token ${v} of type ${typeof v}`, () => {
|
|
const t = new Token(v)
|
|
|
|
expect(t.toBigNumber.eq(v.toString())).toBe(true)
|
|
})
|
|
})
|
|
})
|
|
})
|