Skip to content

Query 20 - Terminal USDM Provenance

Result

This table is the CSV result produced by Apache Jena over the state-audit graph. USDM quantities are decimal USDM.

terminalTxId terminalIx terminalUsdm producerKind producerNetworkUsdm swapOrderInputs swapOrderAda swapOrderRefs beneficiaryPaymentUsdm
68a1277af23755376967e788752c603044f45ea0d99220b3b5dfc7d617642b6b 1 5011.215241 swap-receipt 5011.215241 1 20411.443266 9f119393a85bb9aa0c94f8c649288dabb956b88dcbe055b10e741a2237123420#0 0.000000
affe90d1fa9a93b3e2a48009ef80634e9de8428640f5d673e85b002a86399982 0 1349.523953 beneficiary-payment-change 1349.523953 n/a n/a n/a 400000.000000
cda0126e9ea7b336bbb338d2bfc7622a41b584e3bebc33c9c320e8895b9bc082 1 10.439974 swap-receipt 20.879498 2 85.783609 10a5c1dafe7dd8d4ab680e35dc53b8b550da90bea55f2c758f36474064f2e598#0, 10a5c1dafe7dd8d4ab680e35dc53b8b550da90bea55f2c758f36474064f2e598#1 0.000000
cda0126e9ea7b336bbb338d2bfc7622a41b584e3bebc33c9c320e8895b9bc082 2 10.439524 swap-receipt 20.879498 2 85.783609 10a5c1dafe7dd8d4ab680e35dc53b8b550da90bea55f2c758f36474064f2e598#0, 10a5c1dafe7dd8d4ab680e35dc53b8b550da90bea55f2c758f36474064f2e598#1 0.000000

Summed terminal USDM:

5,011.215241 + 1,349.523953 + 10.439974 + 10.439524
= 6,381.618692

What

This query explains the terminal USDM UTxOs that make up the remaining 6,381.618692 USDM in the network_compliance treasury. It excludes terminal ADA-only outputs and focuses on UTxOs that still carry USDM.

Why

Query 14 proves the terminal state. Query 17 proves the aggregate accounting equation. Query 20 answers the provenance question for the remaining USDM: which producer transaction created each terminal USDM UTxO, and what kind of producer was it?

The result shows that the remaining USDM is split across:

  • two swap-receipt producers, and
  • one beneficiary-payment transaction's treasury change output.

Diagram

flowchart LR
  orders[Sundae order inputs]
  swapProducers[Swap receipt producers]
  payProducer[Beneficiary payment producer]
  terminal[Terminal USDM UTxOs]
  remain[6,381.618692 USDM remains]

  orders --> swapProducers
  swapProducers --> terminal
  payProducer --> terminal
  terminal --> remain

How

The terminal subquery finds every USDM-bearing output at network_compliance that is not spent by any loaded transaction. That is the graph-derived final-state test.

A producer summary subquery computes the total network_compliance USDM created by the same producer transaction. This matters when one producer creates more than one terminal output.

An optional swap subquery classifies a producer as swap-receipt when it consumes one or more SundaeSwap V3 order script outputs.

An optional payment subquery classifies a producer as beneficiary-payment-change when the same transaction also emits USDM to amaru.cag-payee.

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/final-network-compliance-state/20-terminal-usdm-provenance.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#>

# Provenance for each terminal network_compliance UTxO that still holds USDM.
#
# Query 14 proves the terminal UTxO set. Query 17 proves the aggregate
# accounting equation. This query explains the USDM-bearing terminal outputs:
# which transaction produced each remaining UTxO, how much USDM the producer
# returned to network_compliance, and whether that producer looks like a swap,
# a beneficiary-payment change transaction, or plain treasury change.
#
# The producer kind is inferred only from graph structure:
#
# - swap-receipt: the producer consumes at least one Sundae V3 order output;
# - beneficiary-payment-change: the producer emits USDM to the CAG payee;
# - treasury-change: neither of the above is visible in the loaded graph.
SELECT ?terminalTxId ?terminalIx
       ((xsd:decimal(?terminalUsdmRaw) / 1000000) AS ?terminalUsdm)
       ?producerKind
       ((xsd:decimal(?producerNetworkUsdmRaw) / 1000000) AS ?producerNetworkUsdm)
       ?swapOrderInputs
       ((xsd:decimal(?swapOrderLovelace) / 1000000) AS ?swapOrderAda)
       ?swapOrderRefs
       ((xsd:decimal(COALESCE(?beneficiaryPaymentUsdmRaw, 0)) / 1000000) AS ?beneficiaryPaymentUsdm)
WHERE {
  {
    # Base terminal-state subquery. A terminal output is a network_compliance
    # output carrying USDM whose exact (tx id, index) is not consumed anywhere
    # in the loaded graph.
    SELECT ?producer ?terminalTxId ?terminalIx ?terminalUsdmRaw
    WHERE {
      # Full USDM asset id, not ticker.
      VALUES ?usdmAssetId {
        "c48cbb3d5e57ed56e276bc45f99ab39abe94e6cd7ac39fb402da47ad0014df105553444d"
      }

      # Resolve the treasury address through the semantic label.
      {
        SELECT DISTINCT ?networkComplianceBech32
        WHERE {
          ?networkCompliance rdfs:label "amaru-treasury.network_compliance" ;
                             cardano:bech32 ?networkComplianceBech32 .
        }
      }

      ?producer cardano:hasTxId/cardano:bytesHex ?terminalTxId ;
                cardano:hasOutput ?terminalOut .
      ?terminalOut cardano:hasIndex ?terminalIx ;
                   cardano:atAddress/cardano:bech32 ?networkComplianceBech32 ;
                   cardano:hasAssetValue/rdf:rest*/rdf:first ?asset .
      ?asset cardano:hasIdentifier/cardano:bytesHex ?usdmAssetId ;
             cardano:quantity ?terminalUsdmRaw .
      FILTER(?terminalUsdmRaw > 0)

      # UTxO finality test: if any loaded transaction references this output
      # as an input, it is not part of the terminal state.
      FILTER NOT EXISTS {
        ?spendingTx cardano:hasInput ?input .
        ?input cardano:fromTxOutRef ?ref .
        ?ref cardano:hasTxId/cardano:bytesHex ?terminalTxId ;
             cardano:hasIndex ?terminalIx .
      }
    }
  }
  {
    # Producer summary. A producer can create more than one
    # network_compliance USDM output, so keep the producer-level total beside
    # each terminal output row.
    SELECT ?producer (SUM(?qty) AS ?producerNetworkUsdmRaw)
    WHERE {
      # Same USDM asset id used by the terminal-state subquery.
      VALUES ?usdmAssetId {
        "c48cbb3d5e57ed56e276bc45f99ab39abe94e6cd7ac39fb402da47ad0014df105553444d"
      }

      # Same semantic treasury address.
      {
        SELECT DISTINCT ?networkComplianceBech32
        WHERE {
          ?networkCompliance rdfs:label "amaru-treasury.network_compliance" ;
                             cardano:bech32 ?networkComplianceBech32 .
        }
      }

      ?producer 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 .
    }
    GROUP BY ?producer
  }
  OPTIONAL {
    # Optional swap classifier. If the producer consumed Sundae V3 order
    # outputs, report the count, total order lovelace, and exact order refs.
    SELECT ?producer (COUNT(?orderRef) AS ?swapOrderInputs)
           (SUM(?oneOrderLovelace) AS ?swapOrderLovelace)
           (GROUP_CONCAT(?orderRef; separator=", ") AS ?swapOrderRefs)
    WHERE {
      # Resolve script hash by label rather than duplicating the hash.
      {
        SELECT DISTINCT ?orderScriptHash
        WHERE {
          ?orderScript rdfs:label "sundae.swap.v3.order" ;
                       cardano:hasIdentifier/cardano:bytesHex ?orderScriptHash .
        }
      }
      ?producer 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 .

      # Display/audit handle for the consumed order output.
      BIND(CONCAT(?orderTxId, "#", STR(?orderIx)) AS ?orderRef)
    }
    GROUP BY ?producer
  }
  OPTIONAL {
    # Optional beneficiary-payment classifier. If the same producer also emits
    # USDM to the CAG payee bridge, the remaining network_compliance output is
    # treasury change from a beneficiary payment transaction.
    SELECT ?producer (SUM(?qty) AS ?beneficiaryPaymentUsdmRaw)
    WHERE {
      # Same pinned asset id.
      VALUES ?usdmAssetId {
        "c48cbb3d5e57ed56e276bc45f99ab39abe94e6cd7ac39fb402da47ad0014df105553444d"
      }

      # Resolve the CAG payee bridge address by label.
      {
        SELECT DISTINCT ?payeeBech32
        WHERE {
          ?payee rdfs:label "amaru.cag-payee" ;
                 cardano:bech32 ?payeeBech32 .
        }
      }

      ?producer cardano:hasOutput ?out .
      ?out cardano:atAddress/cardano:bech32 ?payeeBech32 ;
           cardano:hasAssetValue/rdf:rest*/rdf:first ?asset .
      ?asset cardano:hasIdentifier/cardano:bytesHex ?usdmAssetId ;
             cardano:quantity ?qty .
    }
    GROUP BY ?producer
  }

  # OPTIONAL variables are unbound when the producer does not match that
  # classifier. COALESCE turns them into zero for deterministic IF tests.
  BIND(COALESCE(?swapOrderInputs, 0) AS ?swapInputs)
  BIND(COALESCE(?beneficiaryPaymentUsdmRaw, 0) AS ?beneficiaryPaid)

  # Classification precedence matters: a swap producer that also happens to
  # have other outputs should still be described by the order-consumption fact.
  BIND(
    IF(
      ?swapInputs > 0,
      "swap-receipt",
      IF(?beneficiaryPaid > 0, "beneficiary-payment-change", "treasury-change")
    ) AS ?producerKind
  )
}
ORDER BY DESC(?terminalUsdmRaw) ?terminalTxId ?terminalIx