Quotes Channel
Filter Message (Quotes)
chainId (Required) : Designate whether the stream is for a production chain or testnet chain
/*
TypeScript Sample Code
NOTE: since this is an API, WS logic can be written in any language. This example
is in typescript, but the overall sequence will be the same for any language.
*/
import { RawData, WebSocket } from 'ws'
// Example if Containerized API runtime is local
const url = `ws://localhost:${process.env.HTTP_PORT}`
const wsConnection = new WebSocket(url)
const MOCK_API_KEY = '3423ee2bfd89491f82b351404'
const authMessage: AuthMessage = {
type: 'AUTH',
apiKey: MOCK_API_KEY,
body: null
}
// send AuthMessage
wsConnection.send(JSON.stringify(authMsg))
/*
This filter message will listen to all PUBLIC quote and will additionally
receive PRIVATE quotes from the RFQ network.
*/
const webSocketFilter: FilterMessage = {
type: 'FILTER',
channel: 'QUOTES',
body: {
chainId: '42161',
taker: '0x170f9e3eb81ed29491a2efdcfa2edd34fdd24a71' // mock address
}
}
// handle message ingestion from ws
const wsCallback = (data: RawData) => {
const message: InfoMessage | PostQuoteMessage | FillQuoteMessage | DeleteQuoteMessage = JSON.parse(data.toString())
switch (message.type) {
case 'INFO': {
// auth result message will arrive here
break
}
case 'POST_QUOTE': {
// new quotes will arrive here
break
}
case 'FILL_QUOTE': {
// filled quotes will arrive here
break
}
case 'DELETE_QUOTE': {
// cancelled quotes will arrive here
break
}
case 'ERROR': {
// any error message will arrive here
break
}
// throw error if case not covered
default: {
throw `Unexpected message type ${message.type}`
}
}
}
// subscribe to WS messages
wsConnection.on('message', wsCallback)
// send FilterMessage to start getting quotes stream
wsConnection.send(JSON.stringify(webSocketFilter))
// unsubscribe to WS messages
// NOTE: unsubscribing does NOT close the ws connection
const unsubscribeMsg: WSUnsubscribeMessage = {
type: 'UNSUBSCRIBE',
channel: 'QUOTES',
body: null,
}
wsConnection.send(JSON.stringify(unsubscribeMsg))Last updated
Was this helpful?