|
1 | 1 | -- Boundless On-chain: Current TVL |
2 | 2 | -- Panel type: counter |
3 | | --- Description: Total escrow balance currently held by the contract. |
| 3 | +-- Total escrow currently held by the contract = inflows - outflows. |
4 | 4 | -- |
5 | | --- Real Dune column names (stellar.history_contract_events): |
6 | | --- topics_decoded VARCHAR — JSON array; index 0 is the event-name symbol |
7 | | --- data_decoded VARCHAR — JSON object with all event fields |
| 5 | +-- Decoding (see 10_event_created_decode_test.sql for the why): |
| 6 | +-- event name -> topics_decoded '$[0].symbol' |
| 7 | +-- fields -> rebuild data_decoded '$.map' into MAP(name -> ScVal JSON), |
| 8 | +-- then read each field by its ScVal type ($.i128, $.u64, ...). |
8 | 9 | -- |
9 | | --- Inflows: |
10 | | --- EventCreated.total_budget (non-Crowdfunding pillars — escrowed at creation) |
11 | | --- FundsAdded.amount (partner top-ups and crowdfunding contributions) |
12 | | --- Outflows: |
13 | | --- WinnerPaid.amount (single-release payout at select_winners) |
14 | | --- MilestoneClaimed.amount (grant / crowdfunding milestone payout) |
15 | | --- ContributorRefunded.amount (partner refund during paged cancel) |
16 | | --- OwnerResidualRefunded.amount (owner residual at cancel) |
17 | | --- |
18 | | --- All amounts are net-of-fee (protocol fee is deducted before events fire). |
19 | | --- Divide by 1e7 to convert from stroops to USDC / XLM display units. |
| 10 | +-- Inflows: EventCreated.total_budget (non-Crowdfunding — escrowed at creation) |
| 11 | +-- FundsAdded.amount (partner top-ups + crowdfunding contributions) |
| 12 | +-- Outflows: WinnerPaid.amount, MilestoneClaimed.amount, |
| 13 | +-- ContributorRefunded.amount, OwnerResidualRefunded.amount |
| 14 | +-- Amounts are the values the contract actually escrows/releases (the protocol |
| 15 | +-- fee is charged separately, not embedded here). Divide by 1e7 for display. |
20 | 16 |
|
21 | | -WITH inflows AS ( |
22 | | - -- Non-crowdfunding events: budget deposited at creation |
23 | | - SELECT CAST(JSON_EXTRACT_SCALAR(data_decoded, '$.total_budget') AS DOUBLE) AS amount |
| 17 | +WITH ev AS ( |
| 18 | + SELECT |
| 19 | + JSON_EXTRACT_SCALAR(topics_decoded, '$[0].symbol') AS ev_name, |
| 20 | + map_from_entries( |
| 21 | + transform( |
| 22 | + CAST(JSON_EXTRACT(data_decoded, '$.map') AS ARRAY(JSON)), |
| 23 | + e -> ROW(JSON_EXTRACT_SCALAR(e, '$.key.symbol'), JSON_EXTRACT(e, '$.val')) |
| 24 | + ) |
| 25 | + ) AS f |
24 | 26 | FROM stellar.history_contract_events |
25 | 27 | WHERE contract_id = '{{CONTRACT_ADDRESS}}' |
26 | | - AND JSON_EXTRACT_SCALAR(topics_decoded, '$[0]') = 'EventCreated' |
27 | | - AND JSON_EXTRACT_SCALAR(data_decoded, '$.pillar') != 'Crowdfunding' |
28 | | - |
| 28 | + AND closed_at_date >= DATE '{{START_DATE}}' |
| 29 | + AND data_decoded LIKE '%"map"%' |
| 30 | +), |
| 31 | +inflows AS ( |
| 32 | + SELECT CAST(JSON_EXTRACT_SCALAR(f['total_budget'], '$.i128') AS DOUBLE) AS amount |
| 33 | + FROM ev |
| 34 | + WHERE ev_name = 'EventCreated' |
| 35 | + AND JSON_EXTRACT_SCALAR(f['pillar'], '$.vec[0].symbol') <> 'Crowdfunding' |
29 | 36 | UNION ALL |
30 | | - |
31 | | - -- All add_funds deposits (crowdfunding contributions + partner top-ups) |
32 | | - SELECT CAST(JSON_EXTRACT_SCALAR(data_decoded, '$.amount') AS DOUBLE) AS amount |
33 | | - FROM stellar.history_contract_events |
34 | | - WHERE contract_id = '{{CONTRACT_ADDRESS}}' |
35 | | - AND JSON_EXTRACT_SCALAR(topics_decoded, '$[0]') = 'FundsAdded' |
| 37 | + SELECT CAST(JSON_EXTRACT_SCALAR(f['amount'], '$.i128') AS DOUBLE) |
| 38 | + FROM ev |
| 39 | + WHERE ev_name = 'FundsAdded' |
36 | 40 | ), |
37 | 41 | outflows AS ( |
38 | | - SELECT CAST(JSON_EXTRACT_SCALAR(data_decoded, '$.amount') AS DOUBLE) AS amount |
39 | | - FROM stellar.history_contract_events |
40 | | - WHERE contract_id = '{{CONTRACT_ADDRESS}}' |
41 | | - AND JSON_EXTRACT_SCALAR(topics_decoded, '$[0]') IN ( |
42 | | - 'WinnerPaid', |
43 | | - 'MilestoneClaimed', |
44 | | - 'ContributorRefunded', |
45 | | - 'OwnerResidualRefunded' |
46 | | - ) |
| 42 | + SELECT CAST(JSON_EXTRACT_SCALAR(f['amount'], '$.i128') AS DOUBLE) AS amount |
| 43 | + FROM ev |
| 44 | + WHERE ev_name IN ('WinnerPaid', 'MilestoneClaimed', 'ContributorRefunded', 'OwnerResidualRefunded') |
47 | 45 | ) |
48 | 46 | SELECT |
49 | | - (COALESCE(SUM(i.amount), 0) - COALESCE(SUM(o.amount), 0)) / 1e7 AS tvl_usdc |
50 | | -FROM inflows i |
51 | | -FULL OUTER JOIN outflows o ON 1 = 1 |
| 47 | + (COALESCE((SELECT SUM(amount) FROM inflows), 0) |
| 48 | + - COALESCE((SELECT SUM(amount) FROM outflows), 0)) / 1e7 AS tvl_display |
0 commit comments