Skip to content

Query 10 - Swap Consumer Output Roles

Result

ADA quantities are decimal ADA. USDM quantities are decimal USDM.

outputRole outputs ada usdm
wallet.other 249 96254260.243609 24525287.591011
amaru-treasury.network_compliance 55 52949.457333 425131.618692
amaru.network-operator 1 19.755739 0.000000

What

This query looks at all outputs produced by the 51 SundaeSwap V3 order consumer transactions and groups those outputs by resolved address role.

It is wider than Query 19. Query 19 only looks at USDM returned to network_compliance; this query shows what else those same swap-consumer transactions emitted.

Why

The key line is amaru-treasury.network_compliance: 55 outputs carrying 425,131.618692 USDM. That agrees with Query 17 and Query 19.

The wallet.other row is not treasury income. It is the rest of the swap-consumer output surface, grouped as unlabeled wallet/script addresses because those addresses are not declared in rules.yaml.

Diagram

flowchart LR
  orders[Sundae V3 order inputs]
  consumers[Swap consumer txs]
  outputs[All consumer outputs]
  roles[Address roles]
  nc[network_compliance receipts]

  orders --> consumers
  consumers --> outputs
  outputs --> roles
  roles --> nc

How

The query first identifies swap consumers by proving that they consume outputs at the SundaeSwap V3 order script hash.

It then emits one row per produced output, resolves the output address role from the rules overlay when available, samples one role per output to avoid repeated overlay facts, and finally groups by role.

USDM is matched by the full asset id, not by ticker.

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/10-swap-consumer-output-roles.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#>

# Output roles produced by SundaeSwap V3 order consumer transactions.
SELECT ?outputRole
       (COUNT(DISTINCT ?outputRef) AS ?outputs)
       ((xsd:decimal(SUM(?lovelace)) / 1000000) AS ?ada)
       ((xsd:decimal(SUM(?usdm0)) / 1000000) AS ?usdm)
WHERE {
  {
    # Return one row per produced output, with a single sampled role. This
    # prevents repeated overlay entities from multiplying ledger quantities.
    SELECT ?outputRef
           (SAMPLE(?role0) AS ?outputRole)
           (SAMPLE(?oneLovelace) AS ?lovelace)
           (SAMPLE(?oneUsdmRaw) AS ?usdm0)
    WHERE {
      {
        SELECT DISTINCT ?swapTx
        WHERE {
          {
            # The script entity is emitted once per transaction graph. Use a
            # distinct script hash before asking which transactions consumed it.
            SELECT DISTINCT ?orderScriptHash
            WHERE {
              ?orderScript rdfs:label "sundae.swap.v3.order" ;
                           cardano:hasIdentifier/cardano:bytesHex ?orderScriptHash .
            }
          }
          ?swapTx 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 .
        }
      }
      ?swapTx cardano:hasTxId/cardano:bytesHex ?swapTxId ;
              cardano:hasOutput ?out .
      ?out cardano:hasIndex ?ix ;
           cardano:atAddress ?addr ;
           cardano:lovelace ?oneLovelace .
      ?addr cardano:bech32 ?bech32 .
      OPTIONAL {
        ?addr cardano:hasPaymentCredential/cardano:hasIdentifier/cardano:bytesHex ?payHash .
      }
      OPTIONAL {
        VALUES ?usdmAssetId0 {
          "c48cbb3d5e57ed56e276bc45f99ab39abe94e6cd7ac39fb402da47ad0014df105553444d"
        }
        ?out cardano:hasAssetValue/rdf:rest*/rdf:first ?asset .
        ?asset cardano:hasIdentifier/cardano:bytesHex ?usdmAssetId0 ;
               cardano:quantity ?usdmRaw .
      }
      BIND(COALESCE(?usdmRaw, 0) AS ?oneUsdmRaw)
      BIND(CONCAT(?swapTxId, "#", STR(?ix)) AS ?outputRef)

      OPTIONAL {
        SELECT DISTINCT ?bech32 ?addressRole
        WHERE {
          ?entity cardano:bech32 ?bech32 ;
                  rdfs:label ?addressRole .
        }
      }
      OPTIONAL {
        SELECT DISTINCT ?payHash ?credentialRole
        WHERE {
          ?credEntity cardano:hasIdentifier/cardano:bytesHex ?payHash ;
                      rdfs:label ?credentialRole .
        }
      }
      BIND(COALESCE(?addressRole, ?credentialRole, "wallet.other") AS ?role0)
    }
    GROUP BY ?outputRef
  }
}
GROUP BY ?outputRole
ORDER BY DESC(?usdm) DESC(?ada) ?outputRole