Skip to main content

Class: GdexStreamService

Client for the gSwap realtime stream (gdex WebSocket Protocol v1): JSON-RPC 2.0 over one raw WebSocket, with automatic reconnect and resubscribe.

Example​

const stream = new GdexStreamService();
await stream.connect();
await stream.subscribeLiquidity(event => console.log(event.action, event.owner), {
owner: 'client|618ae395c1c653111d3315be',
});

Constructors​

Constructor​

new GdexStreamService(config?): GdexStreamService

Parameters​

ParameterType
config?GdexStreamConfig

Returns​

GdexStreamService

Methods​

connect()​

connect(): Promise<void>

Open the connection. Resolves at once when already connected.

Returns​

Promise<void>


disconnect()​

disconnect(): void

Close the connection and stop reconnecting.

Returns​

void


getBalance()​

getBalance(owner, tokenClassKey?): Promise<Record<string, unknown>>

gdex_getBalance: an owner's balances, or only the one of tokenClassKey when given.

Parameters​

ParameterType
ownerstring
tokenClassKey?string

Returns​

Promise<Record<string, unknown>>


getBlockHeight()​

getBlockHeight(): Promise<{ blockNumber: number; timestamp: string; }>

gdex_getBlockHeight: the block number the server reports, with its timestamp.

Returns​

Promise<{ blockNumber: number; timestamp: string; }>


getPool()​

getPool(selector): Promise<Record<string, unknown>>

gdex_getPool: the current state of one pool, selected by its key or hash.

Parameters​

ParameterType
selectorGdexPoolSelector

Returns​

Promise<Record<string, unknown>>


getPositions()​

getPositions(opts): Promise<{ bookmark: string; positions: Record<string, unknown>[]; }>

gdex_getPositions: an owner's positions, optionally in one pool, a page at a time. Pass the returned bookmark back in as bookmark for the next page.

Parameters​

ParameterType
opts{ bookmark?: string; limit?: number; owner: string; pool?: GdexPoolKey; poolHash?: string; }
opts.bookmark?string
opts.limit?number
opts.ownerstring
opts.pool?GdexPoolKey
opts.poolHash?string

Returns​

Promise<{ bookmark: string; positions: Record<string, unknown>[]; }>


getToken()​

getToken(tokenClassKey): Promise<Record<string, unknown>>

gdex_getToken: the server's record of one token class, as it sends it.

Parameters​

ParameterType
tokenClassKeystring

Returns​

Promise<Record<string, unknown>>


isConnected()​

isConnected(): boolean

true while the socket is open (false while connecting, reconnecting or after disconnect()).

Returns​

boolean


ping()​

ping(): Promise<{ pong: boolean; serverTime: string; }>

gdex_ping: a round trip that answers { pong, serverTime }.

Returns​

Promise<{ pong: boolean; serverTime: string; }>


quote()​

quote(params): Promise<Record<string, unknown>>

gdex_quote: a quote over the stream; give exactly one of amountIn or amountOut.

Parameters​

ParameterType
params{ amountIn?: string; amountOut?: string; fee?: number; tokenIn: string; tokenOut: string; }
params.amountIn?string
params.amountOut?string
params.fee?number
params.tokenInstring
params.tokenOutstring

Returns​

Promise<Record<string, unknown>>


request()​

request<T>(method, params?): Promise<T>

Raw JSON-RPC escape hatch.

Type Parameters​

Type ParameterDefault type
Tunknown

Parameters​

ParameterType
methodstring
params?Record<string, unknown>

Returns​

Promise<T>


serverInfo()​

serverInfo(): Promise<Record<string, unknown>>

gdex_serverInfo: the server's own description of itself, as it sends it.

Returns​

Promise<Record<string, unknown>>


subscribeBalances()​

subscribeBalances(owner, cb, opts?): Promise<GdexSubscription>

Balance changes of one wallet.

Parameters​

ParameterType
ownerstring
cb(event) => void
opts?GdexSubscribeOptions & object

Returns​

Promise<GdexSubscription>


subscribeBlocks()​

subscribeBlocks(cb, opts?): Promise<GdexSubscription>

Each block-height advance.

Parameters​

ParameterType
cb(event) => void
opts?GdexSubscribeOptions

Returns​

Promise<GdexSubscription>


subscribeLiquidity()​

subscribeLiquidity(cb, opts?): Promise<GdexSubscription>

One event per committed AddLiquidity, RemoveLiquidity and CollectPositionFees, on both contract versions. Every filter is optional; with none, every liquidity event arrives. These events never appear on subscribeSwaps. There is no snapshot: read positions with getPositions. Deduplicate by transactionId plus positionId: a v1 BatchSubmit can hold several operations under one transaction id.

Parameters​

ParameterType
cb(event) => void
opts?GdexSubscribeOptions & GdexLiquidityFilter

Returns​

Promise<GdexSubscription>


subscribePool()​

subscribePool(selector, cb, opts?): Promise<GdexSubscription>

State changes of one pool (price, tick, reserves, liquidity). These events carry no acting wallet: on v2 creator is whoever created the pool, not whoever just traded or added liquidity. For "who did it", use subscribeLiquidity (owner) or subscribeSwaps (sender).

Parameters​

ParameterType
selectorGdexPoolSelector
cb(event) => void
opts?GdexSubscribeOptions

Returns​

Promise<GdexSubscription>


subscribePoolCreated()​

subscribePoolCreated(cb, opts?): Promise<GdexSubscription>

Each newly created pool.

Parameters​

ParameterType
cb(event) => void
opts?GdexSubscribeOptions & object

Returns​

Promise<GdexSubscription>


subscribeSwaps()​

subscribeSwaps(cb, opts?): Promise<GdexSubscription>

One event per committed swap. Omit the pool to receive every pool's swaps.

Parameters​

ParameterType
cb(event) => void
opts?GdexSubscribeOptions & object

Returns​

Promise<GdexSubscription>