diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-10-04 18:30:00 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2017-10-04 18:30:00 +0800 |
commit | 504beeb2f321c6c35cfcc3edfff1669bdebc939b (patch) | |
tree | 760abc8cf5591bda3d27e98a835d5f3ec15bf43a /src | |
parent | 9af47eb063406753456f16bf3efb916666c76d7f (diff) | |
download | dexon-sol-tools-504beeb2f321c6c35cfcc3edfff1669bdebc939b.tar dexon-sol-tools-504beeb2f321c6c35cfcc3edfff1669bdebc939b.tar.gz dexon-sol-tools-504beeb2f321c6c35cfcc3edfff1669bdebc939b.tar.bz2 dexon-sol-tools-504beeb2f321c6c35cfcc3edfff1669bdebc939b.tar.lz dexon-sol-tools-504beeb2f321c6c35cfcc3edfff1669bdebc939b.tar.xz dexon-sol-tools-504beeb2f321c6c35cfcc3edfff1669bdebc939b.tar.zst dexon-sol-tools-504beeb2f321c6c35cfcc3edfff1669bdebc939b.zip |
Add filtering by topic
Diffstat (limited to 'src')
-rw-r--r-- | src/contract_wrappers/contract_wrapper.ts | 26 |
1 files changed, 23 insertions, 3 deletions
diff --git a/src/contract_wrappers/contract_wrapper.ts b/src/contract_wrappers/contract_wrapper.ts index 6f074a976..7732f2f50 100644 --- a/src/contract_wrappers/contract_wrapper.ts +++ b/src/contract_wrappers/contract_wrapper.ts @@ -1,5 +1,6 @@ import * as _ from 'lodash'; import * as Web3 from 'web3'; +import * as ethUtil from 'ethereumjs-util'; import {Web3Wrapper} from '../web3_wrapper'; import {AbiDecoder} from '../utils/abi_decoder'; import { @@ -13,6 +14,8 @@ import { } from '../types'; import {utils} from '../utils/utils'; +const TOPIC_LENGTH = 32; + export class ContractWrapper { protected _web3Wrapper: Web3Wrapper; private _abiDecoder?: AbiDecoder; @@ -23,11 +26,11 @@ export class ContractWrapper { protected async _getLogsAsync(address: string, eventName: ContractEvents, subscriptionOpts: SubscriptionOpts, indexFilterValues: IndexedFilterValues, abi: Web3.ContractAbi): Promise<LogWithDecodedArgs[]> { - // TODO include indexFilterValues in topics - const eventAbi = _.find(abi, {name: eventName}) as Web3.EventAbi; + const eventAbi = _.filter(abi, {name: eventName})[0] as Web3.EventAbi; const eventSignature = this._getEventSignatureFromAbiByName(eventAbi, eventName); const topicForEventSignature = this._web3Wrapper.keccak256(eventSignature); - const topics = [topicForEventSignature]; + const topicsForIndexedArgs = this._getTopicsForIndexedArgs(eventAbi, indexFilterValues); + const topics = [topicForEventSignature, ...topicsForIndexedArgs]; const filter = { fromBlock: subscriptionOpts.fromBlock, toBlock: subscriptionOpts.toBlock, @@ -57,4 +60,21 @@ export class ContractWrapper { const signature = `${eventAbi.name}(${types.join(',')})`; return signature; } + private _getTopicsForIndexedArgs(abi: Web3.EventAbi, indexFilterValues: IndexedFilterValues): Array<string|null> { + const topics: Array<string|null> = []; + for (const eventInput of abi.inputs) { + if (eventInput.indexed) { + if (_.isUndefined(indexFilterValues[eventInput.name])) { + topics.push(null); + } else { + const value = indexFilterValues[eventInput.name] as string; + const buffer = ethUtil.toBuffer(value); + const paddedBuffer = ethUtil.setLengthLeft(buffer, TOPIC_LENGTH); + const topic = ethUtil.bufferToHex(paddedBuffer); + topics.push(topic); + } + } + } + return topics; + } } |