Skip to content

Query 19 - Swap Receipts And Rates

Result

This summary is computed from the 51 rows returned by the query. ADA quantities are decimal ADA; USDM quantities are decimal USDM; rates are parts per million USDM per ADA.

rows totalOrderAda totalReceivedUsdm minRatePpm maxRatePpm weightedRatePpm
51 1654998.000000 425131.618692 243397 263822 256877

Selected rows from the same result set:

swapTxId orderInputs orderAda receivedUsdm realizedUsdmPerAdaPpm orderRefs
26542f223ee27990e35555a7a328299c61e6f802b075b1e00b01befcdb597871 1 38120.299249 10056.971059 263822 22e914892e83c22e19514937914ca32a0c059f9d1c5b555429edde0ea3406ae4#1
68a1277af23755376967e788752c603044f45ea0d99220b3b5dfc7d617642b6b 1 20411.443266 5011.215241 245510 9f119393a85bb9aa0c94f8c649288dabb956b88dcbe055b10e741a2237123420#0
cda0126e9ea7b336bbb338d2bfc7622a41b584e3bebc33c9c320e8895b9bc082 2 85.783609 20.879498 243397 10a5c1dafe7dd8d4ab680e35dc53b8b550da90bea55f2c758f36474064f2e598#1, 10a5c1dafe7dd8d4ab680e35dc53b8b550da90bea55f2c758f36474064f2e598#0

The full runnable query returns all 51 swap receipt rows with their order references.

What

This query lists each SundaeSwap V3 order consumer that returned USDM to the network_compliance treasury. For each swap receipt it reports:

  • the consumer transaction id,
  • the number of consumed order inputs,
  • the ADA locked in those order inputs,
  • the USDM returned to the treasury,
  • the realized USDM-per-ADA rate in parts per million.

245000 in the rate column means 0.245000 USDM / ADA.

Why

The accounting query says the treasury received 425,131.618692 USDM from swaps. Query 19 shows the individual swap receipts and their realized rates, so the aggregate is inspectable instead of a black-box sum.

This is also a rate sanity check. The realized rates cluster around the submitted floor and the actual scoop outcomes; outliers are visible by sorting or filtering the returned rows.

Diagram

flowchart LR
  orders[Sundae V3 order outputs]
  consumers[Order consumer txs]
  receipts[USDM outputs to network_compliance]
  rate[received USDM / order ADA]
  table[Swap-rate rows]

  orders --> consumers
  consumers --> receipts
  orders --> rate
  receipts --> rate
  rate --> table

How

The query has two subqueries joined by transaction node.

The first subquery finds producer transactions that consume outputs at the SundaeSwap V3 order script hash. It sums the ADA at those consumed order UTxOs and records the consumed order references.

The second subquery finds USDM outputs from the same producer transactions back to the network_compliance treasury address.

The final projection computes:

round(receivedUsdm * 1,000,000 / orderLovelace)

Because both ADA and USDM use six decimal places, that ratio is USDM per ADA in parts per million.

Run

From the repository root, run this query through the tutorial setup script:

bash docs/may-2026-amaru-lattice/setup.sh \
  docs/may-2026-amaru-lattice/swaps-and-exchange-rates/19-swap-receipts-and-rates.rq

SPARQL

PREFIX cardano: <https://lambdasistemi.github.io/cardano-knowledge-maps/vocab/cardano#>
PREFIX rdf:     <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs:    <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd:     <http://www.w3.org/2001/XMLSchema#>

# Each SundaeSwap V3 order consumer that returns USDM to the
# network_compliance treasury, with its graph-derived effective rate.
#
# This is the receipt-side drill-down for Query 17. It answers:
#
# - which transactions consumed Sundae V3 order UTxOs,
# - how much ADA those consumed order UTxOs carried,
# - how much USDM the same transaction returned to network_compliance,
# - and the realized USDM/ADA rate implied by those two ledger values.
#
# realizedUsdmPerAdaPpm is parts-per-million USDM per ADA:
#   245000 means 0.245000 USDM / ADA.
SELECT ?swapTxId ?orderInputs
       ((xsd:decimal(?orderLovelace) / 1000000) AS ?orderAda)
       ((xsd:decimal(?receivedUsdmRaw) / 1000000) AS ?receivedUsdm)
       (xsd:integer(ROUND((xsd:decimal(?receivedUsdmRaw) * 1000000) / xsd:decimal(?orderLovelace))) AS ?realizedUsdmPerAdaPpm)
       ?orderRefs
WHERE {
  {
    # First half of the join: identify swap consumer transactions by their
    # inputs. A transaction is in scope when it consumes one or more UTxOs at
    # the SundaeSwap V3 order script hash.
    SELECT ?swapTx ?swapTxId (COUNT(?orderRef) AS ?orderInputs)
           (SUM(?oneOrderLovelace) AS ?orderLovelace)
           (GROUP_CONCAT(?orderRef; separator=", ") AS ?orderRefs)
    WHERE {
      # Script hash comes from the operator overlay/rules graph. The query
      # therefore follows the named script entity, not a hand-copied hash.
      {
        SELECT DISTINCT ?orderScriptHash
        WHERE {
          ?orderScript rdfs:label "sundae.swap.v3.order" ;
                       cardano:hasIdentifier/cardano:bytesHex ?orderScriptHash .
        }
      }
      ?swapTx cardano:hasTxId/cardano:bytesHex ?swapTxId ;
              cardano:hasInput ?in .
      ?in cardano:fromTxOutRef ?ref .
      ?ref cardano:hasTxId/cardano:bytesHex ?orderTxId ;
           cardano:hasIndex ?orderIx .
      ?orderTx cardano:hasTxId/cardano:bytesHex ?orderTxId ;
               cardano:hasOutput ?orderOut .
      ?orderOut cardano:hasIndex ?orderIx ;
                cardano:atAddress/cardano:hasPaymentCredential/cardano:hasIdentifier/cardano:bytesHex
                  ?orderScriptHash ;
                cardano:lovelace ?oneOrderLovelace .

      # The reference string is display/audit material. It lets a reader jump
      # from an aggregate swap row back to the consumed order outputs.
      BIND(CONCAT(?orderTxId, "#", STR(?orderIx)) AS ?orderRef)
    }

    # One row per consumer transaction. The rate denominator is the total ADA
    # locked in all order inputs consumed by that transaction.
    GROUP BY ?swapTx ?swapTxId
  }
  {
    # Second half of the join: from those same producer transactions, sum the
    # USDM outputs that return to the network_compliance treasury.
    SELECT ?swapTx (SUM(?qty) AS ?receivedUsdmRaw)
    WHERE {
      # Full asset id, not ticker.
      VALUES ?usdmAssetId {
        "c48cbb3d5e57ed56e276bc45f99ab39abe94e6cd7ac39fb402da47ad0014df105553444d"
      }

      # Resolve the receiving treasury address by label.
      {
        SELECT DISTINCT ?networkComplianceBech32
        WHERE {
          ?networkCompliance rdfs:label "amaru-treasury.network_compliance" ;
                             cardano:bech32 ?networkComplianceBech32 .
        }
      }
      ?swapTx cardano:hasOutput ?out .
      ?out cardano:atAddress/cardano:bech32 ?networkComplianceBech32 ;
           cardano:hasAssetValue/rdf:rest*/rdf:first ?asset .
      ?asset cardano:hasIdentifier/cardano:bytesHex ?usdmAssetId ;
             cardano:quantity ?qty .
    }

    # Several output asset-value nodes from the same producer transaction are
    # part of one realized swap receipt total.
    GROUP BY ?swapTx
  }
}
ORDER BY ?swapTxId