Skip to content

Commit 38df647

Browse files
danjmdanfinlay
authored andcommitted
Ensure correct tx category when sending to contracts without tx data (#7252)
* Ensure correct transaction category when sending to contracts but there is no txParams data * Update gas when pasting address in send * Gracefully fall back is send.util/estimateGas when blockGasLimit from background is falsy * Remove network request frontend fallback for blockGasLimit * Add some needed slow downs to e2e tests
1 parent d7a4dfe commit 38df647

File tree

6 files changed

+51
-12
lines changed

6 files changed

+51
-12
lines changed

app/scripts/controllers/transactions/index.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -603,21 +603,21 @@ class TransactionController extends EventEmitter {
603603
].find(tokenMethodName => tokenMethodName === name && name.toLowerCase())
604604

605605
let result
606-
let code
607-
if (!txParams.data) {
608-
result = SEND_ETHER_ACTION_KEY
609-
} else if (tokenMethodName) {
606+
if (txParams.data && tokenMethodName) {
610607
result = tokenMethodName
611-
} else if (!to) {
608+
} else if (txParams.data && !to) {
612609
result = DEPLOY_CONTRACT_ACTION_KEY
613-
} else {
610+
}
611+
612+
let code
613+
if (!result) {
614614
try {
615615
code = await this.query.getCode(to)
616616
} catch (e) {
617617
code = null
618618
log.warn(e)
619619
}
620-
// For an address with no code, geth will return '0x', and ganache-core v2.2.1 will return '0x0'
620+
621621
const codeIsEmpty = !code || code === '0x' || code === '0x0'
622622

623623
result = codeIsEmpty ? SEND_ETHER_ACTION_KEY : CONTRACT_INTERACTION_KEY

test/e2e/metamask-ui.spec.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -567,18 +567,18 @@ describe('MetaMask', function () {
567567

568568
const send3eth = await findElement(driver, By.xpath(`//button[contains(text(), 'Send')]`), 10000)
569569
await send3eth.click()
570-
await delay(regularDelayMs)
570+
await delay(largeDelayMs * 2)
571571

572572
const contractDeployment = await findElement(driver, By.xpath(`//button[contains(text(), 'Deploy Contract')]`), 10000)
573573
await contractDeployment.click()
574-
await delay(regularDelayMs)
574+
await delay(largeDelayMs * 2)
575575

576576
await send3eth.click()
577577
await contractDeployment.click()
578-
await delay(regularDelayMs)
578+
await delay(largeDelayMs * 2)
579579

580580
await driver.switchTo().window(extension)
581-
await delay(regularDelayMs)
581+
await delay(largeDelayMs * 2)
582582

583583
let transactions = await findElements(driver, By.css('.transaction-list-item'))
584584
await transactions[3].click()

test/unit/app/controllers/transactions/tx-controller-test.js

+30
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,36 @@ describe('Transaction Controller', function () {
622622
})
623623
assert.deepEqual(result, { transactionCategory: CONTRACT_INTERACTION_KEY, getCodeResponse: '0x0a' })
624624
})
625+
626+
it('should return a contract interaction transactionCategory with the correct getCodeResponse when to is a contract address and data is falsey', async function () {
627+
const _providerResultStub = {
628+
// 1 gwei
629+
eth_gasPrice: '0x0de0b6b3a7640000',
630+
// by default, all accounts are external accounts (not contracts)
631+
eth_getCode: '0xa',
632+
}
633+
const _provider = createTestProviderTools({ scaffold: _providerResultStub }).provider
634+
const _fromAccount = getTestAccounts()[0]
635+
const _blockTrackerStub = new EventEmitter()
636+
_blockTrackerStub.getCurrentBlock = noop
637+
_blockTrackerStub.getLatestBlock = noop
638+
const _txController = new TransactionController({
639+
provider: _provider,
640+
getGasPrice: function () { return '0xee6b2800' },
641+
networkStore: new ObservableStore(currentNetworkId),
642+
txHistoryLimit: 10,
643+
blockTracker: _blockTrackerStub,
644+
signTransaction: (ethTx) => new Promise((resolve) => {
645+
ethTx.sign(_fromAccount.key)
646+
resolve()
647+
}),
648+
})
649+
const result = await _txController._determineTransactionCategory({
650+
to: '0x9e673399f795D01116e9A8B2dD2F156705131ee9',
651+
data: '',
652+
})
653+
assert.deepEqual(result, { transactionCategory: CONTRACT_INTERACTION_KEY, getCodeResponse: '0x0a' })
654+
})
625655
})
626656

627657
describe('#getPendingTransactions', function () {

ui/app/pages/send/send.component.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ export default class SendTransactionScreen extends PersistentForm {
304304
}}
305305
onChange={this.onRecipientInputChange}
306306
onValidAddressTyped={(address) => this.props.updateSendTo(address, '')}
307-
onPaste={text => this.props.updateSendTo(text)}
307+
onPaste={text => { this.props.updateSendTo(text) && this.updateGas() }}
308308
onReset={() => this.props.updateSendTo('', '')}
309309
updateEnsResolution={this.props.updateSendEnsResolution}
310310
updateEnsResolutionError={this.props.updateSendEnsResolutionError}

ui/app/pages/send/send.constants.js

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ const MIN_GAS_PRICE_HEX = (parseInt(MIN_GAS_PRICE_DEC)).toString(16)
66
const MIN_GAS_LIMIT_DEC = '21000'
77
const MIN_GAS_LIMIT_HEX = (parseInt(MIN_GAS_LIMIT_DEC)).toString(16)
88

9+
const ARBITRARY_HIGH_BLOCK_GAS_LIMIT = (parseInt('8000000')).toString(16)
10+
911
const MIN_GAS_PRICE_GWEI = ethUtil.addHexPrefix(conversionUtil(MIN_GAS_PRICE_HEX, {
1012
fromDenomination: 'WEI',
1113
toDenomination: 'GWEI',
@@ -58,4 +60,5 @@ module.exports = {
5860
SIMPLE_GAS_COST,
5961
TOKEN_TRANSFER_FUNCTION_SIGNATURE,
6062
BASE_TOKEN_GAS_COST,
63+
ARBITRARY_HIGH_BLOCK_GAS_LIMIT,
6164
}

ui/app/pages/send/send.utils.js

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const {
1818
ONE_GWEI_IN_WEI_HEX,
1919
SIMPLE_GAS_COST,
2020
TOKEN_TRANSFER_FUNCTION_SIGNATURE,
21+
ARBITRARY_HIGH_BLOCK_GAS_LIMIT,
2122
} = require('./send.constants')
2223
const abi = require('ethereumjs-abi')
2324
const ethUtil = require('ethereumjs-util')
@@ -243,12 +244,17 @@ async function estimateGas ({
243244
}
244245

245246
// if not, fall back to block gasLimit
247+
if (!blockGasLimit) {
248+
blockGasLimit = ARBITRARY_HIGH_BLOCK_GAS_LIMIT
249+
}
250+
246251
paramsForGasEstimate.gas = ethUtil.addHexPrefix(multiplyCurrencies(blockGasLimit, 0.95, {
247252
multiplicandBase: 16,
248253
multiplierBase: 10,
249254
roundDown: '0',
250255
toNumericBase: 'hex',
251256
}))
257+
252258
// run tx
253259
return new Promise((resolve, reject) => {
254260
return estimateGasMethod(paramsForGasEstimate, (err, estimatedGas) => {

0 commit comments

Comments
 (0)