Skip to content

Commit

Permalink
add order cancelled event
Browse files Browse the repository at this point in the history
  • Loading branch information
frolovdev committed Jan 16, 2024
1 parent 9d160eb commit 1d841a1
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/ws-api/active-websocket-orders-api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {WsProviderConnector} from '../connector/ws'
import {orderEvents} from './constants'
import {
OnOrderCancelledCb,
OnOrderCb,
OnOrderCreatedCb,
OnOrderFilledCb,
Expand Down Expand Up @@ -59,6 +60,14 @@ export class ActiveOrdersWebSocketApi {
})
}

onOrderCancelled(cb: OnOrderCancelledCb): void {
this.provider.onMessage((data: OrderEventType) => {
if (data.event === 'order_cancelled') {
cb(data)
}
})
}

onOrderFilledPartially(cb: OnOrderFilledPartiallyCb): void {
this.provider.onMessage((data: OrderEventType) => {
if (data.event === 'order_filled_partially') {
Expand Down
3 changes: 2 additions & 1 deletion src/ws-api/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export const orderEvents: OrderEventType['event'][] = [
'order_invalid',
'order_balance_or_allowance_change',
'order_filled',
'order_filled_partially'
'order_filled_partially',
'order_cancelled'
]
10 changes: 10 additions & 0 deletions src/ws-api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type OrderEventType =
| OrderBalanceOrAllowanceChangeEvent
| OrderFilledEvent
| OrderFilledPartiallyEvent
| OrderCancelledEvent

export type OrderCreatedEvent = Event<
'order_created',
Expand Down Expand Up @@ -43,6 +44,13 @@ export type OrderInvalidEvent = Event<
}
>

export type OrderCancelledEvent = Event<
'order_cancelled',
{
orderHash: string
}
>

export type OrderFilledEvent = Event<'order_filled', {orderHash: string}>

export type OrderFilledPartiallyEvent = Event<
Expand All @@ -56,6 +64,8 @@ export type OnOrderCreatedCb = (data: OrderCreatedEvent) => any

export type OnOrderInvalidCb = (data: OrderInvalidEvent) => any

Check warning on line 65 in src/ws-api/types.ts

View workflow job for this annotation

GitHub Actions / Static checks

Unexpected any. Specify a different type

export type OnOrderCancelledCb = (data: OrderCancelledEvent) => any

Check warning on line 67 in src/ws-api/types.ts

View workflow job for this annotation

GitHub Actions / Static checks

Unexpected any. Specify a different type

export type OnOrderNotEnoughBalanceOrAllowanceCb = (
data: OrderBalanceOrAllowanceChangeEvent
) => any

Check warning on line 71 in src/ws-api/types.ts

View workflow job for this annotation

GitHub Actions / Static checks

Unexpected any. Specify a different type
Expand Down
66 changes: 66 additions & 0 deletions src/ws-api/ws.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {WebSocketServer, WebSocket} from 'ws'
import {
GetActiveOrdersRpcEvent,
OrderBalanceOrAllowanceChangeEvent,
OrderCancelledEvent,
OrderCreatedEvent,
OrderEventType,
OrderFilledEvent,
Expand Down Expand Up @@ -781,6 +782,71 @@ describe(__filename, () => {
}
})
})

it('can subscribe to order cancelled events', (done) => {
const message1: OrderCreatedEvent = {
event: 'order_created',
result: {
orderHash:
'0x1beee023ab933cf5446c298eaddb61c0-5705f2156ef5b2db36c160b36f31ce4',
order: {
salt: '45144194282371711345892930501725766861375817078109214409479816083205610767025',
maker: '0x6f250c769001617aff9bdf4b9fd878062e94af83',
offsets:
'970558080243398695134547109586957793750899628853613079895592438595584',
receiver: '0x0000000000000000000000000000000000000000',
makerAsset:
'0x6eb15148d0ea88433dd8088a3acc515d27e36c1b',
takerAsset:
'0xdac17f958d2ee523a2206206994597c13d831ec7',
interactions:
'0x2cc2878d000063ceb60f0000000000006f250c769001617aff9bdf4b9fd878062e94af83006c00c2fe001800c44c0000000084d99aa569d93a9ca187d83734c8c4a519c4e9b1ffffffff0a',
makingAmount: '2246481050155000',
takingAmount: '349837736598',
allowedSender:
'0xa88800cd213da5ae406ce248380802bd53b47647'
},
signature:
'0x21ef770f9bedbb97542033bd3b1a2ad611917567102545c93ce66668b8524b7c609bead7829113e104be41fbbd14fea027c85bc4668214b81d52f02c2f9010551b',
deadline: '2023-01-31T11:01:02.000Z',
auctionStartDate: '2023-01-31T10:58:02.000Z',
auctionEndDate: '2023-01-31T11:01:02.000Z',
remainingMakerAmount: '57684207067582695'
}
}

const message2: OrderCancelledEvent = {
event: 'order_cancelled',
result: {
orderHash:
'0x1beee023ab933cf5446c298eaddb61c0-5705f2156ef5b2db36c160b36f31ce4'
}
}

const messages = [message1, message1, message2]
const expectedMessages = [message2]
const {url, wss} = createWebsocketServerMock(messages)

const wsSdk = new WebSocketApi({
url,
network: NetworkEnum.ETHEREUM,
authKey: ''
})

const resArray: OrderEventType[] = []
wsSdk.order.onOrderCancelled((data) => {
resArray.push(data)
})

wsSdk.onMessage(() => {
if (resArray.length === 1) {
expect(resArray).toEqual(expectedMessages)
wsSdk.close()
wss.close()
done()
}
})
})
})
})

Expand Down

0 comments on commit 1d841a1

Please sign in to comment.