mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Update gas when hex data changes on send screen
This commit is contained in:
parent
3741927d8d
commit
918fb71df3
@ -917,6 +917,7 @@ function updateGasData ({
|
||||
selectedToken,
|
||||
to,
|
||||
value,
|
||||
data,
|
||||
}) {
|
||||
return (dispatch) => {
|
||||
dispatch(actions.gasLoadingStarted())
|
||||
@ -937,6 +938,7 @@ function updateGasData ({
|
||||
to,
|
||||
value,
|
||||
estimateGasPrice,
|
||||
data,
|
||||
}),
|
||||
])
|
||||
})
|
||||
|
@ -15,18 +15,24 @@ export default class SendContent extends Component {
|
||||
showHexData: PropTypes.bool,
|
||||
};
|
||||
|
||||
updateGas = (updateData) => this.props.updateGas(updateData)
|
||||
|
||||
render () {
|
||||
return (
|
||||
<PageContainerContent>
|
||||
<div className="send-v2__form">
|
||||
<SendFromRow />
|
||||
<SendToRow
|
||||
updateGas={(updateData) => this.props.updateGas(updateData)}
|
||||
updateGas={this.updateGas}
|
||||
scanQrCode={ _ => this.props.scanQrCode()}
|
||||
/>
|
||||
<SendAmountRow updateGas={(updateData) => this.props.updateGas(updateData)} />
|
||||
<SendAmountRow updateGas={this.updateGas} />
|
||||
<SendGasRow />
|
||||
{ this.props.showHexData ? <SendHexDataRow /> : null }
|
||||
{(this.props.showHexData && (
|
||||
<SendHexDataRow
|
||||
updateGas={this.updateGas}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
</PageContainerContent>
|
||||
)
|
||||
|
@ -7,6 +7,7 @@ export default class SendHexDataRow extends Component {
|
||||
data: PropTypes.string,
|
||||
inError: PropTypes.bool,
|
||||
updateSendHexData: PropTypes.func.isRequired,
|
||||
updateGas: PropTypes.func.isRequired,
|
||||
};
|
||||
|
||||
static contextTypes = {
|
||||
@ -14,9 +15,10 @@ export default class SendHexDataRow extends Component {
|
||||
};
|
||||
|
||||
onInput = (event) => {
|
||||
const {updateSendHexData} = this.props
|
||||
event.target.value = event.target.value.replace(/\n/g, '')
|
||||
updateSendHexData(event.target.value || null)
|
||||
const {updateSendHexData, updateGas} = this.props
|
||||
const data = event.target.value.replace(/\n/g, '') || null
|
||||
updateSendHexData(data)
|
||||
updateGas({ data })
|
||||
}
|
||||
|
||||
render () {
|
||||
|
@ -62,7 +62,7 @@ export default class SendTransactionScreen extends PersistentForm {
|
||||
}
|
||||
}
|
||||
|
||||
updateGas ({ to: updatedToAddress, amount: value } = {}) {
|
||||
updateGas ({ to: updatedToAddress, amount: value, data } = {}) {
|
||||
const {
|
||||
amount,
|
||||
blockGasLimit,
|
||||
@ -86,6 +86,7 @@ export default class SendTransactionScreen extends PersistentForm {
|
||||
selectedToken,
|
||||
to: getToAddressForGasUpdate(updatedToAddress, currentToAddress),
|
||||
value: value || amount,
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -86,9 +86,10 @@ function mapDispatchToProps (dispatch) {
|
||||
selectedToken,
|
||||
to,
|
||||
value,
|
||||
data,
|
||||
}) => {
|
||||
!editingTransactionId
|
||||
? dispatch(updateGasData({ recentBlocks, selectedAddress, selectedToken, blockGasLimit, to, value }))
|
||||
? dispatch(updateGasData({ recentBlocks, selectedAddress, selectedToken, blockGasLimit, to, value, data }))
|
||||
: dispatch(setGasTotal(calcGasTotal(gasLimit, gasPrice)))
|
||||
},
|
||||
updateSendTokenBalance: ({ selectedToken, tokenContract, address }) => {
|
||||
|
@ -200,16 +200,34 @@ function doesAmountErrorRequireUpdate ({
|
||||
return amountErrorRequiresUpdate
|
||||
}
|
||||
|
||||
async function estimateGas ({ selectedAddress, selectedToken, blockGasLimit, to, value, gasPrice, estimateGasMethod }) {
|
||||
async function estimateGas ({
|
||||
selectedAddress,
|
||||
selectedToken,
|
||||
blockGasLimit,
|
||||
to,
|
||||
value,
|
||||
data,
|
||||
gasPrice,
|
||||
estimateGasMethod,
|
||||
}) {
|
||||
const paramsForGasEstimate = { from: selectedAddress, value, gasPrice }
|
||||
|
||||
if (selectedToken) {
|
||||
paramsForGasEstimate.value = '0x0'
|
||||
paramsForGasEstimate.data = generateTokenTransferData({ toAddress: to, amount: value, selectedToken })
|
||||
paramsForGasEstimate.to = selectedToken.address
|
||||
} else {
|
||||
if (data) {
|
||||
paramsForGasEstimate.data = data
|
||||
}
|
||||
|
||||
if (to) {
|
||||
paramsForGasEstimate.to = to
|
||||
}
|
||||
}
|
||||
|
||||
// if recipient has no code, gas is 21k max:
|
||||
if (!selectedToken) {
|
||||
if (!selectedToken && !data) {
|
||||
const code = Boolean(to) && await global.eth.getCode(to)
|
||||
if (!code || code === '0x') {
|
||||
return SIMPLE_GAS_COST
|
||||
@ -218,8 +236,6 @@ async function estimateGas ({ selectedAddress, selectedToken, blockGasLimit, to,
|
||||
return BASE_TOKEN_GAS_COST
|
||||
}
|
||||
|
||||
paramsForGasEstimate.to = selectedToken ? selectedToken.address : to
|
||||
|
||||
// if not, fall back to block gasLimit
|
||||
paramsForGasEstimate.gas = ethUtil.addHexPrefix(multiplyCurrencies(blockGasLimit, 0.95, {
|
||||
multiplicandBase: 16,
|
||||
|
@ -289,6 +289,7 @@ describe('Send Component', function () {
|
||||
selectedToken: 'mockSelectedToken',
|
||||
to: '',
|
||||
value: 'mockAmount',
|
||||
data: undefined,
|
||||
}
|
||||
)
|
||||
})
|
||||
|
@ -105,6 +105,7 @@ describe('send container', () => {
|
||||
selectedToken: { address: '0x1' },
|
||||
to: 'mockTo',
|
||||
value: 'mockValue',
|
||||
data: undefined,
|
||||
}
|
||||
|
||||
it('should dispatch a setGasTotal action when editingTransactionId is truthy', () => {
|
||||
@ -117,14 +118,14 @@ describe('send container', () => {
|
||||
})
|
||||
|
||||
it('should dispatch an updateGasData action when editingTransactionId is falsy', () => {
|
||||
const { selectedAddress, selectedToken, recentBlocks, blockGasLimit, to, value } = mockProps
|
||||
const { selectedAddress, selectedToken, recentBlocks, blockGasLimit, to, value, data } = mockProps
|
||||
mapDispatchToPropsObject.updateAndSetGasTotal(
|
||||
Object.assign({}, mockProps, {editingTransactionId: false})
|
||||
)
|
||||
assert(dispatchSpy.calledOnce)
|
||||
assert.deepEqual(
|
||||
actionSpies.updateGasData.getCall(0).args[0],
|
||||
{ selectedAddress, selectedToken, recentBlocks, blockGasLimit, to, value }
|
||||
{ selectedAddress, selectedToken, recentBlocks, blockGasLimit, to, value, data }
|
||||
)
|
||||
})
|
||||
})
|
||||
|
@ -367,6 +367,18 @@ describe('send utils', () => {
|
||||
assert.equal(result, '0xabc16')
|
||||
})
|
||||
|
||||
it('should call ethQuery.estimateGas without a recipient if the recipient is empty and data passed', async () => {
|
||||
const data = 'mockData'
|
||||
const to = ''
|
||||
const result = await estimateGas({...baseMockParams, data, to})
|
||||
assert.equal(baseMockParams.estimateGasMethod.callCount, 1)
|
||||
assert.deepEqual(
|
||||
baseMockParams.estimateGasMethod.getCall(0).args[0],
|
||||
{ gasPrice: undefined, value: undefined, data, from: baseExpectedCall.from, gas: baseExpectedCall.gas},
|
||||
)
|
||||
assert.equal(result, '0xabc16')
|
||||
})
|
||||
|
||||
it(`should return ${SIMPLE_GAS_COST} if ethQuery.getCode does not return '0x'`, async () => {
|
||||
assert.equal(baseMockParams.estimateGasMethod.callCount, 0)
|
||||
const result = await estimateGas(Object.assign({}, baseMockParams, { to: '0x123' }))
|
||||
|
Loading…
x
Reference in New Issue
Block a user