From 222e62d7f10ffe22dd606aea9c15e1547986c4ab Mon Sep 17 00:00:00 2001 From: Howard Braham Date: Mon, 17 Sep 2018 20:04:10 -0700 Subject: Bug Fix: #1789 and #4525 eth.getCode() with no contract --- .../controllers/transactions/tx-gas-utils.js | 23 +++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'app/scripts/controllers/transactions') diff --git a/app/scripts/controllers/transactions/tx-gas-utils.js b/app/scripts/controllers/transactions/tx-gas-utils.js index 3dd45507f..5ec728085 100644 --- a/app/scripts/controllers/transactions/tx-gas-utils.js +++ b/app/scripts/controllers/transactions/tx-gas-utils.js @@ -7,6 +7,8 @@ const { const { addHexPrefix } = require('ethereumjs-util') const SIMPLE_GAS_COST = '0x5208' // Hex for 21000, cost of a simple send. +import { TRANSACTION_NO_CONTRACT_ERROR_KEY } from '../../../../ui/app/constants/error-keys' + /** tx-gas-utils are gas utility methods for Transaction manager its passed ethquery @@ -32,6 +34,7 @@ class TxGasUtil { } catch (err) { txMeta.simulationFails = { reason: err.message, + errorKey: err.errorKey, } return txMeta } @@ -56,16 +59,22 @@ class TxGasUtil { return txParams.gas } - // if recipient has no code, gas is 21k max: const recipient = txParams.to const hasRecipient = Boolean(recipient) - let code - if (recipient) code = await this.query.getCode(recipient) - if (hasRecipient && (!code || code === '0x')) { - txParams.gas = SIMPLE_GAS_COST - txMeta.simpleSend = true // Prevents buffer addition - return SIMPLE_GAS_COST + if (hasRecipient) { + let code = await this.query.getCode(recipient) + + // If there's data in the params, but there's no code, it's not a valid contract + // For no code, Infura will return '0x', and Ganache will return '0x0' + if (txParams.data && (!code || code === '0x' || code === '0x0')) { + throw {errorKey: TRANSACTION_NO_CONTRACT_ERROR_KEY} + } + else if (!code) { + txParams.gas = SIMPLE_GAS_COST // For a standard ETH send, gas is 21k max + txMeta.simpleSend = true // Prevents buffer addition + return SIMPLE_GAS_COST + } } // if not, fall back to block gasLimit -- cgit From 4cc0b1ef01573e1541d18bdcd89650e1db32ae9a Mon Sep 17 00:00:00 2001 From: Howard Braham Date: Fri, 28 Sep 2018 11:01:34 -0700 Subject: ganache-core merged my PR, so I changed some comments to clarify that ganache-core v2.2.1 and below will return the non-standard '0x0' --- app/scripts/controllers/transactions/tx-gas-utils.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'app/scripts/controllers/transactions') diff --git a/app/scripts/controllers/transactions/tx-gas-utils.js b/app/scripts/controllers/transactions/tx-gas-utils.js index 5ec728085..ac57dfe1d 100644 --- a/app/scripts/controllers/transactions/tx-gas-utils.js +++ b/app/scripts/controllers/transactions/tx-gas-utils.js @@ -63,14 +63,15 @@ class TxGasUtil { const hasRecipient = Boolean(recipient) if (hasRecipient) { - let code = await this.query.getCode(recipient) + const code = await this.query.getCode(recipient) // If there's data in the params, but there's no code, it's not a valid contract - // For no code, Infura will return '0x', and Ganache will return '0x0' + // For no code, Infura will return '0x', and ganache-core v2.2.1 will return '0x0' if (txParams.data && (!code || code === '0x' || code === '0x0')) { - throw {errorKey: TRANSACTION_NO_CONTRACT_ERROR_KEY} - } - else if (!code) { + const err = new Error() + err.errorKey = TRANSACTION_NO_CONTRACT_ERROR_KEY + throw err + } else if (!code) { txParams.gas = SIMPLE_GAS_COST // For a standard ETH send, gas is 21k max txMeta.simpleSend = true // Prevents buffer addition return SIMPLE_GAS_COST -- cgit From 600f755dbf8d4cfdc152e3d521b537ee9a046a35 Mon Sep 17 00:00:00 2001 From: kumavis Date: Tue, 9 Oct 2018 23:17:05 -0400 Subject: tx-gas-utils - improve format + comments --- .../controllers/transactions/tx-gas-utils.js | 30 +++++++++++++--------- 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'app/scripts/controllers/transactions') diff --git a/app/scripts/controllers/transactions/tx-gas-utils.js b/app/scripts/controllers/transactions/tx-gas-utils.js index ac57dfe1d..436900715 100644 --- a/app/scripts/controllers/transactions/tx-gas-utils.js +++ b/app/scripts/controllers/transactions/tx-gas-utils.js @@ -62,28 +62,34 @@ class TxGasUtil { const recipient = txParams.to const hasRecipient = Boolean(recipient) + // see if we can set the gas based on the recipient if (hasRecipient) { const code = await this.query.getCode(recipient) - - // If there's data in the params, but there's no code, it's not a valid contract - // For no code, Infura will return '0x', and ganache-core v2.2.1 will return '0x0' - if (txParams.data && (!code || code === '0x' || code === '0x0')) { - const err = new Error() - err.errorKey = TRANSACTION_NO_CONTRACT_ERROR_KEY - throw err - } else if (!code) { - txParams.gas = SIMPLE_GAS_COST // For a standard ETH send, gas is 21k max - txMeta.simpleSend = true // Prevents buffer addition + // For an address with no code, geth will return '0x', and ganache-core v2.2.1 will return '0x0' + const codeIsEmpty = !code || code === '0x' || code === '0x0' + + if (codeIsEmpty) { + // if there's data in the params, but there's no contract code, it's not a valid transaction + if (txParams.data) { + const err = new Error() + err.errorKey = TRANSACTION_NO_CONTRACT_ERROR_KEY + throw err + } + + // This is a standard ether simple send, gas requirement is exactly 21k + txParams.gas = SIMPLE_GAS_COST + // prevents buffer addition + txMeta.simpleSend = true return SIMPLE_GAS_COST } } - // if not, fall back to block gasLimit + // fallback to block gasLimit const blockGasLimitBN = hexToBn(blockGasLimitHex) const saferGasLimitBN = BnMultiplyByFraction(blockGasLimitBN, 19, 20) txParams.gas = bnToHex(saferGasLimitBN) - // run tx + // estimate tx gas requirements return await this.query.estimateGas(txParams) } -- cgit From c821a8354c8eba05885ca219f39aedafbd4f8052 Mon Sep 17 00:00:00 2001 From: Alexander Tseung Date: Tue, 16 Oct 2018 06:00:47 +0800 Subject: Add txReceipt data to transaction details (#5513) --- app/scripts/controllers/transactions/index.js | 35 +++++++++++++++++++++- .../controllers/transactions/tx-state-manager.js | 5 ++++ 2 files changed, 39 insertions(+), 1 deletion(-) (limited to 'app/scripts/controllers/transactions') diff --git a/app/scripts/controllers/transactions/index.js b/app/scripts/controllers/transactions/index.js index a57c85f50..9f2290924 100644 --- a/app/scripts/controllers/transactions/index.js +++ b/app/scripts/controllers/transactions/index.js @@ -366,7 +366,40 @@ class TransactionController extends EventEmitter { this.txStateManager.setTxStatusSubmitted(txId) } - confirmTransaction (txId) { + /** + * Sets the status of the transaction to confirmed and sets the status of nonce duplicates as + * dropped if the txParams have data it will fetch the txReceipt + * @param {number} txId - The tx's ID + * @returns {Promise} + */ + async confirmTransaction (txId) { + // get the txReceipt before marking the transaction confirmed + // to ensure the receipt is gotten before the ui revives the tx + const txMeta = this.txStateManager.getTx(txId) + + if (!txMeta) { + return + } + + try { + const txReceipt = await this.query.getTransactionReceipt(txMeta.hash) + + // It seems that sometimes the numerical values being returned from + // this.query.getTransactionReceipt are BN instances and not strings. + const gasUsed = typeof txReceipt.gasUsed !== 'string' + ? txReceipt.gasUsed.toString(16) + : txReceipt.gasUsed + + txMeta.txReceipt = { + ...txReceipt, + gasUsed, + } + + this.txStateManager.updateTx(txMeta, 'transactions#confirmTransaction - add txReceipt') + } catch (err) { + log.error(err) + } + this.txStateManager.setTxStatusConfirmed(txId) this._markNonceDuplicatesDropped(txId) } diff --git a/app/scripts/controllers/transactions/tx-state-manager.js b/app/scripts/controllers/transactions/tx-state-manager.js index daa6cc388..58c48e34e 100644 --- a/app/scripts/controllers/transactions/tx-state-manager.js +++ b/app/scripts/controllers/transactions/tx-state-manager.js @@ -400,6 +400,11 @@ class TransactionStateManager extends EventEmitter { */ _setTxStatus (txId, status) { const txMeta = this.getTx(txId) + + if (!txMeta) { + return + } + txMeta.status = status setTimeout(() => { try { -- cgit From 31e5cad1e34c1b07079c430bb1903f7914021111 Mon Sep 17 00:00:00 2001 From: kumavis Date: Sun, 21 Oct 2018 01:01:21 -0400 Subject: tx-gas-util - set error message when invalidating tx based on tx data but no contract code --- app/scripts/controllers/transactions/tx-gas-utils.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'app/scripts/controllers/transactions') diff --git a/app/scripts/controllers/transactions/tx-gas-utils.js b/app/scripts/controllers/transactions/tx-gas-utils.js index 436900715..def67c2c3 100644 --- a/app/scripts/controllers/transactions/tx-gas-utils.js +++ b/app/scripts/controllers/transactions/tx-gas-utils.js @@ -62,20 +62,21 @@ class TxGasUtil { const recipient = txParams.to const hasRecipient = Boolean(recipient) - // see if we can set the gas based on the recipient + // see if we can set the gas based on the recipient if (hasRecipient) { const code = await this.query.getCode(recipient) // For an address with no code, geth will return '0x', and ganache-core v2.2.1 will return '0x0' const codeIsEmpty = !code || code === '0x' || code === '0x0' - + if (codeIsEmpty) { // if there's data in the params, but there's no contract code, it's not a valid transaction if (txParams.data) { - const err = new Error() + const err = new Error('TxGasUtil - Trying to call a function on a non-contract address') + // set error key so ui can display localized error message err.errorKey = TRANSACTION_NO_CONTRACT_ERROR_KEY throw err } - + // This is a standard ether simple send, gas requirement is exactly 21k txParams.gas = SIMPLE_GAS_COST // prevents buffer addition -- cgit