diff options
author | Steve Klebanoff <steve.klebanoff@gmail.com> | 2019-01-16 02:43:55 +0800 |
---|---|---|
committer | Steve Klebanoff <steve.klebanoff@gmail.com> | 2019-01-16 02:43:55 +0800 |
commit | 600b44c9620494c976b5aac6e8a1912543cda81c (patch) | |
tree | fd78c65dd2fdd7b1cbee1b5eff97a95a984a9186 /packages/utils | |
parent | 8a8a5332d76ae8429d1c6449e0ea693a2e8c8a8a (diff) | |
parent | f570f80674c22f69712c45e8e3c48e948b51f357 (diff) | |
download | dexon-sol-tools-600b44c9620494c976b5aac6e8a1912543cda81c.tar dexon-sol-tools-600b44c9620494c976b5aac6e8a1912543cda81c.tar.gz dexon-sol-tools-600b44c9620494c976b5aac6e8a1912543cda81c.tar.bz2 dexon-sol-tools-600b44c9620494c976b5aac6e8a1912543cda81c.tar.lz dexon-sol-tools-600b44c9620494c976b5aac6e8a1912543cda81c.tar.xz dexon-sol-tools-600b44c9620494c976b5aac6e8a1912543cda81c.tar.zst dexon-sol-tools-600b44c9620494c976b5aac6e8a1912543cda81c.zip |
Merge branch 'development' into feature/instant/asset-buyer-check-liquidity
Diffstat (limited to 'packages/utils')
25 files changed, 533 insertions, 112 deletions
diff --git a/packages/utils/CHANGELOG.json b/packages/utils/CHANGELOG.json index 6b4bc3e82..851c106d8 100644 --- a/packages/utils/CHANGELOG.json +++ b/packages/utils/CHANGELOG.json @@ -1,12 +1,22 @@ [ { + "timestamp": 1547561734, + "version": "3.0.1", + "changes": [ + { + "note": "Dependencies updated" + } + ] + }, + { "version": "3.0.0", "changes": [ { "note": "Make `promisify` resolve when the callback error is undefined.", "pr": 1501 } - ] + ], + "timestamp": 1547225310 }, { "version": "2.1.1", diff --git a/packages/utils/CHANGELOG.md b/packages/utils/CHANGELOG.md index 15d78e321..ed3f00158 100644 --- a/packages/utils/CHANGELOG.md +++ b/packages/utils/CHANGELOG.md @@ -5,6 +5,14 @@ Edit the package's CHANGELOG.json file only. CHANGELOG +## v3.0.1 - _January 15, 2019_ + + * Dependencies updated + +## v3.0.0 - _January 11, 2019_ + + * Make `promisify` resolve when the callback error is undefined. (#1501) + ## v2.1.1 - _January 9, 2019_ * Add `should` prefix to names of properties in EncodingRules and DecodingRules (#1363) diff --git a/packages/utils/package.json b/packages/utils/package.json index 49d6e3013..315f5a08f 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -1,6 +1,6 @@ { "name": "@0x/utils", - "version": "2.1.1", + "version": "3.0.1", "engines": { "node": ">=6.12" }, @@ -28,7 +28,7 @@ }, "homepage": "https://github.com/0xProject/0x-monorepo/packages/utils/README.md", "devDependencies": { - "@0x/tslint-config": "^2.0.0", + "@0x/tslint-config": "^2.0.2", "@types/detect-node": "2.0.0", "@types/lodash": "4.14.104", "@types/mocha": "^2.2.42", @@ -44,14 +44,14 @@ "typescript": "3.0.1" }, "dependencies": { - "@0x/types": "^1.5.0", - "@0x/typescript-typings": "^3.0.6", + "@0x/types": "^1.5.2", + "@0x/typescript-typings": "^3.0.8", "@types/node": "*", "abortcontroller-polyfill": "^1.1.9", "bignumber.js": "~4.1.0", "chalk": "^2.4.1", "detect-node": "2.0.3", - "ethereum-types": "^1.1.4", + "ethereum-types": "^1.1.6", "ethereumjs-util": "^5.1.1", "ethers": "~4.0.4", "isomorphic-fetch": "^2.2.1", 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 13cc87e2a..f23324721 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,8 +51,26 @@ 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(isDetailed?: boolean): string { + if (_.isEmpty(this._dataItem.name) || !isDetailed) { + return this.getSignatureType(); + } + const name = this.getDataItem().name; + const lastIndexOfScopeDelimiter = name.lastIndexOf('.'); + const isScopedName = !_.isUndefined(lastIndexOfScopeDelimiter) && lastIndexOfScopeDelimiter > 0; + const shortName = isScopedName ? name.substr((lastIndexOfScopeDelimiter as number) + 1) : name; + const detailedSignature = `${shortName} ${this.getSignatureType()}`; + return detailedSignature; + } + public abstract generateCalldataBlock(value: any, parentBlock?: CalldataBlock): CalldataBlock; public abstract generateValue(calldata: RawCalldata, rules: DecodingRules): any; - public abstract getSignature(): string; + public abstract getSignatureType(): string; public abstract isStatic(): boolean; } diff --git a/packages/utils/src/abi_encoder/abstract_data_types/types/set.ts b/packages/utils/src/abi_encoder/abstract_data_types/types/set.ts index 00059a4b6..2c6c4b0f6 100644 --- a/packages/utils/src/abi_encoder/abstract_data_types/types/set.ts +++ b/packages/utils/src/abi_encoder/abstract_data_types/types/set.ts @@ -1,3 +1,4 @@ +import { ObjectMap } from '@0x/types'; import { DataItem } from 'ethereum-types'; import * as ethUtil from 'ethereumjs-util'; import * as _ from 'lodash'; @@ -134,31 +135,26 @@ export abstract class AbstractSetDataType extends DataType { const block = new SetCalldataBlock(this.getDataItem().name, this.getSignature(), parentName); // Create blocks for members of set. const memberCalldataBlocks: CalldataBlock[] = []; - const childMap = _.cloneDeep(this._memberIndexByName); - _.forOwn(obj, (value: any, key: string) => { - if (!(key in childMap)) { + _.forEach(this._memberIndexByName, (memberIndex: number, memberName: string) => { + if (!(memberName in obj)) { throw new Error( - `Could not assign tuple to object: unrecognized key '${key}' in object ${this.getDataItem().name}`, + `Could not assign tuple to object: missing key '${memberName}' in object ${JSON.stringify(obj)}`, ); } - const memberBlock = this._members[this._memberIndexByName[key]].generateCalldataBlock(value, block); + const memberValue: any = (obj as ObjectMap<any>)[memberName]; + const memberBlock = this._members[memberIndex].generateCalldataBlock(memberValue, block); memberCalldataBlocks.push(memberBlock); - delete childMap[key]; }); - // Sanity check that all members have been included. - if (Object.keys(childMap).length !== 0) { - throw new Error(`Could not assign tuple to object: missing keys ${Object.keys(childMap)}`); - } // Associate member blocks with Set block. block.setMembers(memberCalldataBlocks); return block; } - protected _computeSignatureOfMembers(): string { + protected _computeSignatureOfMembers(isDetailed?: boolean): string { // Compute signature of members let signature = `(`; _.each(this._members, (member: DataType, i: number) => { - signature += member.getSignature(); + signature += member.getSignature(isDetailed); if (i < this._members.length - 1) { signature += ','; } @@ -179,17 +175,27 @@ export abstract class AbstractSetDataType extends DataType { // Create one member for each component of `dataItem` const members: DataType[] = []; const memberIndexByName: MemberIndexByName = {}; + const memberNames: string[] = []; _.each(dataItem.components, (memberItem: DataItem) => { + // If a component with `name` already exists then + // rename to `name_nameIdx` to avoid naming conflicts. + let memberName = memberItem.name; + let nameIdx = 0; + while (_.includes(memberNames, memberName) || _.isEmpty(memberName)) { + nameIdx++; + memberName = `${memberItem.name}_${nameIdx}`; + } + memberNames.push(memberName); const childDataItem: DataItem = { type: memberItem.type, - name: `${dataItem.name}.${memberItem.name}`, + name: `${dataItem.name}.${memberName}`, }; const components = memberItem.components; if (!_.isUndefined(components)) { childDataItem.components = components; } const child = this.getFactory().create(childDataItem, this); - memberIndexByName[memberItem.name] = members.length; + memberIndexByName[memberName] = members.length; members.push(child); }); return [members, memberIndexByName]; 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 4cc124e0a..268649148 100644 --- a/packages/utils/src/abi_encoder/evm_data_type_factory.ts +++ b/packages/utils/src/abi_encoder/evm_data_type_factory.ts @@ -2,6 +2,8 @@ import { DataItem, MethodAbi } from 'ethereum-types'; import * as _ from 'lodash'; +import { generateDataItemsFromSignature } from './utils/signature_parser'; + import { DataType } from './abstract_data_types/data_type'; import { DataTypeFactory } from './abstract_data_types/interfaces'; import { AddressDataType } from './evm_data_types/address'; @@ -129,4 +131,35 @@ export class EvmDataTypeFactory implements DataTypeFactory { private constructor() {} } + +/** + * Convenience function for creating a DataType from different inputs. + * @param input A single or set of DataItem or a DataType signature. + * A signature in the form of '<type>' is interpreted as a `DataItem` + * For example, 'string' is interpreted as {type: 'string'} + * A signature in the form '(<type1>, <type2>, ..., <typen>)' is interpreted as `DataItem[]` + * For eaxmple, '(string, uint256)' is interpreted as [{type: 'string'}, {type: 'uint256'}] + * @return DataType corresponding to input. + */ +export function create(input: DataItem | DataItem[] | string): DataType { + // Handle different types of input + const isSignature = typeof input === 'string'; + const isTupleSignature = isSignature && (input as string).startsWith('('); + const shouldParseAsTuple = isTupleSignature || _.isArray(input); + // Create input `dataItem` + let dataItem: DataItem; + if (shouldParseAsTuple) { + const dataItems = isSignature ? generateDataItemsFromSignature(input as string) : (input as DataItem[]); + dataItem = { + name: '', + type: 'tuple', + components: dataItems, + }; + } else { + dataItem = isSignature ? generateDataItemsFromSignature(input as string)[0] : (input as DataItem); + } + // Create data type + const dataType = EvmDataTypeFactory.getInstance().create(dataItem); + return dataType; +} /* tslint:enable no-construct */ diff --git a/packages/utils/src/abi_encoder/evm_data_types/address.ts b/packages/utils/src/abi_encoder/evm_data_types/address.ts index 2e3a206c6..2278830eb 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/address.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/address.ts @@ -39,10 +39,11 @@ export class AddressDataType extends AbstractBlobDataType { const valueBufPadded = calldata.popWord(); const valueBuf = valueBufPadded.slice(AddressDataType._DECODED_ADDRESS_OFFSET_IN_BYTES); const value = ethUtil.bufferToHex(valueBuf); - return value; + const valueLowercase = _.toLower(value); + return valueLowercase; } - public getSignature(): string { + public getSignatureType(): string { return SolidityTypes.Address; } /* tslint:enable prefer-function-over-method */ diff --git a/packages/utils/src/abi_encoder/evm_data_types/array.ts b/packages/utils/src/abi_encoder/evm_data_types/array.ts index 7595cb667..d9607f47e 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/array.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/array.ts @@ -7,7 +7,6 @@ import { constants } from '../utils/constants'; export class ArrayDataType extends AbstractSetDataType { private static readonly _MATCHER = RegExp('^(.+)\\[([0-9]*)\\]$'); - private readonly _arraySignature: string; private readonly _elementType: string; public static matchType(type: string): boolean { @@ -35,25 +34,36 @@ export class ArrayDataType extends AbstractSetDataType { super(dataItem, dataTypeFactory, isArray, arrayLength, arrayElementType); // Set array properties this._elementType = arrayElementType; - this._arraySignature = this._computeSignature(); } - public getSignature(): string { - return this._arraySignature; + public getSignatureType(): string { + return this._computeSignature(false); } - private _computeSignature(): string { + public getSignature(isDetailed?: boolean): string { + if (_.isEmpty(this.getDataItem().name) || !isDetailed) { + return this.getSignatureType(); + } + const name = this.getDataItem().name; + const lastIndexOfScopeDelimiter = name.lastIndexOf('.'); + const isScopedName = !_.isUndefined(lastIndexOfScopeDelimiter) && lastIndexOfScopeDelimiter > 0; + const shortName = isScopedName ? name.substr((lastIndexOfScopeDelimiter as number) + 1) : name; + const detailedSignature = `${shortName} ${this._computeSignature(isDetailed)}`; + return detailedSignature; + } + + private _computeSignature(isDetailed?: boolean): string { // Compute signature for a single array element const elementDataItem: DataItem = { type: this._elementType, - name: 'N/A', + name: '', }; const elementComponents = this.getDataItem().components; if (!_.isUndefined(elementComponents)) { elementDataItem.components = elementComponents; } const elementDataType = this.getFactory().create(elementDataItem); - const elementSignature = elementDataType.getSignature(); + const elementSignature = elementDataType.getSignature(isDetailed); // Construct signature for array of type `element` if (_.isUndefined(this._arrayLength)) { return `${elementSignature}[]`; diff --git a/packages/utils/src/abi_encoder/evm_data_types/bool.ts b/packages/utils/src/abi_encoder/evm_data_types/bool.ts index d713d5a94..7f91f34e6 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/bool.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/bool.ts @@ -46,7 +46,7 @@ export class BoolDataType extends AbstractBlobDataType { return value; } - public getSignature(): string { + public getSignatureType(): string { return SolidityTypes.Bool; } /* tslint:enable prefer-function-over-method */ diff --git a/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts b/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts index 5277efd6c..fa38b63c0 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/dynamic_bytes.ts @@ -65,7 +65,7 @@ export class DynamicBytesDataType extends AbstractBlobDataType { return value; } - public getSignature(): string { + public getSignatureType(): string { return SolidityTypes.Bytes; } /* tslint:enable prefer-function-over-method */ diff --git a/packages/utils/src/abi_encoder/evm_data_types/int.ts b/packages/utils/src/abi_encoder/evm_data_types/int.ts index f1dcf5ea1..8d98e195b 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/int.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/int.ts @@ -47,13 +47,17 @@ export class IntDataType extends AbstractBlobDataType { return encodedValue; } - public decodeValue(calldata: RawCalldata): BigNumber { + public decodeValue(calldata: RawCalldata): BigNumber | number { const valueBuf = calldata.popWord(); const value = EncoderMath.safeDecodeNumericValue(valueBuf, this._minValue, this._maxValue); + const numberOfBytesInUint8 = 8; + if (this._width === numberOfBytesInUint8) { + return value.toNumber(); + } return value; } - public getSignature(): string { + public getSignatureType(): string { return `${SolidityTypes.Int}${this._width}`; } } 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 b1cd1377f..c852a0fdf 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/method.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/method.ts @@ -44,7 +44,20 @@ export class MethodDataType extends AbstractSetDataType { return returnValues; } - public getSignature(): string { + public strictDecodeReturnValue<T>(returndata: string, rules?: DecodingRules): T { + const returnValues = this._returnDataType.decode(returndata, rules); + const returnValuesAsArray: any = _.isObject(returnValues) ? _.values(returnValues) : [returnValues]; + switch (returnValuesAsArray.length) { + case 0: + return undefined as any; + case 1: + return returnValuesAsArray[0]; + default: + return returnValuesAsArray; + } + } + + public getSignatureType(): string { return this._methodSignature; } diff --git a/packages/utils/src/abi_encoder/evm_data_types/pointer.ts b/packages/utils/src/abi_encoder/evm_data_types/pointer.ts index 389e75927..250db7c64 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/pointer.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/pointer.ts @@ -11,7 +11,11 @@ export class PointerDataType extends AbstractPointerDataType { super(dataItem, dataTypeFactory, destDataType, parentDataType); } - public getSignature(): string { - return this._destination.getSignature(); + public getSignatureType(): string { + return this._destination.getSignature(false); + } + + public getSignature(isDetailed?: boolean): string { + return this._destination.getSignature(isDetailed); } } diff --git a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts index 2e371c505..cbf1957d7 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/static_bytes.ts @@ -36,7 +36,7 @@ export class StaticBytesDataType extends AbstractBlobDataType { this._width = StaticBytesDataType._decodeWidthFromType(dataItem.type); } - public getSignature(): string { + public getSignatureType(): string { // Note that `byte` reduces to `bytes1` return `${SolidityTypes.Bytes}${this._width}`; } diff --git a/packages/utils/src/abi_encoder/evm_data_types/string.ts b/packages/utils/src/abi_encoder/evm_data_types/string.ts index 91a72ad3f..97ac46442 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/string.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/string.ts @@ -52,7 +52,7 @@ export class StringDataType extends AbstractBlobDataType { return value; } - public getSignature(): string { + public getSignatureType(): string { return SolidityTypes.String; } /* tslint:enable prefer-function-over-method */ diff --git a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts index 31593c882..5000c85e8 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/tuple.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/tuple.ts @@ -1,11 +1,10 @@ import { DataItem, SolidityTypes } from 'ethereum-types'; +import * as _ from 'lodash'; import { DataTypeFactory } from '../abstract_data_types/interfaces'; import { AbstractSetDataType } from '../abstract_data_types/types/set'; export class TupleDataType extends AbstractSetDataType { - private readonly _signature: string; - public static matchType(type: string): boolean { return type === SolidityTypes.Tuple; } @@ -15,10 +14,21 @@ export class TupleDataType extends AbstractSetDataType { if (!TupleDataType.matchType(dataItem.type)) { throw new Error(`Tried to instantiate Tuple with bad input: ${dataItem}`); } - this._signature = this._computeSignatureOfMembers(); } - public getSignature(): string { - return this._signature; + public getSignatureType(): string { + return this._computeSignatureOfMembers(false); + } + + public getSignature(isDetailed?: boolean): string { + if (_.isEmpty(this.getDataItem().name) || !isDetailed) { + return this.getSignatureType(); + } + const name = this.getDataItem().name; + const lastIndexOfScopeDelimiter = name.lastIndexOf('.'); + const isScopedName = !_.isUndefined(lastIndexOfScopeDelimiter) && lastIndexOfScopeDelimiter > 0; + const shortName = isScopedName ? name.substr((lastIndexOfScopeDelimiter as number) + 1) : name; + const detailedSignature = `${shortName} ${this._computeSignatureOfMembers(isDetailed)}`; + return detailedSignature; } } diff --git a/packages/utils/src/abi_encoder/evm_data_types/uint.ts b/packages/utils/src/abi_encoder/evm_data_types/uint.ts index 5180f0cf3..8e382e8dc 100644 --- a/packages/utils/src/abi_encoder/evm_data_types/uint.ts +++ b/packages/utils/src/abi_encoder/evm_data_types/uint.ts @@ -46,13 +46,17 @@ export class UIntDataType extends AbstractBlobDataType { return encodedValue; } - public decodeValue(calldata: RawCalldata): BigNumber { + public decodeValue(calldata: RawCalldata): BigNumber | number { const valueBuf = calldata.popWord(); const value = EncoderMath.safeDecodeNumericValue(valueBuf, UIntDataType._MIN_VALUE, this._maxValue); + const numberOfBytesInUint8 = 8; + if (this._width === numberOfBytesInUint8) { + return value.toNumber(); + } return value; } - public getSignature(): string { + public getSignatureType(): string { return `${SolidityTypes.Uint}${this._width}`; } } diff --git a/packages/utils/src/abi_encoder/index.ts b/packages/utils/src/abi_encoder/index.ts index baf844ac6..cfacfe075 100644 --- a/packages/utils/src/abi_encoder/index.ts +++ b/packages/utils/src/abi_encoder/index.ts @@ -11,4 +11,6 @@ export { String, Tuple, UInt, + create, } from './evm_data_type_factory'; +export { DataType } from './abstract_data_types/data_type'; diff --git a/packages/utils/src/abi_encoder/utils/constants.ts b/packages/utils/src/abi_encoder/utils/constants.ts index 36de2dd4f..fc586f295 100644 --- a/packages/utils/src/abi_encoder/utils/constants.ts +++ b/packages/utils/src/abi_encoder/utils/constants.ts @@ -11,7 +11,7 @@ export const constants = { HEX_SELECTOR_BYTE_OFFSET_IN_CALLDATA: 0, // Disable no-object-literal-type-assertion so we can enforce cast /* tslint:disable no-object-literal-type-assertion */ - DEFAULT_DECODING_RULES: { shouldConvertStructsToObjects: false } as DecodingRules, + DEFAULT_DECODING_RULES: { shouldConvertStructsToObjects: true } as DecodingRules, DEFAULT_ENCODING_RULES: { shouldOptimize: true, shouldAnnotate: false } as EncodingRules, /* tslint:enable no-object-literal-type-assertion */ }; diff --git a/packages/utils/src/abi_encoder/utils/signature_parser.ts b/packages/utils/src/abi_encoder/utils/signature_parser.ts new file mode 100644 index 000000000..315784cea --- /dev/null +++ b/packages/utils/src/abi_encoder/utils/signature_parser.ts @@ -0,0 +1,101 @@ +import { DataItem } from 'ethereum-types'; +import * as _ from 'lodash'; + +/** + * Returns an array of DataItem's corresponding to the input signature. + * A signature can be in two forms: '<DataItem.type>' or '(<DataItem1.type>, <DataItem2.type>, ...) + * An example of the first form would be 'address' or 'uint256' + * An example of the second form would be '(address, uint256)' + * Signatures can also include a name field, for example: 'foo address' or '(foo address, bar uint256)' + * @param signature of input DataItems + * @return DataItems derived from input signature + */ +export function generateDataItemsFromSignature(signature: string): DataItem[] { + let trimmedSignature = signature; + if (signature.startsWith('(')) { + if (!signature.endsWith(')')) { + throw new Error(`Failed to generate data item. Must end with ')'`); + } + trimmedSignature = signature.substr(1, signature.length - 2); + } + trimmedSignature += ','; + let isCurrTokenArray = false; + let currTokenArrayModifier = ''; + let isParsingArrayModifier = false; + let currToken = ''; + let parenCount = 0; + let currTokenName = ''; + const dataItems: DataItem[] = []; + for (const char of trimmedSignature) { + // Tokenize the type string while keeping track of parentheses. + switch (char) { + case '(': + parenCount += 1; + currToken += char; + break; + case ')': + parenCount -= 1; + currToken += char; + break; + case '[': + if (parenCount === 0) { + isParsingArrayModifier = true; + isCurrTokenArray = true; + currTokenArrayModifier += '['; + } else { + currToken += char; + } + break; + case ']': + if (parenCount === 0) { + isParsingArrayModifier = false; + currTokenArrayModifier += ']'; + } else { + currToken += char; + } + break; + case ' ': + if (parenCount === 0) { + currTokenName = currToken; + currToken = ''; + } else { + currToken += char; + } + break; + case ',': + if (parenCount === 0) { + // Generate new DataItem from token + const components = currToken.startsWith('(') ? generateDataItemsFromSignature(currToken) : []; + const isTuple = !_.isEmpty(components); + const dataItem: DataItem = { name: currTokenName, type: '' }; + if (isTuple) { + dataItem.type = 'tuple'; + dataItem.components = components; + } else { + dataItem.type = currToken; + } + if (isCurrTokenArray) { + dataItem.type += currTokenArrayModifier; + } + dataItems.push(dataItem); + // reset token state + currTokenName = ''; + currToken = ''; + isCurrTokenArray = false; + currTokenArrayModifier = ''; + break; + } else { + currToken += char; + break; + } + default: + if (isParsingArrayModifier) { + currTokenArrayModifier += char; + } else { + currToken += char; + } + break; + } + } + return dataItems; +} diff --git a/packages/utils/test/abi_encoder/evm_data_types_test.ts b/packages/utils/test/abi_encoder/evm_data_types_test.ts index 55d582d10..54d536a7e 100644 --- a/packages/utils/test/abi_encoder/evm_data_types_test.ts +++ b/packages/utils/test/abi_encoder/evm_data_types_test.ts @@ -26,6 +26,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Dynamic size; Static elements', async () => { // Create DataType object @@ -41,6 +45,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Fixed size; Dynamic elements', async () => { // Create DataType object @@ -56,6 +64,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Dynamic size; Dynamic elements', async () => { // Create DataType object @@ -71,6 +83,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Dynamic Size; Multidimensional; Dynamic Elements', async () => { // Create DataType object @@ -89,6 +105,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Dynamic Size; Multidimensional; Static Elements', async () => { // Create DataType object @@ -107,6 +127,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Static Size; Multidimensional; Static Elements', async () => { // Create DataType object @@ -124,6 +148,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Static Size; Multidimensional; Dynamic Elements', async () => { // Create DataType object @@ -141,6 +169,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Static size; Too Few Elements', async () => { // Create DataType object @@ -197,6 +229,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { const decodingRules: AbiEncoder.DecodingRules = { shouldConvertStructsToObjects: true }; const decodedArgs = dataType.decode(encodedArgs, decodingRules); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args, encodingRules); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Dynamic elements only', async () => { // Create DataType object @@ -217,6 +253,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { const decodingRules: AbiEncoder.DecodingRules = { shouldConvertStructsToObjects: true }; const decodedArgs = dataType.decode(encodedArgs, decodingRules); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Nested Static Array', async () => { // Create DataType object @@ -237,6 +277,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { const decodingRules: AbiEncoder.DecodingRules = { shouldConvertStructsToObjects: true }; const decodedArgs = dataType.decode(encodedArgs, decodingRules); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Nested Dynamic Array', async () => { // Create DataType object @@ -257,6 +301,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { const decodingRules: AbiEncoder.DecodingRules = { shouldConvertStructsToObjects: true }; const decodedArgs = dataType.decode(encodedArgs, decodingRules); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Nested Static Multidimensional Array', async () => { // Create DataType object @@ -279,6 +327,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { const decodingRules: AbiEncoder.DecodingRules = { shouldConvertStructsToObjects: true }; const decodedArgs = dataType.decode(encodedArgs, decodingRules); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Nested Dynamic Multidimensional Array', async () => { // Create DataType object @@ -301,6 +353,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { const decodingRules: AbiEncoder.DecodingRules = { shouldConvertStructsToObjects: true }; const decodedArgs = dataType.decode(encodedArgs, decodingRules); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Static and dynamic elements mixed', async () => { // Create DataType object @@ -331,6 +387,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { const decodingRules: AbiEncoder.DecodingRules = { shouldConvertStructsToObjects: true }; const decodedArgs = dataType.decode(encodedArgs, decodingRules); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Missing Key', async () => { // Create DataType object @@ -345,22 +405,7 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Encode Args and validate result expect(() => { dataType.encode(args, encodingRules); - }).to.throw('Could not assign tuple to object: missing keys field_2'); - }); - it('Bad Key', async () => { - // Create DataType object - const testDataItem = { - name: 'Tuple', - type: 'tuple', - components: [{ name: 'field_1', type: 'int32' }, { name: 'field_2', type: 'bool' }], - }; - const dataType = new AbiEncoder.Tuple(testDataItem); - // Construct args to be encoded - const args = { unknown_field: new BigNumber(-5) }; - // Encode Args and validate result - expect(() => { - dataType.encode(args, encodingRules); - }).to.throw("Could not assign tuple to object: unrecognized key 'unknown_field' in object Tuple"); + }).to.throw('Could not assign tuple to object: missing key \'field_2\' in object {"field_1":"-5"}'); }); }); @@ -378,6 +423,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Invalid Address - input is not valid hex', async () => { // Create DataType object @@ -417,6 +466,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('False', async () => { // Create DataType object @@ -431,6 +484,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); }); @@ -455,6 +512,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Int256 - Negative Base Case', async () => { // Create DataType object @@ -469,6 +530,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Int256 - Positive Value', async () => { // Create DataType object @@ -483,6 +548,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Int256 - Negative Value', async () => { // Create DataType object @@ -497,6 +566,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Int256 - Value too large', async () => { // Create DataType object @@ -533,6 +606,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Int32 - Negative Base Case', async () => { // Create DataType object @@ -547,6 +624,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Int32 - Positive Value', async () => { // Create DataType object @@ -561,6 +642,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Int32 - Negative Value', async () => { // Create DataType object @@ -575,6 +660,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Int32 - Value too large', async () => { // Create DataType object @@ -621,6 +710,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('UInt256 - Positive Value', async () => { // Create DataType object @@ -635,6 +728,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('UInt256 - Zero Value', async () => { // Create DataType object @@ -649,6 +746,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('UInt256 - Value too large', async () => { // Create DataType object @@ -685,6 +786,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('UInt32 - Positive Value', async () => { // Create DataType object @@ -699,6 +804,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('UInt32 - Zero Value', async () => { // Create DataType object @@ -713,6 +822,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('UInt32 - Value too large', async () => { // Create DataType object @@ -752,6 +865,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Single Byte (bytes1)', async () => { // Create DataType object @@ -766,6 +883,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('4 Bytes (bytes4)', async () => { // Create DataType object @@ -780,6 +901,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('4 Bytes (bytes4); Encoder must pad input', async () => { // Create DataType object @@ -796,6 +921,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { const decodedArgs = dataType.decode(encodedArgs); const paddedArgs = '0x1a180000'; expect(decodedArgs).to.be.deep.equal(paddedArgs); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('32 Bytes (bytes32)', async () => { // Create DataType object @@ -810,6 +939,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('32 Bytes (bytes32); Encoder must pad input', async () => { // Create DataType object @@ -826,6 +959,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { const decodedArgs = dataType.decode(encodedArgs); const paddedArgs = '0x1a18bf6100000000000000000000000000000000000000000000000000000000'; expect(decodedArgs).to.be.deep.equal(paddedArgs); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Should throw when pass in too many bytes (bytes4)', async () => { // Create DataType object @@ -893,6 +1030,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Spans multiple EVM words', async () => { // Create DataType object @@ -910,6 +1051,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Input as Buffer', async () => { // Create DataType object @@ -927,6 +1072,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Should throw when pass in bad hex (no 0x prefix)', async () => { // Create DataType object @@ -968,6 +1117,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('Spans multiple EVM words', async () => { // Create DataType object @@ -985,6 +1138,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); it('String that begins with 0x prefix', async () => { // Create DataType object @@ -1002,6 +1159,10 @@ describe('ABI Encoder: EVM Data Type Encoding/Decoding', () => { // Decode Encoded Args and validate result const decodedArgs = dataType.decode(encodedArgs); expect(decodedArgs).to.be.deep.equal(args); + // Validate signature + const dataTypeFromSignature = AbiEncoder.create(dataType.getSignature(true)); + const argsEncodedFromSignature = dataTypeFromSignature.encode(args); + expect(argsEncodedFromSignature).to.be.deep.equal(expectedEncodedArgs); }); }); }); diff --git a/packages/utils/test/abi_encoder/methods_test.ts b/packages/utils/test/abi_encoder/methods_test.ts index a0525967e..d8045fece 100644 --- a/packages/utils/test/abi_encoder/methods_test.ts +++ b/packages/utils/test/abi_encoder/methods_test.ts @@ -21,7 +21,8 @@ describe('ABI Encoder: Method Encoding / Decoding', () => { '0x09f2b0c30000000000000000000000000000000000000000000000000000000000000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff560000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000000000015600000000000000000000000000000000000000000000000000000000000000'; expect(calldata).to.be.equal(expectedCalldata); // Validate decoding - const decodedValue = method.decode(calldata); + const decodingRules = { shouldConvertStructsToObjects: false }; + const decodedValue = method.decode(calldata, decodingRules); expect(decodedValue).to.be.deep.equal(args); }); it('Array of Static Tuples (Array has defined length)', async () => { @@ -40,7 +41,8 @@ describe('ABI Encoder: Method Encoding / Decoding', () => { '0x9eb20969000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f0000000000000000000000000000000000000000000000000000000000000010'; expect(calldata).to.be.equal(expectedCalldata); // Validate decoding - const decodedValue = method.decode(calldata); + const decodingRules = { shouldConvertStructsToObjects: false }; + const decodedValue = method.decode(calldata, decodingRules); expect(decodedValue).to.be.deep.equal(args); }); it('Array of Static Tuples (Array has dynamic length)', async () => { @@ -59,7 +61,8 @@ describe('ABI Encoder: Method Encoding / Decoding', () => { '0x63275d6e00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000700000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000f0000000000000000000000000000000000000000000000000000000000000010'; expect(calldata).to.be.equal(expectedCalldata); // Validate decoding - const decodedValue = method.decode(calldata); + const decodingRules = { shouldConvertStructsToObjects: false }; + const decodedValue = method.decode(calldata, decodingRules); expect(decodedValue).to.be.deep.equal(args); }); it('Array of Dynamic Tuples (Array has defined length)', async () => { @@ -78,7 +81,8 @@ describe('ABI Encoder: Method Encoding / Decoding', () => { '0xdeedb00f00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000280000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000003800000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000048000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000132000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000013400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000138000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000023130000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000023132000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000023134000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000023136000000000000000000000000000000000000000000000000000000000000'; expect(calldata).to.be.equal(expectedCalldata); // Validate decoding - const decodedValue = method.decode(calldata); + const decodingRules = { shouldConvertStructsToObjects: false }; + const decodedValue = method.decode(calldata, decodingRules); expect(decodedValue).to.be.deep.equal(args); }); it('Array of Dynamic Tuples (Array has dynamic length)', async () => { @@ -97,7 +101,8 @@ describe('ABI Encoder: Method Encoding / Decoding', () => { '0x60c847fb000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000280000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000003800000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000048000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000132000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000013400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000001360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000138000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000023130000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000023132000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000023134000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000023136000000000000000000000000000000000000000000000000000000000000'; expect(calldata).to.be.equal(expectedCalldata); // Validate decoding - const decodedValue = method.decode(calldata); + const decodingRules = { shouldConvertStructsToObjects: false }; + const decodedValue = method.decode(calldata, decodingRules); expect(decodedValue).to.be.deep.equal(args); }); it('Multidimensional Arrays / Static Members', async () => { @@ -108,19 +113,16 @@ describe('ABI Encoder: Method Encoding / Decoding', () => { const args = []; const argsLength = 8; for (let i = 0; i < argsLength; ++i) { - args.push([ - [[new BigNumber(++value), new BigNumber(++value)], [new BigNumber(++value), new BigNumber(++value)]], - [[new BigNumber(++value), new BigNumber(++value)], [new BigNumber(++value), new BigNumber(++value)]], - ]); + args.push([[[++value, ++value], [++value, ++value]], [[++value, ++value], [++value, ++value]]]); } const calldata = method.encode(args, encodingRules); // Validate calldata const expectedCalldata = '0xc2f47d6f00000000000000000000000000000000000000000000000000000000000001e00000000000000000000000000000000000000000000000000000000000000480000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000009600000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000d400000000000000000000000000000000000000000000000000000000000000e600000000000000000000000000000000000000000000000000000000000000039000000000000000000000000000000000000000000000000000000000000003a000000000000000000000000000000000000000000000000000000000000003b000000000000000000000000000000000000000000000000000000000000003c000000000000000000000000000000000000000000000000000000000000003d000000000000000000000000000000000000000000000000000000000000003e000000000000000000000000000000000000000000000000000000000000003f00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000300000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001600000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000009000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000b000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000d000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000f0000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000130000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000001500000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000001700000000000000000000000000000000000000000000000000000000000000180000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000019000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001b000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000001d000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000001f000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000140000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000210000000000000000000000000000000000000000000000000000000000000022000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000230000000000000000000000000000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000025000000000000000000000000000000000000000000000000000000000000002600000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000027000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000029000000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000002b000000000000000000000000000000000000000000000000000000000000002c000000000000000000000000000000000000000000000000000000000000002d000000000000000000000000000000000000000000000000000000000000002e000000000000000000000000000000000000000000000000000000000000002f0000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000003100000000000000000000000000000000000000000000000000000000000000320000000000000000000000000000000000000000000000000000000000000033000000000000000000000000000000000000000000000000000000000000003400000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000035000000000000000000000000000000000000000000000000000000000000003600000000000000000000000000000000000000000000000000000000000000370000000000000000000000000000000000000000000000000000000000000038'; expect(calldata).to.be.equal(expectedCalldata); - expect(calldata).to.be.equal(expectedCalldata); // Validate decoding - const decodedValue = method.decode(calldata); + const decodingRules = { shouldConvertStructsToObjects: false }; + const decodedValue = method.decode(calldata, decodingRules); expect(decodedValue).to.be.deep.equal(args); }); it('Multidimensional Arrays / Dynamic Members', async () => { @@ -148,7 +150,8 @@ describe('ABI Encoder: Method Encoding / Decoding', () => { '0x81534ebd0000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000052000000000000000000000000000000000000000000000000000000000000009a00000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000260000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000131000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000013300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000134000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000001350000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000137000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000260000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000139000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002313000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000023131000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000231320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000002313300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023134000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000231350000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002313600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000231370000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002313800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000023139000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000232300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000012000000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000023231000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000232320000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000002323300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023234000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000232350000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002323600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000232370000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002323800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000002323900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023330000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000002333100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000023332000000000000000000000000000000000000000000000000000000000000'; expect(calldata).to.be.equal(expectedCalldata); // Validate decoding - const decodedValue = method.decode(calldata); + const decodingRules = { shouldConvertStructsToObjects: false }; + const decodedValue = method.decode(calldata, decodingRules); expect(decodedValue).to.be.deep.equal(args); }); it('Fixed Length Array / Dynamic Members', async () => { @@ -161,7 +164,8 @@ describe('ABI Encoder: Method Encoding / Decoding', () => { '0x243a6e6e0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000005427261766500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034e657700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c64000000000000000000000000000000000000000000000000000000'; expect(calldata).to.be.equal(expectedCalldata); // Validate decoding - const decodedValue = method.decode(calldata); + const decodingRules = { shouldConvertStructsToObjects: false }; + const decodedValue = method.decode(calldata, decodingRules); expect(decodedValue).to.be.deep.equal(args); }); it('Fixed Length Array / Dynamic Members', async () => { @@ -174,7 +178,8 @@ describe('ABI Encoder: Method Encoding / Decoding', () => { '0x243a6e6e0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000005427261766500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034e657700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c64000000000000000000000000000000000000000000000000000000'; expect(calldata).to.be.equal(expectedCalldata); // Validate decoding - const decodedValue = method.decode(calldata); + const decodingRules = { shouldConvertStructsToObjects: false }; + const decodedValue = method.decode(calldata, decodingRules); expect(decodedValue).to.be.deep.equal(args); }); it('Unfixed Length Array / Dynamic Members ABI', async () => { @@ -187,33 +192,40 @@ describe('ABI Encoder: Method Encoding / Decoding', () => { '0x13e751a900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000005427261766500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000034e657700000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c64000000000000000000000000000000000000000000000000000000'; expect(calldata).to.be.equal(expectedCalldata); // Validate decoding - const decodedValue = method.decode(calldata); + const decodingRules = { shouldConvertStructsToObjects: false }; + const decodedValue = method.decode(calldata, decodingRules); expect(decodedValue).to.be.deep.equal(args); }); it('Unfixed Length Array / Static Members ABI', async () => { // Generate calldata const method = new AbiEncoder.Method(AbiSamples.dynamicArrayStaticMembersAbi); - const args = [[new BigNumber(127), new BigNumber(14), new BigNumber(54)]]; + // tslint:disable custom-no-magic-numbers + const args = [[127, 14, 54]]; + // tslint:enable custom-no-magic-numbers const calldata = method.encode(args, encodingRules); // Validate calldata const expectedCalldata = '0x4fc8a83300000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000007f000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000036'; expect(calldata).to.be.equal(expectedCalldata); // Validate decoding - const decodedValue = method.decode(calldata); + const decodingRules = { shouldConvertStructsToObjects: false }; + const decodedValue = method.decode(calldata, decodingRules); expect(decodedValue).to.be.deep.equal(args); }); it('Fixed Length Array / Static Members ABI', async () => { // Generate calldata const method = new AbiEncoder.Method(AbiSamples.staticArrayAbi); - const args = [[new BigNumber(127), new BigNumber(14), new BigNumber(54)]]; + // tslint:disable custom-no-magic-numbers + const args = [[127, 14, 54]]; + // tslint:enable custom-no-magic-numbers const calldata = method.encode(args, encodingRules); // Validate calldata const expectedCalldata = '0xf68ade72000000000000000000000000000000000000000000000000000000000000007f000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000036'; expect(calldata).to.be.equal(expectedCalldata); // Validate decoding - const decodedValue = method.decode(calldata); + const decodingRules = { shouldConvertStructsToObjects: false }; + const decodedValue = method.decode(calldata, decodingRules); expect(decodedValue).to.be.deep.equal(args); }); it('Array ABI', async () => { @@ -226,7 +238,8 @@ describe('ABI Encoder: Method Encoding / Decoding', () => { '0x13e751a900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000046669766500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000373697800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005736576656e000000000000000000000000000000000000000000000000000000'; expect(calldata).to.be.equal(expectedCalldata); // Validate decoding - const decodedValue = method.decode(calldata); + const decodingRules = { shouldConvertStructsToObjects: false }; + const decodedValue = method.decode(calldata, decodingRules); expect(decodedValue).to.be.deep.equal(args); }); it('Static Tuple', async () => { @@ -240,7 +253,8 @@ describe('ABI Encoder: Method Encoding / Decoding', () => { '0xa9125e150000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000f0000000000000000000000000000000000000000000000000000000000000000'; expect(calldata).to.be.equal(expectedCalldata); // Validate decoding - const decodedValue = method.decode(calldata); + const decodingRules = { shouldConvertStructsToObjects: false }; + const decodedValue = method.decode(calldata, decodingRules); expect(decodedValue).to.be.deep.equal(args); }); it('Dynamic Tuple (Array input)', async () => { @@ -254,7 +268,8 @@ describe('ABI Encoder: Method Encoding / Decoding', () => { '0x5b998f3500000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000046669766500000000000000000000000000000000000000000000000000000000'; expect(calldata).to.be.equal(expectedCalldata); // Validate decoding - const decodedValue = method.decode(calldata); + const decodingRules = { shouldConvertStructsToObjects: false }; + const decodedValue = method.decode(calldata, decodingRules); expect(decodedValue).to.be.deep.equal(args); }); it('Dynamic Tuple (Object input)', async () => { @@ -268,7 +283,8 @@ describe('ABI Encoder: Method Encoding / Decoding', () => { '0x5b998f3500000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000046669766500000000000000000000000000000000000000000000000000000000'; expect(calldata).to.be.equal(expectedCalldata); // Validate decoding - const decodedValue = method.decode(calldata); + const decodingRules = { shouldConvertStructsToObjects: false }; + const decodedValue = method.decode(calldata, decodingRules); expect(decodedValue).to.be.deep.equal(args); }); it('Large, Flat ABI', async () => { @@ -291,13 +307,16 @@ describe('ABI Encoder: Method Encoding / Decoding', () => { '0x312d4d42000000000000000000000000000000000000000000000000000000000f4d9feefffffffffffffffffffffffffffffffffffffffffffffffffffffffff0b26012000000000000000000000000000000000000000000000000000000000006a0444300000000000000000000000000000000000000000000000000000000000000000102030405060708091112131415161718192021222324252627282930313200000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000180000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f4980000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000003800010203040506070809111213141516171819202122232425262728293031320809111213141516171819202122232425262728293031320000000000000000000000000000000000000000000000000000000000000000000000000000002c4c6974746c65207065746572207069706572207069706564206120706970696e672070657070657220706f740000000000000000000000000000000000000000'; expect(calldata).to.be.equal(expectedCalldata); // Validate decoding - const decodedValue = method.decode(calldata); + const decodingRules = { shouldConvertStructsToObjects: false }; + const decodedValue = method.decode(calldata, decodingRules); expect(decodedValue).to.be.deep.equal(args); }); it('Large, Nested ABI', async () => { // Construct Calldata const method = new AbiEncoder.Method(AbiSamples.largeNestedAbi); - const someStaticArray = [new BigNumber(127), new BigNumber(14), new BigNumber(54)]; + // tslint:disable custom-no-magic-numbers + const someStaticArray = [127, 14, 54]; + // tslint:enable custom-no-magic-numbers const someStaticArrayWithDynamicMembers = [ 'the little piping piper piped a piping pipper papper', 'the kid knows how to write poems, what can I say -- I guess theres a lot I could say to try to fill this line with a lot of text.', diff --git a/packages/utils/test/abi_encoder/optimizer_test.ts b/packages/utils/test/abi_encoder/optimizer_test.ts index ee0654ec3..c2881f14c 100644 --- a/packages/utils/test/abi_encoder/optimizer_test.ts +++ b/packages/utils/test/abi_encoder/optimizer_test.ts @@ -23,7 +23,7 @@ describe('ABI Encoder: Optimized Method Encoding/Decoding', () => { '0x7221063300000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000640000000000000000000000000000000000000000000000000000000000000096'; expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata); // Validate decoding - const decodedArgs = method.decode(optimizedCalldata); + const decodedArgs = method.decode(optimizedCalldata, { shouldConvertStructsToObjects: false }); expect(decodedArgs).to.be.deep.equal(args); }); it('Duplicate Dynamic Arrays with Dynamic Elements', async () => { @@ -38,7 +38,7 @@ describe('ABI Encoder: Optimized Method Encoding/Decoding', () => { '0xbb4f12e300000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c64000000000000000000000000000000000000000000000000000000'; expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata); // Validate decoding - const decodedArgs = method.decode(optimizedCalldata); + const decodedArgs = method.decode(optimizedCalldata, { shouldConvertStructsToObjects: false }); expect(decodedArgs).to.be.deep.equal(args); }); it('Duplicate Static Arrays with Static Elements (should not optimize)', async () => { @@ -55,7 +55,7 @@ describe('ABI Encoder: Optimized Method Encoding/Decoding', () => { const unoptimizedCalldata = method.encode(args); expect(optimizedCalldata).to.be.equal(unoptimizedCalldata); // Validate decoding - const decodedArgs = method.decode(optimizedCalldata); + const decodedArgs = method.decode(optimizedCalldata, { shouldConvertStructsToObjects: false }); expect(decodedArgs).to.be.deep.equal(args); }); it('Duplicate Static Arrays with Dynamic Elements', async () => { @@ -70,7 +70,7 @@ describe('ABI Encoder: Optimized Method Encoding/Decoding', () => { '0x9fe31f8e0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c64000000000000000000000000000000000000000000000000000000'; expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata); // Validate decoding - const decodedArgs = method.decode(optimizedCalldata); + const decodedArgs = method.decode(optimizedCalldata, { shouldConvertStructsToObjects: false }); expect(decodedArgs).to.be.deep.equal(args); }); it('Duplicate Array Elements (should optimize)', async () => { @@ -84,7 +84,7 @@ describe('ABI Encoder: Optimized Method Encoding/Decoding', () => { '0x13e751a900000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c64000000000000000000000000000000000000000000000000000000'; expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata); // Validate decoding - const decodedArgs = method.decode(optimizedCalldata); + const decodedArgs = method.decode(optimizedCalldata, { shouldConvertStructsToObjects: false }); expect(decodedArgs).to.be.deep.equal(args); }); it('Duplicate Tuple Fields', async () => { @@ -98,7 +98,7 @@ describe('ABI Encoder: Optimized Method Encoding/Decoding', () => { '0x16780a5e000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata); // Validate decoding - const decodedArgs = method.decode(optimizedCalldata); + const decodedArgs = method.decode(optimizedCalldata, { shouldConvertStructsToObjects: false }); expect(decodedArgs).to.be.deep.equal(args); }); it('Duplicate Strings', async () => { @@ -115,7 +115,7 @@ describe('ABI Encoder: Optimized Method Encoding/Decoding', () => { '0x07370bfa00000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata); // Validate decoding - const decodedArgs = method.decode(optimizedCalldata); + const decodedArgs = method.decode(optimizedCalldata, { shouldConvertStructsToObjects: false }); expect(decodedArgs).to.be.deep.equal(args); }); it('Duplicate Bytes', async () => { @@ -133,7 +133,7 @@ describe('ABI Encoder: Optimized Method Encoding/Decoding', () => { '0x6045e42900000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000002801020304050607080910111213141516171819202122232425262728293031323334353637383940000000000000000000000000000000000000000000000000'; expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata); // Validate decoding - const decodedArgs = method.decode(optimizedCalldata); + const decodedArgs = method.decode(optimizedCalldata, { shouldConvertStructsToObjects: false }); expect(decodedArgs).to.be.deep.equal(args); }); it('Duplicate Tuples', async () => { @@ -148,7 +148,7 @@ describe('ABI Encoder: Optimized Method Encoding/Decoding', () => { '0x564f826d000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000006792a000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20576f726c642100000000000000000000000000000000000000'; expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata); // Validate decoding - const decodedArgs = method.decode(optimizedCalldata); + const decodedArgs = method.decode(optimizedCalldata, { shouldConvertStructsToObjects: false }); expect(decodedArgs).to.be.deep.equal(args); }); it('Duplicate Fields Across Two Tuples', async () => { @@ -164,7 +164,7 @@ describe('ABI Encoder: Optimized Method Encoding/Decoding', () => { '0x564f826d000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20576f726c642100000000000000000000000000000000000000'; expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata); // Validate decoding - const decodedArgs = method.decode(optimizedCalldata); + const decodedArgs = method.decode(optimizedCalldata, { shouldConvertStructsToObjects: false }); expect(decodedArgs).to.be.deep.equal(args); }); it('Duplicate Arrays, Nested in Separate Tuples', async () => { @@ -180,7 +180,7 @@ describe('ABI Encoder: Optimized Method Encoding/Decoding', () => { '0x18970a9e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000064000000000000000000000000000000000000000000000000000000000000009600000000000000000000000000000000000000000000000000000000000000c80000000000000000000000000000000000000000000000000000000000000035657874726120617267756d656e7420746f2070726576656e742065786163746c79206d61746368696e6720746865207475706c65730000000000000000000000'; expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata); // Validate decoding - const decodedArgs = method.decode(optimizedCalldata); + const decodedArgs = method.decode(optimizedCalldata, { shouldConvertStructsToObjects: false }); expect(decodedArgs).to.be.deep.equal(args); }); it('Duplicate Tuples, Nested in Separate Tuples', async () => { @@ -196,7 +196,7 @@ describe('ABI Encoder: Optimized Method Encoding/Decoding', () => { '0x0b4d2e6a000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20576f726c6421000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000035657874726120617267756d656e7420746f2070726576656e742065786163746c79206d61746368696e6720746865207475706c65730000000000000000000000'; expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata); // Validate decoding - const decodedArgs = method.decode(optimizedCalldata); + const decodedArgs = method.decode(optimizedCalldata, { shouldConvertStructsToObjects: false }); expect(decodedArgs).to.be.deep.equal(args); }); it('Duplicate Two-Dimensional Arrays', async () => { @@ -211,7 +211,7 @@ describe('ABI Encoder: Optimized Method Encoding/Decoding', () => { '0x0d28c4f9000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000003466f6f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003426172000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000035a61610000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000003466f6f00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003426172000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000035a61610000000000000000000000000000000000000000000000000000000000'; expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata); // Validate decoding - const decodedArgs = method.decode(optimizedCalldata); + const decodedArgs = method.decode(optimizedCalldata, { shouldConvertStructsToObjects: false }); expect(decodedArgs).to.be.deep.equal(args); }); it('Duplicate Array, Nested within Separate Two-Dimensional Arrays', async () => { @@ -226,14 +226,21 @@ describe('ABI Encoder: Optimized Method Encoding/Decoding', () => { '0x0d28c4f900000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000001200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000003466f6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000548656c6c6f0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005576f726c640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000034261720000000000000000000000000000000000000000000000000000000000'; expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata); // Validate decoding - const decodedArgs = method.decode(optimizedCalldata); + const decodedArgs = method.decode(optimizedCalldata, { shouldConvertStructsToObjects: false }); expect(decodedArgs).to.be.deep.equal(args); }); it('Array Elements Duplicated as Tuple Fields', async () => { // Generate calldata const method = new AbiEncoder.Method(OptimizedAbis.arrayElementsDuplicatedAsTupleFields); - const array = [new BigNumber(100), new BigNumber(150), new BigNumber(200), new BigNumber(225)]; - const tuple = [[array[0]], [array[1]], [array[2]], [array[3]]]; + // tslint:disable custom-no-magic-numbers + const array = [100, 150, 200, 225]; + // tslint:enable custom-no-magic-numbers + const tuple = [ + [new BigNumber(array[0])], + [new BigNumber(array[1])], + [new BigNumber(array[2])], + [new BigNumber(array[3])], + ]; const args = [array, tuple]; // Validata calldata const optimizedCalldata = method.encode(args, encodingRules); @@ -241,7 +248,7 @@ describe('ABI Encoder: Optimized Method Encoding/Decoding', () => { '0x5b5c78fd0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000064000000000000000000000000000000000000000000000000000000000000009600000000000000000000000000000000000000000000000000000000000000c800000000000000000000000000000000000000000000000000000000000000e1'; expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata); // Validate decoding - const decodedArgs = method.decode(optimizedCalldata); + const decodedArgs = method.decode(optimizedCalldata, { shouldConvertStructsToObjects: false }); expect(decodedArgs).to.be.deep.equal(args); }); it('Array Elements Duplicated as Separate Parameter', async () => { @@ -256,7 +263,7 @@ describe('ABI Encoder: Optimized Method Encoding/Decoding', () => { '0xe0e0d34900000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000005576f726c64000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000548656c6c6f000000000000000000000000000000000000000000000000000000'; expect(optimizedCalldata).to.be.equal(expectedOptimizedCalldata); // Validate decoding - const decodedArgs = method.decode(optimizedCalldata); + const decodedArgs = method.decode(optimizedCalldata, { shouldConvertStructsToObjects: false }); expect(decodedArgs).to.be.deep.equal(args); }); }); diff --git a/packages/utils/test/abi_encoder/return_values_test.ts b/packages/utils/test/abi_encoder/return_values_test.ts index 104c7f5db..308d11ee5 100644 --- a/packages/utils/test/abi_encoder/return_values_test.ts +++ b/packages/utils/test/abi_encoder/return_values_test.ts @@ -15,7 +15,7 @@ describe('ABI Encoder: Return Value Encoding/Decoding', () => { // Decode return value const method = new AbiEncoder.Method(ReturnValueAbis.noReturnValues); const returnValue = '0x'; - const decodedReturnValue = method.decodeReturnValues(returnValue); + const decodedReturnValue = method.decodeReturnValues(returnValue, { shouldConvertStructsToObjects: false }); const expectedDecodedReturnValue: any[] = []; expect(decodedReturnValue).to.be.deep.equal(expectedDecodedReturnValue); }); @@ -24,7 +24,9 @@ describe('ABI Encoder: Return Value Encoding/Decoding', () => { const method = new AbiEncoder.Method(ReturnValueAbis.singleStaticReturnValue); const returnValue = ['0x01020304']; const encodedReturnValue = method.encodeReturnValues(returnValue, encodingRules); - const decodedReturnValue = method.decodeReturnValues(encodedReturnValue); + const decodedReturnValue = method.decodeReturnValues(encodedReturnValue, { + shouldConvertStructsToObjects: false, + }); // Validate decoded return value expect(decodedReturnValue).to.be.deep.equal(returnValue); }); @@ -33,7 +35,9 @@ describe('ABI Encoder: Return Value Encoding/Decoding', () => { const method = new AbiEncoder.Method(ReturnValueAbis.multipleStaticReturnValues); const returnValue = ['0x01020304', '0x05060708']; const encodedReturnValue = method.encodeReturnValues(returnValue, encodingRules); - const decodedReturnValue = method.decodeReturnValues(encodedReturnValue); + const decodedReturnValue = method.decodeReturnValues(encodedReturnValue, { + shouldConvertStructsToObjects: false, + }); // Validate decoded return value expect(decodedReturnValue).to.be.deep.equal(returnValue); }); @@ -42,7 +46,9 @@ describe('ABI Encoder: Return Value Encoding/Decoding', () => { const method = new AbiEncoder.Method(ReturnValueAbis.singleDynamicReturnValue); const returnValue = ['0x01020304']; const encodedReturnValue = method.encodeReturnValues(returnValue, encodingRules); - const decodedReturnValue = method.decodeReturnValues(encodedReturnValue); + const decodedReturnValue = method.decodeReturnValues(encodedReturnValue, { + shouldConvertStructsToObjects: false, + }); // Validate decoded return value expect(decodedReturnValue).to.be.deep.equal(returnValue); }); @@ -51,7 +57,9 @@ describe('ABI Encoder: Return Value Encoding/Decoding', () => { const method = new AbiEncoder.Method(ReturnValueAbis.multipleDynamicReturnValues); const returnValue = ['0x01020304', '0x05060708']; const encodedReturnValue = method.encodeReturnValues(returnValue, encodingRules); - const decodedReturnValue = method.decodeReturnValues(encodedReturnValue); + const decodedReturnValue = method.decodeReturnValues(encodedReturnValue, { + shouldConvertStructsToObjects: false, + }); // Validate decoded return value expect(decodedReturnValue).to.be.deep.equal(returnValue); }); @@ -60,7 +68,9 @@ describe('ABI Encoder: Return Value Encoding/Decoding', () => { const method = new AbiEncoder.Method(ReturnValueAbis.mixedStaticAndDynamicReturnValues); const returnValue = ['0x01020304', '0x05060708']; const encodedReturnValue = method.encodeReturnValues(returnValue, encodingRules); - const decodedReturnValue = method.decodeReturnValues(encodedReturnValue); + const decodedReturnValue = method.decodeReturnValues(encodedReturnValue, { + shouldConvertStructsToObjects: false, + }); // Validate decoded return value expect(decodedReturnValue).to.be.deep.equal(returnValue); }); diff --git a/packages/utils/test/abi_encoder/signature_tests.ts b/packages/utils/test/abi_encoder/signature_tests.ts new file mode 100644 index 000000000..e69de29bb --- /dev/null +++ b/packages/utils/test/abi_encoder/signature_tests.ts |