Description: provides high level functionality to working with fusion mode
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const wsSdk = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM,
authKey: 'your-auth-key'
})
wsSdk.order.onOrder((data) => {
console.log('received order event', data)
})
With constructor:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM,
authKey: 'your-auth-key'
})
Custom provider:
User can provide custom provider for websocket (be default we are using ws library)
import {WsProviderConnector, WebSocketApi} from '@1inch/fusion-sdk'
class MyFancyProvider implements WsProviderConnector {
// ... user implementation
}
const url = 'wss://api.1inch.dev/fusion/ws/v2.0/1'
const provider = new MyFancyProvider({url})
const wsSdk = new WebSocketApi(provider)
With new static method:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = WebSocketApi.new({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
Lazy initialization:
By default, when user creates an instance of WebSocketApi, it automatically opens websocket connection which might be a problem for some use cases
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM,
lazyInit: true
})
ws.init()
Base methods
Description: You can subscribe to any event
Arguments:
- [0] event: string
- [1] cb: Function
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.on('error', console.error)
ws.on('open', function open() {
ws.send('something')
})
ws.on('message', function message(data) {
console.log('received: %s', data)
})
Description: You can unsubscribe from any event
Arguments:
- [0] event: string
- [1] сb: Function
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.on('error', console.error)
ws.on('open', function open() {
ws.send('something')
})
function message(data) {
console.log('received: %s', data)
}
ws.on('message', message)
ws.off('message', message)
Description: subscribe to open event
Arguments:
- [0] cb: Function
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.onOpen(() => {
console.log('connection is opened')
})
Description: send event to backend
Arguments:
- [0] message: any message which can be serialized with JSON.stringify
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.send('my message')
Description: close connection
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.close()
Description: subscribe to message event
Arguments:
- [0] cb: (data: any) => void
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.onMessage((data) => {
console.log('message received', data)
})
Description: subscribe to close event
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.onClose(() => {
console.log('connection is closed')
})
Description: subscribe to error event
Arguments:
- [0] cb: (error: any) => void
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.onError((error) => {
console.log('error is received', error)
})
Order namespace
Description: subscribe to order events
Arguments:
- [0] cb: (data: OrderEventType) => void
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.order.onOrder((data) => {
if (data.event === 'order_created') {
// do something
}
if (data.event === 'order_invalid') {
// do something
}
})
Description: subscribe to order_created events
Arguments:
- [0] cb: (data: OrderCreatedEvent) => void
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.order.onOrderCreated((data) => {
// do something
})
Description: subscribe to order_invalid events
Arguments:
- [0] cb: (data: OrderInvalidEvent) => void
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.order.onOrderInvalid((data) => {
// do something
})
Description: subscribe to order_balance_or_allowance_change events
Arguments:
- [0] cb: (data: OrderBalanceOrAllowanceChangeEvent) => void
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.order.onOrderBalanceOrAllowanceChange((data) => {
// do something
})
Description: subscribe to order_filled events
Arguments:
- [0] cb: (data: OrderFilledEvent) => void
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.order.onOrderFilled((data) => {
// do something
})
Description: subscribe to order_filled_partially events
Arguments:
- [0] cb: (data: OrderFilledPartiallyEvent) => void
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.order.onOrderFilledPartially((data) => {
// do something
})
Description: subscribe to order_cancelled events
Arguments:
- [0] cb: (data: OrderCancelledEvent) => void
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.order.onOrderCancelled((data) => {
// do something
})
Rpc namespace
Description: subscribe to ping response
Arguments:
- [0] cb: (data: string) => void
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.rpc.onPong((data) => {
// do something
})
Description: ping healthcheck
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.rpc.ping()
Description: get the list of allowed methods
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.rpc.getAllowedMethods()
Description: subscribe to get allowed methods response
Arguments:
- [0] cb: (data: RpcMethod[]) => void
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.rpc.onGetAllowedMethods((data) => {
// do something
})
Description: get the list of active orders
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.rpc.getActiveOrders()
Description: subscribe to get active orders events
Arguments:
- [0] cb: (data: PaginationOutput<ActiveOrder>) => void
Example:
import {WebSocketApi, NetworkEnum} from '@1inch/fusion-sdk'
const ws = new WebSocketApi({
url: 'wss://api.1inch.dev/fusion/ws',
network: NetworkEnum.ETHEREUM
})
ws.rpc.onGetActiveOrders((data) => {
// do something
})
type Event<K extends string, T> = {event: K; data: T}
type OrderEventType =
| OrderCreatedEvent
| OrderInvalidEvent
| OrderBalanceOrAllowanceChangeEvent
| OrderFilledEvent
| OrderFilledPartiallyEvent
| OrderCancelledEvent
type OrderCreatedEvent = Event<
'order_created',
{
orderHash: string
signature: string
order: LimitOrderV3Struct
deadline: string
auctionStartDate: string
auctionEndDate: string
remainingMakerAmount: string
}
>
type OrderBalanceOrAllowanceChangeEvent = Event<
'order_balance_or_allowance_change',
{
orderHash: string
remainingMakerAmount: string
balance: string
allowance: string
}
>
type OrderInvalidEvent = Event<
'order_invalid',
{
orderHash: string
}
>
type OrderCancelledEvent = Event<
'order_cancelled',
{
orderHash: string
}
>
type OrderFilledEvent = Event<'order_filled', {orderHash: string}>
type OrderFilledPartiallyEvent = Event<
'order_filled_partially',
{orderHash: string; remainingMakerAmount: string}
>
type RpcMethod = 'getAllowedMethods' | 'ping'