Skip to content

Commit

Permalink
Unit denomination and conversion (#301)
Browse files Browse the repository at this point in the history
* 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
LeonFLK authored and Dudleyneedham committed Oct 12, 2020
1 parent 7121ce1 commit 4358fc4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
10 changes: 5 additions & 5 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 Femto-Kilt (1e-15) to transfer.
* @param exponent
* @param amount Amount of Units to transfer.
* @param exponent Magnitude of the amount. Default magnitude of Femto-Kilt.
* @returns Promise containing the transaction status.
* @example
* <BR>
Expand All @@ -120,7 +120,7 @@ export async function makeTransfer(
accountAddressTo: IPublicIdentity['address'],
amount: BN,
exponent = -15
): Promise<SubmittableExtrinsic> {
): Promise<SubmittableResult> {
const blockchain = await getCached()
const cleanExponent =
(exponent >= 0 ? 1 : -1) * Math.floor(Math.abs(exponent))
Expand All @@ -130,5 +130,5 @@ export async function makeTransfer(
? amount
: BalanceUtils.convertToTxUnit(amount, cleanExponent)
)
return blockchain.signTx(identity, transfer)
return blockchain.submitTx(identity, transfer)
}
22 changes: 22 additions & 0 deletions src/balance/Balance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {
makeTransfer,
} from './Balance.chain'
import TYPE_REGISTRY from '../blockchainApiConnection/__mocks__/BlockchainQuery'
<<<<<<< HEAD
import { submitSignedTx } from '../blockchain'
=======
>>>>>>> 643f76b... Unit denomination and conversion (#301)
import BalanceUtils from './Balance.utils'

jest.mock('../blockchainApiConnection/BlockchainApiConnection')
Expand Down Expand Up @@ -64,6 +67,7 @@ describe('Balance', () => {
})

it('should make transfer', async () => {
<<<<<<< HEAD
const status = await makeTransfer(
alice,
bob.address,
Expand All @@ -89,6 +93,24 @@ describe('Balance', () => {
bob.address,
expectedAmount
)
=======
const status = await makeTransfer(alice, bob.address, new BN(100))
>>>>>>> 643f76b... Unit denomination and conversion (#301)
expect(status).toBeInstanceOf(SubmittableResult)
expect(status.isFinalized).toBeTruthy()
})
it('should make transfer of amount with arbitrary exponent', async () => {
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()
})
Expand Down

0 comments on commit 4358fc4

Please sign in to comment.