diff options
author | Alex Browne <stephenalexbrowne@gmail.com> | 2018-09-20 03:09:36 +0800 |
---|---|---|
committer | Alex Browne <stephenalexbrowne@gmail.com> | 2018-12-05 06:23:32 +0800 |
commit | d71fa6535987a0f13900f8e31dbb51772c12fc4f (patch) | |
tree | e2354e5c5b75dc4a48b737d646f5a89821fce48f /packages/pipeline | |
parent | 75d3f24835fc68a758cfb44c6bc05095c3e87ad3 (diff) | |
download | dexon-sol-tools-d71fa6535987a0f13900f8e31dbb51772c12fc4f.tar dexon-sol-tools-d71fa6535987a0f13900f8e31dbb51772c12fc4f.tar.gz dexon-sol-tools-d71fa6535987a0f13900f8e31dbb51772c12fc4f.tar.bz2 dexon-sol-tools-d71fa6535987a0f13900f8e31dbb51772c12fc4f.tar.lz dexon-sol-tools-d71fa6535987a0f13900f8e31dbb51772c12fc4f.tar.xz dexon-sol-tools-d71fa6535987a0f13900f8e31dbb51772c12fc4f.tar.zst dexon-sol-tools-d71fa6535987a0f13900f8e31dbb51772c12fc4f.zip |
Make contractAbi a parameter of getContractEventsAsync
Diffstat (limited to 'packages/pipeline')
4 files changed, 29 insertions, 11 deletions
diff --git a/packages/pipeline/src/data-sources/etherscan/events.ts b/packages/pipeline/src/data-sources/etherscan/events.ts index 3083af267..89b3ffac1 100644 --- a/packages/pipeline/src/data-sources/etherscan/events.ts +++ b/packages/pipeline/src/data-sources/etherscan/events.ts @@ -1,9 +1,7 @@ import { AbiDecoder } from '@0xproject/utils'; -import { DecodedLogArgs, LogEntry, LogWithDecodedArgs } from 'ethereum-types'; +import { AbiDefinition, DecodedLogArgs, LogEntry, LogWithDecodedArgs } from 'ethereum-types'; import * as R from 'ramda'; -import { artifacts } from '../../artifacts'; - // Raw events response from etherescan.io export interface EventsResponse { status: string; @@ -48,16 +46,26 @@ export function _convertResponseToLogEntry(result: EventsResponseResult): LogEnt // Decodes a LogEntry into a LogWithDecodedArgs // tslint:disable-next-line:completed-docs -export function _decodeLogEntry(log: LogEntry): LogWithDecodedArgs<DecodedLogArgs> { - const abiDecoder = new AbiDecoder([artifacts.Exchange.compilerOutput.abi]); +export const _decodeLogEntry = R.curry((contractAbi: AbiDefinition[], log: LogEntry): LogWithDecodedArgs< + DecodedLogArgs +> => { + const abiDecoder = new AbiDecoder([contractAbi]); const logWithDecodedArgs = abiDecoder.tryToDecodeLogOrNoop(log); // tslint:disable-next-line:no-unnecessary-type-assertion return logWithDecodedArgs as LogWithDecodedArgs<DecodedLogArgs>; -} +}); /** * Parses and abi-decodes the raw events response from etherscan.io. + * @param contractAbi The ABI for the contract that the events where emited from. * @param rawEventsResponse The raw events response from etherescan.io. * @returns Parsed and decoded events. */ -export const parseRawEventsResponse = R.pipe(R.map(_convertResponseToLogEntry), R.map(_decodeLogEntry)); +export function parseRawEventsResponse( + contractAbi: AbiDefinition[], + rawEventsResponse: EventsResponse, +): Array<LogWithDecodedArgs<DecodedLogArgs>> { + return R.pipe(R.map(_convertResponseToLogEntry), R.map(_decodeLogEntry(contractAbi)))(rawEventsResponse.result); +} + +// export const parseRawEventsResponse = R.pipe(R.map(_convertResponseToLogEntry), R.map(_decodeLogEntry)); diff --git a/packages/pipeline/src/data-sources/etherscan/index.ts b/packages/pipeline/src/data-sources/etherscan/index.ts index 0891d351a..66b6b1a8d 100644 --- a/packages/pipeline/src/data-sources/etherscan/index.ts +++ b/packages/pipeline/src/data-sources/etherscan/index.ts @@ -1,5 +1,5 @@ import { default as axios } from 'axios'; -import { BlockParam, BlockParamLiteral, DecodedLogArgs, LogWithDecodedArgs } from 'ethereum-types'; +import { AbiDefinition, BlockParam, BlockParamLiteral, DecodedLogArgs, LogWithDecodedArgs } from 'ethereum-types'; import { EventsResponse, parseRawEventsResponse } from './events'; @@ -14,12 +14,14 @@ export class Etherscan { /** * Gets the decoded events for a specific contract and block range. * @param contractAddress The address of the contract to get the events for. + * @param constractAbi The ABI of the contract. * @param fromBlock The start of the block range to get events for (inclusive). * @param toBlock The end of the block range to get events for (inclusive). * @returns A list of decoded events. */ public async getContractEventsAsync( contractAddress: string, + contractAbi: AbiDefinition[], fromBlock: BlockParam = BlockParamLiteral.Earliest, toBlock: BlockParam = BlockParamLiteral.Latest, ): Promise<Array<LogWithDecodedArgs<DecodedLogArgs>>> { @@ -28,7 +30,7 @@ export class Etherscan { }`; const resp = await axios.get<EventsResponse>(fullURL); // TODO(albrow): Check response code. - const decodedEvents = parseRawEventsResponse(resp.data.result); + const decodedEvents = parseRawEventsResponse(contractAbi, resp.data); return decodedEvents; } } diff --git a/packages/pipeline/src/index.ts b/packages/pipeline/src/index.ts index baed6933e..eccb24d18 100644 --- a/packages/pipeline/src/index.ts +++ b/packages/pipeline/src/index.ts @@ -1,7 +1,13 @@ import { Etherscan } from './data-sources/etherscan'; +import { artifacts } from './artifacts'; + const etherscan = new Etherscan(process.env.ETHERSCAN_API_KEY as string); (async () => { - await etherscan.getContractEventsAsync('0x4f833a24e1f95d70f028921e27040ca56e09ab0b'); + const events = await etherscan.getContractEventsAsync( + '0x4f833a24e1f95d70f028921e27040ca56e09ab0b', + artifacts.Exchange.compilerOutput.abi, + ); + console.log(events); })(); diff --git a/packages/pipeline/test/data-sources/etherscan/events_test.ts b/packages/pipeline/test/data-sources/etherscan/events_test.ts index 1151757f2..cb8e9d8e8 100644 --- a/packages/pipeline/test/data-sources/etherscan/events_test.ts +++ b/packages/pipeline/test/data-sources/etherscan/events_test.ts @@ -3,6 +3,8 @@ import * as chai from 'chai'; import { DecodedLogArgs, LogEntry, LogWithDecodedArgs } from 'ethereum-types'; import 'mocha'; +import { artifacts } from '../../../src/artifacts'; + import { _convertResponseToLogEntry, _decodeLogEntry, @@ -81,7 +83,7 @@ describe('etherscan#events', () => { takerAssetData: '0xf47261b0000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2', }, }; - const actual = _decodeLogEntry(input); + const actual = _decodeLogEntry(artifacts.Exchange.compilerOutput.abi, input); expect(actual).deep.equal(expected); }); }); |