A Metamask fork with Infura removed and default networks editable
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ciphermask/app/scripts/lib/nonce-tracker.js

58 lines
1.8 KiB

const EthQuery = require('eth-query')
class NonceTracker {
7 years ago
constructor ({ blockTracker, provider, getPendingTransactions }) {
this.blockTracker = blockTracker
this.ethQuery = new EthQuery(provider)
this.getPendingTransactions = getPendingTransactions
this.lockMap = {}
}
// releaseLock must be called
// releaseLock must be called after adding signed tx to pending transactions (or discarding)
7 years ago
async getNonceLock (address) {
// await lock free
await this.lockMap[address]
// take lock
const releaseLock = this._takeLock(address)
// calculate next nonce
const currentBlock = await this._getCurrentBlock()
const blockNumber = currentBlock.number
const pendingTransactions = this.getPendingTransactions(address)
const baseCount = await this._getTxCount(address, blockNumber)
7 years ago
const nextNonce = parseInt(baseCount) + pendingTransactions.length
// return next nonce and release cb
return { nextNonce: nextNonce.toString(16), releaseLock }
}
7 years ago
async _getCurrentBlock () {
const currentBlock = this.blockTracker.getCurrentBlock()
if (currentBlock) return currentBlock
return await Promise((reject, resolve) => {
this.blockTracker.once('latest', resolve)
})
}
7 years ago
_takeLock (lockId) {
let releaseLock = null
// create and store lock
const lock = new Promise((resolve, reject) => { releaseLock = resolve })
this.lockMap[lockId] = lock
// setup lock teardown
7 years ago
lock.then(() => delete this.lockMap[lockId])
return releaseLock
}
_getTxCount (address, blockNumber) {
return new Promise((resolve, reject) => {
this.ethQuery.getTransactionCount(address, blockNumber, (err, result) => {
err ? reject(err) : resolve(result)
})
})
}
}
module.exports = NonceTracker