diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-12-24 11:27:38 +0800 |
---|---|---|
committer | Greg Hysen <greg.hysen@gmail.com> | 2019-01-15 02:49:44 +0800 |
commit | d1fd4421be0a7639b99b374bf04e57fac4e8a486 (patch) | |
tree | a97d00cbea579864f23803fa391e169814d93f6e /packages | |
parent | b06f8239e1fe75703f88d34c0d225701406e28c2 (diff) | |
download | dexon-0x-contracts-d1fd4421be0a7639b99b374bf04e57fac4e8a486.tar.gz dexon-0x-contracts-d1fd4421be0a7639b99b374bf04e57fac4e8a486.tar.zst dexon-0x-contracts-d1fd4421be0a7639b99b374bf04e57fac4e8a486.zip |
Use string argument encoding with new encoder
Diffstat (limited to 'packages')
6 files changed, 36 insertions, 48 deletions
diff --git a/packages/abi-gen-templates/contract.handlebars b/packages/abi-gen-templates/contract.handlebars index 7e7171c70..2b328e6fa 100644 --- a/packages/abi-gen-templates/contract.handlebars +++ b/packages/abi-gen-templates/contract.handlebars @@ -68,6 +68,7 @@ export class {{contractName}}Contract extends BaseContract { ); const iface = new ethers.utils.Interface(abi); const deployInfo = iface.deployFunction; + console.log('*'.repeat(50), `\n${JSON.stringify(deployInfo)}`); const txData = deployInfo.encode(bytecode, [{{> params inputs=ctor.inputs}}]); const web3Wrapper = new Web3Wrapper(provider); const txDataWithDefaults = await BaseContract._applyDefaultsToTxDataAsync( diff --git a/packages/base-contract/src/index.ts b/packages/base-contract/src/index.ts index aeb90e6f7..bb806c2e5 100644 --- a/packages/base-contract/src/index.ts +++ b/packages/base-contract/src/index.ts @@ -41,12 +41,6 @@ export class BaseContract { ): any { return _.map(values, (value: any, i: number) => formatABIDataItem(abis[i], value, formatter)); } - protected static _lowercaseAddress(type: string, value: string): string { - return type === 'address' ? value.toLowerCase() : value; - } - protected static _bigNumberToString(_type: string, value: any): any { - return _.isObject(value) && value.isBigNumber ? value.toString() : value; - } protected static _lookupConstructorAbi(abi: ContractAbi): ConstructorAbi { const constructorAbiIfExists = _.find( abi, @@ -67,9 +61,6 @@ export class BaseContract { return defaultConstructorAbi; } } - protected static _bnToBigNumber(_type: string, value: any): any { - return _.isObject(value) && value._hex ? new BigNumber(value.toString()) : value; - } protected static async _applyDefaultsToTxDataAsync<T extends Partial<TxData | TxDataPayable>>( txData: T, txDefaults: Partial<TxData>, @@ -99,11 +90,11 @@ export class BaseContract { // 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 = new ethers.utils.AbiCoder(); + public static strictArgumentEncodingCheck(inputAbi: DataItem[], args: any[]): string { + const abiEncoder = AbiEncoder.create(inputAbi); const params = abiUtils.parseEthersParams(inputAbi); - const rawEncoded = coder.encode(inputAbi, args); - const rawDecoded = coder.decode(inputAbi, rawEncoded); + const rawEncoded = abiEncoder.encode(args); + const rawDecoded = abiEncoder.decodeAsArray(rawEncoded); for (let i = 0; i < rawDecoded.length; i++) { const original = args[i]; const decoded = rawDecoded[i]; @@ -115,7 +106,14 @@ export class BaseContract { ); } } + return rawEncoded; } + protected static _lowercaseAddress(type: string, value: string): string { + return type === 'address' ? value.toLowerCase() : value; + } + protected static _bigNumberToString(_type: string, value: any): any { + return _.isObject(value) && value.isBigNumber ? value.toString() : value; + } protected _lookupAbiEncoder(functionSignature: string): AbiEncoder.Method { const abiEncoder = this._abiEncoderByFunctionSignature[functionSignature]; if (_.isUndefined(abiEncoder)) { @@ -143,21 +141,8 @@ export class BaseContract { if (inputAbi === undefined) { throw new Error(`Undefined Method Input ABI`); } - const params = abiUtils.parseEthersParams(inputAbi); - const rawEncoded = abiEncoder.encode(functionArguments); - const rawDecoded = abiEncoder.decodeAsArray(rawEncoded); - for (let i = 0; i < rawDecoded.length; i++) { - const original = functionArguments[i]; - const decoded = rawDecoded[i]; - if (!abiUtils.isAbiDataEqual(params.names[i], params.types[i], original, decoded)) { - throw new Error( - `Cannot safely encode argument: ${params.names[i]} (${original}) of type ${ - params.types[i] - }. (Possible type overflow or other encoding error)`, - ); - } - } - return rawEncoded; + const abiEncodedArguments = abiEncoder.encode(functionArguments);////BaseContract.strictArgumentEncodingCheck(inputAbi, functionArguments); + return abiEncodedArguments; } constructor( contractName: string, diff --git a/packages/utils/src/abi_encoder/abstract_data_types/data_type.ts b/packages/utils/src/abi_encoder/abstract_data_types/data_type.ts index 5bd6aae03..dc897d810 100644 --- a/packages/utils/src/abi_encoder/abstract_data_types/data_type.ts +++ b/packages/utils/src/abi_encoder/abstract_data_types/data_type.ts @@ -51,6 +51,12 @@ export abstract class DataType { return value; } + public decodeAsArray(returndata: string, rules?: DecodingRules): any { + const value = this.decode(returndata, rules); + const valuesAsArray = _.isObject(value) ? _.values(value) : [value]; + return valuesAsArray; + } + public getSignature(detailed?: boolean): string { if (_.isEmpty(this._dataItem.name) || !detailed) return this.getSignatureType(); const name = this.getDataItem().name; diff --git a/packages/utils/src/abi_encoder/evm_data_type_factory.ts b/packages/utils/src/abi_encoder/evm_data_type_factory.ts index 5ff7366d6..fdd4f0fde 100644 --- a/packages/utils/src/abi_encoder/evm_data_type_factory.ts +++ b/packages/utils/src/abi_encoder/evm_data_type_factory.ts @@ -135,21 +135,23 @@ export class EvmDataTypeFactory implements DataTypeFactory { // Convenience function export function create(input: DataItem | DataItem[] | string): DataType { // Handle different types of input - let dataItems: DataItem[] = []; - if (typeof(input) === 'string') { - dataItems = generateDataItemsFromSignature(input); - } else if(input instanceof Array) { - dataItems = input as DataItem[]; + const isSignature = typeof(input) === 'string'; + const isTupleSignature = isSignature && (input as string).startsWith('('); + const parseAsTuple = isTupleSignature || _.isArray(input); + // Create input `dataItem` + let dataItem: DataItem; + if(parseAsTuple) { + const dataItems = isSignature ? generateDataItemsFromSignature(input as string) : input as DataItem[]; + dataItem = { + name: '', + type: 'tuple', + components: dataItems + }; } else { - dataItems = [input as DataItem]; + dataItem = isSignature ? generateDataItemsFromSignature(input as string)[0] : input as DataItem; } - // Create single data item from input - let dataItem: DataItem = dataItems.length === 1 ? dataItems[0] : { - name: '', - type: 'tuple', - components: dataItems - }; // Create data type - return EvmDataTypeFactory.getInstance().create(dataItem); + const dataType = EvmDataTypeFactory.getInstance().create(dataItem); + return dataType; } /* tslint:enable no-construct */ diff --git a/packages/utils/src/abi_encoder/evm_data_types/method.ts b/packages/utils/src/abi_encoder/evm_data_types/method.ts index 212e2ca6b..44456cd0a 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/method.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/method.ts @@ -35,12 +35,6 @@ export class MethodDataType extends AbstractSetDataType { return value; } - public decodeAsArray(returndata: string, rules?: DecodingRules): any { - const value = this.decode(returndata, rules); - const valuesAsArray = _.isObject(value) ? _.values(value) : [value]; - return valuesAsArray; - } - public encodeReturnValues(value: any, rules?: EncodingRules): string { const returnData = this._returnDataType.encode(value, rules); return returnData; diff --git a/packages/utils/src/abi_encoder/index.ts b/packages/utils/src/abi_encoder/index.ts index f7f1ceedc..cfacfe075 100644 --- a/packages/utils/src/abi_encoder/index.ts +++ b/packages/utils/src/abi_encoder/index.ts @@ -13,4 +13,4 @@ export { UInt, create, } from './evm_data_type_factory'; -//export { fromSignature } from './utils/signatureParser'; +export { DataType } from './abstract_data_types/data_type'; |