diff options
Diffstat (limited to 'packages')
-rw-r--r-- | packages/order-utils/test/abi/abi_encoder.ts | 1 | ||||
-rw-r--r-- | packages/order-utils/test/abi/config.ts | 4 | ||||
-rw-r--r-- | packages/order-utils/test/abi/data_type.ts | 72 | ||||
-rw-r--r-- | packages/order-utils/test/abi/evm_data_types.ts | 36 | ||||
-rw-r--r-- | packages/order-utils/test/abi_encoder_test.ts | 4 |
5 files changed, 44 insertions, 73 deletions
diff --git a/packages/order-utils/test/abi/abi_encoder.ts b/packages/order-utils/test/abi/abi_encoder.ts index ddcfb1fd1..4f4906550 100644 --- a/packages/order-utils/test/abi/abi_encoder.ts +++ b/packages/order-utils/test/abi/abi_encoder.ts @@ -1,4 +1,3 @@ -export * from './config'; export * from './calldata'; export * from './data_type'; export * from './evm_data_types';
\ No newline at end of file diff --git a/packages/order-utils/test/abi/config.ts b/packages/order-utils/test/abi/config.ts deleted file mode 100644 index 015cee59a..000000000 --- a/packages/order-utils/test/abi/config.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { DataTypeFactory } from './data_type'; -import { EvmDataTypeFactoryImpl } from './evm_data_types'; - -DataTypeFactory.setImpl(new EvmDataTypeFactoryImpl());
\ No newline at end of file diff --git a/packages/order-utils/test/abi/data_type.ts b/packages/order-utils/test/abi/data_type.ts index 7884cf3cf..4c4537a69 100644 --- a/packages/order-utils/test/abi/data_type.ts +++ b/packages/order-utils/test/abi/data_type.ts @@ -5,20 +5,31 @@ import { BigNumber } from '@0x/utils'; import ethUtil = require('ethereumjs-util'); var _ = require('lodash'); +export interface DataTypeFactory { + create: (dataItem: DataItem, parentDataType: DataType) => DataType; + mapDataItemToDataType: (dataItem: DataItem) => DataType; +} + export abstract class DataType { private static DEFAULT_ENCODING_RULES = { optimize: false, annotate: false } as EncodingRules; private static DEFAULT_DECODING_RULES = { structsAsObjects: false } as DecodingRules; private dataItem: DataItem; + private factory: DataTypeFactory; - constructor(dataItem: DataItem) { + constructor(dataItem: DataItem, factory: DataTypeFactory) { this.dataItem = dataItem; + this.factory = factory; } public getDataItem(): DataItem { return this.dataItem; } + public getFactory(): DataTypeFactory { + return this.factory; + } + public encode(value: any, rules?: EncodingRules, selector?: string): string { const rules_ = rules ? rules : DataType.DEFAULT_ENCODING_RULES; const calldata = new Calldata(rules_); @@ -45,8 +56,8 @@ export abstract class DataType { export abstract class PayloadDataType extends DataType { protected hasConstantSize: boolean; - public constructor(dataItem: DataItem, hasConstantSize: boolean) { - super(dataItem); + public constructor(dataItem: DataItem, factory: DataTypeFactory, hasConstantSize: boolean) { + super(dataItem, factory); this.hasConstantSize = hasConstantSize; } @@ -78,8 +89,8 @@ export abstract class DependentDataType extends DataType { protected dependency: DataType; protected parent: DataType; - public constructor(dataItem: DataItem, dependency: DataType, parent: DataType) { - super(dataItem); + public constructor(dataItem: DataItem, factory: DataTypeFactory, dependency: DataType, parent: DataType) { + super(dataItem, factory); this.dependency = dependency; this.parent = parent; } @@ -125,8 +136,8 @@ export abstract class MemberDataType extends DataType { protected arrayElementType: string | undefined; - public constructor(dataItem: DataItem, isArray: boolean = false, arrayLength?: number, arrayElementType?: string) { - super(dataItem); + public constructor(dataItem: DataItem, factory: DataTypeFactory, isArray: boolean = false, arrayLength?: number, arrayElementType?: string) { + super(dataItem, factory); this.memberMap = {}; this.members = []; this.isArray = isArray; @@ -156,7 +167,7 @@ export abstract class MemberDataType extends DataType { if (components !== undefined) { childDataItem.components = components; } - const child = DataTypeFactory.create(childDataItem, this); + const child = this.getFactory().create(childDataItem, this); memberMap[memberItem.name] = members.length; members.push(child); }); @@ -177,7 +188,7 @@ export abstract class MemberDataType extends DataType { if (components !== undefined) { childDataItem.components = components; } - const child = DataTypeFactory.create(childDataItem, this); + const child = this.getFactory().create(childDataItem, this); memberMap[idx.toString(10)] = members.length; members.push(child); }); @@ -309,46 +320,3 @@ export abstract class MemberDataType extends DataType { return isStatic; } } - -export interface DataTypeFactoryImpl { - create: (dataItem: DataItem, parentDataType: DataType) => DataType; - mapDataItemToDataType: (dataItem: DataItem) => DataType; -} - -export class DataTypeFactory { - private static instance: DataTypeFactory; - private provider: DataTypeFactoryImpl | undefined; - - private constructor() { } - - private static getInstance(): DataTypeFactory { - if (!DataTypeFactory.instance) { - DataTypeFactory.instance = new DataTypeFactory(); - } - return DataTypeFactory.instance; - } - - public static setImpl(provider: DataTypeFactoryImpl) { - const instance = DataTypeFactory.getInstance(); - if (instance.provider !== undefined) { - throw new Error(`Tried to set implementation more than once`); - } - DataTypeFactory.getInstance().provider = provider; - } - - public static create(dataItem: DataItem, parentDataType: DataType): DataType { - const instance = DataTypeFactory.getInstance(); - if (instance.provider === undefined) { - throw new Error(`Tried to create before implementation is set`); - } - return instance.provider.create(dataItem, parentDataType); - } - - public static mapDataItemToDataType(dataItem: DataItem): DataType { - const instance = DataTypeFactory.getInstance(); - if (instance.provider === undefined) { - throw new Error(`Tried to create before implementation is set`); - } - return instance.provider.mapDataItemToDataType(dataItem); - } -}
\ No newline at end of file diff --git a/packages/order-utils/test/abi/evm_data_types.ts b/packages/order-utils/test/abi/evm_data_types.ts index aff4bc991..bb7c1d81d 100644 --- a/packages/order-utils/test/abi/evm_data_types.ts +++ b/packages/order-utils/test/abi/evm_data_types.ts @@ -1,4 +1,4 @@ -import { DataType, DataTypeFactory, DataTypeFactoryImpl, PayloadDataType, DependentDataType, MemberDataType } from './data_type'; +import { DataType, DataTypeFactory, PayloadDataType, DependentDataType, MemberDataType } from './data_type'; import { DecodingRules, EncodingRules } from './calldata'; @@ -22,7 +22,7 @@ export class Address extends PayloadDataType { private static SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; constructor(dataItem: DataItem) { - super(dataItem, Address.SIZE_KNOWN_AT_COMPILE_TIME); + super(dataItem, EvmDataTypeFactory.getInstance(), Address.SIZE_KNOWN_AT_COMPILE_TIME); if (!Address.matchGrammar(dataItem.type)) { throw new Error(`Tried to instantiate Address with bad input: ${dataItem}`); } @@ -54,7 +54,7 @@ export class Bool extends PayloadDataType { private static SIZE_KNOWN_AT_COMPILE_TIME: boolean = true; constructor(dataItem: DataItem) { - super(dataItem, Bool.SIZE_KNOWN_AT_COMPILE_TIME); + super(dataItem, EvmDataTypeFactory.getInstance(), Bool.SIZE_KNOWN_AT_COMPILE_TIME); if (!Bool.matchGrammar(dataItem.type)) { throw new Error(`Tried to instantiate Bool with bad input: ${dataItem}`); } @@ -94,7 +94,7 @@ abstract class Number extends PayloadDataType { width: number = Number.DEFAULT_WIDTH; constructor(dataItem: DataItem, matcher: RegExp) { - super(dataItem, Number.SIZE_KNOWN_AT_COMPILE_TIME); + super(dataItem, EvmDataTypeFactory.getInstance(), Number.SIZE_KNOWN_AT_COMPILE_TIME); const matches = matcher.exec(dataItem.type); if (matches === null) { throw new Error(`Tried to instantiate Number with bad input: ${dataItem}`); @@ -243,7 +243,7 @@ export class Byte extends PayloadDataType { width: number = Byte.DEFAULT_WIDTH; constructor(dataItem: DataItem) { - super(dataItem, Byte.SIZE_KNOWN_AT_COMPILE_TIME); + super(dataItem, EvmDataTypeFactory.getInstance(), Byte.SIZE_KNOWN_AT_COMPILE_TIME); const matches = Byte.matcher.exec(dataItem.type); if (!Byte.matchGrammar(dataItem.type)) { throw new Error(`Tried to instantiate Byte with bad input: ${dataItem}`); @@ -297,7 +297,7 @@ export class Bytes extends PayloadDataType { length: BigNumber = Bytes.UNDEFINED_LENGTH; constructor(dataItem: DataItem) { - super(dataItem, Bytes.SIZE_KNOWN_AT_COMPILE_TIME); + super(dataItem, EvmDataTypeFactory.getInstance(), Bytes.SIZE_KNOWN_AT_COMPILE_TIME); if (!Bytes.matchGrammar(dataItem.type)) { throw new Error(`Tried to instantiate Bytes with bad input: ${dataItem}`); } @@ -343,7 +343,7 @@ export class Bytes extends PayloadDataType { export class SolString extends PayloadDataType { private static SIZE_KNOWN_AT_COMPILE_TIME: boolean = false; constructor(dataItem: DataItem) { - super(dataItem, SolString.SIZE_KNOWN_AT_COMPILE_TIME); + super(dataItem, EvmDataTypeFactory.getInstance(), SolString.SIZE_KNOWN_AT_COMPILE_TIME); if (!SolString.matchGrammar(dataItem.type)) { throw new Error(`Tried to instantiate String with bad input: ${dataItem}`); } @@ -383,7 +383,7 @@ export class Pointer extends DependentDataType { constructor(destDataType: DataType, parentDataType: DataType) { const destDataItem = destDataType.getDataItem(); const dataItem = { name: `ptr<${destDataItem.name}>`, type: `ptr<${destDataItem.type}>` } as DataItem; - super(dataItem, destDataType, parentDataType); + super(dataItem, EvmDataTypeFactory.getInstance(), destDataType, parentDataType); } public getSignature(): string { @@ -395,7 +395,7 @@ export class Tuple extends MemberDataType { private tupleSignature: string; constructor(dataItem: DataItem) { - super(dataItem); + super(dataItem, EvmDataTypeFactory.getInstance()); if (!Tuple.matchGrammar(dataItem.type)) { throw new Error(`Tried to instantiate Tuple with bad input: ${dataItem}`); } @@ -430,7 +430,7 @@ export class SolArray extends MemberDataType { const isArray = true; const arrayElementType = matches[1]; const arrayLength = (matches[2] === '') ? undefined : parseInt(matches[2], 10); - super(dataItem, isArray, arrayLength, arrayElementType); + super(dataItem, EvmDataTypeFactory.getInstance(), isArray, arrayLength, arrayElementType); this.elementType = arrayElementType; this.arraySignature = this.computeSignature(); } @@ -444,7 +444,7 @@ export class SolArray extends MemberDataType { if (components !== undefined) { dataItem.components = components; } - const elementDataType = DataTypeFactory.mapDataItemToDataType(dataItem); + const elementDataType = this.getFactory().mapDataItemToDataType(dataItem); const type = elementDataType.getSignature(); if (this.arrayLength === undefined) { return `${type}[]`; @@ -470,7 +470,7 @@ export class Method extends MemberDataType { public selector: string; constructor(abi: MethodAbi) { - super({ type: 'method', name: abi.name, components: abi.inputs }); + super({ type: 'method', name: abi.name, components: abi.inputs }, EvmDataTypeFactory.getInstance()); this.methodSignature = this.computeSignature(); this.selector = this.methodSelector = this.computeSelector(); @@ -510,7 +510,17 @@ export class Method extends MemberDataType { } } -export class EvmDataTypeFactoryImpl implements DataTypeFactoryImpl { +export class EvmDataTypeFactory implements DataTypeFactory { + private static instance: DataTypeFactory; + + private constructor() { } + + public static getInstance(): DataTypeFactory { + if (!EvmDataTypeFactory.instance) { + EvmDataTypeFactory.instance = new EvmDataTypeFactory(); + } + return EvmDataTypeFactory.instance; + } public mapDataItemToDataType(dataItem: DataItem): DataType { if (SolArray.matchGrammar(dataItem.type)) return new SolArray(dataItem); diff --git a/packages/order-utils/test/abi_encoder_test.ts b/packages/order-utils/test/abi_encoder_test.ts index 8ae23a222..295311acc 100644 --- a/packages/order-utils/test/abi_encoder_test.ts +++ b/packages/order-utils/test/abi_encoder_test.ts @@ -19,8 +19,6 @@ import * as AbiEncoder from './abi/abi_encoder'; import * as AbiSamples from './abi_samples'; import { Calldata } from './abi/calldata'; -AbiEncoder.DataTypeFactory.setImpl(new AbiEncoder.EvmDataTypeFactoryImpl()); - chaiSetup.configure(); const expect = chai.expect; @@ -56,7 +54,7 @@ describe.only('ABI Encoder', () => { console.log('*'.repeat(100), '\n', method.encode(args, { optimize: true }), '\n', '*'.repeat(100)); }); - it.only('Should point array elements to a duplicated value from another parameter', async () => { + it('Should point array elements to a duplicated value from another parameter', async () => { const method = new AbiEncoder.Method(AbiSamples.optimizerAbi2); const stringArray = [ "Test String", |