Last active
March 4, 2021 15:57
-
-
Save shmish111/4d9b49bf45606f11210f0ae5da295ad6 to your computer and use it in GitHub Desktop.
Untitled Project
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
{-# 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 |
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
/* Parties */ | |
const alice : Party = Role("alice"); | |
const bob : Party = Role("bob"); | |
const carol : Party = Role("carol"); | |
/* Value under escrow */ | |
const price : SomeNumber = 450n; | |
/* helper function to build Actions */ | |
const choiceName : string = "choice"; | |
const choiceIdBy = function (party : Party) : ChoiceId { | |
return ChoiceId(choiceName, party); | |
} | |
const choiceBy = function(party : Party, bounds : [Bound]) : Action { | |
return Choice(choiceIdBy(party), bounds); | |
}; | |
const choiceValueBy = function(party : Party) : Value { | |
return ChoiceValue(choiceIdBy(party)); | |
}; | |
/* Names for choices */ | |
const pay : [Bound] = [Bound(0n, 0n)]; | |
const refund : [Bound] = [Bound(1n, 1n)]; | |
const both : [Bound] = [Bound(0n, 1n)]; | |
/* Name choices according to person making choice and choice made */ | |
const alicePay : Action = choiceBy(alice, pay); | |
const aliceRefund : Action = choiceBy(alice, refund); | |
const aliceChoice : Action = choiceBy(alice, both); | |
const bobPay : Action = choiceBy(bob, pay); | |
const bobRefund : Action = choiceBy(bob, refund); | |
const bobChoice : Action = choiceBy(bob, both); | |
const carolPay : Action = choiceBy(carol, pay); | |
const carolRefund : Action = choiceBy(carol, refund); | |
const carolChoice : Action = choiceBy(carol, both); | |
/* the values chosen in choices */ | |
const aliceChosen : Value = choiceValueBy(alice); | |
const bobChosen : Value = choiceValueBy(bob); | |
/* The contract to follow when Alice and Bob disagree, or if | |
Carol has to intervene after a single choice from Alice or Bob. */ | |
const arbitrate : Contract = When([Case(carolRefund, Close), | |
Case(carolPay, Pay(alice, Party(bob), ada, price, Close))], | |
100n, Close); | |
/* The contract to follow when Alice and Bob have made the same choice. */ | |
const agreement : Contract = If(ValueEQ(aliceChosen, 0n), | |
Pay(alice, Party(bob), ada, price, Close), | |
Close); | |
/* Inner part of contract */ | |
const inner : Contract = When([Case(aliceChoice, | |
When([Case(bobChoice, | |
If(ValueEQ(aliceChosen, bobChosen), | |
agreement, | |
arbitrate))], | |
60n, arbitrate))], | |
40n, Close); | |
/* 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. */ | |
const contract : Contract = When([Case(Deposit(alice, alice, ada, price), inner)], | |
10n, | |
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
Close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment