From 4e6919a6e58d76766098262664c78f92d888337d Mon Sep 17 00:00:00 2001 From: Alex Browne Date: Wed, 19 Sep 2018 17:30:34 -0700 Subject: Add typeorm and begin inserting database records --- .../pipeline/src/data-sources/etherscan/events.ts | 9 ++--- .../pipeline/src/entities/ExchangeFillEvent.ts | 24 ++++++++++++++ packages/pipeline/src/index.ts | 38 ++++++++++++++++++++-- packages/pipeline/src/ormconfig.ts | 14 ++++++++ 4 files changed, 78 insertions(+), 7 deletions(-) create mode 100644 packages/pipeline/src/entities/ExchangeFillEvent.ts create mode 100644 packages/pipeline/src/ormconfig.ts (limited to 'packages/pipeline/src') diff --git a/packages/pipeline/src/data-sources/etherscan/events.ts b/packages/pipeline/src/data-sources/etherscan/events.ts index 89b3ffac1..edc8cde7b 100644 --- a/packages/pipeline/src/data-sources/etherscan/events.ts +++ b/packages/pipeline/src/data-sources/etherscan/events.ts @@ -1,5 +1,6 @@ +import { ExchangeEventArgs } from '@0xproject/contract-wrappers'; import { AbiDecoder } from '@0xproject/utils'; -import { AbiDefinition, DecodedLogArgs, LogEntry, LogWithDecodedArgs } from 'ethereum-types'; +import { AbiDefinition, LogEntry, LogWithDecodedArgs } from 'ethereum-types'; import * as R from 'ramda'; // Raw events response from etherescan.io @@ -47,12 +48,12 @@ export function _convertResponseToLogEntry(result: EventsResponseResult): LogEnt // Decodes a LogEntry into a LogWithDecodedArgs // tslint:disable-next-line:completed-docs export const _decodeLogEntry = R.curry((contractAbi: AbiDefinition[], log: LogEntry): LogWithDecodedArgs< - DecodedLogArgs + ExchangeEventArgs > => { const abiDecoder = new AbiDecoder([contractAbi]); const logWithDecodedArgs = abiDecoder.tryToDecodeLogOrNoop(log); // tslint:disable-next-line:no-unnecessary-type-assertion - return logWithDecodedArgs as LogWithDecodedArgs; + return logWithDecodedArgs as LogWithDecodedArgs; }); /** @@ -64,7 +65,7 @@ export const _decodeLogEntry = R.curry((contractAbi: AbiDefinition[], log: LogEn export function parseRawEventsResponse( contractAbi: AbiDefinition[], rawEventsResponse: EventsResponse, -): Array> { +): Array> { return R.pipe(R.map(_convertResponseToLogEntry), R.map(_decodeLogEntry(contractAbi)))(rawEventsResponse.result); } diff --git a/packages/pipeline/src/entities/ExchangeFillEvent.ts b/packages/pipeline/src/entities/ExchangeFillEvent.ts new file mode 100644 index 000000000..1e9e8d986 --- /dev/null +++ b/packages/pipeline/src/entities/ExchangeFillEvent.ts @@ -0,0 +1,24 @@ +import { Column, Entity, PrimaryColumn } from 'typeorm'; + +@Entity() +export class ExchangeFillEvent { + @PrimaryColumn() public logIndex!: number; + + @Column() public address!: string; + @Column() public rawData!: string; + @Column() public blockNumber!: number; + + @Column() public makerAddress!: string; + @Column() public takerAddress!: string; + @Column() public feeRecepientAddress!: string; + @Column() public senderAddress!: string; + @Column() public makerAssetFilledAmount!: string; + @Column() public takerAssetFilledAmount!: string; + @Column() public makerFeePaid!: string; + @Column() public takerFeePaid!: string; + @Column() public orderHash!: string; + // TODO(albrow): Decode asset data. + @Column() public makerAssetData!: string; + @Column() public takerAssetData!: string; + // TODO(albrow): Include topics? +} diff --git a/packages/pipeline/src/index.ts b/packages/pipeline/src/index.ts index eccb24d18..d70ec3e3e 100644 --- a/packages/pipeline/src/index.ts +++ b/packages/pipeline/src/index.ts @@ -1,13 +1,45 @@ -import { Etherscan } from './data-sources/etherscan'; +import { ExchangeFillEventArgs } from '@0xproject/contract-wrappers'; +import { LogWithDecodedArgs } from 'ethereum-types'; +import 'reflect-metadata'; +import { createConnection } from 'typeorm'; import { artifacts } from './artifacts'; +import { Etherscan } from './data-sources/etherscan'; +import { ExchangeFillEvent } from './entities/ExchangeFillEvent'; +import { config } from './ormconfig'; const etherscan = new Etherscan(process.env.ETHERSCAN_API_KEY as string); (async () => { - const events = await etherscan.getContractEventsAsync( + const connection = await createConnection(config); + const repository = connection.getRepository(ExchangeFillEvent); + console.log(`found ${await repository.count()} existing fill events`); + const eventLogs = await etherscan.getContractEventsAsync( '0x4f833a24e1f95d70f028921e27040ca56e09ab0b', artifacts.Exchange.compilerOutput.abi, ); - console.log(events); + for (const eventLog of eventLogs) { + if (eventLog.event !== 'Fill') { + continue; + } + const fillEventLog = eventLog as LogWithDecodedArgs; + const exchangeFillEvent = new ExchangeFillEvent(); + exchangeFillEvent.logIndex = fillEventLog.logIndex as number; + exchangeFillEvent.address = fillEventLog.address as string; + exchangeFillEvent.rawData = fillEventLog.data as string; + exchangeFillEvent.blockNumber = fillEventLog.blockNumber as number; + exchangeFillEvent.makerAddress = fillEventLog.args.makerAddress.toString(); + exchangeFillEvent.takerAddress = fillEventLog.args.takerAddress.toString(); + exchangeFillEvent.feeRecepientAddress = fillEventLog.args.feeRecipientAddress; + exchangeFillEvent.senderAddress = fillEventLog.args.senderAddress; + exchangeFillEvent.makerAssetFilledAmount = fillEventLog.args.makerAssetFilledAmount.toString(); + exchangeFillEvent.takerAssetFilledAmount = fillEventLog.args.takerAssetFilledAmount.toString(); + exchangeFillEvent.makerFeePaid = fillEventLog.args.makerFeePaid.toString(); + exchangeFillEvent.takerFeePaid = fillEventLog.args.takerFeePaid.toString(); + exchangeFillEvent.orderHash = fillEventLog.args.orderHash; + exchangeFillEvent.makerAssetData = fillEventLog.args.makerAssetData; + exchangeFillEvent.takerAssetData = fillEventLog.args.takerAssetData; + await repository.save(exchangeFillEvent); + } + console.log(`now ${await repository.count()} total fill events`); })(); diff --git a/packages/pipeline/src/ormconfig.ts b/packages/pipeline/src/ormconfig.ts new file mode 100644 index 000000000..48e316078 --- /dev/null +++ b/packages/pipeline/src/ormconfig.ts @@ -0,0 +1,14 @@ +import { ConnectionOptions } from 'typeorm'; + +export const config: ConnectionOptions = { + type: 'sqlite', + database: 'database.sqlite', + synchronize: true, + logging: false, + entities: ['./lib/src/entities/**/*.js'], + migrations: ['./lib/src/migrations/**/*.js'], + cli: { + entitiesDir: 'lib/src/entities', + migrationsDir: 'lib/src/migrations', + }, +}; -- cgit