Added comments for transaction decoder
This commit is contained in:
parent
f7b58e7f64
commit
171618d32b
@ -37,6 +37,7 @@ export { OrderValidatorWrapper } from './contract_wrappers/order_validator_wrapp
|
|||||||
export { DutchAuctionWrapper } from './contract_wrappers/dutch_auction_wrapper';
|
export { DutchAuctionWrapper } from './contract_wrappers/dutch_auction_wrapper';
|
||||||
|
|
||||||
export { TransactionEncoder } from './utils/transaction_encoder';
|
export { TransactionEncoder } from './utils/transaction_encoder';
|
||||||
|
export { ZeroExTransactionDecoder } from './utils/zeroex_transaction_decoder';
|
||||||
|
|
||||||
export {
|
export {
|
||||||
ContractWrappersError,
|
ContractWrappersError,
|
||||||
|
@ -8,29 +8,48 @@ import { DeployedContractInfo, DeployedContractInfoByName, TransactionData, Tran
|
|||||||
|
|
||||||
export class ZeroExTransactionDecoder extends TransactionDecoder {
|
export class ZeroExTransactionDecoder extends TransactionDecoder {
|
||||||
private static _instance: ZeroExTransactionDecoder;
|
private static _instance: ZeroExTransactionDecoder;
|
||||||
|
/**
|
||||||
|
* Adds a set of ABI definitions, after which transaction data targeting these ABI's can be decoded.
|
||||||
|
* Additional properties can be included to disambiguate similar ABI's. For example, if two functions
|
||||||
|
* have the same signature but different parameter names, then their ABI definitions can be disambiguated
|
||||||
|
* by specifying a contract name.
|
||||||
|
* @param abiDefinitions ABI definitions for a given contract.
|
||||||
|
* @param contractName Name of contract that encapsulates the ABI definitions (optional).
|
||||||
|
* @param deploymentInfos A collection of network/address pairs where this contract is deployed (optional).
|
||||||
|
*/
|
||||||
public static addABI(
|
public static addABI(
|
||||||
abiArray: AbiDefinition[],
|
abiDefinitions: AbiDefinition[],
|
||||||
contractName: string,
|
contractName: string,
|
||||||
deploymentInfos?: DeployedContractInfo[],
|
deploymentInfos?: DeployedContractInfo[],
|
||||||
): void {
|
): void {
|
||||||
const instance = ZeroExTransactionDecoder._getInstance();
|
const instance = ZeroExTransactionDecoder._getInstance();
|
||||||
instance.addABI(abiArray, contractName, deploymentInfos);
|
instance.addABI(abiDefinitions, contractName, deploymentInfos);
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Decodes transaction data for a known ABI.
|
||||||
|
* @param txData hex-encoded transaction data.
|
||||||
|
* @param txProperties Properties about the transaction used to disambiguate similar ABI's (optional).
|
||||||
|
* @return Decoded transaction data. Includes: function name and signature, along with the decoded arguments.
|
||||||
|
*/
|
||||||
public static decode(calldata: string, txProperties?: TransactionProperties): TransactionData {
|
public static decode(calldata: string, txProperties?: TransactionProperties): TransactionData {
|
||||||
const instance = ZeroExTransactionDecoder._getInstance();
|
const instance = ZeroExTransactionDecoder._getInstance();
|
||||||
const decodedCalldata = instance.decode(calldata, txProperties);
|
const decodedCalldata = instance.decode(calldata, txProperties);
|
||||||
return decodedCalldata;
|
return decodedCalldata;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Gets instance for singleton.
|
||||||
|
* @return singleton instance.
|
||||||
|
*/
|
||||||
private static _getInstance(): ZeroExTransactionDecoder {
|
private static _getInstance(): ZeroExTransactionDecoder {
|
||||||
if (!ZeroExTransactionDecoder._instance) {
|
if (!ZeroExTransactionDecoder._instance) {
|
||||||
ZeroExTransactionDecoder._instance = new ZeroExTransactionDecoder();
|
ZeroExTransactionDecoder._instance = new ZeroExTransactionDecoder();
|
||||||
}
|
}
|
||||||
return ZeroExTransactionDecoder._instance;
|
return ZeroExTransactionDecoder._instance;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Adds all known contract ABI's defined by the @0x/Artifacts package, along with known 0x
|
||||||
|
* contract addresses.
|
||||||
|
*/
|
||||||
private constructor() {
|
private constructor() {
|
||||||
super();
|
super();
|
||||||
// Load addresses by contract name
|
// Load addresses by contract name
|
||||||
|
@ -6,29 +6,43 @@ import { DeployedContractInfo, FunctionInfoBySelector, TransactionData, Transact
|
|||||||
|
|
||||||
export class TransactionDecoder {
|
export class TransactionDecoder {
|
||||||
private readonly _functionInfoBySelector: FunctionInfoBySelector = {};
|
private readonly _functionInfoBySelector: FunctionInfoBySelector = {};
|
||||||
|
/**
|
||||||
private static _getFunctionSelector(calldata: string): string {
|
* Retrieves the function selector from tranasction data.
|
||||||
|
* @param txData hex-encoded transaction data.
|
||||||
|
* @return hex-encoded function selector.
|
||||||
|
*/
|
||||||
|
private static _getFunctionSelector(txData: string): string {
|
||||||
const functionSelectorLength = 10;
|
const functionSelectorLength = 10;
|
||||||
if (!calldata.startsWith('0x') || calldata.length < functionSelectorLength) {
|
if (!txData.startsWith('0x') || txData.length < functionSelectorLength) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Malformed calldata. Must include hex prefix '0x' and 4-byte function selector. Got '${calldata}'`,
|
`Malformed transaction data. Must include a hex prefix '0x' and 4-byte function selector. Got '${txData}'`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const functionSelector = calldata.substr(0, functionSelectorLength);
|
const functionSelector = calldata.substr(0, functionSelectorLength);
|
||||||
return functionSelector;
|
return functionSelector;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
public addABI(abiArray: AbiDefinition[], contractName: string, deploymentInfos?: DeployedContractInfo[]): void {
|
* Adds a set of ABI definitions, after which transaction data targeting these ABI's can be decoded.
|
||||||
const functionAbis: MethodAbi[] = _.filter(abiArray, abiEntry => {
|
* Additional properties can be included to disambiguate similar ABI's. For example, if two functions
|
||||||
|
* have the same signature but different parameter names, then their ABI definitions can be disambiguated
|
||||||
|
* by specifying a contract name.
|
||||||
|
* @param abiDefinitions ABI definitions for a given contract.
|
||||||
|
* @param contractName Name of contract that encapsulates the ABI definitions (optional).
|
||||||
|
* @param deploymentInfos A collection of network/address pairs where this contract is deployed (optional).
|
||||||
|
*/
|
||||||
|
public addABI(abiDefinitions: AbiDefinition[], contractName?: string, deploymentInfos?: DeployedContractInfo[]): void {
|
||||||
|
// Disregard definitions that are not functions
|
||||||
|
const functionAbis: MethodAbi[] = _.filter(abiDefinitions, abiEntry => {
|
||||||
return abiEntry.type === 'function';
|
return abiEntry.type === 'function';
|
||||||
});
|
});
|
||||||
|
// Record function ABI's
|
||||||
_.each(functionAbis, functionAbi => {
|
_.each(functionAbis, functionAbi => {
|
||||||
const abiEncoder = new AbiEncoder.Method(functionAbi);
|
const abiEncoder = new AbiEncoder.Method(functionAbi);
|
||||||
const functionSelector = abiEncoder.getSelector();
|
const functionSelector = abiEncoder.getSelector();
|
||||||
if (!(functionSelector in this._functionInfoBySelector)) {
|
if (!(functionSelector in this._functionInfoBySelector)) {
|
||||||
this._functionInfoBySelector[functionSelector] = [];
|
this._functionInfoBySelector[functionSelector] = [];
|
||||||
}
|
}
|
||||||
// Recored deployed versions of this decoder
|
// Recored a copy of this ABI for each deployment
|
||||||
const functionSignature = abiEncoder.getSignature();
|
const functionSignature = abiEncoder.getSignature();
|
||||||
_.each(deploymentInfos, deploymentInfo => {
|
_.each(deploymentInfos, deploymentInfo => {
|
||||||
this._functionInfoBySelector[functionSelector].push({
|
this._functionInfoBySelector[functionSelector].push({
|
||||||
@ -39,7 +53,7 @@ export class TransactionDecoder {
|
|||||||
networkId: deploymentInfo.networkId,
|
networkId: deploymentInfo.networkId,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
// If there isn't a deployed version of this contract, record it without address/network id
|
// There is no deployment info for this contract; record it without an address/network id
|
||||||
if (_.isEmpty(deploymentInfos)) {
|
if (_.isEmpty(deploymentInfos)) {
|
||||||
this._functionInfoBySelector[functionSelector].push({
|
this._functionInfoBySelector[functionSelector].push({
|
||||||
functionSignature,
|
functionSignature,
|
||||||
@ -49,9 +63,15 @@ export class TransactionDecoder {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
public decode(calldata: string, txProperties_?: TransactionProperties): TransactionData {
|
* Decodes transaction data for a known ABI.
|
||||||
const functionSelector = TransactionDecoder._getFunctionSelector(calldata);
|
* @param txData hex-encoded transaction data.
|
||||||
|
* @param txProperties Properties about the transaction used to disambiguate similar ABI's (optional).
|
||||||
|
* @return Decoded transaction data. Includes: function name and signature, along with the decoded arguments.
|
||||||
|
*/
|
||||||
|
public decode(txData: string, txProperties_?: TransactionProperties): TransactionData {
|
||||||
|
// Lookup
|
||||||
|
const functionSelector = TransactionDecoder._getFunctionSelector(txData);
|
||||||
const txProperties = _.isUndefined(txProperties_) ? {} : txProperties_;
|
const txProperties = _.isUndefined(txProperties_) ? {} : txProperties_;
|
||||||
const candidateFunctionInfos = this._functionInfoBySelector[functionSelector];
|
const candidateFunctionInfos = this._functionInfoBySelector[functionSelector];
|
||||||
if (_.isUndefined(candidateFunctionInfos)) {
|
if (_.isUndefined(candidateFunctionInfos)) {
|
||||||
@ -75,7 +95,7 @@ export class TransactionDecoder {
|
|||||||
}
|
}
|
||||||
const functionName = functionInfo.abiEncoder.getDataItem().name;
|
const functionName = functionInfo.abiEncoder.getDataItem().name;
|
||||||
const functionSignature = functionInfo.abiEncoder.getSignatureType();
|
const functionSignature = functionInfo.abiEncoder.getSignatureType();
|
||||||
const functionArguments = functionInfo.abiEncoder.decode(calldata);
|
const functionArguments = functionInfo.abiEncoder.decode(txData);
|
||||||
const decodedCalldata = {
|
const decodedCalldata = {
|
||||||
functionName,
|
functionName,
|
||||||
functionSignature,
|
functionSignature,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user