Skip to content

Commit

Permalink
#11 Add isTransactionMined in TransactionLib
Browse files Browse the repository at this point in the history
  • Loading branch information
ceres3idoo committed Feb 22, 2019
1 parent 5fbd82e commit c355a1e
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/lib/TransactionLib/TransactionLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ const providerUrl = 'urlToProvider'
const web3Instance = new Web3(new Web3.providers.HttpProvider(providerUrl))
const ethApiLibClient = new EidooEthApiLib(ethApi)


function waitForMsAsync (time) {
return new Promise(resolve => setTimeout(resolve, time))
}

/**
* It gets nonce from input address.
*
Expand Down Expand Up @@ -251,6 +256,44 @@ class TransactionLib extends ITransactionLib {
throw new TransactionCallError(err)
}
}

async getTransactionReceipt(hash, fromAddress) {
try {
const { transactions } = await this.ethApiClient.getAccountTxsDetailsAsync(fromAddress)

const transactionReceipt = _.find(transactions, item => item.transactionReceipt.transactionHash === hash)
return transactionReceipt || null
} catch (err) {
throw new Error(`Error retriving transaction Receipt: ${hash}`)
}
}

/**
* It checks if the transaction is mined.
*
* @param {String} hash The transaction hash
*/
async isTransactionMined (hash, fromAddress) {
const maxWaitAllowedInMs = 5 * 1000
const startedAt = new Date()
let transactionReceipit = this.getTransactionReceipt(hash, fromAddress)
let isEnd = false
while (transactionReceipit !== null && !isEnd) {
const now = new Date()
isEnd = (+now - +startedAt) > maxWaitAllowedInMs
transactionReceipit = this.getTransactionReceipt(hash, fromAddress)
this.log.info({
fn: 'isTransactionMined',
hash,
transactionReceipit,
},
'Checking transaction...')
await waitForMsAsync(1000)
}

const isMined = !!transactionReceipit
return isMined
}
}

module.exports = TransactionLib

0 comments on commit c355a1e

Please sign in to comment.