diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-11-08 05:44:27 +0800 |
---|---|---|
committer | Greg Hysen <greg.hysen@gmail.com> | 2018-11-29 08:38:10 +0800 |
commit | 5053c19599e96b9886e6b36173350162b92405a3 (patch) | |
tree | 07d140f84c8ac6a5fd6dd907bedb92db2f002b1a | |
parent | f35cf030308c0f18ad26669bfb3b269a5b96a39e (diff) | |
download | dexon-0x-contracts-5053c19599e96b9886e6b36173350162b92405a3.tar.gz dexon-0x-contracts-5053c19599e96b9886e6b36173350162b92405a3.tar.zst dexon-0x-contracts-5053c19599e96b9886e6b36173350162b92405a3.zip |
constructor for an Array + a test -- appears to work
-rw-r--r-- | packages/order-utils/test/abi_encoder_test.ts | 87 |
1 files changed, 58 insertions, 29 deletions
diff --git a/packages/order-utils/test/abi_encoder_test.ts b/packages/order-utils/test/abi_encoder_test.ts index 926f7bc81..bed75b935 100644 --- a/packages/order-utils/test/abi_encoder_test.ts +++ b/packages/order-utils/test/abi_encoder_test.ts @@ -528,7 +528,7 @@ namespace AbiEncoder { } } - export class Bytes extends StaticDataType { + export class Bytes extends DynamicDataType { static UNDEFINED_LENGTH = new BigNumber(-1); length: BigNumber = SolArray.UNDEFINED_LENGTH; @@ -565,7 +565,7 @@ namespace AbiEncoder { } } - export class SolString extends StaticDataType { + export class SolString extends DynamicDataType { constructor(dataItem: DataItem) { super(dataItem); expect(SolString.matchGrammar(dataItem.type)).to.be.true(); @@ -591,42 +591,63 @@ namespace AbiEncoder { } } - export class Tuple extends DynamicDataType { + export class SolArray extends DynamicDataType { + static matcher = RegExp('^(.+)\\[([0-9]d*)\\]$'); + static UNDEFINED_LENGTH = new BigNumber(-1); + length: BigNumber = SolArray.UNDEFINED_LENGTH; + type: string = '[undefined]'; + constructor(dataItem: DataItem) { super(dataItem); - expect(Tuple.matchGrammar(dataItem.type)).to.be.true(); + const matches = SolArray.matcher.exec(dataItem.type); + expect(matches).to.be.not.null(); + if (matches === null || matches.length !== 3) { + throw new Error(`Could not parse array: ${dataItem.type}`); + } else if (matches[1] === undefined) { + throw new Error(`Could not parse array type: ${dataItem.type}`); + } else if (matches[2] === undefined) { + // Parse out array type and length + this.type = matches[1]; + this.length = new BigNumber(SolArray.UNDEFINED_LENGTH); + return; + } + + // Parse out array type and length + 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)}`); + } + + // Construct children + for (let idx = new BigNumber(0); idx.lessThan(this.length); idx = idx.plus(1)) { + const childDataItem = { + type: this.type, + name: `${this.getDataItem().name}[${idx.toString(10)}]`, + } as DataItem; + const child = DataTypeFactory.create(childDataItem); + this.children.push(child); + } } - public assignValue(value: string) { + public assignValue(value: any[]) { //const hexValue = ethUtil.bufferToHex(new Buffer(value)); //this.assignHexValue(hexValue); } - public getSignature(): string { - throw 1; - } - - public encodeToCalldata(calldata: Calldata): void { - throw 2; + public static matchGrammar(type: string): boolean { + return this.matcher.test(type); } - public static matchGrammar(type: string): boolean { - return type === 'tuple'; + public getSignature(): string { + return `${this.type}[${this.length}]`; } } - export class SolArray extends DynamicDataType { - static matcher = RegExp('^.+\\[([0-9]d*)\\]$'); - static UNDEFINED_LENGTH = new BigNumber(-1); - length: BigNumber = SolArray.UNDEFINED_LENGTH; - + export class Tuple extends DynamicDataType { constructor(dataItem: DataItem) { super(dataItem); - const matches = SolArray.matcher.exec(dataItem.type); - expect(matches).to.be.not.null(); - if (matches !== null && matches.length === 1) { - this.length = new BigNumber(matches[1], 10); - } + expect(Tuple.matchGrammar(dataItem.type)).to.be.true(); } public assignValue(value: string) { @@ -634,16 +655,16 @@ namespace AbiEncoder { //this.assignHexValue(hexValue); } + public getSignature(): string { + throw 1; + } + public encodeToCalldata(calldata: Calldata): void { throw 2; } public static matchGrammar(type: string): boolean { - return this.matcher.test(type); - } - - public getSignature(): string { - throw 1; + return type === 'tuple'; } } @@ -816,7 +837,15 @@ namespace AbiEncoder { } describe.only('ABI Encoder', () => { - describe.only('Just a Greg, Eh', () => { + describe.only('Array', () => { + it('sample', async () => { + const testDataItem = { name: 'testArray', type: 'string[2]' }; + const dataType = new AbiEncoder.SolArray(testDataItem); + console.log(JSON.stringify(dataType, null, 4)); + }); + }); + + describe('Just a Greg, Eh', () => { it('Yessir', async () => { const method = new AbiEncoder.Method(simpleAbi); const calldata = method.encode([new BigNumber(5), 'five']); |