Skip to content

Commit

Permalink
feat(Chain): add new method waitFOrTxConfirm. Add new option { conf…
Browse files Browse the repository at this point in the history
…irm: 3 } to all of high lvl SDK API. Add tests. Adjust docs (#874)
  • Loading branch information
nduchak committed Jan 23, 2020
1 parent 41b7bd1 commit 43528f9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
15 changes: 14 additions & 1 deletion es/chain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const Chain = Oracle.compose({
Ae: {
methods: [
'sendTransaction', 'height', 'awaitHeight', 'poll', 'balance', 'getBalance', 'tx',
'mempool', 'topBlock', 'getTxInfo', 'txDryRun', 'getName', 'getNodeInfo', 'getAccount', 'getContractByteCode', 'getContract'
'mempool', 'topBlock', 'getTxInfo', 'txDryRun', 'getName', 'getNodeInfo', 'getAccount', 'getContractByteCode', 'getContract', 'waitForTxConfirm'
]
}
}
Expand Down Expand Up @@ -190,6 +190,19 @@ const Chain = Oracle.compose({
* @return {Object} Generation
*/

/**
* Wait for transaction confirmation
* @function waitForTxConfirm
* @instance
* @abstract
* @category async
* @rtype (txHash: String, { confirm: Number | Boolean } = { confirm: 3 }) => Promise<Number>
* @param {String} txHash - Generation hash or height
* @param {String} [options={}] - options
* @param {String} [options.confirm=3] - Block confirmation count
* @return {Promise<Number>} Current Height
*/

/**
* Get micro block transactions
* @function getMicroBlockTransactions
Expand Down
20 changes: 18 additions & 2 deletions es/chain/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,16 @@ async function sendTransaction (tx, options = {}) {

try {
const { txHash } = await this.api.postTransaction({ tx })
return waitMined ? { ...(await this.poll(txHash, options)), rawTx: tx } : { hash: txHash, rawTx: tx }

if (waitMined) {
const txData = { ...(await this.poll(txHash, options)), rawTx: tx }
// wait for transaction confirmation
if (options.confirm) {
return { ...txData, confirmationHeight: await this.waitForTxConfirm(txHash, options) }
}
return txData
}
return { hash: txHash, rawTx: tx }
} catch (e) {
throw Object.assign(
(new Error(e.message)),
Expand All @@ -59,6 +68,12 @@ async function sendTransaction (tx, options = {}) {
}
}

async function waitForTxConfirm (txHash, options = { confirm: 3 }) {
options.confirm = typeof options.confirm === 'boolean' && options.confirm ? 3 : options.confirm
const { blockHeight } = await this.tx(txHash)
return this.awaitHeight(blockHeight + options.confirm, options)
}

async function getAccount (address, { height, hash } = {}) {
if (height) return this.api.getAccountByPubkeyAndHeight(address, height)
if (hash) return this.api.getAccountByPubkeyAndHash(address, hash)
Expand Down Expand Up @@ -227,7 +242,8 @@ const ChainNode = Chain.compose(Oracle, TransactionValidator, NodePool, {
txDryRun,
getContractByteCode,
getContract,
getName
getName,
waitForTxConfirm
}
})

Expand Down
11 changes: 11 additions & 0 deletions test/integration/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,15 @@ describe('Node Chain', function () {
await client.poll(txHash).should.eventually.be.fulfilled
return client.poll('th_xxx', { blocks: 1 }).should.eventually.be.rejected
})

it('Wait for transaction confirmation', async () => {
const txData = await client.spend(1000, await client.address(), { confirm: true })
const isConfirmed = (await client.height()) >= txData.blockHeight + 3

isConfirmed.should.be.equal(true)

const txData2 = await client.spend(1000, await client.address(), { confirm: 4 })
const isConfirmed2 = (await client.height()) >= txData2.blockHeight + 4
isConfirmed2.should.be.equal(true)
})
})

0 comments on commit 43528f9

Please sign in to comment.