Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support overriding encodeInput and decodeOutput #36

Merged
merged 7 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions abi-gen/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
[
{
"version": "5.6.0",
"changes": [
{
"note": "Support overriding encodeInput and decodeOutput",
"pr": 36
}
]
},
{
"version": "5.5.2",
"changes": [
Expand Down
8 changes: 6 additions & 2 deletions abi-gen/templates/TypeScript/contract.handlebars
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// tslint:disable:no-unused-variable
import {
AwaitTransactionSuccessOpts,
EncoderOverrides,
ContractFunctionObj,
ContractTxFunctionObj,
SendTransactionOpts,
Expand Down Expand Up @@ -260,6 +261,9 @@ export class {{contractName}}Contract extends BaseContract {
}

public getABIDecodedReturnData<T>(methodName: string, callData: string): T {
if (this._encoderOverrides.decodeOutput) {
return this._encoderOverrides.decodeOutput(methodName, callData);
}
const functionSignature = this.getFunctionSignature(methodName);
const self = (this as any) as {{contractName}}Contract;
const abiEncoder = self._lookupAbiEncoder(functionSignature);
Expand Down Expand Up @@ -381,9 +385,9 @@ export class {{contractName}}Contract extends BaseContract {
txDefaults?: Partial<TxData>,
logDecodeDependencies?: { [contractName: string]: ContractAbi },
deployedBytecode: string | undefined = {{contractName}}Contract.deployedBytecode,
encodingRules?: EncodingRules,
encoderOverrides?: Partial<EncoderOverrides>,
) {
super('{{contractName}}', {{contractName}}Contract.ABI(), address, supportedProvider, txDefaults, logDecodeDependencies, deployedBytecode, encodingRules);
super('{{contractName}}', {{contractName}}Contract.ABI(), address, supportedProvider, txDefaults, logDecodeDependencies, deployedBytecode, encoderOverrides);
classUtils.bindAll(this, ['_abiEncoderByFunctionSignature', 'address', '_web3Wrapper']);
{{#if events~}}
this._subscriptionManager = new SubscriptionManager<{{contractName}}EventArgs, {{contractName}}Events>(
Expand Down
8 changes: 6 additions & 2 deletions abi-gen/test-cli/output/typescript/abi_gen_dummy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// tslint:disable:no-unused-variable
import {
AwaitTransactionSuccessOpts,
EncoderOverrides,
ContractFunctionObj,
ContractTxFunctionObj,
SendTransactionOpts,
Expand Down Expand Up @@ -955,6 +956,9 @@ export class AbiGenDummyContract extends BaseContract {
}

public getABIDecodedReturnData<T>(methodName: string, callData: string): T {
if (this._encoderOverrides.decodeOutput) {
return this._encoderOverrides.decodeOutput(methodName, callData);
}
const functionSignature = this.getFunctionSignature(methodName);
const self = (this as any) as AbiGenDummyContract;
const abiEncoder = self._lookupAbiEncoder(functionSignature);
Expand Down Expand Up @@ -2140,7 +2144,7 @@ export class AbiGenDummyContract extends BaseContract {
txDefaults?: Partial<TxData>,
logDecodeDependencies?: { [contractName: string]: ContractAbi },
deployedBytecode: string | undefined = AbiGenDummyContract.deployedBytecode,
encodingRules?: EncodingRules,
encoderOverrides?: Partial<EncoderOverrides>,
) {
super(
'AbiGenDummy',
Expand All @@ -2150,7 +2154,7 @@ export class AbiGenDummyContract extends BaseContract {
txDefaults,
logDecodeDependencies,
deployedBytecode,
encodingRules,
encoderOverrides,
);
classUtils.bindAll(this, ['_abiEncoderByFunctionSignature', 'address', '_web3Wrapper']);
this._subscriptionManager = new SubscriptionManager<AbiGenDummyEventArgs, AbiGenDummyEvents>(
Expand Down
8 changes: 6 additions & 2 deletions abi-gen/test-cli/output/typescript/lib_dummy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// tslint:disable:no-unused-variable
import {
AwaitTransactionSuccessOpts,
EncoderOverrides,
ContractFunctionObj,
ContractTxFunctionObj,
SendTransactionOpts,
Expand Down Expand Up @@ -202,6 +203,9 @@ export class LibDummyContract extends BaseContract {
}

public getABIDecodedReturnData<T>(methodName: string, callData: string): T {
if (this._encoderOverrides.decodeOutput) {
return this._encoderOverrides.decodeOutput(methodName, callData);
}
const functionSignature = this.getFunctionSignature(methodName);
const self = (this as any) as LibDummyContract;
const abiEncoder = self._lookupAbiEncoder(functionSignature);
Expand All @@ -222,7 +226,7 @@ export class LibDummyContract extends BaseContract {
txDefaults?: Partial<TxData>,
logDecodeDependencies?: { [contractName: string]: ContractAbi },
deployedBytecode: string | undefined = LibDummyContract.deployedBytecode,
encodingRules?: EncodingRules,
encoderOverrides?: Partial<EncoderOverrides>,
) {
super(
'LibDummy',
Expand All @@ -232,7 +236,7 @@ export class LibDummyContract extends BaseContract {
txDefaults,
logDecodeDependencies,
deployedBytecode,
encodingRules,
encoderOverrides,
);
classUtils.bindAll(this, ['_abiEncoderByFunctionSignature', 'address', '_web3Wrapper']);
LibDummyContract.ABI().forEach((item, index) => {
Expand Down
8 changes: 6 additions & 2 deletions abi-gen/test-cli/output/typescript/test_lib_dummy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// tslint:disable:no-unused-variable
import {
AwaitTransactionSuccessOpts,
EncoderOverrides,
ContractFunctionObj,
ContractTxFunctionObj,
SendTransactionOpts,
Expand Down Expand Up @@ -242,6 +243,9 @@ export class TestLibDummyContract extends BaseContract {
}

public getABIDecodedReturnData<T>(methodName: string, callData: string): T {
if (this._encoderOverrides.decodeOutput) {
return this._encoderOverrides.decodeOutput(methodName, callData);
}
const functionSignature = this.getFunctionSignature(methodName);
const self = (this as any) as TestLibDummyContract;
const abiEncoder = self._lookupAbiEncoder(functionSignature);
Expand Down Expand Up @@ -315,7 +319,7 @@ export class TestLibDummyContract extends BaseContract {
txDefaults?: Partial<TxData>,
logDecodeDependencies?: { [contractName: string]: ContractAbi },
deployedBytecode: string | undefined = TestLibDummyContract.deployedBytecode,
encodingRules?: EncodingRules,
encoderOverrides?: Partial<EncoderOverrides>,
) {
super(
'TestLibDummy',
Expand All @@ -325,7 +329,7 @@ export class TestLibDummyContract extends BaseContract {
txDefaults,
logDecodeDependencies,
deployedBytecode,
encodingRules,
encoderOverrides,
);
classUtils.bindAll(this, ['_abiEncoderByFunctionSignature', 'address', '_web3Wrapper']);
TestLibDummyContract.ABI().forEach((item, index) => {
Expand Down
9 changes: 9 additions & 0 deletions base-contract/CHANGELOG.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
[
{
"version": "6.4.0",
"changes": [
{
"note": "Support overriding encodeInput and decodeOutput",
"pr": 36
}
]
},
{
"timestamp": 1619481141,
"version": "6.3.2",
Expand Down
1 change: 1 addition & 0 deletions base-contract/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"build": "tsc -b",
"build:ci": "yarn build",
"clean": "shx rm -rf lib",
"watch": "tsc -b -w",
"test": "yarn run_mocha",
"rebuild_and_test": "run-s clean build test",
"test:circleci": "yarn test:coverage",
Expand Down
18 changes: 13 additions & 5 deletions base-contract/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { assert } from '@0x/assert';
import { schemas } from '@0x/json-schemas';
import {
AbiEncoder,
AbiEncoderConstants,
abiUtils,
BigNumber,
decodeBytesAsRevertError,
Expand Down Expand Up @@ -88,17 +87,23 @@ export class PromiseWithTransactionHash<T> implements Promise<T> {
}
}

export interface EncoderOverrides {
encodeInput: (functionName: string, values: any) => string;
decodeOutput: (functionName: string, data: string) => any;
}

export class BaseContract {
protected _abiEncoderByFunctionSignature: AbiEncoderByFunctionSignature;
protected _web3Wrapper: Web3Wrapper;
protected _encodingRules: AbiEncoder.EncodingRules;
protected _encoderOverrides: Partial<EncoderOverrides>;
public abi: ContractAbi;
public address: string;
public contractName: string;
public constructorArgs: any[] = [];
public _deployedBytecodeIfExists?: Buffer;
private _evmIfExists?: VM;
private _evmAccountIfExists?: Buffer;

protected static _formatABIDataItemList(
abis: DataItem[],
values: any[],
Expand Down Expand Up @@ -331,12 +336,15 @@ export class BaseContract {
return methodAbi;
}
protected _strictEncodeArguments(functionSignature: string, functionArguments: any): string {
if (this._encoderOverrides.encodeInput) {
return this._encoderOverrides.encodeInput(functionSignature.split('(')[0], functionArguments);
}
const abiEncoder = this._lookupAbiEncoder(functionSignature);
const inputAbi = abiEncoder.getDataItem().components;
if (inputAbi === undefined) {
throw new Error(`Undefined Method Input ABI`);
}
const abiEncodedArguments = abiEncoder.encode(functionArguments, this._encodingRules);
const abiEncodedArguments = abiEncoder.encode(functionArguments);
return abiEncodedArguments;
}
/// @dev Constructs a contract wrapper.
Expand All @@ -356,7 +364,7 @@ export class BaseContract {
callAndTxnDefaults?: Partial<CallData>,
logDecodeDependencies?: { [contractName: string]: ContractAbi },
deployedBytecode?: string,
encodingRules?: AbiEncoder.EncodingRules,
encoderOverrides?: Partial<EncoderOverrides>,
) {
assert.isString('contractName', contractName);
assert.isETHAddressHex('address', address);
Expand All @@ -379,7 +387,7 @@ export class BaseContract {
}
this.contractName = contractName;
this._web3Wrapper = new Web3Wrapper(provider, callAndTxnDefaults);
this._encodingRules = encodingRules || AbiEncoderConstants.DEFAULT_ENCODING_RULES;
this._encoderOverrides = encoderOverrides || {};
this.abi = abi;
this.address = address;
const methodAbis = this.abi.filter(
Expand Down
1 change: 1 addition & 0 deletions utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"build": "tsc -b",
"build:ci": "yarn build",
"clean": "shx rm -rf lib",
"watch": "tsc -b -w",
"lint": "tslint --format stylish --project .",
"fix": "tslint --fix --format stylish --project .",
"test": "yarn run_mocha",
Expand Down
Loading