feat(api): index swaps settled by NEAR Intents, off by default

The price of a token on NEAR Intents is the ratio inside a trade that
`intents.near` actually executed; there is no pool, oracle or stored price,
and a 1Click quote is an offer. This reads `execute_intents` transactions on
the contract's shard, keeps the `token_diff` legs whose receipt succeeded and
whose intent is in `intents_executed`, and records one trade per asset/counter
pair per outcome, counting each unit once whichever side the taker was on.

- core: event parsing, trade building, shard lookup, price and VWAP, tested
  against the logs of mainnet tx 85uy9uCF (USDT on Tron for ZEC).
- data: a NEAR client over the existing RpcClient, live then archival;
  migration 0009 (intents_trade, intents_scan, near_token).
- api: a contiguous-cursor scan and a supply/listing poll per chain, and
  GET /v1/chains/{chain}/intents/{swaps,price}. Prices are in counter-token
  units; no USD until a stablecoin is named by contract.

Gated on `[chains.intents]`, which the config template leaves commented out:
FastNEAR's free RPC sustains about two requests a second against ~1.7 needed
to follow the tip, and neardata allows 30 blocks a minute. It waits for our
own node. A test renders the template and fails if the block is enabled.

Verified end to end against mainnet with ZEC configured: the trade at NEAR
218,261,895 was recorded and served by both routes.

Refs #27

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015pkwBsR54NRmZyU4QVhvfj
This commit is contained in:
2026-10-03 18:35:32 +03:00
co-authored by Claude Opus 5.5
parent dc68fe3be3
commit 8128201ad9
32 changed files with 2670 additions and 0 deletions
@@ -0,0 +1,28 @@
{
"db_name": "PostgreSQL",
"query": "select symbol, decimals from near_token where token_id = $1",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "symbol",
"type_info": "Text"
},
{
"ordinal": 1,
"name": "decimals",
"type_info": "Int2"
}
],
"parameters": {
"Left": [
"Text"
]
},
"nullable": [
false,
false
]
},
"hash": "07052e42232e1816532a2652cf17ae3e55e7fb3b5dcffeea482a52f33624de5c"
}
@@ -0,0 +1,116 @@
{
"db_name": "PostgreSQL",
"query": "\n select chain, asset, near_height, trade_index, at, tx_hash, receipt_id,\n counter_token, asset_amount::text as \"asset_amount!\",\n counter_amount::text as \"counter_amount!\", side, taker, referral,\n accounts, intent_hashes, legs\n from intents_trade\n where chain = $1 and asset = $2 and (near_height, trade_index) > ($3, $4)\n order by near_height, trade_index\n limit $5\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "chain",
"type_info": "Text"
},
{
"ordinal": 1,
"name": "asset",
"type_info": "Text"
},
{
"ordinal": 2,
"name": "near_height",
"type_info": "Int8"
},
{
"ordinal": 3,
"name": "trade_index",
"type_info": "Int4"
},
{
"ordinal": 4,
"name": "at",
"type_info": "Timestamptz"
},
{
"ordinal": 5,
"name": "tx_hash",
"type_info": "Text"
},
{
"ordinal": 6,
"name": "receipt_id",
"type_info": "Text"
},
{
"ordinal": 7,
"name": "counter_token",
"type_info": "Text"
},
{
"ordinal": 8,
"name": "asset_amount!",
"type_info": "Text"
},
{
"ordinal": 9,
"name": "counter_amount!",
"type_info": "Text"
},
{
"ordinal": 10,
"name": "side",
"type_info": "Text"
},
{
"ordinal": 11,
"name": "taker",
"type_info": "Text"
},
{
"ordinal": 12,
"name": "referral",
"type_info": "Text"
},
{
"ordinal": 13,
"name": "accounts",
"type_info": "TextArray"
},
{
"ordinal": 14,
"name": "intent_hashes",
"type_info": "TextArray"
},
{
"ordinal": 15,
"name": "legs",
"type_info": "Jsonb"
}
],
"parameters": {
"Left": [
"Text",
"Text",
"Int8",
"Int4",
"Int8"
]
},
"nullable": [
false,
false,
false,
false,
false,
false,
false,
false,
null,
null,
true,
true,
true,
false,
false,
false
]
},
"hash": "0c9cb8089edbad6caa8b933e334317f54b0081aa5f4f3caa5cef2a73321835d2"
}
@@ -0,0 +1,29 @@
{
"db_name": "PostgreSQL",
"query": "select low, high from intents_scan where chain = $1 and asset = $2",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "low",
"type_info": "Int8"
},
{
"ordinal": 1,
"name": "high",
"type_info": "Int8"
}
],
"parameters": {
"Left": [
"Text",
"Text"
]
},
"nullable": [
false,
false
]
},
"hash": "31bf30fc5a538346f8187e3f069e1c872e31f9ba103b27fdec00f52f1a9b7417"
}
@@ -0,0 +1,29 @@
{
"db_name": "PostgreSQL",
"query": "\n insert into intents_trade (\n chain, asset, near_height, trade_index, at, tx_hash, receipt_id,\n counter_token, asset_amount, counter_amount, side, taker, referral,\n accounts, intent_hashes, legs\n ) values ($1, $2, $3, $4, $5, $6, $7, $8, $9::text::numeric, $10::text::numeric,\n $11, $12, $13, $14, $15, $16)\n on conflict (chain, asset, near_height, trade_index) do update set\n at = excluded.at, tx_hash = excluded.tx_hash,\n receipt_id = excluded.receipt_id, counter_token = excluded.counter_token,\n asset_amount = excluded.asset_amount,\n counter_amount = excluded.counter_amount, side = excluded.side,\n taker = excluded.taker, referral = excluded.referral,\n accounts = excluded.accounts, intent_hashes = excluded.intent_hashes,\n legs = excluded.legs\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Text",
"Text",
"Int8",
"Int4",
"Timestamptz",
"Text",
"Text",
"Text",
"Text",
"Text",
"Text",
"Text",
"Text",
"TextArray",
"TextArray",
"Jsonb"
]
},
"nullable": []
},
"hash": "52a060c30ca48a63d2fe9dafaa4df386ee582a83346e5c560dfcec5b00b3f26c"
}
@@ -0,0 +1,17 @@
{
"db_name": "PostgreSQL",
"query": "\n insert into intents_scan (chain, asset, low, high) values ($1, $2, $3, $4)\n on conflict (chain, asset) do update\n set low = least(intents_scan.low, excluded.low),\n high = greatest(intents_scan.high, excluded.high),\n updated_at = now()\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Text",
"Text",
"Int8",
"Int8"
]
},
"nullable": []
},
"hash": "6b025ce59057975f0feb1e7f7687e065e0ccdf5c4aeb56690c9c15667b2a7314"
}
@@ -0,0 +1,16 @@
{
"db_name": "PostgreSQL",
"query": "\n insert into near_token (token_id, symbol, decimals) values ($1, $2, $3)\n on conflict (token_id) do update\n set symbol = excluded.symbol, decimals = excluded.decimals, fetched_at = now()\n ",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Text",
"Text",
"Int2"
]
},
"nullable": []
},
"hash": "7b002140797907f9344085c0f17e3098c659e998aaf0ce413c93080949db73f3"
}
@@ -0,0 +1,114 @@
{
"db_name": "PostgreSQL",
"query": "\n select chain as \"chain!\", asset as \"asset!\", near_height as \"near_height!\",\n trade_index as \"trade_index!\", at as \"at!\", tx_hash as \"tx_hash!\",\n receipt_id as \"receipt_id!\", counter_token as \"counter_token!\",\n asset_amount::text as \"asset_amount!\",\n counter_amount::text as \"counter_amount!\", side, taker, referral,\n accounts as \"accounts!\", intent_hashes as \"intent_hashes!\", legs as \"legs!\"\n from (\n select *, row_number() over (\n partition by counter_token order by near_height desc, trade_index desc\n ) as n\n from intents_trade\n where chain = $1 and asset = $2\n ) ranked\n where n <= $3\n order by counter_token, near_height desc, trade_index desc\n ",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "chain!",
"type_info": "Text"
},
{
"ordinal": 1,
"name": "asset!",
"type_info": "Text"
},
{
"ordinal": 2,
"name": "near_height!",
"type_info": "Int8"
},
{
"ordinal": 3,
"name": "trade_index!",
"type_info": "Int4"
},
{
"ordinal": 4,
"name": "at!",
"type_info": "Timestamptz"
},
{
"ordinal": 5,
"name": "tx_hash!",
"type_info": "Text"
},
{
"ordinal": 6,
"name": "receipt_id!",
"type_info": "Text"
},
{
"ordinal": 7,
"name": "counter_token!",
"type_info": "Text"
},
{
"ordinal": 8,
"name": "asset_amount!",
"type_info": "Text"
},
{
"ordinal": 9,
"name": "counter_amount!",
"type_info": "Text"
},
{
"ordinal": 10,
"name": "side",
"type_info": "Text"
},
{
"ordinal": 11,
"name": "taker",
"type_info": "Text"
},
{
"ordinal": 12,
"name": "referral",
"type_info": "Text"
},
{
"ordinal": 13,
"name": "accounts!",
"type_info": "TextArray"
},
{
"ordinal": 14,
"name": "intent_hashes!",
"type_info": "TextArray"
},
{
"ordinal": 15,
"name": "legs!",
"type_info": "Jsonb"
}
],
"parameters": {
"Left": [
"Text",
"Text",
"Int8"
]
},
"nullable": [
false,
false,
false,
false,
false,
false,
false,
false,
null,
null,
true,
true,
true,
false,
false,
false
]
},
"hash": "b02824ff4cc6414dc701b019123cf618a3cb4e4cca9bfe817fbf52686de85a9c"
}