Watch AMM Rebalance Swaps
amm.watchRebalanceSwap
Watches RebalanceSwap events on the fee AMM.
Usage
import { } from './viem.config'
const = ..()
.(() => {
for (const of ) .(.)
{ userToken: '0x20c0000000000000000000000000000000000001', validatorToken: '0x20c0000000000000000000000000000000000002', swapper: '0x742d35Cc6634C0532925a3b844Bc9e7595f0bEbb' }
})
// Later, tear down the watcher.
.()import { , } from 'viem/tempo'
export const = .({
: .('0x...'),
})Also callable standalone: Actions.amm.watchRebalanceSwap(client, options), with Actions imported from 'viem/tempo'.
Recipes
Monitor Arbitrage Flow Through a Pool
Filter args.userToken and args.validatorToken to a pool you provide liquidity for, so you can track each rebalance and who performed it.
import { } from './viem.config'
const = ..({
: {
: '0x20c0000000000000000000000000000000000001',
: '0x20c0000000000000000000000000000000000002',
},
})
.(() => {
for (const of )
.('Rebalance:', .., ..)
Rebalance: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 1001000n
})import { , } from 'viem/tempo'
export const = .({
: .('0x...'),
})Refresh Reserve Metrics on Each Rebalance
Combine the watcher with amm.getPool to re-read reserves whenever a swap lands, keeping reserve metrics current without polling.
import { } from './viem.config'
const = '0x20c0000000000000000000000000000000000001'
const = '0x20c0000000000000000000000000000000000002'
const = ..({
: { , },
})
.(async () => {
const = await ..({ , })
.('Reserves:', ., .)
})import { , } from 'viem/tempo'
export const = .({
: .('0x...'),
})Return Value
Returns a watcher handle. Watching starts when the first listener (or async iterator) attaches, and stops when off is called.
type ReturnType = {
/** Registers a log listener. Starts the watcher on first registration. Returns an unregister function. */
onLogs: (fn: (logs: readonly Log[]) => void) => () => void
/** Registers an error listener. Returns an unregister function. */
onError: (fn: (error: Error) => void) => () => void
/** Tears down the watcher: removes listeners, ends iterators, stops the poll or subscription. */
off: () => void
/** Async-iterates emitted log batches (latest-only stream). */
[Symbol.asyncIterator]: () => AsyncIterableIterator<{ logs: readonly Log[] }>
}Each log is decoded, with the event arguments on args:
type Log = {
/** Decoded `RebalanceSwap` event arguments. */
args: {
/** User token address. */
userToken: Address
/** Validator token address. */
validatorToken: Address
/** Address that performed the swap. */
swapper: Address
/** Amount of validator token spent. */
amountIn: bigint
/** Amount of user token received. */
amountOut: bigint
}
/** Name of the emitted event. */
eventName: 'RebalanceSwap'
// ...standard log fields (address, blockHash, blockNumber, logIndex, transactionHash, ...)
}Parameters
args
- Type:
object
type Args = {
/** Filter by user token address. */
userToken?: Address | Address[] | null
/** Filter by validator token address. */
validatorToken?: Address | Address[] | null
/** Filter by address that performed the swap. */
swapper?: Address | Address[] | null
}Indexed argument values to filter logs by.
batch
- Type:
boolean - Default:
true
Whether to batch the logs found within a poll interval into a single emission. When false, each log is emitted on its own.
fromBlock
- Type:
bigint
Block number from which to start watching for logs.
poll
- Type:
boolean
Whether to poll for new logs instead of using a subscription. Defaults to true when the transport cannot subscribe or fromBlock is provided.
pollingInterval
- Type:
number - Default:
client.pollingInterval
Polling frequency (in ms).