> For the complete documentation index, see [llms.txt](https://docs.tradecraft.fi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tradecraft.fi/api/routes/swaps.md).

# Swaps

Everything needed to place a swap.

## Get a token's allocation factory context

> Returns the registry's allocation-factory contract for a token and the choice context\
> for exercising AllocationFactory\_Allocate on it. Cached, refreshed every 24 hours.\
> CC/Amulet is not served. Read it from the Splice Scan API per submission.<br>

```json
{"openapi":"3.0.3","info":{"title":"Tradecraft AMM HTTP API","version":"1.29.0"},"tags":[{"name":"Swaps","description":"Everything needed to place a swap."}],"servers":[{"url":"https://api.tradecraft.fi/v1","description":"Mainnet"},{"url":"https://tradecraft.validator.test.canton.obsidian.systems/amm-http-api","description":"Testnet"},{"url":"https://tradecraft.validator.dev.canton.obsidian.systems/amm-http-api","description":"Devnet"}],"paths":{"/allocation-factory/{token}":{"get":{"summary":"Get a token's allocation factory context","description":"Returns the registry's allocation-factory contract for a token and the choice context\nfor exercising AllocationFactory_Allocate on it. Cached, refreshed every 24 hours.\nCC/Amulet is not served. Read it from the Splice Scan API per submission.\n","operationId":"getAllocationFactoryContext","tags":["Swaps"],"parameters":[{"$ref":"#/components/parameters/token"}],"responses":{"200":{"description":"Allocation factory context","content":{"application/json":{"schema":{"$ref":"#/components/schemas/AllocationFactoryResponse"}}}},"400":{"description":"The token is missing, malformed, unknown, or is CC/Amulet","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Error"}}}},"503":{"description":"No context is available for this token right now.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Error"}}}}}}}},"components":{"parameters":{"token":{"name":"token","in":"path","required":true,"description":"The name/symbol of the token (URL encoded).","schema":{"type":"string"}}},"schemas":{"AllocationFactoryResponse":{"type":"object","description":"The registry's allocation-factory contract and the context for exercising it.","required":["factoryId","choiceContext","cachedAt"],"properties":{"factoryId":{"type":"string","description":"Contract ID to exercise AllocationFactory_Allocate on"},"choiceContext":{"type":"object","description":"The registry's choice-context envelope, not a Daml ChoiceContext.","required":["disclosedContracts","choiceContextData"],"properties":{"disclosedContracts":{"type":"array","description":"Disclosures for the submission, not for ExtraArgs","items":{"$ref":"#/components/schemas/DisclosedContract"}},"choiceContextData":{"type":"object","description":"The Daml ChoiceContext to pass as ExtraArgs.context"}}},"cachedAt":{"type":"string","format":"date-time","description":"When this context was last fetched from the registry"}}},"DisclosedContract":{"type":"object","description":"A disclosed contract blob from the Canton ledger, required for submitting transactions.","required":["templateId","contractId","createdEventBlob","synchronizerId"],"properties":{"templateId":{"type":"string","description":"Fully qualified template identifier (packageId:moduleName:entityName)"},"contractId":{"type":"string","description":"The contract ID on the ledger"},"createdEventBlob":{"type":"string","format":"byte","description":"Base64-encoded created event blob"},"synchronizerId":{"type":"string","description":"The synchronizer (domain) ID where this contract lives"}}},"Error":{"type":"object","required":["error"],"properties":{"error":{"type":"string","description":"Error message"}}}}}}
```

## Get everything needed to place one swap

> To swap by transfer:\
> \
> 1\. Send a token-standard transfer of \`amount\_in\` of \`in.instrument\_id\` to \`pool\_party\`.\
> 2\. Copy \`min\_out\` into the transfer metadata under the key \`memo\_key\`.\
> \
> The ledger refunds the transfer instead of filling it below \`min\_out\`.\
> A transfer without the memo key swaps with no floor.\
> \
> To swap directly, submit against \`amm\_cid\` with the contracts in\
> \`disclosures\` and \`min\_out\` as the floor. \`memo\_key\` does not apply.\
> \
> \`amm\_cid\`, the quote and \`disclosures\` come from one read, so they\
> always agree. All three change when the pool settles a batch, so\
> request a new ticket if the swap is delayed.\
> \
> \`pool\_party\` comes from the pool identity. The endpoint returns it even\
> if no magic-address watcher runs for the pair.\
> \
> \`disclosures\` carries the pool's contracts only. A CC transfer also\
> needs \`AmuletRules\` and a current \`OpenMiningRound\`. Read those from\
> the Splice Scan API per submission.\
> \
> Where enabled, a ticket costs an \[x402]\(<https://x402.org>) payment.\
> A request without one gets a 402 whose \`PAYMENT-REQUIRED\` header\
> lists the accepted prices. Retry with the signed payment in\
> \`PAYMENT-SIGNATURE\`. The payment settles only once the ticket is\
> built, so an error costs nothing. If the facilitator cannot be\
> reached, the answer is a 502: retry with the same payment.<br>

```json
{"openapi":"3.0.3","info":{"title":"Tradecraft AMM HTTP API","version":"1.29.0"},"tags":[{"name":"Swaps","description":"Everything needed to place a swap."}],"servers":[{"url":"https://api.tradecraft.fi/v1","description":"Mainnet"},{"url":"https://tradecraft.validator.test.canton.obsidian.systems/amm-http-api","description":"Testnet"},{"url":"https://tradecraft.validator.dev.canton.obsidian.systems/amm-http-api","description":"Devnet"}],"paths":{"/swap-ticket":{"get":{"summary":"Get everything needed to place one swap","description":"To swap by transfer:\n\n1. Send a token-standard transfer of `amount_in` of `in.instrument_id` to `pool_party`.\n2. Copy `min_out` into the transfer metadata under the key `memo_key`.\n\nThe ledger refunds the transfer instead of filling it below `min_out`.\nA transfer without the memo key swaps with no floor.\n\nTo swap directly, submit against `amm_cid` with the contracts in\n`disclosures` and `min_out` as the floor. `memo_key` does not apply.\n\n`amm_cid`, the quote and `disclosures` come from one read, so they\nalways agree. All three change when the pool settles a batch, so\nrequest a new ticket if the swap is delayed.\n\n`pool_party` comes from the pool identity. The endpoint returns it even\nif no magic-address watcher runs for the pair.\n\n`disclosures` carries the pool's contracts only. A CC transfer also\nneeds `AmuletRules` and a current `OpenMiningRound`. Read those from\nthe Splice Scan API per submission.\n\nWhere enabled, a ticket costs an [x402](https://x402.org) payment.\nA request without one gets a 402 whose `PAYMENT-REQUIRED` header\nlists the accepted prices. Retry with the signed payment in\n`PAYMENT-SIGNATURE`. The payment settles only once the ticket is\nbuilt, so an error costs nothing. If the facilitator cannot be\nreached, the answer is a 502: retry with the same payment.\n","operationId":"getSwapTicket","tags":["Swaps"],"parameters":[{"name":"PAYMENT-SIGNATURE","in":"header","required":false,"description":"Base64 JSON of an x402 v2 payment payload: a signed\n`TransferFactory_Transfer` paying one entry of the 402's\n`accepts`.\n","schema":{"type":"string"}},{"name":"in","in":"query","required":true,"description":"Symbol of the token to sell.","schema":{"type":"string"}},{"name":"out","in":"query","required":true,"description":"Symbol of the token to buy.","schema":{"type":"string"}},{"name":"amount","in":"query","required":true,"description":"Amount of the `in` token to sell. The endpoint cuts this to the\nten decimal places a Daml Decimal carries, then quotes the cut\namount as `amount_in`.\n","schema":{"type":"number","format":"double","minimum":0,"exclusiveMinimum":true}},{"name":"slippage","in":"query","required":true,"description":"Slippage tolerance as a fraction, not a percentage. 0.005 is 0.5%.\nRequired: a ticket with no floor can cost the caller money.\n","schema":{"type":"number","format":"double","minimum":0,"maximum":1,"exclusiveMaximum":true}}],"responses":{"200":{"description":"Swap ticket","headers":{"PAYMENT-RESPONSE":{"description":"Base64 JSON of the settlement, including the ledger `transaction` id. Sent when a payment settled.","schema":{"type":"string"}}},"content":{"application/json":{"schema":{"$ref":"#/components/schemas/SwapTicketResponse"}}}},"400":{"description":"A parameter is missing or malformed, no pool holds the pair, the\nfloor rounds to zero at Daml's precision, or `PAYMENT-SIGNATURE`\ncannot be decoded.\n","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Error"}}}},"402":{"description":"Payment required. Either none was sent, it does not match a\ncurrent price, or the facilitator refused it. `error` and the\nheader both say which.\n","headers":{"PAYMENT-REQUIRED":{"required":true,"description":"Base64 JSON of the x402 v2 payment requirements, with the accepted prices in `accepts`.","schema":{"type":"string"}}},"content":{"application/json":{"schema":{"$ref":"#/components/schemas/Error"}}}},"502":{"description":"The payment facilitator gave no usable answer, so no ticket is\nreturned. The payment may have settled: retry with the same\n`PAYMENT-SIGNATURE`, not a new payment, which could pay twice.\n","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Error"}}}},"503":{"description":"The pool exists, but the endpoint cannot build a ticket for it\nnow, or none of its priced tokens is currently available.\n","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Error"}}}}}}}},"components":{"schemas":{"SwapTicketResponse":{"type":"object","description":"Everything needed to place one swap against a pool.","required":["in","out","amount_in","amm_id","amm_cid","pool_party","expected_out","price_impact","slippage","min_out","memo_key","disclosures"],"properties":{"in":{"$ref":"#/components/schemas/SwapTicketToken"},"out":{"$ref":"#/components/schemas/SwapTicketToken"},"amount_in":{"type":"number","format":"double","description":"Amount of the `in` token to transfer, cut to Daml Decimal\nprecision. Transfer this value, not the `amount` you requested.\n"},"amm_id":{"type":"string","description":"The pool identifier"},"amm_cid":{"type":"string","description":"Contract id of the AMM that produced this quote and that `disclosures` is valid for"},"pool_party":{"type":"string","description":"Send the input transfer to this magic address"},"expected_out":{"type":"number","format":"double","description":"Amount of the `out` token the pool pays at its current state"},"price_impact":{"type":"number","format":"double","description":"The fraction by which this swap moves the pool price"},"slippage":{"type":"number","format":"double","description":"The slippage tolerance used for the floor, as requested"},"min_out":{"type":"string","description":"The slippage floor. Copy this string into the transfer memo\nwithout change. It is a string, not a number, because a JSON\nnumber can re-encode as scientific notation, which the on-ledger\nparser rejects.\n"},"memo_key":{"type":"string","description":"Metadata key that carries the floor"},"disclosures":{"$ref":"#/components/schemas/PoolDisclosuresResponse"}}},"SwapTicketToken":{"type":"object","description":"The identity of one token in the swap.\n`in` is the token you send. `out` is the token the pool pays.\n","required":["code","instrument_id","registry_url"],"properties":{"code":{"type":"string","description":"The token symbol, as requested"},"instrument_id":{"$ref":"#/components/schemas/InstrumentId"},"registry_url":{"type":"string","format":"uri","description":"URL of the token registry"}}},"InstrumentId":{"type":"object","description":"Unique identifier for a financial instrument.","required":["admin","id"],"properties":{"admin":{"type":"string","description":"The party administering the instrument"},"id":{"type":"string","description":"The instrument identifier"}}},"PoolDisclosuresResponse":{"type":"object","description":"Disclosed contracts needed to submit transactions against this pool.\nIncludes the AMM, fees, rules, LP instrument config, allocation factory,\nand optionally the featured app right contract.\n","required":["amm","amm_fees","instrument_config","allocation_factory","amm_data","amm_fees_data"],"properties":{"amm":{"$ref":"#/components/schemas/DisclosedContract"},"amm_fees":{"$ref":"#/components/schemas/DisclosedContract"},"amm_rules":{"$ref":"#/components/schemas/DisclosedContract"},"instrument_config":{"$ref":"#/components/schemas/DisclosedContract"},"allocation_factory":{"$ref":"#/components/schemas/DisclosedContract"},"featured_app_right":{"$ref":"#/components/schemas/DisclosedContract"},"amm_data":{"type":"object","description":"Parsed V4 AMM contract data"},"amm_fees_data":{"type":"object","description":"Parsed V4 AMMFees contract data"},"amm_rules_data":{"type":"object","description":"Parsed AMMRules contract data (if present)"},"featured_app_right_id":{"type":"string","nullable":true,"description":"Contract ID of the FeaturedAppRight (if present)"}}},"DisclosedContract":{"type":"object","description":"A disclosed contract blob from the Canton ledger, required for submitting transactions.","required":["templateId","contractId","createdEventBlob","synchronizerId"],"properties":{"templateId":{"type":"string","description":"Fully qualified template identifier (packageId:moduleName:entityName)"},"contractId":{"type":"string","description":"The contract ID on the ledger"},"createdEventBlob":{"type":"string","format":"byte","description":"Base64-encoded created event blob"},"synchronizerId":{"type":"string","description":"The synchronizer (domain) ID where this contract lives"}}},"Error":{"type":"object","required":["error"],"properties":{"error":{"type":"string","description":"Error message"}}}}}}
```

## Get one page of a party's V7 swap history

> Lists the V7 swap orders of one party from Apollo, newest first.\
> Each entry has the order's current status. The amounts exist only\
> for a settled or settling swap.\
> \
> Without \`after\`, the page starts at the newest swap. With \`after\`,\
> it starts after the named order. \`next\` names the last order of the\
> page when an older page exists.<br>

```json
{"openapi":"3.0.3","info":{"title":"Tradecraft AMM HTTP API","version":"1.29.0"},"tags":[{"name":"Swaps","description":"Everything needed to place a swap."}],"servers":[{"url":"https://api.tradecraft.fi/v1","description":"Mainnet"},{"url":"https://tradecraft.validator.test.canton.obsidian.systems/amm-http-api","description":"Testnet"},{"url":"https://tradecraft.validator.dev.canton.obsidian.systems/amm-http-api","description":"Devnet"}],"paths":{"/v7/swap/history/{actor}":{"get":{"summary":"Get one page of a party's V7 swap history","description":"Lists the V7 swap orders of one party from Apollo, newest first.\nEach entry has the order's current status. The amounts exist only\nfor a settled or settling swap.\n\nWithout `after`, the page starts at the newest swap. With `after`,\nit starts after the named order. `next` names the last order of the\npage when an older page exists.\n","operationId":"getSwapHistoryV7","tags":["Swaps"],"parameters":[{"name":"actor","in":"path","required":true,"description":"The party that placed the orders.","schema":{"type":"string"}},{"name":"limit","in":"query","required":false,"description":"Page size, from 1 to 100. The default is 20.","schema":{"type":"integer","minimum":1,"maximum":100,"default":20}},{"name":"after","in":"query","required":false,"description":"Contract id of the last order of the previous page.","schema":{"type":"string"}}],"responses":{"200":{"description":"One page of swaps","content":{"application/json":{"schema":{"$ref":"#/components/schemas/SwapHistoryV7Response"}}}},"400":{"description":"Invalid limit, or an after value that names no swap order of the party","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Error"}}}},"500":{"description":"Internal error reading the swap history","content":{"application/json":{"schema":{"$ref":"#/components/schemas/Error"}}}}}}}},"components":{"schemas":{"SwapHistoryV7Response":{"type":"object","required":["swaps","next"],"properties":{"swaps":{"type":"array","description":"The page's swaps, newest first.","items":{"$ref":"#/components/schemas/SwapHistoryV7Entry"}},"next":{"type":"string","nullable":true,"description":"Contract id of the page's last order when an older page exists."}}},"SwapHistoryV7Entry":{"type":"object","required":["orderCid","placedAt","executedAt","tokenIn","tokenOut","amountIn","amountOut","status","reason","updateId"],"properties":{"orderCid":{"type":"string","description":"Contract id of the order."},"placedAt":{"type":"string","format":"date-time"},"executedAt":{"type":"string","format":"date-time","nullable":true},"tokenIn":{"$ref":"#/components/schemas/InstrumentId"},"tokenOut":{"$ref":"#/components/schemas/InstrumentId"},"amountIn":{"type":"string","nullable":true},"amountOut":{"type":"string","nullable":true},"status":{"type":"string","enum":["pending","completed","failed","cancelled"]},"reason":{"type":"string","nullable":true,"description":"The rejection text of a failed swap."},"updateId":{"type":"string","description":"The update that set the current status."}}},"InstrumentId":{"type":"object","description":"Unique identifier for a financial instrument.","required":["admin","id"],"properties":{"admin":{"type":"string","description":"The party administering the instrument"},"id":{"type":"string","description":"The instrument identifier"}}},"Error":{"type":"object","required":["error"],"properties":{"error":{"type":"string","description":"Error message"}}}}}}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.tradecraft.fi/api/routes/swaps.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
