Created
September 15, 2021 02:26
-
-
Save 4dcode/4310c1ac14b001fe754309bcc51d25b9 to your computer and use it in GitHub Desktop.
Swap of Ada and dollar tokens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"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":[]} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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.party, src.party, src.currency, src.amount), | |
continuation)], | |
timeout, | |
timeoutContinuation); | |
} | |
function refundSwapParty(party: SwapParty): Contract { | |
if (explicitRefunds) { | |
return Pay(party.party, Party(party.party), party.currency, party.amount, Close); | |
} else { | |
return Close; | |
} | |
} | |
const makePayment = function (src: SwapParty, dest: SwapParty, | |
continuation: Contract): Contract { | |
return Pay(src.party, Party(dest.party), src.currency, src.amount, | |
continuation); | |
} | |
const contract: Contract = makeDeposit(adaProvider, adaDepositTimeout, Close, | |
makeDeposit(dollarProvider, dollarDepositTimeout, refundSwapParty(adaProvider), | |
makePayment(adaProvider, dollarProvider, | |
makePayment(dollarProvider, adaProvider, | |
Close)))) | |
return contract; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment