diff options
author | Alex Browne <stephenalexbrowne@gmail.com> | 2018-09-20 02:25:46 +0800 |
---|---|---|
committer | Fred Carlsen <fred@sjelfull.no> | 2018-12-13 01:11:53 +0800 |
commit | cd7e41bd5bbf1abae4c855d6a666e6d9f7da2294 (patch) | |
tree | c5d07d4fdbe304295a1ad6dfc7ec0a246a578dc1 /packages | |
parent | c46d2f8f9d4e632352637376b2556101bccad748 (diff) | |
download | dexon-sol-tools-cd7e41bd5bbf1abae4c855d6a666e6d9f7da2294.tar dexon-sol-tools-cd7e41bd5bbf1abae4c855d6a666e6d9f7da2294.tar.gz dexon-sol-tools-cd7e41bd5bbf1abae4c855d6a666e6d9f7da2294.tar.bz2 dexon-sol-tools-cd7e41bd5bbf1abae4c855d6a666e6d9f7da2294.tar.lz dexon-sol-tools-cd7e41bd5bbf1abae4c855d6a666e6d9f7da2294.tar.xz dexon-sol-tools-cd7e41bd5bbf1abae4c855d6a666e6d9f7da2294.tar.zst dexon-sol-tools-cd7e41bd5bbf1abae4c855d6a666e6d9f7da2294.zip |
Restructure pipeline package. Create data-sources dir
Diffstat (limited to 'packages')
-rw-r--r-- | packages/pipeline/src/data-sources/etherscan/events.ts | 54 | ||||
-rw-r--r-- | packages/pipeline/src/data-sources/etherscan/index.ts | 34 | ||||
-rw-r--r-- | packages/pipeline/src/events.ts | 75 | ||||
-rw-r--r-- | packages/pipeline/src/index.ts | 8 |
4 files changed, 96 insertions, 75 deletions
diff --git a/packages/pipeline/src/data-sources/etherscan/events.ts b/packages/pipeline/src/data-sources/etherscan/events.ts new file mode 100644 index 000000000..50962a266 --- /dev/null +++ b/packages/pipeline/src/data-sources/etherscan/events.ts @@ -0,0 +1,54 @@ +import { AbiDecoder } from '@0xproject/utils'; +import { 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; + message: string; + result: EventsResponseResult[]; +} + +// Events as represented in the response from etherscan.io +export interface EventsResponseResult { + address: string; + topics: string[]; + data: string; + blockNumber: string; + timeStamp: string; + gasPrice: string; + gasUsed: string; + logIndex: string; + transactionHash: string; + transactionIndex: string; +} + +function convertResponseToLogEntry(result: EventsResponseResult): LogEntry { + const radix = 10; + return { + logIndex: parseInt(result.logIndex, radix), + transactionIndex: parseInt(result.logIndex, radix), + transactionHash: result.transactionHash, + blockHash: '', + blockNumber: parseInt(result.blockNumber, radix), + address: result.address, + data: result.data, + topics: result.topics, + }; +} + +function tryToDecodeLogOrNoop(log: LogEntry): LogWithDecodedArgs<DecodedLogArgs> { + const abiDecoder = new AbiDecoder([artifacts.Exchange.compilerOutput.abi]); + 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 rawEventsResponse The raw events response from etherescan.io. + * @returns Parsed and decoded events. + */ +export const parseRawEventsResponse = R.pipe(R.map(convertResponseToLogEntry), R.map(tryToDecodeLogOrNoop)); diff --git a/packages/pipeline/src/data-sources/etherscan/index.ts b/packages/pipeline/src/data-sources/etherscan/index.ts new file mode 100644 index 000000000..0891d351a --- /dev/null +++ b/packages/pipeline/src/data-sources/etherscan/index.ts @@ -0,0 +1,34 @@ +import { default as axios } from 'axios'; +import { BlockParam, BlockParamLiteral, DecodedLogArgs, LogWithDecodedArgs } from 'ethereum-types'; + +import { EventsResponse, parseRawEventsResponse } from './events'; + +const ETHERSCAN_URL = 'https://api.etherscan.io/api'; + +export class Etherscan { + private readonly _apiKey: string; + constructor(apiKey: string) { + this._apiKey = apiKey; + } + + /** + * Gets the decoded events for a specific contract and block range. + * @param contractAddress The address of the contract to get the events for. + * @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, + fromBlock: BlockParam = BlockParamLiteral.Earliest, + toBlock: BlockParam = BlockParamLiteral.Latest, + ): Promise<Array<LogWithDecodedArgs<DecodedLogArgs>>> { + const fullURL = `${ETHERSCAN_URL}?module=logs&action=getLogs&address=${contractAddress}&fromBlock=${fromBlock}&toBlock=${toBlock}&apikey=${ + this._apiKey + }`; + const resp = await axios.get<EventsResponse>(fullURL); + // TODO(albrow): Check response code. + const decodedEvents = parseRawEventsResponse(resp.data.result); + return decodedEvents; + } +} diff --git a/packages/pipeline/src/events.ts b/packages/pipeline/src/events.ts deleted file mode 100644 index a6e1e4ad3..000000000 --- a/packages/pipeline/src/events.ts +++ /dev/null @@ -1,75 +0,0 @@ -import { AbiDecoder } from '@0xproject/utils'; -import { default as axios } from 'axios'; -import { BlockParam, BlockParamLiteral, DecodedLogArgs, LogEntry, LogWithDecodedArgs } from 'ethereum-types'; -import * as R from 'ramda'; - -import { artifacts } from './artifacts'; - -// const EXCHANGE_ADDRESS = '0x4f833a24e1f95d70f028921e27040ca56e09ab0b'; -const ETHERSCAN_URL = 'https://api.etherscan.io/api'; -// TOOD(albrow): Pass this in as a constructor argument instead of an env var. -const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY; - -// Raw response from etherescan.io -interface EventsResponse { - status: string; - message: string; - result: EventsResponseResult[]; -} - -// Events as represented in the response from etherscan.io -interface EventsResponseResult { - address: string; - topics: string[]; - data: string; - blockNumber: string; - timeStamp: string; - gasPrice: string; - gasUsed: string; - logIndex: string; - transactionHash: string; - transactionIndex: string; -} - -// Parses and abi-decodes the fill events response from etherscan.io. -const parseFillEventsResponse = R.pipe(R.map(convertResponseToLogEntry), R.map(tryToDecodeLogOrNoop)); - -function convertResponseToLogEntry(result: EventsResponseResult): LogEntry { - const radix = 10; - return { - logIndex: parseInt(result.logIndex, radix), - transactionIndex: parseInt(result.logIndex, radix), - transactionHash: result.transactionHash, - blockHash: '', - blockNumber: parseInt(result.blockNumber, radix), - address: result.address, - data: result.data, - topics: result.topics, - }; -} - -function tryToDecodeLogOrNoop(log: LogEntry): LogWithDecodedArgs<DecodedLogArgs> { - const abiDecoder = new AbiDecoder([artifacts.Exchange.compilerOutput.abi]); - const logWithDecodedArgs = abiDecoder.tryToDecodeLogOrNoop(log); - // tslint:disable-next-line:no-unnecessary-type-assertion - return logWithDecodedArgs as LogWithDecodedArgs<DecodedLogArgs>; -} - -/** - * Gets the decoded events for a specific contract and block range. - * @param contractAddress The address of the contract to get the events for. - * @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. - */ -export async function getContractEventsAsync( - contractAddress: string, - fromBlock: BlockParam = BlockParamLiteral.Earliest, - toBlock: BlockParam = BlockParamLiteral.Latest, -): Promise<Array<LogWithDecodedArgs<DecodedLogArgs>>> { - const fullURL = `${ETHERSCAN_URL}?module=logs&action=getLogs&address=${contractAddress}&fromBlock=${fromBlock}&toBlock=${toBlock}&apikey=${ETHERSCAN_API_KEY}`; - const resp = await axios.get<EventsResponse>(fullURL); - // TODO(albrow): Check response code. - const decodedEvents = parseFillEventsResponse(resp.data.result); - return decodedEvents; -} diff --git a/packages/pipeline/src/index.ts b/packages/pipeline/src/index.ts new file mode 100644 index 000000000..c9254cc2a --- /dev/null +++ b/packages/pipeline/src/index.ts @@ -0,0 +1,8 @@ +import { Etherscan } from './data-sources/etherscan'; + +const etherscan = new Etherscan(process.env.ETHERSCAN_API_KEY as string); + +(async () => { + const events = await etherscan.getContractEventsAsync('0x4f833a24e1f95d70f028921e27040ca56e09ab0b'); + console.log(events); +})(); |