Add typeorm and begin inserting database records

This commit is contained in:
Alex Browne
2018-09-19 17:30:34 -07:00
committed by Fred Carlsen
parent ed3737c1f1
commit 90e1028d2f
3 changed files with 64 additions and 7 deletions

View File

@@ -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<DecodedLogArgs>;
return logWithDecodedArgs as LogWithDecodedArgs<ExchangeEventArgs>;
});
/**
@@ -64,7 +65,7 @@ export const _decodeLogEntry = R.curry((contractAbi: AbiDefinition[], log: LogEn
export function parseRawEventsResponse(
contractAbi: AbiDefinition[],
rawEventsResponse: EventsResponse,
): Array<LogWithDecodedArgs<DecodedLogArgs>> {
): Array<LogWithDecodedArgs<ExchangeEventArgs>> {
return R.pipe(R.map(_convertResponseToLogEntry), R.map(_decodeLogEntry(contractAbi)))(rawEventsResponse.result);
}

View File

@@ -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?
}

View File

@@ -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<ExchangeFillEventArgs>;
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`);
})();