Merge pull request #540 from 0xProject/feature/abi-gen/tuple-return-types

Support Tuples returned from functions
This commit is contained in:
Jacob Evans 2018-04-20 08:49:23 +10:00 committed by GitHub
commit 62a55c0135
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 90 additions and 23 deletions

View File

@ -109,7 +109,7 @@
"ethereumjs-abi": "^0.6.4",
"ethereumjs-blockstream": "^2.0.6",
"ethereumjs-util": "^5.1.1",
"ethers-contracts": "^2.2.1",
"ethers": "^3.0.15",
"js-sha3": "^0.7.0",
"lodash": "^4.17.4",
"uuid": "^3.1.0",

View File

@ -1,4 +1,13 @@
[
{
"version": "0.3.0",
"changes": [
{
"note": "Update ethers-contracts to ethers.js",
"pr": 540
}
]
},
{
"timestamp": 1524044013,
"version": "0.2.1",

View File

@ -42,7 +42,7 @@
"@0xproject/typescript-typings": "^0.2.0",
"@0xproject/utils": "^0.5.2",
"@0xproject/web3-wrapper": "^0.6.1",
"ethers-contracts": "^2.2.1",
"ethers": "^3.0.15",
"lodash": "^4.17.4"
},
"publishConfig": {

View File

@ -10,13 +10,13 @@ import {
} from '@0xproject/types';
import { abiUtils, BigNumber } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as ethersContracts from 'ethers-contracts';
import * as ethers from 'ethers';
import * as _ from 'lodash';
import { formatABIDataItem } from './utils';
export interface EthersInterfaceByFunctionSignature {
[key: string]: ethersContracts.Interface;
[key: string]: ethers.Interface;
}
export class BaseContract {
@ -62,7 +62,7 @@ export class BaseContract {
}
return txDataWithDefaults;
}
protected _lookupEthersInterface(functionSignature: string): ethersContracts.Interface {
protected _lookupEthersInterface(functionSignature: string): ethers.Interface {
const ethersInterface = this._ethersInterfacesByFunctionSignature[functionSignature];
if (_.isUndefined(ethersInterface)) {
throw new Error(`Failed to lookup method with function signature '${functionSignature}'`);
@ -92,7 +92,7 @@ export class BaseContract {
this._ethersInterfacesByFunctionSignature = {};
_.each(methodAbis, methodAbi => {
const functionSignature = abiUtils.getFunctionSignature(methodAbi);
this._ethersInterfacesByFunctionSignature[functionSignature] = new ethersContracts.Interface([methodAbi]);
this._ethersInterfacesByFunctionSignature[functionSignature] = new ethers.Interface([methodAbi]);
});
}
}

View File

@ -8,7 +8,7 @@ import { BaseContract } from '@0xproject/base-contract';
import { BlockParam, BlockParamLiteral, CallData, ContractAbi, DataItem, MethodAbi, Provider, TxData, TxDataPayable } from '@0xproject/types';
import { BigNumber, classUtils, promisify } from '@0xproject/utils';
import { Web3Wrapper } from '@0xproject/web3-wrapper';
import * as ethersContracts from 'ethers-contracts';
import * as ethers from 'ethers';
import * as _ from 'lodash';
{{#if events}}

View File

@ -5,20 +5,21 @@ async callAsync(
defaultBlock?: BlockParam,
): Promise<{{> return_type outputs=outputs}}> {
const self = this as any as {{contractName}}Contract;
const inputAbi = self._lookupAbi('{{this.functionSignature}}').inputs;
const functionSignature = '{{this.functionSignature}}';
const inputAbi = self._lookupAbi(functionSignature).inputs;
[{{> params inputs=inputs}}] = BaseContract._formatABIDataItemList(inputAbi, [{{> params inputs=inputs}}], BaseContract._bigNumberToString.bind(self));
const encodedData = self._lookupEthersInterface('{{this.functionSignature}}').functions.{{this.name}}(
const ethersFunction = self._lookupEthersInterface(functionSignature).functions.{{this.name}}(
{{> params inputs=inputs}}
).data;
) as ethers.CallDescription;
const encodedData = ethersFunction.data;
const callDataWithDefaults = await self._applyDefaultsToTxDataAsync(
{
data: encodedData,
}
)
const rawCallResult = await self._web3Wrapper.callAsync(callDataWithDefaults, defaultBlock);
let resultArray = ethersFunction.parse(rawCallResult);
const outputAbi = (_.find(self.abi, {name: '{{this.name}}'}) as MethodAbi).outputs;
const outputParamsTypes = _.map(outputAbi, 'type');
let resultArray = ethersContracts.Interface.decodeParams(outputParamsTypes, rawCallResult) as any;
resultArray = BaseContract._formatABIDataItemList(outputAbi, resultArray, BaseContract._lowercaseAddress.bind(this));
resultArray = BaseContract._formatABIDataItemList(outputAbi, resultArray, BaseContract._bnToBigNumber.bind(this));
return resultArray{{#singleReturnValue}}[0]{{/singleReturnValue}};

View File

@ -1,6 +1,6 @@
{{#if outputs.length}}
{{#singleReturnValue}}
{{#returnType outputs.0.type components}}{{/returnType}}
{{#returnType outputs.0.type outputs.0.components}}{{/returnType}}
{{/singleReturnValue}}
{{^singleReturnValue}}
[{{#each outputs}}{{#returnType type components}}{{/returnType}}{{#unless @last}}, {{/unless}}{{/each}}]

View File

@ -32,7 +32,7 @@
"@0xproject/types": "^0.6.1",
"@0xproject/utils": "^0.5.2",
"@0xproject/web3-wrapper": "^0.6.1",
"ethers-contracts": "^2.2.1",
"ethers": "^3.0.15",
"lodash": "^4.17.4",
"web3-provider-engine": "^13.0.1"
},

View File

@ -1,4 +1,13 @@
[
{
"version": "0.3.0",
"changes": [
{
"note": "Add types for `ethers.js`, removing `ethers-contracts`",
"pr": 540
}
]
},
{
"version": "0.2.0",
"changes": [

View File

@ -1,4 +1,4 @@
declare module 'ethers-contracts' {
declare module 'ethers' {
export interface TransactionDescription {
name: string;
signature: string;
@ -12,17 +12,20 @@ declare module 'ethers-contracts' {
(...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;
topic: string;
topics: string[];
}
export class Interface {
public functions: { [functionName: string]: FunctionDescription };
public events: { [eventName: string]: EventDescription };
public static decodeParams(types: string[], data: string): any[];
constructor(abi: any);
}
export class Contract {
constructor(address: string, abi: any, provider: any);
}
}

View File

@ -1,4 +1,13 @@
[
{
"version": "0.6.0",
"changes": [
{
"note": "Update ethers-contracts to ethers.js",
"pr": 540
}
]
},
{
"version": "0.5.2",
"changes": [

View File

@ -35,7 +35,7 @@
"@0xproject/typescript-typings": "^0.2.0",
"@types/node": "^8.0.53",
"bignumber.js": "~4.1.0",
"ethers-contracts": "^2.2.1",
"ethers": "^3.0.15",
"js-sha3": "^0.7.0",
"lodash": "^4.17.4",
"web3": "^0.20.0"

View File

@ -9,7 +9,7 @@ import {
RawLog,
SolidityTypes,
} from '@0xproject/types';
import * as ethersContracts from 'ethers-contracts';
import * as ethers from 'ethers';
import * as _ from 'lodash';
import { BigNumber } from './configured_bignumber';
@ -36,7 +36,7 @@ export class AbiDecoder {
if (_.isUndefined(event)) {
return log;
}
const ethersInterface = new ethersContracts.Interface([event]);
const ethersInterface = new ethers.Interface([event]);
const logData = log.data;
const decodedParams: DecodedLogArgs = {};
let topicsIndex = 1;
@ -77,10 +77,10 @@ export class AbiDecoder {
if (_.isUndefined(abiArray)) {
return;
}
const ethersInterface = new ethersContracts.Interface(abiArray);
const ethersInterface = new ethers.Interface(abiArray);
_.map(abiArray, (abi: AbiDefinition) => {
if (abi.type === AbiType.Event) {
const topic = ethersInterface.events[abi.name].topic;
const topic = ethersInterface.events[abi.name].topics[0];
this._methodIds[topic] = abi;
}
});

View File

@ -61,7 +61,7 @@
"@0xproject/types": "^0.6.1",
"@0xproject/typescript-typings": "^0.2.0",
"@0xproject/utils": "^0.5.2",
"ethers-contracts": "^2.2.1",
"ethers": "^3.0.15",
"lodash": "^4.17.4",
"web3": "^0.20.0"
},

View File

@ -491,6 +491,10 @@ add-stream@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/add-stream/-/add-stream-1.0.0.tgz#6a7990437ca736d5e1288db92bd3266d5f5cb2aa"
aes-js@3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/aes-js/-/aes-js-3.0.0.tgz#e21df10ad6c2053295bcbb8dab40b09dbea87e4d"
aes-js@^0.2.3:
version "0.2.4"
resolved "https://registry.yarnpkg.com/aes-js/-/aes-js-0.2.4.tgz#94b881ab717286d015fa219e08fb66709dda5a3d"
@ -3283,6 +3287,15 @@ electron-to-chromium@^1.2.7, electron-to-chromium@^1.3.30:
version "1.3.40"
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.3.40.tgz#1fbd6d97befd72b8a6f921dc38d22413d2f6fddf"
elliptic@6.3.3:
version "6.3.3"
resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.3.3.tgz#5482d9646d54bcb89fd7d994fc9e2e9568876e3f"
dependencies:
bn.js "^4.4.0"
brorand "^1.0.1"
hash.js "^1.0.0"
inherits "^2.0.1"
elliptic@^6.0.0, elliptic@^6.2.3, elliptic@^6.4.0:
version "6.4.0"
resolved "https://registry.yarnpkg.com/elliptic/-/elliptic-6.4.0.tgz#cac9af8762c85836187003c8dfe193e5e2eae5df"
@ -3688,6 +3701,21 @@ ethers-utils@^2.1.0:
js-sha3 "0.5.7"
xmlhttprequest "1.8.0"
ethers@^3.0.15:
version "3.0.15"
resolved "https://registry.yarnpkg.com/ethers/-/ethers-3.0.15.tgz#7cdea4e23025681f69f575bf481b227315e0e7ab"
dependencies:
aes-js "3.0.0"
bn.js "^4.4.0"
elliptic "6.3.3"
hash.js "^1.0.0"
inherits "2.0.1"
js-sha3 "0.5.7"
scrypt-js "2.0.3"
setimmediate "1.0.4"
uuid "2.0.1"
xmlhttprequest "1.8.0"
ethjs-abi@0.1.8:
version "0.1.8"
resolved "https://registry.yarnpkg.com/ethjs-abi/-/ethjs-abi-0.1.8.tgz#cd288583ed628cdfadaf8adefa3ba1dbcbca6c18"
@ -9235,6 +9263,10 @@ schema-utils@^0.4.5:
ajv "^6.1.0"
ajv-keywords "^3.1.0"
scrypt-js@2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/scrypt-js/-/scrypt-js-2.0.3.tgz#bb0040be03043da9a012a2cea9fc9f852cfc87d4"
scrypt.js@0.2.0, scrypt.js@^0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/scrypt.js/-/scrypt.js-0.2.0.tgz#af8d1465b71e9990110bedfc593b9479e03a8ada"
@ -9419,6 +9451,10 @@ set-value@^2.0.0:
is-plain-object "^2.0.3"
split-string "^3.0.1"
setimmediate@1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/setimmediate/-/setimmediate-1.0.4.tgz#20e81de622d4a02588ce0c8da8973cbcf1d3138f"
setimmediate@^1.0.4, setimmediate@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/setimmediate/-/setimmediate-1.0.5.tgz#290cbb232e306942d7d7ea9b83732ab7856f8285"