Skip to content
This repository has been archived by the owner on Jan 15, 2021. It is now read-only.

Trades/types #1038

Merged
merged 3 commits into from
May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@fortawesome/free-solid-svg-icons": "^5.12.0",
"@fortawesome/react-fontawesome": "^0.1.8",
"@gnosis.pm/dapp-ui": "^0.5.3",
"@gnosis.pm/dex-js": "0.3.1-RC.0",
"@gnosis.pm/dex-js": "0.3.1-RC.1",
"@hapi/joi": "^17.1.1",
"@hot-loader/react-dom": "^16.11.0",
"@popperjs/core": "^2.0.6",
Expand Down
63 changes: 9 additions & 54 deletions src/api/exchange/ExchangeApi.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import BN from 'bn.js'
import BigNumber from 'bignumber.js'
// import { Subscription } from 'web3-core-subscriptions'

import { assert, TokenDetails, BatchExchangeContract } from '@gnosis.pm/dex-js'
import { Trade } from '@gnosis.pm/dex-js/build-esm/contracts/gen/BatchExchange'
import { Subscription } from 'web3-core-subscriptions'

import { assert, BatchExchangeEvents } from '@gnosis.pm/dex-js'
import { DepositApiImpl, DepositApi, Params } from 'api/deposit/DepositApi'
import { Receipt, TxOptionalParams } from 'types'
import { logDebug } from 'utils'
Expand Down Expand Up @@ -132,21 +129,6 @@ export interface BaseTradeEvent {
id: string // txHash + | + txIndex
}

/**
* TradeEvent enriches BaseTradeEvent with block, order and token data
*/
export interface TradeEvent extends BaseTradeEvent {
batchId: number
hashKey: string // orderId + batchId, to find reverts
indexOnBatch: number // tracks trade position on batch, in case of reverts
time: Date
buyToken: TokenDetails
sellToken: TokenDetails
limitPrice: BigNumber
fillPrice: BigNumber
reverted: boolean
}
Comment on lines -135 to -148
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh no, my beautiful yet unused interface :(


export interface GetOrdersPaginatedResult {
orders: AuctionElement[]
nextIndex?: number
Expand All @@ -158,9 +140,8 @@ const CONTRACT_DEPLOYMENT_BLOCK = {
4: 5844678,
}

// Syntactic sugar. Aliasing event types to make code cleaner
type TradeSubscription = ReturnType<BatchExchangeContract['events']['Trade']>
// type TradeEventData = TradeSubscription extends Subscription<infer U> ? U : never
type TradeEvent = BatchExchangeEvents['Trade']
type TradeSubscription = Subscription<TradeEvent>

interface Subscriptions {
trade: { [networkId: number]: TradeSubscription }
Expand Down Expand Up @@ -202,40 +183,14 @@ export class ExchangeApiImpl extends DepositApiImpl implements ExchangeApi {
logDebug(`[ExchangeApiImpl] subscribing to trade events for address ${userAddress} and networkId ${networkId}`)

const subscription = await this.getTradeSubscription(params)
// Two ways of subscribing

// 1: with error handling
subscription.subscribe((error, event) => {
if (error) {
subscription
.on('data', event => callback(this.parseTradeEvent(event)))
.on('error', error => {
alfetopito marked this conversation as resolved.
Show resolved Hide resolved
console.error(
`[ExchangeApiImpl] Failed to receive Trade event for address ${userAddress} on network ${networkId}: ${error}`,
)
} else {
// TODO: this double casting hack solves the type problem, but is this the right way?
callback(this.parseTradeEvent((event as unknown) as Trade))
}
})
// 2: Without, watching only `data` events
// subscription.on('data', event => callback(this.parseTradeEvent(event)))

// TODO: doesn't matter how I subscribe, TS says the type returned by the event does not contain metadata fields, only event fields.
// What TS says it returns:
// (parameter) event: {
// owner: string;
// orderId: string;
// sellToken: string;
// buyToken: string;
// executedSellAmount: string;
// executedBuyAmount: string;
// 0: string;
// 1: string;
// 2: string;
// 3: string;
// 4: string;
// 5: string;
// }
// Argument of type '{ owner: string; orderId: string; sellToken: string; buyToken: string; executedSellAmount: string; executedBuyAmount: string; 0: string; 1: string; 2: string; 3: string; 4: string; 5: string; }' is not assignable to parameter of type 'Trade | ContractEventLog<{ owner: string; orderId: string; sellToken: string; buyToken: string; executedSellAmount: string; executedBuyAmount: string; 0: string; 1: string; 2: string; 3: string; 4: string; 5: string; }>'.
// Type '{ owner: string; orderId: string; sellToken: string; buyToken: string; executedSellAmount: string; executedBuyAmount: string; 0: string; 1: string; 2: string; 3: string; 4: string; 5: string; }' is missing the following properties from type 'ContractEventLog<{ owner: string; orderId: string; sellToken: string; buyToken: string; executedSellAmount: string; executedBuyAmount: string; 0: string; 1: string; 2: string; 3: string; 4: string; 5: string; }>': returnValues, event, address, logIndex, and 4 more.ts(2345)
})

return (): void => this.unsubscribeToTradeEvent()
}
Expand All @@ -261,7 +216,7 @@ export class ExchangeApiImpl extends DepositApiImpl implements ExchangeApi {
return this.subscriptions.trade[networkId]
}

private parseTradeEvent(event: Trade): BaseTradeEvent {
private parseTradeEvent(event: TradeEvent): BaseTradeEvent {
const {
returnValues: { orderId, sellToken: sellTokenId, buyToken: buyTokenId, executedSellAmount, executedBuyAmount },
transactionHash: txHash,
Expand Down
6 changes: 2 additions & 4 deletions src/api/exchange/ExchangeApiMock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import {
GetOrdersPaginatedParams,
GetOrdersPaginatedResult,
HasTokenParams,
PastEventsParams,
SubscriptionParams,
BaseTradeEvent,
} from './ExchangeApi'
import { Erc20Api } from 'api/erc20/Erc20Api'
Expand Down Expand Up @@ -63,11 +61,11 @@ export class ExchangeApiMock extends DepositApiMock implements ExchangeApi {
this.orders = ordersByUser
}

public async getPastTrades(_: PastEventsParams): Promise<BaseTradeEvent[]> {
public async getPastTrades(): Promise<BaseTradeEvent[]> {
return []
}

public async subscribeToTradeEvent(_: SubscriptionParams): Promise<() => void> {
public async subscribeToTradeEvent(): Promise<() => void> {
return this.unsubscribeToTradeEvent
}

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1113,10 +1113,10 @@
dependencies:
bn.js "^5.1.1"

"@gnosis.pm/dex-js@0.3.1-RC.0":
version "0.3.1-RC.0"
resolved "https://registry.yarnpkg.com/@gnosis.pm/dex-js/-/dex-js-0.3.1-RC.0.tgz#12b18cf8949b9a30380404547d3a8679c4b4d072"
integrity sha512-MIl2SsspJumh3PQNwyzEHz8AWLNIXXenhmDwNHMaBlnDSZrD/xFjvp9hozTSEiAGRLhICSX1yC/0M7tQdwu7fA==
"@gnosis.pm/dex-js@0.3.1-RC.1":
version "0.3.1-RC.1"
resolved "https://registry.yarnpkg.com/@gnosis.pm/dex-js/-/dex-js-0.3.1-RC.1.tgz#44eaeaca006cc43a82fc4924ede0ffbfb78380e4"
integrity sha512-+cJwHl/lNDk/ozo2qUFd8X8+BgZTSDO3pU4VQfB2Ztz3H8PZRl7OAXUCY417vRyf+8jn0KSolubbgXVqZ8eCkA==
dependencies:
"@gnosis.pm/dex-contracts" "^0.2.0"
bignumber.js "^9.0.0"
Expand Down