Unmarshall txn data in SignerSubprovider before calling web3wrapper sendTransactionAsync
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { Web3Wrapper } from '@0xproject/web3-wrapper';
|
||||
import { Web3Wrapper, marshaller } from '@0xproject/web3-wrapper';
|
||||
|
||||
import { JSONRPCRequestPayload, Provider } from 'ethereum-types';
|
||||
|
||||
import { Callback, ErrorCallback } from '../types';
|
||||
@@ -51,7 +52,8 @@ export class SignerSubprovider extends Subprovider {
|
||||
case 'eth_sendTransaction':
|
||||
const [txParams] = payload.params;
|
||||
try {
|
||||
const txHash = await this._web3Wrapper.sendTransactionAsync(txParams);
|
||||
const txData = marshaller.unmarshalTxData(txParams);
|
||||
const txHash = await this._web3Wrapper.sendTransactionAsync(txData);
|
||||
end(null, txHash);
|
||||
} catch (err) {
|
||||
end(err);
|
||||
|
||||
@@ -8,3 +8,4 @@ export { logUtils } from './log_utils';
|
||||
export { abiUtils } from './abi_utils';
|
||||
export { NULL_BYTES } from './constants';
|
||||
export { errorUtils } from './error_utils';
|
||||
export { conversion } from './conversion';
|
||||
|
||||
@@ -1,2 +1,3 @@
|
||||
export { Web3Wrapper, uniqueVersionIds, NodeType } from './web3_wrapper';
|
||||
export { Web3WrapperErrors } from './types';
|
||||
export { marshaller } from './marshaller';
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { addressUtils } from '@0xproject/utils';
|
||||
import { addressUtils, conversion } from '@0xproject/utils';
|
||||
import {
|
||||
BlockParam,
|
||||
BlockParamLiteral,
|
||||
@@ -14,8 +14,6 @@ import {
|
||||
import ethUtil = require('ethereumjs-util');
|
||||
import * as _ from 'lodash';
|
||||
|
||||
import { utils } from './utils';
|
||||
|
||||
import {
|
||||
BlockWithoutTransactionDataRPC,
|
||||
BlockWithTransactionDataRPC,
|
||||
@@ -31,26 +29,30 @@ export const marshaller = {
|
||||
): BlockWithoutTransactionData {
|
||||
const block = {
|
||||
...blockWithHexValues,
|
||||
gasLimit: utils.convertHexToNumber(blockWithHexValues.gasLimit),
|
||||
gasUsed: utils.convertHexToNumber(blockWithHexValues.gasUsed),
|
||||
size: utils.convertHexToNumber(blockWithHexValues.size),
|
||||
timestamp: utils.convertHexToNumber(blockWithHexValues.timestamp),
|
||||
number: _.isNull(blockWithHexValues.number) ? null : utils.convertHexToNumber(blockWithHexValues.number),
|
||||
difficulty: utils.convertAmountToBigNumber(blockWithHexValues.difficulty),
|
||||
totalDifficulty: utils.convertAmountToBigNumber(blockWithHexValues.totalDifficulty),
|
||||
gasLimit: conversion.convertHexToNumber(blockWithHexValues.gasLimit),
|
||||
gasUsed: conversion.convertHexToNumber(blockWithHexValues.gasUsed),
|
||||
size: conversion.convertHexToNumber(blockWithHexValues.size),
|
||||
timestamp: conversion.convertHexToNumber(blockWithHexValues.timestamp),
|
||||
number: _.isNull(blockWithHexValues.number)
|
||||
? null
|
||||
: conversion.convertHexToNumber(blockWithHexValues.number),
|
||||
difficulty: conversion.convertAmountToBigNumber(blockWithHexValues.difficulty),
|
||||
totalDifficulty: conversion.convertAmountToBigNumber(blockWithHexValues.totalDifficulty),
|
||||
};
|
||||
return block;
|
||||
},
|
||||
unmarshalIntoBlockWithTransactionData(blockWithHexValues: BlockWithTransactionDataRPC): BlockWithTransactionData {
|
||||
const block = {
|
||||
...blockWithHexValues,
|
||||
gasLimit: utils.convertHexToNumber(blockWithHexValues.gasLimit),
|
||||
gasUsed: utils.convertHexToNumber(blockWithHexValues.gasUsed),
|
||||
size: utils.convertHexToNumber(blockWithHexValues.size),
|
||||
timestamp: utils.convertHexToNumber(blockWithHexValues.timestamp),
|
||||
number: _.isNull(blockWithHexValues.number) ? null : utils.convertHexToNumber(blockWithHexValues.number),
|
||||
difficulty: utils.convertAmountToBigNumber(blockWithHexValues.difficulty),
|
||||
totalDifficulty: utils.convertAmountToBigNumber(blockWithHexValues.totalDifficulty),
|
||||
gasLimit: conversion.convertHexToNumber(blockWithHexValues.gasLimit),
|
||||
gasUsed: conversion.convertHexToNumber(blockWithHexValues.gasUsed),
|
||||
size: conversion.convertHexToNumber(blockWithHexValues.size),
|
||||
timestamp: conversion.convertHexToNumber(blockWithHexValues.timestamp),
|
||||
number: _.isNull(blockWithHexValues.number)
|
||||
? null
|
||||
: conversion.convertHexToNumber(blockWithHexValues.number),
|
||||
difficulty: conversion.convertAmountToBigNumber(blockWithHexValues.difficulty),
|
||||
totalDifficulty: conversion.convertAmountToBigNumber(blockWithHexValues.totalDifficulty),
|
||||
transactions: [] as Transaction[],
|
||||
};
|
||||
block.transactions = _.map(blockWithHexValues.transactions, (tx: TransactionRPC) => {
|
||||
@@ -62,14 +64,14 @@ export const marshaller = {
|
||||
unmarshalTransaction(txRpc: TransactionRPC): Transaction {
|
||||
const tx = {
|
||||
...txRpc,
|
||||
blockNumber: !_.isNull(txRpc.blockNumber) ? utils.convertHexToNumber(txRpc.blockNumber) : null,
|
||||
blockNumber: !_.isNull(txRpc.blockNumber) ? conversion.convertHexToNumber(txRpc.blockNumber) : null,
|
||||
transactionIndex: !_.isNull(txRpc.transactionIndex)
|
||||
? utils.convertHexToNumber(txRpc.transactionIndex)
|
||||
? conversion.convertHexToNumber(txRpc.transactionIndex)
|
||||
: null,
|
||||
nonce: utils.convertHexToNumber(txRpc.nonce),
|
||||
gas: utils.convertHexToNumber(txRpc.gas),
|
||||
gasPrice: utils.convertAmountToBigNumber(txRpc.gasPrice),
|
||||
value: utils.convertAmountToBigNumber(txRpc.value),
|
||||
nonce: conversion.convertHexToNumber(txRpc.nonce),
|
||||
gas: conversion.convertHexToNumber(txRpc.gas),
|
||||
gasPrice: conversion.convertAmountToBigNumber(txRpc.gasPrice),
|
||||
value: conversion.convertAmountToBigNumber(txRpc.value),
|
||||
};
|
||||
return tx;
|
||||
},
|
||||
@@ -79,10 +81,12 @@ export const marshaller = {
|
||||
}
|
||||
const txData = {
|
||||
...txDataRpc,
|
||||
value: !_.isUndefined(txDataRpc.value) ? utils.convertHexToNumber(txDataRpc.value) : undefined,
|
||||
gas: !_.isUndefined(txDataRpc.gas) ? utils.convertHexToNumber(txDataRpc.gas) : undefined,
|
||||
gasPrice: !_.isUndefined(txDataRpc.gasPrice) ? utils.convertHexToNumber(txDataRpc.gasPrice) : undefined,
|
||||
nonce: !_.isUndefined(txDataRpc.nonce) ? utils.convertHexToNumber(txDataRpc.nonce) : undefined,
|
||||
value: !_.isUndefined(txDataRpc.value) ? conversion.convertHexToNumber(txDataRpc.value) : undefined,
|
||||
gas: !_.isUndefined(txDataRpc.gas) ? conversion.convertHexToNumber(txDataRpc.gas) : undefined,
|
||||
gasPrice: !_.isUndefined(txDataRpc.gasPrice)
|
||||
? conversion.convertHexToNumber(txDataRpc.gasPrice)
|
||||
: undefined,
|
||||
nonce: !_.isUndefined(txDataRpc.nonce) ? conversion.convertHexToNumber(txDataRpc.nonce) : undefined,
|
||||
};
|
||||
return txData;
|
||||
},
|
||||
@@ -129,15 +133,15 @@ export const marshaller = {
|
||||
if (_.isUndefined(blockParam)) {
|
||||
return BlockParamLiteral.Latest;
|
||||
}
|
||||
const encodedBlockParam = _.isNumber(blockParam) ? utils.numberToHex(blockParam) : blockParam;
|
||||
const encodedBlockParam = _.isNumber(blockParam) ? conversion.numberToHex(blockParam) : blockParam;
|
||||
return encodedBlockParam;
|
||||
},
|
||||
unmarshalLog(rawLog: RawLogEntry): LogEntry {
|
||||
const formattedLog = {
|
||||
...rawLog,
|
||||
logIndex: utils.convertHexToNumberOrNull(rawLog.logIndex),
|
||||
blockNumber: utils.convertHexToNumberOrNull(rawLog.blockNumber),
|
||||
transactionIndex: utils.convertHexToNumberOrNull(rawLog.transactionIndex),
|
||||
logIndex: conversion.convertHexToNumberOrNull(rawLog.logIndex),
|
||||
blockNumber: conversion.convertHexToNumberOrNull(rawLog.blockNumber),
|
||||
transactionIndex: conversion.convertHexToNumberOrNull(rawLog.transactionIndex),
|
||||
};
|
||||
return formattedLog;
|
||||
},
|
||||
@@ -147,14 +151,14 @@ export const marshaller = {
|
||||
to: _.isUndefined(callTxDataBase.to) ? undefined : this.marshalAddress(callTxDataBase.to),
|
||||
gasPrice: _.isUndefined(callTxDataBase.gasPrice)
|
||||
? undefined
|
||||
: utils.encodeAmountAsHexString(callTxDataBase.gasPrice),
|
||||
gas: _.isUndefined(callTxDataBase.gas) ? undefined : utils.encodeAmountAsHexString(callTxDataBase.gas),
|
||||
: conversion.encodeAmountAsHexString(callTxDataBase.gasPrice),
|
||||
gas: _.isUndefined(callTxDataBase.gas) ? undefined : conversion.encodeAmountAsHexString(callTxDataBase.gas),
|
||||
value: _.isUndefined(callTxDataBase.value)
|
||||
? undefined
|
||||
: utils.encodeAmountAsHexString(callTxDataBase.value),
|
||||
: conversion.encodeAmountAsHexString(callTxDataBase.value),
|
||||
nonce: _.isUndefined(callTxDataBase.nonce)
|
||||
? undefined
|
||||
: utils.encodeAmountAsHexString(callTxDataBase.nonce),
|
||||
: conversion.encodeAmountAsHexString(callTxDataBase.nonce),
|
||||
};
|
||||
|
||||
return callTxDataBaseRPC;
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
import { BigNumber } from '@0xproject/utils';
|
||||
import * as _ from 'lodash';
|
||||
|
||||
export const utils = {
|
||||
isBigNumber(value: any): boolean {
|
||||
const isBigNumber = _.isObject(value) && value.isBigNumber;
|
||||
return isBigNumber;
|
||||
},
|
||||
convertHexToNumber(value: string): number {
|
||||
const valueBigNumber = new BigNumber(value);
|
||||
const valueNumber = valueBigNumber.toNumber();
|
||||
return valueNumber;
|
||||
},
|
||||
convertHexToNumberOrNull(hex: string | null): number | null {
|
||||
if (_.isNull(hex)) {
|
||||
return null;
|
||||
}
|
||||
const decimal = this.convertHexToNumber(hex);
|
||||
return decimal;
|
||||
},
|
||||
convertAmountToBigNumber(value: string | number | BigNumber): BigNumber {
|
||||
const num = value || 0;
|
||||
const isBigNumber = utils.isBigNumber(num);
|
||||
if (isBigNumber) {
|
||||
return num as BigNumber;
|
||||
}
|
||||
|
||||
if (_.isString(num) && (num.indexOf('0x') === 0 || num.indexOf('-0x') === 0)) {
|
||||
return new BigNumber(num.replace('0x', ''), 16);
|
||||
}
|
||||
|
||||
const baseTen = 10;
|
||||
return new BigNumber((num as number).toString(baseTen), baseTen);
|
||||
},
|
||||
encodeAmountAsHexString(value: string | number | BigNumber): string {
|
||||
const valueBigNumber = utils.convertAmountToBigNumber(value);
|
||||
const hexBase = 16;
|
||||
const valueHex = valueBigNumber.toString(hexBase);
|
||||
|
||||
return valueBigNumber.lessThan(0) ? '-0x' + valueHex.substr(1) : '0x' + valueHex;
|
||||
},
|
||||
numberToHex(value: number): string {
|
||||
if (!isFinite(value) && !this.isHexStrict(value)) {
|
||||
throw new Error(`Given input ${value} is not a number.`);
|
||||
}
|
||||
|
||||
const valueBigNumber = new BigNumber(value);
|
||||
const hexBase = 16;
|
||||
const result = valueBigNumber.toString(hexBase);
|
||||
|
||||
return valueBigNumber.lt(0) ? '-0x' + result.substr(1) : '0x' + result;
|
||||
},
|
||||
isHexStrict(hex: string | number): boolean {
|
||||
return (
|
||||
(_.isString(hex) || _.isNumber(hex)) && /^(-)?0x[0-9a-f]*$/i.test(_.isNumber(hex) ? hex.toString() : hex)
|
||||
);
|
||||
},
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
import { assert } from '@0xproject/assert';
|
||||
import { schemas } from '@0xproject/json-schemas';
|
||||
import { AbiDecoder, addressUtils, BigNumber, intervalUtils, promisify } from '@0xproject/utils';
|
||||
import { AbiDecoder, addressUtils, BigNumber, intervalUtils, promisify, conversion as utils } from '@0xproject/utils';
|
||||
import {
|
||||
BlockParam,
|
||||
BlockParamLiteral,
|
||||
@@ -23,7 +23,6 @@ import * as _ from 'lodash';
|
||||
|
||||
import { marshaller } from './marshaller';
|
||||
import { BlockWithoutTransactionDataRPC, BlockWithTransactionDataRPC, Web3WrapperErrors } from './types';
|
||||
import { utils } from './utils';
|
||||
|
||||
const BASE_TEN = 10;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user