aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib
diff options
context:
space:
mode:
Diffstat (limited to 'app/scripts/lib')
-rw-r--r--app/scripts/lib/auto-faucet.js20
-rw-r--r--app/scripts/lib/nodeify.js27
-rw-r--r--app/scripts/lib/nonce-tracker.js84
-rw-r--r--app/scripts/lib/tx-utils.js18
4 files changed, 106 insertions, 43 deletions
diff --git a/app/scripts/lib/auto-faucet.js b/app/scripts/lib/auto-faucet.js
deleted file mode 100644
index 38d54ba5e..000000000
--- a/app/scripts/lib/auto-faucet.js
+++ /dev/null
@@ -1,20 +0,0 @@
-const uri = 'https://faucet.metamask.io/'
-const METAMASK_DEBUG = 'GULP_METAMASK_DEBUG'
-const env = process.env.METAMASK_ENV
-
-module.exports = function (address) {
- // Don't faucet in development or test
- if (METAMASK_DEBUG === true || env === 'test') return
- global.log.info('auto-fauceting:', address)
- const data = address
- const headers = new Headers()
- headers.append('Content-type', 'application/rawdata')
- fetch(uri, {
- method: 'POST',
- headers,
- body: data,
- })
- .catch((err) => {
- console.error(err)
- })
-}
diff --git a/app/scripts/lib/nodeify.js b/app/scripts/lib/nodeify.js
index 51d89a8fb..299bfe624 100644
--- a/app/scripts/lib/nodeify.js
+++ b/app/scripts/lib/nodeify.js
@@ -1,24 +1,9 @@
-module.exports = function (promiseFn) {
- return function () {
- var args = []
- for (var i = 0; i < arguments.length - 1; i++) {
- args.push(arguments[i])
- }
- var cb = arguments[arguments.length - 1]
+const promiseToCallback = require('promise-to-callback')
- const nodeified = promiseFn.apply(this, args)
-
- if (!nodeified) {
- const methodName = String(promiseFn).split('(')[0]
- throw new Error(`The ${methodName} did not return a Promise, but was nodeified.`)
- }
- nodeified.then(function (result) {
- cb(null, result)
- })
- .catch(function (reason) {
- cb(reason)
- })
-
- return nodeified
+module.exports = function(fn, context) {
+ return function(){
+ const args = [].slice.call(arguments)
+ const callback = args.pop()
+ promiseToCallback(fn.apply(context, args))(callback)
}
}
diff --git a/app/scripts/lib/nonce-tracker.js b/app/scripts/lib/nonce-tracker.js
new file mode 100644
index 000000000..b76dac4e8
--- /dev/null
+++ b/app/scripts/lib/nonce-tracker.js
@@ -0,0 +1,84 @@
+const EthQuery = require('eth-query')
+const assert = require('assert')
+const Mutex = require('await-semaphore').Mutex
+
+class NonceTracker {
+
+ constructor ({ blockTracker, provider, getPendingTransactions }) {
+ this.blockTracker = blockTracker
+ this.ethQuery = new EthQuery(provider)
+ this.getPendingTransactions = getPendingTransactions
+ this.lockMap = {}
+ }
+
+ async getGlobalLock () {
+ const globalMutex = this._lookupMutex('global')
+ // await global mutex free
+ const releaseLock = await globalMutex.acquire()
+ return { releaseLock }
+ }
+
+ // releaseLock must be called
+ // releaseLock must be called after adding signed tx to pending transactions (or discarding)
+ async getNonceLock (address) {
+ // await global mutex free
+ await this._globalMutexFree()
+ // await lock free, then take lock
+ const releaseLock = await this._takeMutex(address)
+ // calculate next nonce
+ // we need to make sure our base count
+ // and pending count are from the same block
+ const currentBlock = await this._getCurrentBlock()
+ const pendingTransactions = this.getPendingTransactions(address)
+ const pendingCount = pendingTransactions.length
+ assert(Number.isInteger(pendingCount), 'nonce-tracker - pendingCount is an integer')
+ const baseCountHex = await this._getTxCount(address, currentBlock)
+ const baseCount = parseInt(baseCountHex, 16)
+ assert(Number.isInteger(baseCount), 'nonce-tracker - baseCount is an integer')
+ const nextNonce = baseCount + pendingCount
+ assert(Number.isInteger(nextNonce), 'nonce-tracker - nextNonce is an integer')
+ // return next nonce and release cb
+ return { nextNonce, releaseLock }
+ }
+
+ async _getCurrentBlock () {
+ const currentBlock = this.blockTracker.getCurrentBlock()
+ if (currentBlock) return currentBlock
+ return await Promise((reject, resolve) => {
+ this.blockTracker.once('latest', resolve)
+ })
+ }
+
+ async _getTxCount (address, currentBlock) {
+ const blockNumber = currentBlock.number
+ return new Promise((resolve, reject) => {
+ this.ethQuery.getTransactionCount(address, blockNumber, (err, result) => {
+ err ? reject(err) : resolve(result)
+ })
+ })
+ }
+
+ async _globalMutexFree () {
+ const globalMutex = this._lookupMutex('global')
+ const release = await globalMutex.acquire()
+ release()
+ }
+
+ async _takeMutex (lockId) {
+ const mutex = this._lookupMutex(lockId)
+ const releaseLock = await mutex.acquire()
+ return releaseLock
+ }
+
+ _lookupMutex (lockId) {
+ let mutex = this.lockMap[lockId]
+ if (!mutex) {
+ mutex = new Mutex()
+ this.lockMap[lockId] = mutex
+ }
+ return mutex
+ }
+
+}
+
+module.exports = NonceTracker
diff --git a/app/scripts/lib/tx-utils.js b/app/scripts/lib/tx-utils.js
index 149d93102..8f6943937 100644
--- a/app/scripts/lib/tx-utils.js
+++ b/app/scripts/lib/tx-utils.js
@@ -106,8 +106,13 @@ module.exports = class txProviderUtils {
return ethTx
}
- publishTransaction (rawTx, cb) {
- this.query.sendRawTransaction(rawTx, cb)
+ publishTransaction (rawTx) {
+ return new Promise((resolve, reject) => {
+ this.query.sendRawTransaction(rawTx, (err, ress) => {
+ if (err) reject(err)
+ else resolve(ress)
+ })
+ })
}
validateTxParams (txParams, cb) {
@@ -118,6 +123,15 @@ module.exports = class txProviderUtils {
}
}
+ sufficientBalance (txParams, hexBalance) {
+ const balance = hexToBn(hexBalance)
+ const value = hexToBn(txParams.value)
+ const gasLimit = hexToBn(txParams.gas)
+ const gasPrice = hexToBn(txParams.gasPrice)
+
+ const maxCost = value.add(gasLimit.mul(gasPrice))
+ return balance.gte(maxCost)
+ }
}