Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: balance utils with conversion and formatting
  • Loading branch information
LeonfLK committed Sep 25, 2020
commit 3f11aa68e8410bdd85132f847cc5b69ffdacd65c
13 changes: 7 additions & 6 deletions src/balance/Balance.chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ export async function listenToBalanceChanges(

/**
* Transfer Kilt [amount] tokens to [toAccountAddress] using the given [[Identity]].
* <B>Note that balance amount is in Femto-Kilt (1e-15) and must be translated to Kilt-Coin</B>.
* <B>Note that the value of the transferred currency and the balance amount reported by the chain is in Femto-Kilt (1e-15), and must be translated to Kilt-Coin</B>.
*
* @param identity Identity to use for token transfer.
* @param accountAddressTo Address of the receiver account.
* @param amount Amount of Units to transfer.
* @param exponent Magnitude of the amount.
* @param exponent Magnitude of the amount. Default magnitude of Femto-Kilt.
* @returns Promise containing the transaction status.
*
* @example
Expand Down Expand Up @@ -123,12 +123,13 @@ export async function makeTransfer(
exponent = -15
): Promise<SubmittableResult> {
const blockchain = await getCached()
const cleanExponent =
(exponent >= 0 ? 1 : -1) * Math.floor(Math.abs(exponent))
const transfer = blockchain.api.tx.balances.transfer(
accountAddressTo,
BalanceUtils.convertToTxUnit(
amount,
(exponent >= 0 ? 1 : -1) * Math.floor(Math.abs(exponent))
)
cleanExponent === -15
? amount
: BalanceUtils.convertToTxUnit(amount, cleanExponent)
)
return blockchain.submitTx(identity, transfer)
}
18 changes: 18 additions & 0 deletions src/balance/Balance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
makeTransfer,
} from './Balance.chain'
import TYPE_REGISTRY from '../blockchainApiConnection/__mocks__/BlockchainQuery'
import BalanceUtils from './Balance.utils'

jest.mock('../blockchainApiConnection/BlockchainApiConnection')

Expand Down Expand Up @@ -65,4 +66,21 @@ describe('Balance', () => {
expect(status).toBeInstanceOf(SubmittableResult)
expect(status.isFinalized).toBeTruthy()
})
it('should make transfer of amount with arbitrary exponent', async () => {
const alice = await Identity.buildFromURI('//Alice')
const bob = await Identity.buildFromURI('//Bob')
const amount = new BN(10)
const exponent = -6
const expectedAmount = BalanceUtils.convertToTxUnit(
amount,
(exponent >= 0 ? 1 : -1) * Math.floor(Math.abs(exponent))
)
const status = await makeTransfer(alice, bob.address, amount, exponent)
expect(blockchainApi.tx.balances.transfer).toHaveBeenCalledWith(
bob.address,
expectedAmount
)
expect(status).toBeInstanceOf(SubmittableResult)
expect(status.isFinalized).toBeTruthy()
})
})
20 changes: 6 additions & 14 deletions src/balance/Balance.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,6 @@ import BN from 'bn.js'
import { formatBalance } from '@polkadot/util'

export const KILT_COIN = new BN(1)
export const KILT_FEMTO_COIN = new BN('1000000000000000')

export const TRANSACTION_FEE = KILT_COIN.divn(1000000000).muln(125)

export const MIN_BALANCE = KILT_COIN.muln(1)

export const ENDOWMENT = KILT_COIN.muln(30)

export function formatKiltBalance(amount: BN): string {
return formatBalance(
Expand All @@ -27,20 +20,19 @@ export function formatKiltBalance(amount: BN): string {
)
}

export function asFemtoKilt(balance: BN): BN {
return new BN(balance).mul(KILT_FEMTO_COIN)
}

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,
KILT_FEMTO_COIN,
TRANSACTION_FEE,
MIN_BALANCE,
ENDOWMENT,
formatKiltBalance,
asFemtoKilt,
convertToTxUnit,
Expand Down