blob: 780332514d184be71fbae67f43b9f9eeccc222f5 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
// Decodes any 0x transaction
import * as ContractArtifacts from '@0x/contract-artifacts';
import { SimpleContractArtifact } from '@0x/types';
import { ContractAbi, MethodAbi } from 'ethereum-types';
import * as _ from 'lodash';
import { AbiEncoder } from '.';
export interface DecodedCalldata {
functionName: string;
functionSignature: string;
functionArguments: any;
}
interface AbiEncoderBySelector {
[index: string]: AbiEncoder.Method;
}
export class CalldataDecoder {
private readonly _abiEncoderBySelector: AbiEncoderBySelector = {};
private static _instance: CalldataDecoder;
public static getInstance(): CalldataDecoder {
if (!CalldataDecoder._instance) {
CalldataDecoder._instance = new CalldataDecoder();
}
return CalldataDecoder._instance;
}
public constructor() {
_.each(ContractArtifacts, (contractArtifactAsJson: any) => {
const conractArtifact = contractArtifactAsJson as SimpleContractArtifact;
const contractAbi: ContractAbi = conractArtifact.compilerOutput.abi;
const functionAbis = _.filter(contractAbi, (abiEntry) => {return abiEntry.type === 'function'}) as MethodAbi[];
_.each(functionAbis, (functionAbi) => {
const abiEncoder = new AbiEncoder.Method(functionAbi);
const functionSelector = abiEncoder.getSelector();
if (_.has(this._abiEncoderBySelector, functionSelector)) {
return;
}
this._abiEncoderBySelector[functionSelector] = abiEncoder;
});
});
}
public static decode(calldata: string, rules?: AbiEncoder.DecodingRules): DecodedCalldata {
if (!calldata.startsWith('0x') || calldata.length < 10) {
throw new Error(`Malformed calldata. Must include hex prefix '0x' and 4-byte function selector. Got '${calldata}'`);
}
const functionSelector = calldata.substr(0, 10);
const instance = CalldataDecoder.getInstance();
const abiEncoder = instance._abiEncoderBySelector[functionSelector];
if (_.isUndefined(abiEncoder)) {
throw new Error(`Could not find matching abi encoder for selector '${functionSelector}'`);
}
const functionName = abiEncoder.getDataItem().name;
const functionSignature = abiEncoder.getSignatureType();
const functionArguments = abiEncoder.decode(calldata, rules);
const decodedCalldata = {
functionName,
functionSignature,
functionArguments
}
return decodedCalldata;
}
}
|