aboutsummaryrefslogtreecommitdiffstats
path: root/packages/utils/src/abi_decoder.ts
diff options
context:
space:
mode:
authorLeonid <logvinov.leon@gmail.com>2018-02-05 19:33:40 +0800
committerGitHub <noreply@github.com>2018-02-05 19:33:40 +0800
commit7b4f2b47de393b7ed6d5c264c8e80864d196180c (patch)
tree8e86af8000e0aedd7241a00c2189d8f7ca0fc2ad /packages/utils/src/abi_decoder.ts
parent400a97e7a8f76d894d47368425bbe1e33fa5b255 (diff)
parentc7ad6ebad6ab65a4b1e4a2084e744c6ca2bc09b8 (diff)
downloaddexon-0x-contracts-7b4f2b47de393b7ed6d5c264c8e80864d196180c.tar.gz
dexon-0x-contracts-7b4f2b47de393b7ed6d5c264c8e80864d196180c.tar.zst
dexon-0x-contracts-7b4f2b47de393b7ed6d5c264c8e80864d196180c.zip
Merge branch 'development' into fix/ether_token_address
Diffstat (limited to 'packages/utils/src/abi_decoder.ts')
-rw-r--r--packages/utils/src/abi_decoder.ts70
1 files changed, 70 insertions, 0 deletions
diff --git a/packages/utils/src/abi_decoder.ts b/packages/utils/src/abi_decoder.ts
new file mode 100644
index 000000000..f96ee2edb
--- /dev/null
+++ b/packages/utils/src/abi_decoder.ts
@@ -0,0 +1,70 @@
+import { AbiType, DecodedLogArgs, LogWithDecodedArgs, RawLog, SolidityTypes } from '@0xproject/types';
+import * as _ from 'lodash';
+import * as Web3 from 'web3';
+import * as SolidityCoder from 'web3/lib/solidity/coder';
+
+import { BigNumber } from './configured_bignumber';
+
+export class AbiDecoder {
+ private _savedABIs: Web3.AbiDefinition[] = [];
+ private _methodIds: { [signatureHash: string]: Web3.EventAbi } = {};
+ private static _padZeros(address: string) {
+ let formatted = address;
+ if (_.startsWith(formatted, '0x')) {
+ formatted = formatted.slice(2);
+ }
+
+ formatted = _.padStart(formatted, 40, '0');
+ return `0x${formatted}`;
+ }
+ constructor(abiArrays: Web3.AbiDefinition[][]) {
+ _.map(abiArrays, this._addABI.bind(this));
+ }
+ // This method can only decode logs from the 0x & ERC20 smart contracts
+ public tryToDecodeLogOrNoop<ArgsType>(log: Web3.LogEntry): LogWithDecodedArgs<ArgsType> | RawLog {
+ const methodId = log.topics[0];
+ const event = this._methodIds[methodId];
+ if (_.isUndefined(event)) {
+ return log;
+ }
+ const logData = log.data;
+ const decodedParams: DecodedLogArgs = {};
+ let dataIndex = 0;
+ let topicsIndex = 1;
+
+ const nonIndexedInputs = _.filter(event.inputs, input => !input.indexed);
+ const dataTypes = _.map(nonIndexedInputs, input => input.type);
+ const decodedData = SolidityCoder.decodeParams(dataTypes, logData.slice('0x'.length));
+
+ _.map(event.inputs, (param: Web3.EventParameter) => {
+ // Indexed parameters are stored in topics. Non-indexed ones in decodedData
+ let value: BigNumber | string = param.indexed ? log.topics[topicsIndex++] : decodedData[dataIndex++];
+ if (param.type === SolidityTypes.Address) {
+ value = AbiDecoder._padZeros(new BigNumber(value).toString(16));
+ } else if (
+ param.type === SolidityTypes.Uint256 ||
+ param.type === SolidityTypes.Uint8 ||
+ param.type === SolidityTypes.Uint
+ ) {
+ value = new BigNumber(value);
+ }
+ decodedParams[param.name] = value;
+ });
+
+ return {
+ ...log,
+ event: event.name,
+ args: decodedParams,
+ };
+ }
+ private _addABI(abiArray: Web3.AbiDefinition[]): void {
+ _.map(abiArray, (abi: Web3.AbiDefinition) => {
+ if (abi.type === AbiType.Event) {
+ const signature = `${abi.name}(${_.map(abi.inputs, input => input.type).join(',')})`;
+ const signatureHash = new Web3().sha3(signature);
+ this._methodIds[signatureHash] = abi;
+ }
+ });
+ this._savedABIs = this._savedABIs.concat(abiArray);
+ }
+}