Skip to content

Instantly share code, notes, and snippets.

Created Sep 15, 2021
What would you like to do?
Swap of Ada and dollar tokens
{"valueParameterDescriptions":[["Amount of Ada","Amount of Ada to be exchanged for dollars."],["Amount of dollars","Amount of dollar tokens to be exchanged for Ada."]],"slotParameterDescriptions":[["Timeout for Ada deposit","Deadline by which Ada must be deposited."],["Timeout for dollar deposit","Deadline by which dollar tokens must be deposited (must be after the deadline for Ada deposit)."]],"roleDescriptions":[["Ada provider","The party that provides the Ada."],["Dollar provider","The party that provides the dollar tokens."]],"contractType":"S","contractName":"Swap of Ada and dollar tokens","contractDescription":"Takes Ada from one party and dollar tokens from another party, and it swaps them atomically.","choiceDescriptions":[]}
/* We can set explicitRefunds true to run Close refund analysis
but we get a shorter contract if we set it to false */
const explicitRefunds: Boolean = false;
const lovelacePerAda: Value = Constant(1000000n);
const amountOfAda: Value = ConstantParam("Amount of Ada");
const amountOfLovelace: Value = MulValue(lovelacePerAda, amountOfAda);
const amountOfDollars: Value = ConstantParam("Amount of dollars");
const adaDepositTimeout: Timeout = SlotParam("Timeout for Ada deposit");
const dollarDepositTimeout: Timeout = SlotParam("Timeout for dollar deposit");
const dollars: Token = Token("85bb65", "dollar")
type SwapParty = {
party: Party;
currency: Token;
amount: Value;
const adaProvider: SwapParty = {
party: Role("Ada provider"),
currency: ada,
amount: amountOfLovelace
const dollarProvider: SwapParty = {
party: Role("Dollar provider"),
currency: dollars,
amount: amountOfDollars
function makeDeposit(src: SwapParty, timeout: Timeout,
timeoutContinuation: Contract, continuation: Contract): Contract {
return When([Case(Deposit(,, src.currency, src.amount),
function refundSwapParty(party: SwapParty): Contract {
if (explicitRefunds) {
return Pay(, Party(, party.currency, party.amount, Close);
} else {
return Close;
const makePayment = function (src: SwapParty, dest: SwapParty,
continuation: Contract): Contract {
return Pay(, Party(, src.currency, src.amount,
const contract: Contract = makeDeposit(adaProvider, adaDepositTimeout, Close,
makeDeposit(dollarProvider, dollarDepositTimeout, refundSwapParty(adaProvider),
makePayment(adaProvider, dollarProvider,
makePayment(dollarProvider, adaProvider,
return contract;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment