Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Chain): add new method waitFOrTxConfirm #874

Merged
merged 2 commits into from
Jan 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
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)
})
})