Skip to content

Commit

Permalink
feat(AE): Allow to spend % of balance. (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
nduchak authored May 3, 2019
1 parent 5909161 commit f97a2ae
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
36 changes: 35 additions & 1 deletion es/ae/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import stampit from '@stamp/it'
import Tx from '../tx'
import Chain from '../chain'
import Account from '../account'
import TxBuilder from '../tx/builder'
import * as R from 'ramda'
import { BigNumber } from 'bignumber.js'

/**
* Sign and post a transaction to the chain
Expand Down Expand Up @@ -60,6 +62,38 @@ async function spend (amount, recipientId, options = {}) {
return this.send(spendTx, opt)
}

/**
* Send a percentage of funds to another account
* @instance
* @category async
* @rtype (percentage: Number|String, recipientId: String, options?: Object) => Promise[String]
* @param {Number|String} percentage - Percentage of amount to spend
* @param {String} recipientId - Address of recipient account
* @param {Object} options - Options
* @return {String|String} Transaction or transaction hash
*/
async function transferFunds (percentage, recipientId, options = { excludeFee: false }) {
if (percentage < 0 || percentage > 1) throw new Error(`Percentage should be a number between 0 and 1, got ${percentage}`)
const opt = R.merge(this.Ae.defaults, options)

const requestTransferAmount = BigNumber(await this.balance(await this.address())).times(percentage)
let spendTx = await this.spendTx(R.merge(opt, { senderId: await this.address(), recipientId, amount: requestTransferAmount }))

const { tx: txObject } = TxBuilder.unpackTx(spendTx)
// If the requestTransferAmount should include the fee keep calculating the fee
let amount = requestTransferAmount
if (!options.excludeFee) {
while (amount.plus(txObject.fee).gt(requestTransferAmount)) {
amount = requestTransferAmount.minus(txObject.fee)
}
}

// Rebuild tx
spendTx = await this.spendTx(R.merge(opt, { senderId: await this.address(), recipientId, amount }))

return this.send(spendTx, opt)
}

/**
* Remove all listeners for RPC
* @instance
Expand Down Expand Up @@ -90,7 +124,7 @@ function destroyInstance () {
* @return {Object} Ae instance
*/
const Ae = stampit(Tx, Account, Chain, {
methods: { send, spend, destroyInstance }
methods: { send, spend, transferFunds, destroyInstance }
})

export default Ae
8 changes: 8 additions & 0 deletions test/integration/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ describe('Accounts', function () {
return wallet.balance(await wallet.address()).should.eventually.be.a('string')
})

it('Spend 50% of balance', async () => {
const balance = await wallet.balance(await wallet.address())

await wallet.transferFunds(0.5, 'ak_DMNCzsVoZnpV5fe8FTQnNsTfQ48YM5C3WbHPsJyHjAuTXebFi')
const balanceAfter = await wallet.balance(await wallet.address())
BigNumber(balance).div(balanceAfter).toNumber().should.be.equal(2)
})

it('spends tokens', async () => {
const ret = await wallet.spend(1, receiver)
ret.should.have.property('tx')
Expand Down

0 comments on commit f97a2ae

Please sign in to comment.