Skip to content

Commit

Permalink
[Events Orderbook] Refactor applyEvent Logic (#725)
Browse files Browse the repository at this point in the history
The refactor that was promised! This PR refactors `applyEvents` switch statement to be more "dumb" by forwarding each event to their individual functions.

This has the added benefit of using the `Event` types so that we have type safety on the event `returnValues` property.

### Test Plan

This PR is just a refactor so unit tests should verify that nothing broke.
  • Loading branch information
nlordell authored May 6, 2020
1 parent 0763337 commit f448458
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 180 deletions.
4 changes: 2 additions & 2 deletions src/streamed/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Contract, EventData } from "web3-eth-contract"
import { ContractEvent } from "../../build/types/types"

/**
* Event type specified by name.
* Event data type specified by name.
*/
export type Event<
C extends Contract,
T extends Exclude<keyof C["events"], "allEvents">,
> = EventMetadata & EventDiscriminant<C, T>
> = EventValues<C["events"][T]>

/**
* Concrete event type with known properties based on the event name.
Expand Down
25 changes: 16 additions & 9 deletions src/streamed/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
*/

import Web3 from "web3"
import { TransactionReceipt } from "web3-core"
import { EventData } from "web3-eth-contract"
import { BlockNumber, TransactionReceipt } from "web3-core"
import { AbiItem } from "web3-utils"
import { BatchExchange, BatchExchangeArtifact } from "../.."
import { AnyEvent } from "./events"
import { AuctionState } from "./state"

/**
Expand Down Expand Up @@ -88,7 +88,7 @@ export const BATCH_DURATION = 300
*/
export class StreamedOrderbook {
private readonly state: AuctionState;
private pendingEvents: EventData[] = [];
private pendingEvents: AnyEvent<BatchExchange>[] = [];

private invalidState?: InvalidAuctionStateError;

Expand Down Expand Up @@ -150,11 +150,8 @@ export class StreamedOrderbook {
endBlock,
)

this.options.logger?.debug(`fetching page ${fromBlock}-${toBlock}`)
const events = await this.contract.getPastEvents(
"allEvents",
{ fromBlock, toBlock },
)
this.options.logger?.debug(`fetching past events from ${fromBlock}-${toBlock}`)
const events = await this.getPastEvents({ fromBlock, toBlock })

this.options.logger?.debug(`applying ${events.length} past events`)
this.state.applyEvents(events)
Expand All @@ -180,7 +177,7 @@ export class StreamedOrderbook {

const fromBlock = this.state.nextBlock
this.options.logger?.debug(`fetching new events from ${fromBlock}-latest`)
const events = await this.contract.getPastEvents("allEvents", { fromBlock, toBlock: "latest" })
const events = await this.getPastEvents({ fromBlock })

const latestBlock = await this.web3.eth.getBlockNumber()
const confirmedBlock = latestBlock - this.options.blockConfirmations
Expand All @@ -201,6 +198,16 @@ export class StreamedOrderbook {
}
this.pendingEvents = pendingEvents
}

private async getPastEvents(
options: { fromBlock: BlockNumber; toBlock?: BlockNumber },
): Promise<AnyEvent<BatchExchange>[]> {
const events = await this.contract.getPastEvents("allEvents", {
toBlock: "latest",
...options,
})
return events as AnyEvent<BatchExchange>[]
}
}

/**
Expand Down
Loading

0 comments on commit f448458

Please sign in to comment.