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
Next Next commit
feat: balance utils with conversion and formatting
  • Loading branch information
LeonfLK committed Sep 25, 2020
commit e0cfe10fbb5df2da0d0158469db84df4f48e884a
15 changes: 12 additions & 3 deletions src/balance/Balance.chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import BN from 'bn.js'
import { getCached } from '../blockchainApiConnection'
import Identity from '../identity/Identity'
import IPublicIdentity from '../types/PublicIdentity'
import BalanceUtils from './Balance.utils'

/**
* Fetches the current balance of the account with [accountAddress].
Expand Down Expand Up @@ -93,7 +94,8 @@ export async function listenToBalanceChanges(
*
* @param identity Identity to use for token transfer.
* @param accountAddressTo Address of the receiver account.
* @param amount Amount of Femto-Kilt (1e-15) to transfer.
* @param amount Amount of Units to transfer.
* @param exponent Magnitude of the amount.
* @returns Promise containing the transaction status.
*
* @example
Expand All @@ -117,9 +119,16 @@ export async function listenToBalanceChanges(
export async function makeTransfer(
identity: Identity,
accountAddressTo: IPublicIdentity['address'],
amount: BN
amount: BN,
exponent = -15
): Promise<SubmittableResult> {
const blockchain = await getCached()
const transfer = blockchain.api.tx.balances.transfer(accountAddressTo, amount)
const transfer = blockchain.api.tx.balances.transfer(
accountAddressTo,
BalanceUtils.convertToTxUnit(
amount,
(exponent >= 0 ? 1 : -1) * Math.floor(Math.abs(exponent))
)
)
return blockchain.submitTx(identity, transfer)
}
47 changes: 47 additions & 0 deletions src/balance/Balance.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @packageDocumentation
* @module BalanceUtils
* @preferred
*/

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(
amount,
{
withSiFull: true,
withUnit: 'KILT',
},
15
)
}

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 default {
KILT_COIN,
KILT_FEMTO_COIN,
TRANSACTION_FEE,
MIN_BALANCE,
ENDOWMENT,
formatKiltBalance,
asFemtoKilt,
convertToTxUnit,
}
1 change: 1 addition & 0 deletions src/balance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*/

export * from './Balance.chain'
export * from './Balance.utils'