diff options
author | Jacob Evans <jacob@dekz.net> | 2018-04-19 09:21:32 +0800 |
---|---|---|
committer | Jacob Evans <jacob@dekz.net> | 2018-04-19 09:21:32 +0800 |
commit | 12ae7c009d548c2b7241b3a3896b159f1829a500 (patch) | |
tree | f00bc364052a4c5d8317350bb077991fe8d2f08e /packages | |
parent | 62fcb51e1a9893b8d18536eefc9497d6c1f36c39 (diff) | |
download | dexon-sol-tools-12ae7c009d548c2b7241b3a3896b159f1829a500.tar dexon-sol-tools-12ae7c009d548c2b7241b3a3896b159f1829a500.tar.gz dexon-sol-tools-12ae7c009d548c2b7241b3a3896b159f1829a500.tar.bz2 dexon-sol-tools-12ae7c009d548c2b7241b3a3896b159f1829a500.tar.lz dexon-sol-tools-12ae7c009d548c2b7241b3a3896b159f1829a500.tar.xz dexon-sol-tools-12ae7c009d548c2b7241b3a3896b159f1829a500.tar.zst dexon-sol-tools-12ae7c009d548c2b7241b3a3896b159f1829a500.zip |
Support Tuples from function returns
Diffstat (limited to 'packages')
-rw-r--r-- | packages/base-contract/package.json | 2 | ||||
-rw-r--r-- | packages/base-contract/src/index.ts | 8 | ||||
-rw-r--r-- | packages/contract_templates/contract.handlebars | 2 | ||||
-rw-r--r-- | packages/contract_templates/partials/callAsync.handlebars | 19 | ||||
-rw-r--r-- | packages/contract_templates/partials/return_type.handlebars | 2 | ||||
-rw-r--r-- | packages/typescript-typings/types/ethers/index.d.ts | 39 |
6 files changed, 57 insertions, 15 deletions
diff --git a/packages/base-contract/package.json b/packages/base-contract/package.json index 22ae43cdc..78dcbfe7f 100644 --- a/packages/base-contract/package.json +++ b/packages/base-contract/package.json @@ -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": { diff --git a/packages/base-contract/src/index.ts b/packages/base-contract/src/index.ts index bfa99fac1..e95b18db6 100644 --- a/packages/base-contract/src/index.ts +++ b/packages/base-contract/src/index.ts @@ -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]); }); } } diff --git a/packages/contract_templates/contract.handlebars b/packages/contract_templates/contract.handlebars index 472452d74..cd50e14cd 100644 --- a/packages/contract_templates/contract.handlebars +++ b/packages/contract_templates/contract.handlebars @@ -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}} diff --git a/packages/contract_templates/partials/callAsync.handlebars b/packages/contract_templates/partials/callAsync.handlebars index a6f4abdf2..3cc16ccc6 100644 --- a/packages/contract_templates/partials/callAsync.handlebars +++ b/packages/contract_templates/partials/callAsync.handlebars @@ -5,22 +5,25 @@ 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 result = 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}}; + result = BaseContract._formatABIDataItemList(outputAbi, result, BaseContract._lowercaseAddress.bind(this)); + result = BaseContract._formatABIDataItemList(outputAbi, result, BaseContract._bnToBigNumber.bind(this)); + + return result{{#singleReturnValue}}[0]{{/singleReturnValue}}; }, {{/hasReturnValue}} diff --git a/packages/contract_templates/partials/return_type.handlebars b/packages/contract_templates/partials/return_type.handlebars index 9dd509953..77d96e8c4 100644 --- a/packages/contract_templates/partials/return_type.handlebars +++ b/packages/contract_templates/partials/return_type.handlebars @@ -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}}] diff --git a/packages/typescript-typings/types/ethers/index.d.ts b/packages/typescript-typings/types/ethers/index.d.ts new file mode 100644 index 000000000..313e8eb8a --- /dev/null +++ b/packages/typescript-typings/types/ethers/index.d.ts @@ -0,0 +1,39 @@ +declare module 'ethers' { + 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; + topic: 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); + } +} + +declare module 'ethers/utils/abi-coder' { + export class Coder { + public decode(names: any[], types: any[], data?: any[]): any[]; + defaultCoder: Coder; + } +} |