Skip to content

Commit

Permalink
feat: add taker and maker account fields to trade message
Browse files Browse the repository at this point in the history
  • Loading branch information
thaaddeus committed Dec 23, 2021
1 parent ff28542 commit 0013a6a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
24 changes: 10 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,19 +370,9 @@ Up to 100 recent trades pushed immediately after successful subscription confirm
"side": "sell",
"price": "160.73",
"size": "4.53",
"eventTimestamp": "2021-12-14T12:32:59.000Z"
},
{
"type": "trade",
"market": "SOL-PERP",
"timestamp": "2021-12-14T12:33:42.437Z",
"slot": 111490426,
"version": 1,
"id": "296476070752659925097329|296512964240807321150640",
"side": "sell",
"price": "160.73",
"size": "0.50",
"eventTimestamp": "2021-12-14T12:33:39.000Z"
"eventTimestamp": "2021-12-14T12:32:59.000Z",
"takerAccount": "AAddgLu9reZCUWW1bNQFaXrCMAtwQpMRvmeusgk4pCM6",
"makerAccount": "EpAdzaqV13Es3x4dukfjFoCrKVXnZ7y9Y76whgMHo5qx"
}
]
}
Expand All @@ -400,6 +390,8 @@ Pushed real-time for each trade as it happens on a DEX (decoded from the `eventQ

- `eventTimestamp` is a timestamp of trade provided by DEX (with seconds precision) in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601)

- `takerAccount` and `makerAccount` fields provide info regarding maker and taker open account addresses that constitute the trade

```ts
{
"type": "trade",
Expand All @@ -412,6 +404,8 @@ Pushed real-time for each trade as it happens on a DEX (decoded from the `eventQ
"price": string,
"size": string,
"eventTimestamp": string
"takerAccount": string
"makerAccount": string
}
```

Expand All @@ -428,7 +422,9 @@ Pushed real-time for each trade as it happens on a DEX (decoded from the `eventQ
"side": "sell",
"price": "160.64",
"size": "0.50",
"eventTimestamp": "2021-12-14T12:35:29.000Z"
"eventTimestamp": "2021-12-14T12:35:29.000Z",
"takerAccount": "AAddgLu9reZCUWW1bNQFaXrCMAtwQpMRvmeusgk4pCM6",
"makerAccount": "EpAdzaqV13Es3x4dukfjFoCrKVXnZ7y9Y76whgMHo5qx"
}
```

Expand Down
46 changes: 30 additions & 16 deletions src/data_mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,21 +369,33 @@ export class DataMapper {

// detect l2 trades based on fills
if (message.type === 'fill' && message.maker === false) {
// this is rather fragile way of finding matching fill, can it be done better?

const matchingMakerFill =
l3Diff[i - 1] !== undefined && l3Diff[i - 1]!.type === 'fill'
? (l3Diff[i - 1] as Fill)
: l3Diff[i - 2] !== undefined && l3Diff[i - 2]!.type === 'fill'
? (l3Diff[i - 2] as Fill)
: undefined

const makerFillOrderId =
matchingMakerFill !== undefined &&
matchingMakerFill.maker === true &&
matchingMakerFill.size === message.size
? matchingMakerFill.orderId
: '_'
let matchingMakerFill

for (let j = i - 1; j >= 0; j--) {
const potentialFillMessage = l3Diff[j]!

if (
potentialFillMessage.type === 'fill' &&
potentialFillMessage.maker === true &&
potentialFillMessage.size === message.size
) {
matchingMakerFill = potentialFillMessage
break
}
}

const makerFillOrderId = matchingMakerFill !== undefined ? matchingMakerFill.orderId : undefined

const makerFillAccount = matchingMakerFill !== undefined ? matchingMakerFill.account : undefined

if (makerFillOrderId === undefined) {
logger.log('warn', 'Trade without matching maker fill order', {
market: this._options.symbol,
slot,
fill: message,
l3Diff
})
}

const tradeId = `${message.orderId}|${makerFillOrderId}`

Expand All @@ -397,7 +409,9 @@ export class DataMapper {
side: message.side,
price: message.price,
size: message.size,
eventTimestamp: message.eventTimestamp
eventTimestamp: message.eventTimestamp,
takerAccount: message.account,
makerAccount: makerFillAccount!
}

yield this._putInEnvelope(tradeMessage, true)
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ export interface Trade extends DataMessage, WithEventTimestamp {
readonly size: string
readonly side: 'buy' | 'sell' // liquidity taker side
readonly id: string
readonly takerAccount: string
readonly makerAccount: string
}

export interface Fill extends DataMessage, OrderItem {
Expand Down

0 comments on commit 0013a6a

Please sign in to comment.