diff options
author | Greg Hysen <greg.hysen@gmail.com> | 2018-11-10 15:00:20 +0800 |
---|---|---|
committer | Greg Hysen <greg.hysen@gmail.com> | 2018-11-29 08:38:10 +0800 |
commit | bb7dd2373893366dcab0fe1c4cffe0c1e31af837 (patch) | |
tree | 19daf64bf9aba4edada4ef3c4014f066ded90fb1 | |
parent | 59206c387e702492d22e975acf806ebe7de36bfd (diff) | |
download | dexon-sol-tools-bb7dd2373893366dcab0fe1c4cffe0c1e31af837.tar.gz dexon-sol-tools-bb7dd2373893366dcab0fe1c4cffe0c1e31af837.tar.zst dexon-sol-tools-bb7dd2373893366dcab0fe1c4cffe0c1e31af837.zip |
passing 18 tests
-rw-r--r-- | packages/order-utils/test/abi/calldata.ts | 54 | ||||
-rw-r--r-- | packages/order-utils/test/abi/data_type.ts | 13 | ||||
-rw-r--r-- | packages/order-utils/test/abi/evm_data_types.ts | 1 | ||||
-rw-r--r-- | packages/order-utils/test/abi_encoder_test.ts | 4 |
4 files changed, 63 insertions, 9 deletions
diff --git a/packages/order-utils/test/abi/calldata.ts b/packages/order-utils/test/abi/calldata.ts index dc67a731e..37dd30e57 100644 --- a/packages/order-utils/test/abi/calldata.ts +++ b/packages/order-utils/test/abi/calldata.ts @@ -1,4 +1,5 @@ import ethUtil = require('ethereumjs-util'); +import CommunicationChatBubbleOutline from 'material-ui/SvgIcon'; var _ = require('lodash'); export abstract class CalldataBlock { @@ -157,12 +158,18 @@ class Queue<T> { pop(): T | undefined { return this.store.shift(); } + merge(q: Queue<T>) { + this.store = this.store.concat(q.store); + } + mergeFront(q: Queue<T>) { + this.store = q.store.concat(this.store); + } } export class Calldata { private selector: string; private sizeInBytes: number; - private root: CalldataBlock | undefined; + private root: MemberCalldataBlock | undefined; constructor() { this.selector = '0x'; @@ -170,12 +177,53 @@ export class Calldata { this.root = undefined; } + private createQueue(memberBlock: MemberCalldataBlock): Queue<CalldataBlock> { + const blockQueue = new Queue<CalldataBlock>(); + _.eachRight(memberBlock.getMembers(), (member: CalldataBlock) => { + if (member instanceof MemberCalldataBlock) { + blockQueue.mergeFront(this.createQueue(member)); + } else { + blockQueue.pushFront(member); + } + }); + + // Children + _.each(memberBlock.getMembers(), (member: CalldataBlock) => { + if (member instanceof DependentCalldataBlock) { + const dependency = member.getDependency(); + if (dependency instanceof MemberCalldataBlock) { + blockQueue.merge(this.createQueue(dependency)); + } else { + blockQueue.push(dependency); + } + } + }); + + blockQueue.pushFront(memberBlock); + return blockQueue; + } + public toHexString(): string { let selectorBuffer = ethUtil.toBuffer(this.selector); if (this.root === undefined) { throw new Error('expected root'); } + const offsetQueue = this.createQueue(this.root); + let block: CalldataBlock | undefined; + let offset = 0; + while ((block = offsetQueue.pop()) !== undefined) { + block.setOffset(offset); + offset += block.getSizeInBytes(); + } + + const valueQueue = this.createQueue(this.root); + const valueBufs: Buffer[] = [selectorBuffer]; + while ((block = valueQueue.pop()) !== undefined) { + valueBufs.push(block.toBuffer()); + } + + /* const blockQueue = new Queue<CalldataBlock>(); blockQueue.push(this.root); @@ -211,7 +259,7 @@ export class Calldata { blockQueue.pushFront(member); }); } - } + }*/ const combinedBuffers = Buffer.concat(valueBufs); const hexValue = ethUtil.bufferToHex(combinedBuffers); @@ -230,7 +278,7 @@ export class Calldata { return ""; } - public setRoot(block: CalldataBlock) { + public setRoot(block: MemberCalldataBlock) { this.root = block; this.sizeInBytes += block.getSizeInBytes(); } diff --git a/packages/order-utils/test/abi/data_type.ts b/packages/order-utils/test/abi/data_type.ts index 0783b9c56..7f110c4fc 100644 --- a/packages/order-utils/test/abi/data_type.ts +++ b/packages/order-utils/test/abi/data_type.ts @@ -43,7 +43,7 @@ export abstract class PayloadDataType extends DataType { public encode(value: any, calldata: Calldata): void { const block = this.generateCalldataBlock(value); - calldata.setRoot(block); + // calldata.setRoot(block); } public isStatic(): boolean { @@ -78,7 +78,7 @@ export abstract class DependentDataType extends DataType { public encode(value: any, calldata: Calldata = new Calldata()): void { const block = this.generateCalldataBlock(value); - calldata.setRoot(block); + //calldata.setRoot(block); } public isStatic(): boolean { @@ -125,15 +125,20 @@ export abstract class MemberDataType extends DataType { type: memberItem.type, name: `${dataItem.name}.${memberItem.name}`, } as DataItem; + const components = memberItem.components; + if (components !== undefined) { + childDataItem.components = components; + } const child = DataTypeFactory.create(childDataItem, this); + memberMap[memberItem.name] = members.length; members.push(child); - memberMap[dataItem.name] = members.length; }); return [members, memberMap]; } private createMembersWithLength(dataItem: DataItem, length: number): [DataType[], MemberMap] { + console.log('!'.repeat(30), dataItem); let members: DataType[] = []; let memberMap: MemberMap = {}; const range = _.range(length); @@ -147,8 +152,8 @@ export abstract class MemberDataType extends DataType { childDataItem.components = components; } const child = DataTypeFactory.create(childDataItem, this); - members.push(child); memberMap[idx.toString(10)] = members.length; + members.push(child); }); return [members, memberMap]; diff --git a/packages/order-utils/test/abi/evm_data_types.ts b/packages/order-utils/test/abi/evm_data_types.ts index fcced646a..dfa541e80 100644 --- a/packages/order-utils/test/abi/evm_data_types.ts +++ b/packages/order-utils/test/abi/evm_data_types.ts @@ -315,6 +315,7 @@ export class Tuple extends MemberDataType { private tupleSignature: string; constructor(dataItem: DataItem) { + console.log(dataItem); super(dataItem); if (!Tuple.matchGrammar(dataItem.type)) { throw new Error(`Tried to instantiate Tuple with bad input: ${dataItem}`); diff --git a/packages/order-utils/test/abi_encoder_test.ts b/packages/order-utils/test/abi_encoder_test.ts index 404f160fd..984507923 100644 --- a/packages/order-utils/test/abi_encoder_test.ts +++ b/packages/order-utils/test/abi_encoder_test.ts @@ -116,7 +116,7 @@ describe.only('ABI Encoder', () => { }); - it.only('Types with default widths', async () => { + it('Types with default widths', async () => { const method = new AbiEncoder.Method(AbiSamples.typesWithDefaultWidthsAbi); console.log(method); const args = [new BigNumber(1), new BigNumber(-1), '0x56', [new BigNumber(1)], [new BigNumber(-1)], ['0x56']]; @@ -201,7 +201,7 @@ describe.only('ABI Encoder', () => { expect(calldata).to.be.equal(expectedCalldata); }); - it.only('Multidimensional Arrays / Static Members', async () => { + it('Multidimensional Arrays / Static Members', async () => { const method = new AbiEncoder.Method(AbiSamples.multiDimensionalArraysStaticTypeAbi); // Eight 3-dimensional arrays of uint8[2][2][2] |