Merge pull request #1069 from 0xProject/feature/ts-ethers

Upgrade to TS version of ethers
This commit is contained in:
Leonid Logvinov
2018-09-27 12:47:00 +02:00
committed by GitHub
23 changed files with 106 additions and 100 deletions

View File

@@ -84,7 +84,7 @@
"@0xproject/utils": "^1.0.11",
"@0xproject/web3-wrapper": "^3.0.1",
"ethereum-types": "^1.0.8",
"ethers": "3.0.22",
"ethers": "4.0.0-beta.14",
"lodash": "^4.17.5",
"web3-provider-engine": "14.0.6"
},

View File

@@ -1,4 +1,17 @@
[
{
"version": "3.0.0",
"changes": [
{
"note": "Change the way we detect BN to work with the newest ethers.js",
"pr": 1069
},
{
"note": "Add baseContract._throwIfRevertWithReasonCallResult",
"pr": 1069
}
]
},
{
"timestamp": 1537907159,
"version": "2.0.5",

View File

@@ -44,7 +44,7 @@
"@0xproject/utils": "^1.0.11",
"@0xproject/web3-wrapper": "^3.0.1",
"ethereum-types": "^1.0.8",
"ethers": "3.0.22",
"ethers": "4.0.0-beta.14",
"lodash": "^4.17.5"
},
"publishConfig": {

View File

@@ -20,6 +20,11 @@ export interface EthersInterfaceByFunctionSignature {
[key: string]: ethers.Interface;
}
const REVERT_ERROR_SELECTOR = '08c379a0';
const REVERT_ERROR_SELECTOR_OFFSET = 2;
const REVERT_ERROR_SELECTOR_BYTES_LENGTH = 4;
const REVERT_ERROR_SELECTOR_END = REVERT_ERROR_SELECTOR_OFFSET + REVERT_ERROR_SELECTOR_BYTES_LENGTH * 2;
export class BaseContract {
protected _ethersInterfacesByFunctionSignature: EthersInterfaceByFunctionSignature;
protected _web3Wrapper: Web3Wrapper;
@@ -61,7 +66,7 @@ export class BaseContract {
}
}
protected static _bnToBigNumber(_type: string, value: any): any {
return _.isObject(value) && value._bn ? new BigNumber(value.toString()) : value;
return _.isObject(value) && value._hex ? new BigNumber(value.toString()) : value;
}
protected static async _applyDefaultsToTxDataAsync<T extends Partial<TxData | TxDataPayable>>(
txData: T,
@@ -82,15 +87,24 @@ export class BaseContract {
}
return txDataWithDefaults;
}
protected static _throwIfRevertWithReasonCallResult(rawCallResult: string): void {
if (rawCallResult.slice(REVERT_ERROR_SELECTOR_OFFSET, REVERT_ERROR_SELECTOR_END) === REVERT_ERROR_SELECTOR) {
const revertReason = ethers.utils.defaultAbiCoder.decode(
['string'],
ethers.utils.hexDataSlice(rawCallResult, REVERT_ERROR_SELECTOR_BYTES_LENGTH),
);
throw new Error(revertReason);
}
}
// Throws if the given arguments cannot be safely/correctly encoded based on
// the given inputAbi. An argument may not be considered safely encodeable
// if it overflows the corresponding Solidity type, there is a bug in the
// encoder, or the encoder performs unsafe type coercion.
public static strictArgumentEncodingCheck(inputAbi: DataItem[], args: any[]): void {
const coder = ethers.utils.AbiCoder.defaultCoder;
const coder = new ethers.AbiCoder();
const params = abiUtils.parseEthersParams(inputAbi);
const rawEncoded = coder.encode(params.names, params.types, args);
const rawDecoded = coder.decode(params.names, params.types, rawEncoded);
const rawEncoded = coder.encode(inputAbi, args);
const rawDecoded = coder.decode(inputAbi, rawEncoded);
for (let i = 0; i < rawDecoded.length; i++) {
const original = args[i];
const decoded = rawDecoded[i];

View File

@@ -84,7 +84,7 @@
"ethereum-types": "^1.0.8",
"ethereumjs-blockstream": "6.0.0",
"ethereumjs-util": "^5.1.1",
"ethers": "3.0.22",
"ethers": "4.0.0-beta.14",
"js-sha3": "^0.7.0",
"lodash": "^4.17.5",
"uuid": "^3.1.0"

View File

@@ -65,10 +65,12 @@ export class {{contractName}}Contract extends BaseContract {
[{{> params inputs=ctor.inputs}}],
BaseContract._bigNumberToString,
);
const txData = ethers.Contract.getDeployTransaction(bytecode, abi, {{> params inputs=ctor.inputs}});
const iface = new ethers.Interface(abi);
const deployInfo = iface.deployFunction;
const txData = deployInfo.encode(bytecode, [{{> params inputs=ctor.inputs}}]);
const web3Wrapper = new Web3Wrapper(provider);
const txDataWithDefaults = await BaseContract._applyDefaultsToTxDataAsync(
txData,
{data: txData},
txDefaults,
web3Wrapper.estimateGasAsync.bind(web3Wrapper),
);

View File

@@ -8,10 +8,8 @@ async callAsync(
const inputAbi = self._lookupAbi(functionSignature).inputs;
[{{> params inputs=inputs}}] = BaseContract._formatABIDataItemList(inputAbi, [{{> params inputs=inputs}}], BaseContract._bigNumberToString.bind(self));
BaseContract.strictArgumentEncodingCheck(inputAbi, [{{> params inputs=inputs}}]);
const ethersFunction = self._lookupEthersInterface(functionSignature).functions.{{this.name}}(
{{> params inputs=inputs}}
) as ethers.CallDescription;
const encodedData = ethersFunction.data;
const ethersFunction = self._lookupEthersInterface(functionSignature).functions.{{this.name}};
const encodedData = ethersFunction.encode([{{> params inputs=inputs}}]);
const callDataWithDefaults = await BaseContract._applyDefaultsToTxDataAsync(
{
to: self.address,
@@ -21,7 +19,8 @@ async callAsync(
self._web3Wrapper.getContractDefaults(),
);
const rawCallResult = await self._web3Wrapper.callAsync(callDataWithDefaults, defaultBlock);
let resultArray = ethersFunction.parse(rawCallResult);
BaseContract._throwIfRevertWithReasonCallResult(rawCallResult);
let resultArray = ethersFunction.decode(rawCallResult);
const outputAbi = (_.find(self.abi, {name: '{{this.name}}'}) as MethodAbi).outputs;
resultArray = BaseContract._formatABIDataItemList(outputAbi, resultArray, BaseContract._lowercaseAddress.bind(this));
resultArray = BaseContract._formatABIDataItemList(outputAbi, resultArray, BaseContract._bnToBigNumber.bind(this));

View File

@@ -12,9 +12,7 @@ public {{this.tsName}} = {
const inputAbi = self._lookupAbi('{{this.functionSignature}}').inputs;
[{{> params inputs=inputs}}] = BaseContract._formatABIDataItemList(inputAbi, [{{> params inputs=inputs}}], BaseContract._bigNumberToString.bind(self));
BaseContract.strictArgumentEncodingCheck(inputAbi, [{{> params inputs=inputs}}]);
const encodedData = self._lookupEthersInterface('{{this.functionSignature}}').functions.{{this.name}}(
{{> params inputs=inputs}}
).data;
const encodedData = self._lookupEthersInterface('{{this.functionSignature}}').functions.{{this.name}}.encode([{{> params inputs=inputs}}]);
const txDataWithDefaults = await BaseContract._applyDefaultsToTxDataAsync(
{
to: self.address,
@@ -37,9 +35,7 @@ public {{this.tsName}} = {
const self = this as any as {{contractName}}Contract;
const inputAbi = self._lookupAbi('{{this.functionSignature}}').inputs;
[{{> params inputs=inputs}}] = BaseContract._formatABIDataItemList(inputAbi, [{{> params inputs=inputs}}], BaseContract._bigNumberToString);
const encodedData = self._lookupEthersInterface('{{this.functionSignature}}').functions.{{this.name}}(
{{> params inputs=inputs}}
).data;
const encodedData = self._lookupEthersInterface('{{this.functionSignature}}').functions.{{this.name}}.encode([{{> params inputs=inputs}}]);
const txDataWithDefaults = await BaseContract._applyDefaultsToTxDataAsync(
{
to: self.address,
@@ -57,9 +53,7 @@ public {{this.tsName}} = {
const self = this as any as {{contractName}}Contract;
const inputAbi = self._lookupAbi('{{this.functionSignature}}').inputs;
[{{> params inputs=inputs}}] = BaseContract._formatABIDataItemList(inputAbi, [{{> params inputs=inputs}}], BaseContract._bigNumberToString);
const abiEncodedTransactionData = self._lookupEthersInterface('{{this.functionSignature}}').functions.{{this.name}}(
{{> params inputs=inputs}}
).data;
const abiEncodedTransactionData = self._lookupEthersInterface('{{this.functionSignature}}').functions.{{this.name}}.encode([{{> params inputs=inputs}}]);
return abiEncodedTransactionData;
},
{{> callAsync}}

View File

@@ -83,7 +83,7 @@
"ethereum-types": "^1.0.8",
"ethereumjs-abi": "0.6.5",
"ethereumjs-util": "^5.1.1",
"ethers": "3.0.22",
"ethers": "4.0.0-beta.14",
"js-combinatorics": "^0.5.3",
"lodash": "^4.17.5"
}

View File

@@ -44,7 +44,7 @@
"@0xproject/utils": "^1.0.11",
"@0xproject/web3-wrapper": "^3.0.1",
"ethereum-types": "^1.0.8",
"ethers": "3.0.22",
"ethers": "4.0.0-beta.14",
"lodash": "^4.17.5"
},
"publishConfig": {

View File

@@ -40,7 +40,7 @@
"@types/mocha": "^5.2.2",
"copyfiles": "^2.0.0",
"ethereum-types": "^1.0.8",
"ethers": "3.0.22",
"ethers": "4.0.0-beta.14",
"lodash": "^4.17.5",
"run-s": "^0.0.0"
},

View File

@@ -53,7 +53,7 @@
"@0xproject/web3-wrapper": "^3.0.1",
"@ledgerhq/hw-app-eth": "^4.3.0",
"ethereum-types": "^1.0.8",
"ethers": "3.0.22",
"ethers": "4.0.0-beta.14",
"lodash": "^4.17.5"
},
"optionalDependencies": {

View File

@@ -69,7 +69,7 @@
"ethereum-types": "^1.0.8",
"ethereumjs-abi": "0.6.5",
"ethereumjs-util": "^5.1.1",
"ethers": "3.0.22",
"ethers": "4.0.0-beta.14",
"lodash": "^4.17.5"
},
"publishConfig": {

View File

@@ -83,7 +83,7 @@
"bintrees": "^1.0.2",
"ethereum-types": "^1.0.8",
"ethereumjs-blockstream": "6.0.0",
"ethers": "3.0.22",
"ethers": "4.0.0-beta.14",
"lodash": "^4.17.5"
},
"publishConfig": {

View File

@@ -1,4 +1,13 @@
[
{
"version": "3.0.0",
"changes": [
{
"note": "Remove types for ethers.js",
"pr": "1069"
}
]
},
{
"timestamp": 1537907159,
"version": "2.0.2",

View File

@@ -1,55 +0,0 @@
declare module 'ethers' {
import { TxData } from 'ethereum-types';
export interface TransactionDescription {
name: string;
signature: string;
sighash: string;
data: string;
}
export interface CallDescription extends TransactionDescription {
parse: (...args: any[]) => any;
}
export interface FunctionDescription {
(...params: any[]): TransactionDescription | CallDescription;
inputs: { names: string[]; types: string[] };
outputs: { names: string[]; types: string[] };
type: string;
}
export interface EventDescription {
parse: (...args: any[]) => any;
inputs: { names: string[]; types: string[] };
signature: string;
topics: string[];
}
export class Interface {
public functions: { [functionName: string]: FunctionDescription };
public events: { [eventName: string]: EventDescription };
constructor(abi: any);
}
export class Contract {
public static getDeployTransaction(bytecode: string, abi: any, ...args: any[]): Partial<TxData>;
constructor(address: string, abi: any, provider: any);
}
const enum errors {
INVALID_ARGUMENT = 'INVALID_ARGUMENT',
}
export type ParamName = null | string | NestedParamName;
export interface NestedParamName {
name: string | null;
names: ParamName[];
}
export const utils: {
AbiCoder: {
defaultCoder: AbiCoder;
};
};
export interface AbiCoder {
encode: (names: ParamName[] | string[], types: string[] | any[], args: any[] | undefined) => string;
decode: (names: ParamName[] | string[], types: string[] | string, data: string | undefined) => any;
}
}

View File

@@ -1,4 +1,13 @@
[
{
"version": "2.0.0",
"changes": [
{
"note": "Make abi_decoder compatible with ethers ^4.0.0",
"pr": 1069
}
]
},
{
"timestamp": 1537907159,
"version": "1.0.11",

View File

@@ -49,7 +49,7 @@
"detect-node": "2.0.3",
"ethereum-types": "^1.0.8",
"ethereumjs-util": "^5.1.1",
"ethers": "3.0.22",
"ethers": "4.0.0-beta.14",
"isomorphic-fetch": "^2.2.1",
"js-sha3": "^0.7.0",
"lodash": "^4.17.5"

View File

@@ -47,7 +47,7 @@ export class AbiDecoder {
let decodedData: any[];
try {
decodedData = ethersInterface.events[event.name].parse(log.data);
decodedData = ethersInterface.events[event.name].decode(log.data);
} catch (error) {
if (error.code === ethers.errors.INVALID_ARGUMENT) {
// Because we index events by Method ID, and Method IDs are derived from the method
@@ -99,7 +99,7 @@ export class AbiDecoder {
const ethersInterface = new ethers.Interface(abiArray);
_.map(abiArray, (abi: AbiDefinition) => {
if (abi.type === AbiType.Event) {
const topic = ethersInterface.events[abi.name].topics[0];
const topic = ethersInterface.events[abi.name].topic;
const numIndexedArgs = _.reduce(abi.inputs, (sum, input) => (input.indexed ? sum + 1 : sum), 0);
this._methodIds[topic] = {
...this._methodIds[topic],

View File

@@ -1,14 +1,19 @@
import { AbiDefinition, AbiType, ContractAbi, DataItem, MethodAbi } from 'ethereum-types';
import * as ethers from 'ethers';
import * as _ from 'lodash';
import { BigNumber } from './configured_bignumber';
type ParamName = null | string | NestedParamName;
interface NestedParamName {
name: string | null;
names: ParamName[];
}
// Note(albrow): This function is unexported in ethers.js. Copying it here for
// now.
// Source: https://github.com/ethers-io/ethers.js/blob/884593ab76004a808bf8097e9753fb5f8dcc3067/contracts/interface.js#L30
function parseEthersParams(params: DataItem[]): { names: ethers.ParamName[]; types: string[] } {
const names: ethers.ParamName[] = [];
function parseEthersParams(params: DataItem[]): { names: ParamName[]; types: string[] } {
const names: ParamName[] = [];
const types: string[] = [];
params.forEach((param: DataItem) => {
@@ -37,7 +42,7 @@ function parseEthersParams(params: DataItem[]): { names: ethers.ParamName[]; typ
// returns true if x is equal to y and false otherwise. Performs some minimal
// type conversion and data massaging for x and y, depending on type. name and
// type should typically be derived from parseEthersParams.
function isAbiDataEqual(name: ethers.ParamName, type: string, x: any, y: any): boolean {
function isAbiDataEqual(name: ParamName, type: string, x: any, y: any): boolean {
if (_.isUndefined(x) && _.isUndefined(y)) {
return true;
} else if (_.isUndefined(x) && !_.isUndefined(y)) {
@@ -89,7 +94,7 @@ function isAbiDataEqual(name: ethers.ParamName, type: string, x: any, y: any): b
//
const nestedName = _.isString(name.names[i])
? (name.names[i] as string)
: ((name.names[i] as ethers.NestedParamName).name as string);
: ((name.names[i] as NestedParamName).name as string);
if (!isAbiDataEqual(name.names[i], types[i], x[nestedName], y[nestedName])) {
return false;
}

View File

@@ -59,7 +59,7 @@
"@0xproject/utils": "^1.0.11",
"ethereum-types": "^1.0.8",
"ethereumjs-util": "^5.1.1",
"ethers": "3.0.22",
"ethers": "4.0.0-beta.14",
"lodash": "^4.17.5"
},
"publishConfig": {