Skip to content

Commit

Permalink
Added less-common, but useful, coding functions to Interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Sep 28, 2019
1 parent 3d25882 commit 778eb3b
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/abi/src.ts/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ export class Interface {
}


_decodeParams(params: Array<ParamType>, data: BytesLike): Array<any> {
return this._abiCoder.decode(params, data)
}

_encodeParams(params: Array<ParamType>, values: Array<any>): string {
return this._abiCoder.encode(params, values)
}
Expand All @@ -200,6 +204,20 @@ export class Interface {
return this._encodeParams(this.deploy.inputs, values || [ ]);
}

decodeFunctionData(functionFragment: FunctionFragment | string, data: BytesLike): Array<any> {
if (typeof(functionFragment) === "string") {
functionFragment = this.getFunction(functionFragment);
}

const bytes = arrayify(data);

if (hexlify(bytes.slice(0, 4)) !== this.getSighash(functionFragment)) {
logger.throwArgumentError(`data signature does not match function ${ functionFragment.name }.`, "data", hexlify(bytes));
}

return this._decodeParams(functionFragment.inputs, bytes.slice(4));
}

encodeFunctionData(functionFragment: FunctionFragment | string, values?: Array<any>): string {
if (typeof(functionFragment) === "string") {
functionFragment = this.getFunction(functionFragment);
Expand Down Expand Up @@ -243,6 +261,14 @@ export class Interface {
});
}

encodeFunctionResult(functionFragment: FunctionFragment | string, values?: Array<any>): string {
if (typeof(functionFragment) === "string") {
functionFragment = this.getFunction(functionFragment);
}

return hexlify(this._abiCoder.encode(functionFragment.outputs, values || [ ]));
}

encodeFilterTopics(eventFragment: EventFragment, values: Array<any>): Array<string | Array<string>> {
if (typeof(eventFragment) === "string") {
eventFragment = this.getEvent(eventFragment);
Expand Down

0 comments on commit 778eb3b

Please sign in to comment.