Skip to content

Instantly share code, notes, and snippets.

@merivale
Created December 16, 2020 13:11
Show Gist options
  • Save merivale/f82babfcbf6442570295d35441839851 to your computer and use it in GitHub Desktop.
Save merivale/f82babfcbf6442570295d35441839851 to your computer and use it in GitHub Desktop.
testing 1 2 3
{-# LANGUAGE OverloadedStrings #-}
module Escrow where
import Language.Marlowe
main :: IO ()
main = print . pretty $ contract
{- What does the vanilla contract look like?
- if Alice and Bob choose
- and agree: do it
- and disagree: Carol decides
- Carol also decides if timeout after one choice has been made;
- refund if no choices are made.
-}
contract :: Contract
contract = When [Case (Deposit "alice" "alice" ada price) inner]
10
Close
inner :: Contract
inner =
When [ Case aliceChoice
(When [ Case bobChoice
(If (aliceChosen `ValueEQ` bobChosen)
agreement
arbitrate) ]
60
arbitrate)
]
40
arbitrate
-- The contract to follow when Alice and Bob have made the same choice.
agreement :: Contract
agreement =
If
(aliceChosen `ValueEQ` Constant 0)
(Pay "alice" (Party "bob") ada price Close)
Close
-- The contract to follow when Alice and Bob disagree, or if
-- Carol has to intervene after a single choice from Alice or Bob.
arbitrate :: Contract
arbitrate =
When [ Case carolRefund Close,
Case carolPay (Pay "alice" (Party "bob") ada price Close) ]
100
Close
-- Names for choices
pay,refund,both :: [Bound]
pay = [Bound 0 0]
refund = [Bound 1 1]
both = [Bound 0 1]
-- helper function to build Actions
choiceName :: ChoiceName
choiceName = "choice"
choice :: Party -> [Bound] -> Action
choice party = Choice (ChoiceId choiceName party)
-- Name choices according to person making choice and choice made
alicePay, aliceRefund, aliceChoice, bobPay, bobRefund, bobChoice, carolPay, carolRefund, carolChoice :: Action
alicePay = choice "alice" pay
aliceRefund = choice "alice" refund
aliceChoice = choice "alice" both
bobPay = choice "bob" pay
bobRefund = choice "bob" refund
bobChoice = choice "bob" both
carolPay = choice "carol" pay
carolRefund = choice "carol" refund
carolChoice = choice "carol" both
-- the values chosen in choices
aliceChosen, bobChosen :: (Value Observation)
aliceChosen = ChoiceValue (ChoiceId choiceName "alice")
bobChosen = ChoiceValue (ChoiceId choiceName "bob")
defValue :: (Value Observation)
defValue = Constant 42
-- Value under escrow
price :: (Value Observation)
price = Constant 450
const lovelacePerAda : SomeNumber = 1000000n;
const amountOfAda : SomeNumber = 1000n;
const amountOfLovelace : SomeNumber = lovelacePerAda * amountOfAda;
const amountOfDollars : SomeNumber = 100n;
const dollars : Token = Token("85bb65", "dollar")
type SwapParty = {
party: Party;
currency: Token;
amount: SomeNumber;
};
const alice : SwapParty = {
party: Role("alice"),
currency: ada,
amount: amountOfLovelace
}
const bob : SwapParty = {
party: Role("bob"),
currency: dollars,
amount: amountOfDollars
}
const makeDeposit = function(src : SwapParty, timeout : SomeNumber,
continuation : Contract) : Contract
{
return When([Case(Deposit(src.party, src.party, src.currency, src.amount),
continuation)],
timeout,
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(alice, 10n,
makeDeposit(bob, 20n,
makePayment(alice, bob,
makePayment(bob, alice,
Close))))
return contract;
When
[Case
(Deposit
(Role "alice")
(Role "alice")
(Token "" "")
(Constant 1000000000)
)
(When
[Case
(Deposit
(Role "bob")
(Role "bob")
(Token "85bb65" "dollar")
(Constant 100)
)
(Pay
(Role "alice")
(Party (Role "bob"))
(Token "" "")
(Constant 1000000000)
(Pay
(Role "bob")
(Party (Role "alice"))
(Token "85bb65" "dollar")
(Constant 100)
Close
)
)]
20 Close
)]
10 Close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment