Last active
September 26, 2021 17:27
-
-
Save loicdrbx/ff81961bae01caaab57c2a867177dba8 to your computer and use it in GitHub Desktop.
Marlowe contract that orchestrates an exercise pact between two parties
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
import { | |
PK, Role, Account, Party, ada, AvailableMoney, Constant, ConstantParam, | |
NegValue, AddValue, SubValue, MulValue, Scale, ChoiceValue, SlotIntervalStart, | |
SlotIntervalEnd, UseValue, Cond, AndObs, OrObs, NotObs, ChoseSomething, | |
ValueGE, ValueGT, ValueLT, ValueLE, ValueEQ, TrueObs, FalseObs, Deposit, | |
Choice, Notify, Close, Pay, If, When, Let, Assert, SomeNumber, AccountId, | |
ChoiceId, Token, ValueId, Value, EValue, Observation, Bound, Action, Payee, | |
Case, Timeout, ETimeout, SlotParam, Contract | |
} from 'marlowe-js'; | |
(function (): Contract { | |
/* 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 person1: Party = Role("Workout partner #1"); | |
const person2: Party = Role("Workout partner #2"); | |
const issuer: Party = Role("Token issuer"); | |
const oracle: Party = Role("Oracle"); | |
const depositAmount: SomeNumber = 10; | |
const deposit: Value = Constant(depositAmount); | |
const strainThresholdAmount: SomeNumber = 7.0; | |
const strainThreshold: Value = Constant(strainThresholdAmount); | |
const strain1: ChoiceId = ChoiceId("p1-strain", oracle); | |
const strain2: ChoiceId = ChoiceId("p2-strain", oracle); | |
const turingToken: Token = Token("2554c7", "TuringToken"); | |
const retryToken: Token = Token("4e8975", "retryToken"); | |
function initialDeposit(by: Party, token: Token, timeout: ETimeout, timeoutContinuation: Contract, | |
continuation: Contract): Contract { | |
return When([Case(Deposit(by, by, token, deposit), continuation)], | |
timeout, | |
timeoutContinuation); | |
} | |
function oracleInput(choiceId: ChoiceId, timeout: ETimeout, timeoutContinuation: Contract, | |
continuation: Contract): Contract { | |
return When([Case(Choice(choiceId, [Bound(0, 21)]), continuation)], | |
timeout, timeoutContinuation); | |
} | |
function wait(timeout: ETimeout, continuation: Contract): Contract { | |
return When([], timeout, continuation); | |
} | |
function geToVal(value1: Value, value2: Value, value3: Value ,geContinuation: Contract, | |
ltContinuation: Contract): Contract { | |
return If(AndObs(ValueGE(value1, value3), ValueGE(value2, value3)), | |
geContinuation, | |
ltContinuation); | |
} | |
function debitDeposits(continuation: Contract) : Contract { | |
return Pay(person1, Party(issuer), ada, deposit, | |
Pay(person2, Party(issuer), ada, deposit, | |
continuation)); | |
} | |
function mintTokens(token: Token, amount: EValue, timeout: ETimeout, timeoutContinuation: Contract, | |
continuation: Contract): Contract { | |
return When([Case(Deposit(issuer, issuer, token, amount), continuation)], | |
timeout, | |
timeoutContinuation); | |
}; | |
function issueTokens(token: Token): Contract { | |
return Pay(issuer, Party(person1), token, 1, | |
Pay(issuer, Party(person2), token, 1, | |
Close)); | |
} | |
function refund(who: Party, amount: Value, continuation: Contract): Contract { | |
if (explicitRefunds) { | |
return Pay(who, Party(who), ada, amount, | |
continuation); | |
} else { | |
return continuation; | |
} | |
} | |
const refundBoth: Contract = refund(person1, deposit, refund(person2, deposit, Close)); | |
const contract: Contract = | |
initialDeposit(person1, ada, 10, Close, | |
initialDeposit(person2, ada, 20, refund(person1, deposit, Close), | |
wait(60, | |
oracleInput(strain1, 70, refundBoth, | |
oracleInput(strain2, 80, refundBoth, | |
debitDeposits( | |
geToVal(ChoiceValue(strain1), ChoiceValue(strain2), strainThreshold, | |
mintTokens(turingToken, 2, 85, refundBoth, | |
issueTokens(turingToken)), | |
mintTokens(retryToken, 2, 90, refundBoth, | |
issueTokens(retryToken)) | |
))))))); | |
return contract; | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment