Craft Transaction without SDK

Create Swap Intent

Building a swap intent is straightforward - you just need to create a transaction output at the swap intent script address, locking fromAmount + deposit with a valid SwapIntentDatum.

circle-exclamation

SwapIntentDatum Structure

// Aiken
pub type SwapIntentDatum {
  account_address: Address, // User's address to receive output tokens
  from_amount: MValue, // Assets user is selling
  to_amount: MValue, // Minimum assets user expects to receive
  created_at: Int, // Slot number (used for cancellation timing)
  deposit: Lovelace, // Min UTxO deposit in lovelace (typically 2000000)
}

pub type MValue = Pairs<PolicyId, Pairs<AssetName, Int>>
// Type definition (imported from @meshsdk/core)
type SwapIntentDatum = ConStr0<
  [
    PubKeyAddress | ScriptAddress, // account_address
    Pairs<PolicyId, Pairs<AssetName, Integer>>, // from_amount (MValue)
    Pairs<PolicyId, Pairs<AssetName, Integer>>, // to_amount (MValue)
    Integer, // created_at (slot)
    Lovelace, // deposit
  ]
>;

// Constructor function that builds a SwapIntentDatum for on-chain storage
const datum = swapIntentDatum({
  accountAddress: "addr_test1...",
  fromAmount: [{ unit: config.tokens.night, quantity: "100000000" }],
  toAmount: [{ unit: config.tokens.usdm, quantity: "5000000" }],
  createdAt: 12345678,
  deposit: 2000000, // optional, default 2 ADA
});

The transaction must

  1. Reference the Oracle UTxO (read-only)

  2. Create output at swap intent script address with:

    • Value: fromAmount + deposit

    • Inline datum: SwapIntentDatum

Example Datum

  • SELL 50 ADA for 2.5 USDM

  • BUY 60 ADA with 3 USDM


Cancel Swap Intent

Cancellation is only allowed after the intent expires (~10 minutes from createdAt).

circle-exclamation

CancelIntent Redeemer

The transaction must

  1. Reference the oracle UTxO (read-only)

  2. Spend the swap intent UTxO with redeemer CancelIntent

  3. Set invalidBefore to createdAt + 600 slot (~10 minutes)

  4. Either

    • Must set extra_signatories with accountAddress's payment key hash, OR

    • Input value sent back to accountAddress

Last updated