-
Notifications
You must be signed in to change notification settings - Fork 25
Unit denomination and conversion #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e0cfe10
feat: balance utils with conversion and formatting
3f11aa6
feat: balance utils with conversion and formatting
b2ac210
test: updated formatBalance call and tests
1dc1623
test: remove deprecated GAS value
74dbfd6
chore: add to index
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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') | ||
| ) | ||
| }) | ||
LeonFLK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }) | ||
| 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 hidden or 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, | ||
| } | ||
LeonFLK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.