Swap to candle
One venue, one pipeline, every step a view running in this tab. Place a swap and follow it.
Click Buy or Sell, then follow your swap through each view.
-How it works
Trades arrive as raw integers and flags, and your app wants candles. Usually that means a batch job. Here every step is a view that keeps itself up to date.
1Your swap lands as a raw venue row: integer amounts and a direction flag.
create table market::raw_swaps { id: int8, slot: int8, pool: utf8, amount0: int8, amount1: int8, zero_for_one: bool }2A deferred view joins the pool's decimals and turns it into a trade with a price.
create deferred view market::normalized { id: int8, slot: int8, venue: utf8, price: float8, base_volume: float8, quote_volume: float8, is_buy: bool } as { from market::raw_swaps inner join { from market::pools } as p using (pool, p.pool) map { id, slot, venue: "rdb-usd", base_volume: cast(amount0, float8) / math::power(10.0, cast(p_base_decimals, float8)), quote_volume: cast(amount1, float8) / math::power(10.0, cast(p_quote_decimals, float8)), is_buy: zero_for_one == false } extend { price: quote_volume / base_volume } }3Each later view builds on the one before: per-slot VWAP, then volume, TWAP and candles per 10 slots.
create deferred view market::candles { bucket: int8, open: float8, high: float8, low: float8, close: float8 } as { from market::candle_span inner join { from market::block } as o using (first_slot, o.slot) inner join { from market::block } as c using (last_slot, c.slot) map { bucket, open: o_vwap, high, low, close: c_vwap } }4Nothing refreshes them. Your app just reads market::candles.
from market::candles sort { bucket } take 8
Subscribe to the candles
create subscription as {
from market::candles
take 10
}Waiting for the first read...
Read the live tables
Integer amounts and a direction flag, before any view touches them.
from market::raw_swaps sort { id } take 4The same trades, filtered on the fly. Press Buy and run it again.
from market::trades filter { is_buy == true } sort { id } take 525 RDB or more. Buy 25 or 100 to see your own swap here.
from market::trades filter { base_volume >= 25.0 } sort { id } take 5Slots with the most swaps, read from the per-slot view.
from market::block sort { swaps } take 3The share of volume that was buying, computed at read time.
from market::volume map { bucket, buy_share: buy_volume / volume } take 2How far price moved inside each candle.
from market::candles extend { range: high - low } map { bucket, open, close, range } take 8One row over every trade.
from market::trades aggregate { swaps: math::count(id), volume: math::sum(quote_volume) }