diff --git a/README.md b/README.md index 35f0d9ce..383139dd 100644 --- a/README.md +++ b/README.md @@ -43,16 +43,16 @@ yarn add bnc-assist #### Script Tag The library uses [semantic versioning](https://semver.org/spec/v2.0.0.html). -The current version is 0.8.8. +The current version is 0.8.9. There are minified and non-minified versions. Put this script at the top of your `` ```html - + - + ``` ### Initialize the Library diff --git a/package.json b/package.json index a551d1db..2639b6f1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "bnc-assist", - "version": "0.8.8", + "version": "0.8.9", "description": "Blocknative Assist js library for Dapp developers", "main": "lib/assist.min.js", "scripts": { diff --git a/src/js/helpers/transaction-queue.js b/src/js/helpers/transaction-queue.js index 04be2a38..f0235cc0 100644 --- a/src/js/helpers/transaction-queue.js +++ b/src/js/helpers/transaction-queue.js @@ -34,9 +34,12 @@ export function getTxObjFromQueue(id) { export function isDuplicateTransaction({ value, to }, contract) { const { transactionQueue } = state let duplicate = transactionQueue.find(txObj => { + if (contract && typeof txObj.contract === 'undefined') return false + const sameMethod = contract ? contract.methodName === txObj.contract.methodName : true + const sameParams = contract ? argsEqual(contract.parameters, txObj.contract.parameters) : true diff --git a/src/js/helpers/utilities.js b/src/js/helpers/utilities.js index b79b8596..084e632d 100644 --- a/src/js/helpers/utilities.js +++ b/src/js/helpers/utilities.js @@ -242,7 +242,13 @@ export function stepToImageKey(step) { export function handleError(handlers = {}) { return errorObj => { - const { callback, reject, resolve } = handlers + const { callback, reject, resolve, promiEvent } = handlers + + if (promiEvent) { + promiEvent.emit('error', errorObj) + resolve() + return + } if (callback) { callback(errorObj) diff --git a/src/js/helpers/web3.js b/src/js/helpers/web3.js index a665f07f..41d5a50c 100644 --- a/src/js/helpers/web3.js +++ b/src/js/helpers/web3.js @@ -193,20 +193,26 @@ export function getTransactionParams( } }) - const gasPromise = new Promise(async (resolve, reject) => { + const gasPromise = new Promise(async resolve => { + let gas try { // Get a gas estimate based on if the tx is a contract method call // or regular transaction - const gas = contractMethod + gas = contractMethod ? await web3Functions.contractGas(version)( contractMethod, contractEventObj.parameters, txObject ) : await web3Functions.transactionGas(version)(txObject) - resolve(web3Functions.bigNumber(version)(gas)) } catch (e) { - reject(e) + // Sometimes MM can't estimate the gas, and will throw. + // In this case, use either the gas specified by the dapp + // dev, or if that doesn't exist 0 as we are unable to predict + // how much gas the transaction will consume. + gas = txObject.gas ? txObject.gas : 0 + } finally { + resolve(web3Functions.bigNumber(version)(gas)) } }) diff --git a/src/js/index.js b/src/js/index.js index 23786b76..25d791f4 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -58,6 +58,9 @@ function init(config) { configureWeb3(web3) } + // This is needed to ensure MetaMask will reload on every network change + window.web3 && window.web3.eth + // Get browser info getUserAgent() diff --git a/src/js/logic/send-transaction.js b/src/js/logic/send-transaction.js index dc51fd97..315029f1 100644 --- a/src/js/logic/send-transaction.js +++ b/src/js/logic/send-transaction.js @@ -45,7 +45,7 @@ function sendTransaction( const [sufficientBalance, ready] = await Promise.all([ hasSufficientBalance(transactionParams), prepareForTransaction('activePreflight').catch( - handleError({ resolve, reject, callback }) + handleError({ resolve, reject, callback, promiEvent }) ) ]) @@ -88,7 +88,7 @@ function sendTransaction( ) errorObj.eventCode = 'nsfFail' - handleError({ resolve, reject, callback })(errorObj) + handleError({ resolve, reject, callback, promiEvent })(errorObj) return }