Verify
Checking quotes on-chain
Re-read a pool's slot0 at the recorded block and confirm sqrtPriceX96 and the derived price.
What you need#
- A quote from the API or an archive. It gives you
market,blockandsqrt_price. - A Base RPC endpoint that serves historical state (an archive node). Public endpoints usually only serve recent blocks.
Get some quotes:
bash
curl "https://dapp.forgeus.science/v1/quotes?market=ETH-USDC&limit=3"Read slot0 at the block#
slot0() has the selector 0x3850c7bd. The first 32 bytes of the result are sqrtPriceX96. Pass the block number as hex.
bash
RPC=https://your-base-archive-rpc.example # your Base archive RPC
POOL=0xd0b53D9277642d899DF5C87A3966A349A798F224 # ETH-USDC pool
BLOCK=$(printf '0x%x' 12345678) # the quote's block
curl -s "$RPC" -H 'content-type: application/json' --data '{
"jsonrpc": "2.0", "id": 1, "method": "eth_call",
"params": [{ "to": "'"$POOL"'", "data": "0x3850c7bd" }, "'"$BLOCK"'"]
}'The block number above is a placeholder. Use the block from the quote you are checking.
Compare sqrtPriceX96#
Take the first 64 hex characters after 0x and convert them to a decimal integer. It must equal the quote's sqrt_price.
check-quote.mjs·js
// Usage: node check-quote.mjs <eth_call result hex> <ETH-USDC|BTC-USDC>
const [hex, market] = process.argv.slice(2);
const sqrt = BigInt("0x" + hex.slice(2, 66));
console.log("sqrtPriceX96", sqrt.toString());
const M = {
"ETH-USDC": { dec0: 18, dec1: 6, invert: false },
"BTC-USDC": { dec0: 6, dec1: 8, invert: true },
}[market];
// Same steps as the engine.
const s = Number(sqrt) / 2 ** 96;
let p = s * s;
const shift = M.dec0 - M.dec1;
for (let i = 0; i < Math.abs(shift); i++) p = shift > 0 ? p * 10 : p / 10;
const price = Math.round((M.invert ? 1 / p : p) * 1e6) / 1e6;
console.log("price (USDC per base)", price);The price formula#
text
s = sqrtPriceX96 / 2^96
p = s^2 × 10^(dec0 - dec1)
price = invert ? 1/p : p, rounded to 6 decimals| Market | dec0 | dec1 | Shift | Invert | Why |
|---|---|---|---|---|---|
ETH-USDC | 18 (WETH) | 6 (USDC) | ×10^12 | no | token1 is USDC, so p is USDC per ETH |
BTC-USDC | 6 (USDC) | 8 (cbBTC) | ÷10^2 | yes | p is cbBTC per USDC, so invert to get USDC per BTC |
The timestamp#
The quote's ts is the block's timestamp. Check it with eth_getBlockByNumber on the same block.
bash
curl -s "$RPC" -H 'content-type: application/json' --data '{
"jsonrpc": "2.0", "id": 1, "method": "eth_getBlockByNumber",
"params": ["'"$BLOCK"'", false]
}'The timestamp field is hex seconds.
Paper only. No funds are traded. $FOUS is not launched. Nothing here is financial advice.