Fix Estimate Gas (#1698)

* Properly pad hex encoded data

* Remove to address for gas estimation if its zero value
This commit is contained in:
HenryNguyen5 2018-04-30 21:07:59 -04:00 committed by Daniel Ternyak
parent 34ef8857da
commit 9d1a218a0e
2 changed files with 17 additions and 2 deletions

View File

@ -1,7 +1,7 @@
// Ref: https://github.com/ethereum/wiki/wiki/JSON-RPC
import BN from 'bn.js';
import { toBuffer, addHexPrefix, bufferToHex } from 'ethereumjs-util';
import { toBuffer, addHexPrefix, bufferToHex, stripHexPrefix, padToEven } from 'ethereumjs-util';
import trimStart from 'lodash/trimStart';
// When encoding QUANTITIES (integers, numbers): encode as hex, prefix with "0x", the most compact representation (slight exception: zero should be represented as "0x0").
@ -12,5 +12,10 @@ export function hexEncodeQuantity(value: BN | Buffer): string {
// When encoding UNFORMATTED DATA (byte arrays, account addresses, hashes, bytecode arrays): encode as hex, prefix with "0x", two hex digits per byte.
export function hexEncodeData(value: string | Buffer): string {
return bufferToHex(toBuffer(value));
// convert the value to a buffer
// convert the value to a hex prefixed hex string
// strip the hex prefix
// pad the data to even (two hex digits per byte)
// add the hex prefix back in
return addHexPrefix(padToEven(stripHexPrefix(bufferToHex(toBuffer(value)))));
}

View File

@ -78,6 +78,15 @@ export function* shouldEstimateGas(): SagaIterator {
transaction
);
// gas estimation calls with
// '0x' as an address (contract creation)
// fail, so instead we set it as undefined
// interestingly, the transaction itself as '0x' as the
// to address works fine.
if (rest.to === '0x') {
rest.to = undefined as any;
}
yield put(estimateGasRequested(rest));
}
}
@ -101,6 +110,7 @@ export function* estimateGas(): SagaIterator {
try {
const from: string = yield apply(walletInst, walletInst.getAddressString);
const txObj = { ...payload, from };
const { gasLimit } = yield race({
gasLimit: apply(node, node.estimateGas, [txObj]),
timeout: call(delay, 10000)