Skip to content

Instantly share code, notes, and snippets.

@shmish111
Created January 6, 2021 14:25
Show Gist options
  • Save shmish111/406967adf76a2be0bc6de3b8676d928d to your computer and use it in GitHub Desktop.
Save shmish111/406967adf76a2be0bc6de3b8676d928d to your computer and use it in GitHub Desktop.
Untitled Project
<xml xmlns="https://developers.google.com/blockly/xml"><block type="BaseContractType" id="root_contract" deletable="false" x="10" y="30"><statement name="BaseContractType"><block type="WhenContractType" id="7m..Ww9a%|dI7=^i%BkG"><field name="timeout">10</field><statement name="case"><block type="DepositActionType" id="YC5tK)~aRF299~xXZuM)"><value name="from_party"><block type="RolePartyType" id="n4ybizeD6~(xeZd?F9=,"><field name="role">alice</field></block></value><value name="value"><block type="ConstantValueType" id="$Z!?Dx_Ch+m^Av}N6=2?"><field name="constant">450</field></block></value><value name="token"><block type="AdaTokenType" id="VM,DA?pm$fawI|eYSPOn"/></value><value name="party"><block type="RolePartyType" id="kiVV7uq_I8g/X2s@bPgD"><field name="role">alice</field></block></value><statement name="contract"><block type="WhenContractType" id="x_0F38wU:+z6IZ%_Ww5)"><field name="timeout">40</field><statement name="case"><block type="ChoiceActionType" id="(Db!vZa0PB{;7CTYpz/`"><field name="choice_name">choice</field><value name="party"><block type="RolePartyType" id="pS3rtNz_Y[{{:.W4#Bx}"><field name="role">alice</field></block></value><statement name="bounds"><block type="BoundsType" id="tf(vEjZ3@%,l.-]c,RLQ"><field name="from">0</field><field name="to">1</field></block></statement><statement name="contract"><block type="WhenContractType" id="G?0kKsy-GkwXH1nNI{Fx"><field name="timeout">60</field><statement name="case"><block type="ChoiceActionType" id=":w%OG#,.J3p8a9ef_Z8V"><field name="choice_name">choice</field><value name="party"><block type="RolePartyType" id="JboeOMMV,p2#ezm=8g)R"><field name="role">bob</field></block></value><statement name="bounds"><block type="BoundsType" id="2PjYO0%:q:,ng5{?Az1+"><field name="from">0</field><field name="to">1</field></block></statement><statement name="contract"><block type="IfContractType" id="bD!bBnGowlxe~}qWvtnL"><value name="observation"><block type="ValueEQObservationType" id="D_k,$#vs/:TK+$;u)WtV"><value name="value1"><block type="ChoiceValueValueType" id="|=ptjEi^0EYOZ0Ukfu+%"><field name="choice_name">choice</field><value name="party"><block type="RolePartyType" id="nWsj``)Ok=GQ?G|`gM8L"><field name="role">alice</field></block></value></block></value><value name="value2"><block type="ChoiceValueValueType" id="anZhLk4F3R]QY8E;!lKr"><field name="choice_name">choice</field><value name="party"><block type="RolePartyType" id="v[)?hgV1P;9SQ1Av@7MP"><field name="role">bob</field></block></value></block></value></block></value><statement name="contract1"><block type="IfContractType" id="@=w)AU#]ovs(|p[PSBX_"><value name="observation"><block type="ValueEQObservationType" id="!t4DRvex$,zmh0xIG@j|"><value name="value1"><block type="ChoiceValueValueType" id=";]J$yiGXiW`*7}YdZ%y-"><field name="choice_name">choice</field><value name="party"><block type="RolePartyType" id="%RwY1e=Rr9i*!*o3UkO-"><field name="role">alice</field></block></value></block></value><value name="value2"><block type="ConstantValueType" id="X(o+(YT}!dw@sibzD*a2"><field name="constant">0</field></block></value></block></value><statement name="contract1"><block type="PayContractType" id="GEV)*t|+pF^$}BUJ,t%*"><value name="payee"><block type="PartyPayeeType" id="u7,78J#+@7$`yH?K#ZFG"><value name="party"><block type="RolePartyType" id="GR[P8o(e:E=oH9zp(4D/"><field name="role">bob</field></block></value></block></value><value name="value"><block type="ConstantValueType" id="VUOVtS:sw7v{_]xKr?-="><field name="constant">450</field></block></value><value name="token"><block type="AdaTokenType" id="OfcsAXE:!a5YWO:V.IFf"/></value><value name="party"><block type="RolePartyType" id="KwrJ?VbUE=9anSwQ`C-C"><field name="role">alice</field></block></value><statement name="contract"><block type="CloseContractType" id="W;Lj5mf?pB%[cIIV=HnG"/></statement></block></statement><statement name="contract2"><block type="CloseContractType" id="$~a^tv!iT+Dxq$[V.c#a"/></statement></block></statement><statement name="contract2"><block type="WhenContractType" id="Jr3}Li`_DzG.b`:F:%xd"><field name="timeout">100</field><statement name="case"><block type="ChoiceActionType" id="T7yg-P8|@H{|$*1%5MR4"><field name="choice_name">choice</field><value name="party"><block type="RolePartyType" id="j%Ftk9rU.(?:;We6#VgN"><field name="role">carol</field></block></value><statement name="bounds"><block type="BoundsType" id="FsOiE*Nrznp`m63t9ED`"><field name="from">1</field><field name="to">1</field></block></statement><statement name="contract"><block type="CloseContractType" id="PDuiq3gRwDIXyF+0kTcr"/></statement><next><block type="ChoiceActionType" id="E=MM!/}m1(|oWq%CQ1)n"><field name="choice_name">choice</field><value name="party"><block type="RolePartyType" id="miQF]ibSW_$lAObLJ0;o"><field name="role">carol</field></block></value><statement name="bounds"><block type="BoundsType" id="_YZ*V.Q2;/YmOpae7R$3"><field name="from">0</field><field name="to">0</field></block></statement><statement name="contract"><block type="PayContractType" id="FtRTp?^a1D3=}]W`F,5]"><value name="payee"><block type="PartyPayeeType" id="(rR*XJReYGzp;}Y]{fAj"><value name="party"><block type="RolePartyType" id="m_)yDH]eK7#ZAhX.HQe@"><field name="role">bob</field></block></value></block></value><value name="value"><block type="ConstantValueType" id="3?GzBeznp2tJrd(ft*)R"><field name="constant">450</field></block></value><value name="token"><block type="AdaTokenType" id="nG8$y^cpEgJZJN6i#*8K"/></value><value name="party"><block type="RolePartyType" id="Lo*|R.PJoBL{#mCLq}6B"><field name="role">alice</field></block></value><statement name="contract"><block type="CloseContractType" id="Le.Op1ky[Xx{$%p5Oujx"/></statement></block></statement></block></next></block></statement><statement name="contract"><block type="CloseContractType" id="o3tYpaqoOSxVdmw}NZ_v"/></statement></block></statement></block></statement></block></statement><statement name="contract"><block type="WhenContractType" id="eQS^nJjg$8lCRZ]W^1pu"><field name="timeout">100</field><statement name="case"><block type="ChoiceActionType" id="d4!dG)y){hyZ3nbDSI$j"><field name="choice_name">choice</field><value name="party"><block type="RolePartyType" id="X0tv,?^$/_q$ueSG%{]]"><field name="role">carol</field></block></value><statement name="bounds"><block type="BoundsType" id="/sok$5JX[pJ{a]fTQiA1"><field name="from">1</field><field name="to">1</field></block></statement><statement name="contract"><block type="CloseContractType" id="6f~0:=P*fr;}TbV}neI5"/></statement><next><block type="ChoiceActionType" id="JDwxTeIjb*(HB@[wrzNO"><field name="choice_name">choice</field><value name="party"><block type="RolePartyType" id="Z+*dxNg7iN/HR5+VPU%Q"><field name="role">carol</field></block></value><statement name="bounds"><block type="BoundsType" id="xH2F;K;e#|v*4O-:6erk"><field name="from">0</field><field name="to">0</field></block></statement><statement name="contract"><block type="PayContractType" id="9ZAJ8HR)/[,{3{Xn(h(L"><value name="payee"><block type="PartyPayeeType" id="#S3nNA,JfgG](hQv+Nh2"><value name="party"><block type="RolePartyType" id="rcCACH5X8m^:vkY?=ZOn"><field name="role">bob</field></block></value></block></value><value name="value"><block type="ConstantValueType" id="Fi__[hOY-67@yi.#~(gO"><field name="constant">450</field></block></value><value name="token"><block type="AdaTokenType" id="p/eI~e.!ark=lwDfaz@S"/></value><value name="party"><block type="RolePartyType" id="^y~7Odyz_9![}C:02vYQ"><field name="role">alice</field></block></value><statement name="contract"><block type="CloseContractType" id="((8.0RG6L=8VR{%pRVj}"/></statement></block></statement></block></next></block></statement><statement name="contract"><block type="CloseContractType" id="2%H+(hAMYC1)^N-I.q^;"/></statement></block></statement></block></statement></block></statement><statement name="contract"><block type="WhenContractType" id="XN7|Z_`OUqY,`Quttw||"><field name="timeout">100</field><statement name="case"><block type="ChoiceActionType" id="p[w+-hRMIIhd1bD:I#@F"><field name="choice_name">choice</field><value name="party"><block type="RolePartyType" id="5$pUm)*vN9GW?#BhgJow"><field name="role">carol</field></block></value><statement name="bounds"><block type="BoundsType" id="#IkUw[U4!Cbdh|_58ZS:"><field name="from">1</field><field name="to">1</field></block></statement><statement name="contract"><block type="CloseContractType" id="%MC%(ls3ldJ-;zfO,?K?"/></statement><next><block type="ChoiceActionType" id="yYw::pP3GQV#yf5eC81f"><field name="choice_name">choice</field><value name="party"><block type="RolePartyType" id="X$w;38KOiriLC;igXW0f"><field name="role">carol</field></block></value><statement name="bounds"><block type="BoundsType" id="Y@He%YMjuCzwY5__ZI1G"><field name="from">0</field><field name="to">0</field></block></statement><statement name="contract"><block type="PayContractType" id="/L?l6j7%QGgJr#a!Ri/a"><value name="payee"><block type="PartyPayeeType" id="HR_8sf*zu:4PjBhBv.d="><value name="party"><block type="RolePartyType" id="v`QLveV,kfxi:sHdQ(-+"><field name="role">bob</field></block></value></block></value><value name="value"><block type="ConstantValueType" id="M{sRM:}q,74uFh}%v@~j"><field name="constant">450</field></block></value><value name="token"><block type="AdaTokenType" id=",lYrx=P=_9mGs]F2mr69"/></value><value name="party"><block type="RolePartyType" id="@c]j1IMMg2[4u:^B}l]C"><field name="role">alice</field></block></value><statement name="contract"><block type="CloseContractType" id="t(Fvvv4_6xc6,zZ3Ky~X"/></statement></block></statement></block></next></block></statement><statement name="contract"><block type="CloseContractType" id="I?RklM30z@Lc.niBxYbp"/></statement></block></statement></block></statement></block></statement><statement name="contract"><block type="CloseContractType" id="L:aA]g|}FWozLTyuz-tp"/></statement></block></statement></block></xml>
{-# 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
/* 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;
When
[Case
(Deposit
(Role "alice")
(Role "alice")
(Token "" "")
(Constant 450)
)
(When
[Case
(Choice
(ChoiceId
"choice"
(Role "alice")
)
[Bound 0 1]
)
(When
[Case
(Choice
(ChoiceId
"choice"
(Role "bob")
)
[Bound 0 1]
)
(If
(ValueEQ
(ChoiceValue
(ChoiceId
"choice"
(Role "alice")
))
(ChoiceValue
(ChoiceId
"choice"
(Role "bob")
))
)
(If
(ValueEQ
(ChoiceValue
(ChoiceId
"choice"
(Role "alice")
))
(Constant 0)
)
(Pay
(Role "alice")
(Party (Role "bob"))
(Token "" "")
(Constant 450)
Close
)
Close
)
(When
[Case
(Choice
(ChoiceId
"choice"
(Role "carol")
)
[Bound 1 1]
)
Close , Case
(Choice
(ChoiceId
"choice"
(Role "carol")
)
[Bound 0 0]
)
(Pay
(Role "alice")
(Party (Role "bob"))
(Token "" "")
(Constant 450)
Close
)]
100 Close
)
)]
60
(When
[Case
(Choice
(ChoiceId
"choice"
(Role "carol")
)
[Bound 1 1]
)
Close , Case
(Choice
(ChoiceId
"choice"
(Role "carol")
)
[Bound 0 0]
)
(Pay
(Role "alice")
(Party (Role "bob"))
(Token "" "")
(Constant 450)
Close
)]
100 Close
)
)]
40 Close
)]
10 Close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment