diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-11-08 22:37:25 +0800 |
---|---|---|
committer | Greg Hysen <greg.hysen@gmail.com> | 2018-11-29 08:38:10 +0800 |
commit | 1600820deab0636d37a9a50025576a75ae8feeec (patch) | |
tree | cd887c8daf438ccf21187ddbbd357a915d09060a /packages | |
parent | 993c5f4b4a084a210fa158e643027e63fb9443f8 (diff) | |
download | dexon-sol-tools-1600820deab0636d37a9a50025576a75ae8feeec.tar dexon-sol-tools-1600820deab0636d37a9a50025576a75ae8feeec.tar.gz dexon-sol-tools-1600820deab0636d37a9a50025576a75ae8feeec.tar.bz2 dexon-sol-tools-1600820deab0636d37a9a50025576a75ae8feeec.tar.lz dexon-sol-tools-1600820deab0636d37a9a50025576a75ae8feeec.tar.xz dexon-sol-tools-1600820deab0636d37a9a50025576a75ae8feeec.tar.zst dexon-sol-tools-1600820deab0636d37a9a50025576a75ae8feeec.zip |
static array abi tests
Diffstat (limited to 'packages')
-rw-r--r-- | packages/order-utils/test/abi_encoder_test.ts | 131 |
1 files changed, 90 insertions, 41 deletions
diff --git a/packages/order-utils/test/abi_encoder_test.ts b/packages/order-utils/test/abi_encoder_test.ts index 95b6630ee..b27dc4793 100644 --- a/packages/order-utils/test/abi_encoder_test.ts +++ b/packages/order-utils/test/abi_encoder_test.ts @@ -72,6 +72,21 @@ const tupleAbi = { type: 'function', } as MethodAbi; +const staticArrayAbi = { + constant: false, + inputs: [ + { + name: 'someStaticArray', + type: 'uint8[3]', + } + ], + name: 'simpleFunction', + outputs: [], + payable: false, + stateMutability: 'nonpayable', + type: 'function', +} as MethodAbi; + const crazyAbi = { constant: false, inputs: [ @@ -86,7 +101,7 @@ const crazyAbi = { { name: 'someInt32', type: 'int32', - },*/ + }, { name: 'someByte', type: 'byte', @@ -102,7 +117,7 @@ const crazyAbi = { { name: 'someString', type: 'string', - }, + },*/ /*{ name: 'someAddress', type: 'address', @@ -112,12 +127,10 @@ const crazyAbi = { type: 'bool', },*/ - /* - { name: 'someStaticArray', type: 'uint8[3]', - }, + } /* { name: 'someStaticArrayWithDynamicMembers', type: 'string[2]', @@ -195,7 +208,7 @@ const crazyAbi = { type: 'address', }, ], - },*/ + },*/, ], name: 'simpleFunction', outputs: [], @@ -520,6 +533,7 @@ namespace AbiEncoder { public abstract assignValue(value: any): void; public abstract getSignature(): string; + public abstract isStatic(): boolean; } export abstract class StaticDataType extends DataType { @@ -550,6 +564,10 @@ namespace AbiEncoder { return `address`; } + public isStatic(): boolean { + return true; + } + public static matchGrammar(type: string): boolean { return type === 'address'; } @@ -572,6 +590,10 @@ namespace AbiEncoder { return 'bool'; } + public isStatic(): boolean { + return true; + } + public static matchGrammar(type: string): boolean { return type === 'bool'; } @@ -634,6 +656,10 @@ namespace AbiEncoder { this.assignHexValue(encodedValue); } + public isStatic(): boolean { + return true; + } + public abstract getMaxValue(): BigNumber; public abstract getMinValue(): BigNumber; } @@ -715,7 +741,7 @@ namespace AbiEncoder { if (valueBuf.byteLength > this.width) { throw new Error( `Tried to assign ${value} (${ - valueBuf.byteLength + valueBuf.byteLength } bytes), which exceeds max bytes that can be stored in a ${this.getSignature()}`, ); } else if (value.length % 2 !== 0) { @@ -734,6 +760,10 @@ namespace AbiEncoder { return `bytes${this.width}`; } + public isStatic(): boolean { + return true; + } + public static matchGrammar(type: string): boolean { return this.matcher.test(type); } @@ -771,6 +801,10 @@ namespace AbiEncoder { return 'bytes'; } + public isStatic(): boolean { + return false; + } + public static matchGrammar(type: string): boolean { return type === 'bytes'; } @@ -797,6 +831,10 @@ namespace AbiEncoder { return 'string'; } + public isStatic(): boolean { + return false; + } + public static matchGrammar(type: string): boolean { return type === 'string'; } @@ -807,6 +845,7 @@ namespace AbiEncoder { static UNDEFINED_LENGTH = new BigNumber(-1); length: BigNumber = SolArray.UNDEFINED_LENGTH; type: string = '[undefined]'; + isLengthDefined: boolean; constructor(dataItem: DataItem) { super(dataItem); @@ -825,10 +864,12 @@ namespace AbiEncoder { if (matches[2] === '') { this.type = matches[1]; this.length = SolArray.UNDEFINED_LENGTH; + this.isLengthDefined = false; return; } // Parse out array type/length and construct children + this.isLengthDefined = true; this.type = matches[1]; this.length = new BigNumber(matches[2], 10); if (this.length.lessThan(0)) { @@ -873,6 +914,10 @@ namespace AbiEncoder { } public getHexValue(): string { + if (this.isLengthDefined) { + return '0x'; + } + const lengthBufUnpadded = ethUtil.toBuffer(`0x${this.length.toString(16)}`); const lengthBuf = ethUtil.setLengthLeft(lengthBufUnpadded, 32); let valueBuf = lengthBuf; @@ -888,6 +933,10 @@ namespace AbiEncoder { return valueHex; } + public isStatic(): boolean { + return this.isLengthDefined; + } + public static matchGrammar(type: string): boolean { return this.matcher.test(type); } @@ -1003,6 +1052,10 @@ namespace AbiEncoder { return signature; } + public isStatic(): boolean { + return false; // @TODO: True in every case or only when dynamic data? + } + public static matchGrammar(type: string): boolean { return type === 'tuple'; } @@ -1057,6 +1110,10 @@ namespace AbiEncoder { return this.destDataType.getSignature(); } + public isStatic(): boolean { + return true; + } + public encodeToCalldata(calldata: Calldata): void { throw 2; } @@ -1084,9 +1141,9 @@ namespace AbiEncoder { public static create(dataItem: DataItem, parentDataType: DataType): DataType { const dataType = DataTypeFactory.mapDataItemToDataType(dataItem); - if (dataType instanceof StaticDataType) { + if (dataType.isStatic()) { return dataType; - } else if (dataType instanceof DynamicDataType) { + } else { const pointer = new Pointer(dataType, parentDataType); return pointer; } @@ -1178,6 +1235,10 @@ namespace AbiEncoder { return this.getHexValue(); } + public isStatic(): boolean { + return true; + } + /* encodeOptimized(args: any[]): string { const calldata = new Memory(); @@ -1214,36 +1275,13 @@ namespace AbiEncoder { describe.only('ABI Encoder', () => { describe.only('Just a Greg, Eh', () => { - it.skip('Crazy ABI', async () => { - const method = new AbiEncoder.Method(crazyAbi); + it.only('Crazy ABI', async () => { + const method = new AbiEncoder.Method(staticArrayAbi); console.log(method.getSignature()); - /* - (uint256 b,int256 c,int32 d, - bytes1 e,bytes32 f, - bytes g,string h,address i,bool j,uint8[3] k,string[2] l,bytes[] m, string[][] n, O o,P p, P[]) + const args = [[new BigNumber(127), new BigNumber(14), new BigNumber(54)]]; - struct P { - uint256 a; - string b; - string[] c; - bytes d; - address e; - }*/ - - const args = [ - /*new BigNumber(437829473), // b - new BigNumber(-437829473), // c - new BigNumber(-14), // d*/ - '0xaf', // e (bytes1) - '0x0001020304050607080911121314151617181920212223242526272829303132', // f (bytes32) - '0x616161616161616161616161616161616161616161616161616161616161616161616161616161611114f324567838475647382938475677448899338457668899002020202020', // g - 'My first name is Greg and my last name is Hysen, what do ya know!', // h - ]; const a2 = [ - /*'0xe41d2489571d322189246dafa5ebde1f4699f498', // i - true, // j*/ - [new BigNumber(127), new BigNumber(14), new BigNumber(54)], // k [ '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.', @@ -1330,11 +1368,10 @@ describe.only('ABI Encoder', () => { console.log('*'.repeat(40)); console.log(JSON.stringify(args)); - /*const expectedCalldata = - '0x89771c30af00000000000000000000000000000000000000000000000000000000000000000102030405060708091112131415161718192021222324252627282930313200000000000000000000000000000000000000000000000000000000000001800000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000007f000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000036000000000000000000000000000000000000000000000000000000000000028000000000000000000000000000000000000000000000000000000000000003e000000000000000000000000000000000000000000000000000000000000005a000000000000000000000000000000000000000000000000000000000000009e00000000000000000000000000000000000000000000000000000000000000ae00000000000000000000000000000000000000000000000000000000000000047616161616161616161616161616161616161616161616161616161616161616161616161616161611114f3245678384756473829384756774488993384576688990020202020200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000414d79206669727374206e616d65206973204772656720616e64206d79206c617374206e616d6520697320487973656e2c207768617420646f207961206b6e6f772100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000034746865206c6974746c6520706970696e67207069706572207069706564206120706970696e6720706970706572207061707065720000000000000000000000000000000000000000000000000000000000000000000000000000000000000081746865206b6964206b6e6f777320686f7720746f20777269746520706f656d732c20776861742063616e204920736179202d2d2049206775657373207468657265732061206c6f74204920636f756c642073617920746f2074727920746f2066696c6c2074686973206c696e6520776974682061206c6f74206f6620746578742e000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000163874563783498732482743928742389723894723984700000000000000000000000000000000000000000000000000000000000000000000000000000000006e72834723982374239847239847298472489274987489742847289472394874987498478743294237434923473298472398423748923748923748923472389472894789474893742894728947389427498237432987423894723894732894723894372498237498237428934723980000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000027283473298473248923749238742398742398472894729843278942374982374892374892743982000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002800000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000018000000000000000000000000000000000000000000000000000000000000001c0000000000000000000000000000000000000000000000000000000000000000b736f6d6520737472696e670000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013736f6d6520616e6f7468657220737472696e67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024746865726520617265206a75737420746f6f206d616e7920737472696e6773757020696e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000046865726500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002079616c6c2067686f6e6e61206d616b65206d65206c6f7365206d79206d696e640000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000034746865206c6974746c6520706970696e67207069706572207069706564206120706970696e6720706970706572207061707065720000000000000000000000000000000000000000000000000000000000000000000000000000000000000081746865206b6964206b6e6f777320686f7720746f20777269746520706f656d732c20776861742063616e204920736179202d2d2049206775657373207468657265732061206c6f74204920636f756c642073617920746f2074727920746f2066696c6c2074686973206c696e6520776974682061206c6f74206f6620746578742e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000f0ac511500000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000081746865206b6964206b6e6f777320686f7720746f20777269746520706f656d732c20776861742063616e204920736179202d2d2049206775657373207468657265732061206c6f74204920636f756c642073617920746f2074727920746f2066696c6c2074686973206c696e6520776974682061206c6f74206f6620746578742e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000cf5763d5ec63d500600000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000000000000000000100000000000000000000000000e41d2489571d322189246dafa5ebde1f4699f498000000000000000000000000000000000000000000000000000000000000004e616b64686a61736a6b646861736a6b6c647368646a6168646b6a73616864616a6b73646873616a6b646873616a6b646861646a6b617368646a6b73616468616a6b646873616a6b64687361646a6b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024f7484848484848484848484848484848484848384757687980943399445858584893209100000000000000000000000000000000000000000000000000000000'; - */ + const expectedCalldata = + '0xf68ade72000000000000000000000000000000000000000000000000000000000000007f000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000036'; - //expect(calldata).to.be.equal(expectedCalldata); + expect(calldata).to.be.equal(expectedCalldata); /*const calldata = method.encode([{ someUint: new BigNumber(5), someStr: 'five' }]); console.log(method.getSignature()); @@ -1346,8 +1383,20 @@ describe.only('ABI Encoder', () => { expect(calldata).to.be.equal(expectedCalldata);*/ }); - it.only('Simple ABI 2', async () => { - const method = new AbiEncoder.Method(crazyAbi); + it.only('Static Array ABI', async () => { + const method = new AbiEncoder.Method(staticArrayAbi); + const args = [[new BigNumber(127), new BigNumber(14), new BigNumber(54)]]; + const calldata = method.encode(args); + console.log(calldata); + console.log('*'.repeat(40)); + console.log(JSON.stringify(args)); + const expectedCalldata = + '0xf68ade72000000000000000000000000000000000000000000000000000000000000007f000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000036'; + expect(calldata).to.be.equal(expectedCalldata); + }); + + it('Simple ABI 2', async () => { + const method = new AbiEncoder.Method(simpleAbi2); const args = [ '0xaf', // e (bytes1) |