-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit denomination and conversion (#301)
* feat: balance utils with conversion and formatting * feat: balance utils with conversion and formatting * test: updated formatBalance call and tests * test: remove deprecated GAS value * chore: add to index
- Loading branch information
1 parent
d5006a3
commit 35888bc
Showing
7 changed files
with
174 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import BN from 'bn.js' | ||
import { | ||
formatKiltBalance, | ||
convertToTxUnit, | ||
asFemtoKilt, | ||
TRANSACTION_FEE, | ||
} from './Balance.utils' | ||
|
||
describe('formatKiltBalance', () => { | ||
const TESTVALUE = new BN('123456789000') | ||
const baseValue = new BN('1') | ||
it('formats the given balance', async () => { | ||
expect(formatKiltBalance(TESTVALUE)).toEqual('123.456 micro KILT') | ||
expect(formatKiltBalance(baseValue.mul(new BN(10).pow(new BN(3))))).toEqual( | ||
'1.000 pico KILT' | ||
) | ||
expect(formatKiltBalance(baseValue.mul(new BN(10).pow(new BN(6))))).toEqual( | ||
'1.000 nano KILT' | ||
) | ||
expect(formatKiltBalance(baseValue.mul(new BN(10).pow(new BN(9))))).toEqual( | ||
'1.000 micro KILT' | ||
) | ||
expect( | ||
formatKiltBalance(baseValue.mul(new BN(10).pow(new BN(12)))) | ||
).toEqual('1.000 milli KILT') | ||
expect( | ||
formatKiltBalance(baseValue.mul(new BN(10).pow(new BN(15)))) | ||
).toEqual('1.000 KILT') | ||
expect( | ||
formatKiltBalance(baseValue.mul(new BN(10).pow(new BN(18)))) | ||
).toEqual('1.000 Kilo KILT') | ||
expect( | ||
formatKiltBalance(baseValue.mul(new BN(10).pow(new BN(21)))) | ||
).toEqual('1.000 Mega KILT') | ||
expect( | ||
formatKiltBalance(baseValue.mul(new BN(10).pow(new BN(24)))) | ||
).toEqual('1.000 Giga KILT') | ||
expect( | ||
formatKiltBalance(baseValue.mul(new BN(10).pow(new BN(27)))) | ||
).toEqual('1.000 Tera KILT') | ||
}) | ||
}) | ||
describe('convertToTxUnit', () => { | ||
it('converts given value with given power to femto KILT', () => { | ||
expect(new BN(convertToTxUnit(new BN(1), -15).toString())).toEqual( | ||
new BN(1) | ||
) | ||
expect(new BN(convertToTxUnit(new BN(1), -12).toString())).toEqual( | ||
new BN('1000') | ||
) | ||
expect(new BN(convertToTxUnit(new BN(1), -9).toString())).toEqual( | ||
new BN('1000000') | ||
) | ||
expect(new BN(convertToTxUnit(new BN(1), -6).toString())).toEqual( | ||
new BN('1000000000') | ||
) | ||
expect(new BN(convertToTxUnit(new BN(1), -3).toString())).toEqual( | ||
new BN('1000000000000') | ||
) | ||
expect(new BN(convertToTxUnit(new BN(1), 0).toString())).toEqual( | ||
new BN('1000000000000000') | ||
) | ||
expect(new BN(convertToTxUnit(new BN(1), 3).toString())).toEqual( | ||
new BN('1000000000000000000') | ||
) | ||
expect(new BN(convertToTxUnit(new BN(1), 6).toString())).toEqual( | ||
new BN('1000000000000000000000') | ||
) | ||
expect(new BN(convertToTxUnit(new BN(1), 9).toString())).toEqual( | ||
new BN('1000000000000000000000000') | ||
) | ||
expect(new BN(convertToTxUnit(new BN(1), 12).toString())).toEqual( | ||
new BN('1000000000000000000000000000') | ||
) | ||
expect(new BN(convertToTxUnit(new BN(1), 15).toString())).toEqual( | ||
new BN('1000000000000000000000000000000') | ||
) | ||
expect(new BN(convertToTxUnit(new BN(1), 18).toString())).toEqual( | ||
new BN('1000000000000000000000000000000000') | ||
) | ||
}) | ||
}) | ||
describe('asFemtoKilt', () => { | ||
it('converts whole KILT to femtoKilt using convertToTxUnit', () => { | ||
expect(new BN(asFemtoKilt(new BN(1000)).toString())).toEqual( | ||
new BN('1000000000000000000') | ||
) | ||
}) | ||
}) | ||
|
||
describe('TRANSACTION_FEE', () => { | ||
it('equals 125 nano KILT', () => { | ||
expect(formatKiltBalance(TRANSACTION_FEE)).toEqual('125.000 nano KILT') | ||
}) | ||
}) |
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,36 @@ | ||
/** | ||
* @packageDocumentation | ||
* @module BalanceUtils | ||
* @preferred | ||
*/ | ||
|
||
import BN from 'bn.js' | ||
import { formatBalance } from '@polkadot/util' | ||
|
||
export const KILT_COIN = new BN(1) | ||
|
||
export function formatKiltBalance(amount: BN): string { | ||
return formatBalance(amount, { | ||
decimals: 15, | ||
withSiFull: true, | ||
withUnit: 'KILT', | ||
}) | ||
} | ||
|
||
export function convertToTxUnit(balance: BN, power: number): BN { | ||
return new BN(balance).mul(new BN(10).pow(new BN(15 + power))) | ||
} | ||
|
||
export function asFemtoKilt(balance: BN): BN { | ||
return convertToTxUnit(balance, 0) | ||
} | ||
|
||
export const TRANSACTION_FEE = convertToTxUnit(new BN(125), -9) | ||
|
||
export default { | ||
KILT_COIN, | ||
TRANSACTION_FEE, | ||
formatKiltBalance, | ||
asFemtoKilt, | ||
convertToTxUnit, | ||
} |
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