Get Orderbook
dex.getOrderbook
Gets an orderbook pair summary.
Usage
import { } from './viem.config'
const = await ..({
: '0x20c0000000000000000000000000000000000001',
: '0x20c0000000000000000000000000000000000000',
})
.('Orderbook:', )
Orderbook: { base: '0x20c0000000000000000000000000000000000001', quote: '0x20c0000000000000000000000000000000000000', bestBidTick: -100, bestAskTick: 100 }import { , } from 'viem/tempo'
export const = .({
: .('0x...'),
})Recipes
Display Top-of-Book Prices and the Spread
Use Tick.toPrice to convert the best ticks into quoted prices and derive the spread (one tick is 0.1 basis points) for a market dashboard.
import { } from 'viem/tempo'
import { } from './viem.config'
const = await ..({
: '0x20c0000000000000000000000000000000000001',
: '0x20c0000000000000000000000000000000000000',
})
const = .(.)
const = .(.)
const = (. - .) / 10
.(`Bid ${} / Ask ${} / Spread ${} bps`)
Bid 0.999 / Ask 1.001 / Spread 20 bpsimport { , } from 'viem/tempo'
export const = .({
: .('0x...'),
})Sample Bid Depth for a Depth Chart
Combine dex.getOrderbook with dex.getTickLevel to read resting liquidity below the best bid, stepping by the 10-tick (1 basis point) grid orders rest on.
import { } from './viem.config'
const = '0x20c0000000000000000000000000000000000001'
const = await ..({
,
: '0x20c0000000000000000000000000000000000000',
})
const = []
for (let = 0; < 5; ++) {
const = . - * 10
const = await ..({ , : true, })
.({ , : . })
}import { , } from 'viem/tempo'
export const = .({
: .('0x...'),
})Return Value
type ReturnType = {
/** Base token address. */
base: Address
/** Quote token address. */
quote: Address
/** Best bid tick. */
bestBidTick: number
/** Best ask tick. */
bestAskTick: number
}The orderbook base and quote token addresses with the best bid and ask ticks.
Parameters
base
- Type:
Address | bigint
Base token: a TIP-20 token id or a contract address.
quote
- Type:
Address | bigint
Quote token: a TIP-20 token id or a contract address.
blockNumber (optional)
- Type:
bigint
Block number to read the state from.
blockOverrides (optional)
- Type:
BlockOverrides
Block overrides to apply to the state.
blockTag (optional)
- Type:
BlockTag
Block tag to read the state from.
stateOverride (optional)
- Type:
StateOverride
State override to apply.