diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-11-08 06:18:05 +0800 |
---|---|---|
committer | Greg Hysen <greg.hysen@gmail.com> | 2018-11-29 08:38:10 +0800 |
commit | 7de7fe782398eded47598046699fdc0915597e33 (patch) | |
tree | 27c76041be49fc869fa6d432dc4ae18347ed7d54 /packages | |
parent | 5053c19599e96b9886e6b36173350162b92405a3 (diff) | |
download | dexon-0x-contracts-7de7fe782398eded47598046699fdc0915597e33.tar dexon-0x-contracts-7de7fe782398eded47598046699fdc0915597e33.tar.gz dexon-0x-contracts-7de7fe782398eded47598046699fdc0915597e33.tar.bz2 dexon-0x-contracts-7de7fe782398eded47598046699fdc0915597e33.tar.lz dexon-0x-contracts-7de7fe782398eded47598046699fdc0915597e33.tar.xz dexon-0x-contracts-7de7fe782398eded47598046699fdc0915597e33.tar.zst dexon-0x-contracts-7de7fe782398eded47598046699fdc0915597e33.zip |
Array of basic types works
Diffstat (limited to 'packages')
-rw-r--r-- | packages/order-utils/test/abi_encoder_test.ts | 53 |
1 files changed, 48 insertions, 5 deletions
diff --git a/packages/order-utils/test/abi_encoder_test.ts b/packages/order-utils/test/abi_encoder_test.ts index bed75b935..a455e6a00 100644 --- a/packages/order-utils/test/abi_encoder_test.ts +++ b/packages/order-utils/test/abi_encoder_test.ts @@ -612,14 +612,16 @@ namespace AbiEncoder { return; } - // Parse out array type and length + // Parse out array type/length and construct children this.type = matches[1]; this.length = new BigNumber(matches[2], 10); if (this.length.lessThan(0)) { throw new Error(`Bad array length: ${JSON.stringify(this.length)}`); } + this.constructChildren(); + } - // Construct children + private constructChildren() { for (let idx = new BigNumber(0); idx.lessThan(this.length); idx = idx.plus(1)) { const childDataItem = { type: this.type, @@ -631,8 +633,43 @@ namespace AbiEncoder { } public assignValue(value: any[]) { - //const hexValue = ethUtil.bufferToHex(new Buffer(value)); - //this.assignHexValue(hexValue); + // Sanity check length + const valueLength = new BigNumber(value.length); + if (this.length !== SolArray.UNDEFINED_LENGTH && valueLength.equals(this.length) === false) { + throw new Error( + `Expected array of length ${JSON.stringify(this.length)}, but got array of length ${JSON.stringify( + valueLength, + )}`, + ); + } + + // Assign length if not already set + if (this.length === SolArray.UNDEFINED_LENGTH) { + this.length = valueLength; + this.constructChildren(); + } + + // Assign values to children + for (let idx = new BigNumber(0); idx.lessThan(this.length); idx = idx.plus(1)) { + const idxNumber = idx.toNumber(); + this.children[idxNumber].assignValue(value[idxNumber]); + } + } + + public getHexValue(): string { + const lengthBufUnpadded = ethUtil.toBuffer(`0x${this.length.toString(16)}`); + const lengthBuf = ethUtil.setLengthLeft(lengthBufUnpadded, 32); + let valueBuf = lengthBuf; + for (let idx = new BigNumber(0); idx.lessThan(this.length); idx = idx.plus(1)) { + const idxNumber = idx.toNumber(); + const childValueHex = this.children[idxNumber].getHexValue(); + const childValueBuf = ethUtil.toBuffer(childValueHex); + valueBuf = Buffer.concat([valueBuf, childValueBuf]); + } + + // Convert value buffer to hex + const valueHex = ethUtil.bufferToHex(valueBuf); + return valueHex; } public static matchGrammar(type: string): boolean { @@ -839,9 +876,15 @@ namespace AbiEncoder { describe.only('ABI Encoder', () => { describe.only('Array', () => { it('sample', async () => { - const testDataItem = { name: 'testArray', type: 'string[2]' }; + const testDataItem = { name: 'testArray', type: 'int[2]' }; const dataType = new AbiEncoder.SolArray(testDataItem); console.log(JSON.stringify(dataType, null, 4)); + console.log('*'.repeat(60)); + dataType.assignValue([new BigNumber(5), new BigNumber(6)]); + console.log(JSON.stringify(dataType, null, 4)); + const hexValue = dataType.getHexValue(); + console.log('*'.repeat(60)); + console.log(hexValue); }); }); |