aboutsummaryrefslogtreecommitdiffstats
path: root/src/utils/abi_decoder.ts
diff options
context:
space:
mode:
authorLeonid Logvinov <logvinov.leon@gmail.com>2017-10-03 17:31:46 +0800
committerLeonid Logvinov <logvinov.leon@gmail.com>2017-10-04 16:14:19 +0800
commit6bbdc98ba29633828f0533c8bb40a200cf142436 (patch)
tree3345e7ba89bcca9f8cd470904d555bc5fcd000a1 /src/utils/abi_decoder.ts
parenta9681072ee4b45bda23b754fa46175b68c09b8b9 (diff)
downloaddexon-sol-tools-6bbdc98ba29633828f0533c8bb40a200cf142436.tar
dexon-sol-tools-6bbdc98ba29633828f0533c8bb40a200cf142436.tar.gz
dexon-sol-tools-6bbdc98ba29633828f0533c8bb40a200cf142436.tar.bz2
dexon-sol-tools-6bbdc98ba29633828f0533c8bb40a200cf142436.tar.lz
dexon-sol-tools-6bbdc98ba29633828f0533c8bb40a200cf142436.tar.xz
dexon-sol-tools-6bbdc98ba29633828f0533c8bb40a200cf142436.tar.zst
dexon-sol-tools-6bbdc98ba29633828f0533c8bb40a200cf142436.zip
Move log decoding to AbiDecoder
Diffstat (limited to 'src/utils/abi_decoder.ts')
-rw-r--r--src/utils/abi_decoder.ts67
1 files changed, 35 insertions, 32 deletions
diff --git a/src/utils/abi_decoder.ts b/src/utils/abi_decoder.ts
index 61c8eecd4..88053aade 100644
--- a/src/utils/abi_decoder.ts
+++ b/src/utils/abi_decoder.ts
@@ -1,7 +1,7 @@
import * as Web3 from 'web3';
import * as _ from 'lodash';
import * as BigNumber from 'bignumber.js';
-import {AbiType, DecodedLogArgs, DecodedArgs} from '../types';
+import {AbiType, DecodedLogArgs, DecodedArgs, LogWithDecodedArgs} from '../types';
import * as SolidityCoder from 'web3/lib/solidity/coder';
export class AbiDecoder {
@@ -10,40 +10,43 @@ export class AbiDecoder {
constructor(abiArrays: Web3.AbiDefinition[][]) {
_.map(abiArrays, this.addABI.bind(this));
}
- public decodeLog(logItem: Web3.LogEntry): DecodedArgs|undefined {
- const methodId = logItem.topics[0];
+ public tryToDecodeLogOrNoOp(log: Web3.LogEntry): LogWithDecodedArgs|Web3.LogEntry {
+ const methodId = log.topics[0];
const event = this.methodIds[methodId];
- if (!_.isUndefined(event)) {
- const logData = logItem.data;
- const decodedParams: DecodedLogArgs = {};
- let dataIndex = 0;
- let topicsIndex = 1;
+ 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(2));
- _.map(event.inputs, (param: Web3.EventParameter) => {
- let value;
- if (param.indexed) {
- value = logItem.topics[topicsIndex];
- topicsIndex++;
- } else {
- value = decodedData[dataIndex];
- dataIndex++;
- }
- if (param.type === 'address') {
- value = this.padZeros(new BigNumber(value).toString(16));
- } else if (param.type === 'uint256' || param.type === 'uint8' || param.type === 'int' ) {
- value = new BigNumber(value);
- }
- decodedParams[param.name] = value;
- });
+ const nonIndexedInputs = _.filter(event.inputs, input => !input.indexed);
+ const dataTypes = _.map(nonIndexedInputs, input => input.type);
+ const decodedData = SolidityCoder.decodeParams(dataTypes, logData.slice(2));
- return {
- event: event.name,
- args: decodedParams,
- };
- }
+ _.map(event.inputs, (param: Web3.EventParameter) => {
+ let value;
+ if (param.indexed) {
+ value = log.topics[topicsIndex];
+ topicsIndex++;
+ } else {
+ value = decodedData[dataIndex];
+ dataIndex++;
+ }
+ if (param.type === 'address') {
+ value = this.padZeros(new BigNumber(value).toString(16));
+ } else if (param.type === 'uint256' || param.type === 'uint8' || param.type === 'int' ) {
+ 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) => {