Skip to content

Instantly share code, notes, and snippets.

@edwint88
Last active January 24, 2022 01:12
Show Gist options
  • Save edwint88/2a9f6a9369db2571114e0b0eb78a585e to your computer and use it in GitHub Desktop.
Save edwint88/2a9f6a9369db2571114e0b0eb78a585e to your computer and use it in GitHub Desktop.
Test
{"valueParameterInfo":[["Price",{"valueParameterFormat":{"contents":[6,"₳"],"tag":"DecimalFormat"},"valueParameterDescription":"The price of the item."}]],"slotParameterDescriptions":[["Payment deadline","The _**buyer**_ must pay the _price_ of the item by this time, otherwise the contract is cancelled."],["Complaint deadline","The _**buyer**_ can only complain until this deadline, otherwise the contract will assume the transaction went smoothly and pay the _**seller**_."],["Complaint response deadline","If the _**buyer**_ complained, the _**seller**_ must respond before this deadline, otherwise the contract will assume there was a problem with the transaction and refund the _**buyer**_."],["Mediation deadline","If the _**buyer**_ and the _**seller**_ disagree, the _**mediator**_ must weigh in before this deadline, otherwise the contract will assume there was a problem with the transaction and refund the _**buyer**_."]],"roleDescriptions":[["Buyer","The buyer of the item."],["Mediator","The mediator decides who is right in the case of dispute."],["Seller","The seller of the item."]],"contractType":"ES","contractShortDescription":"In this contract a _**seller**_ wants to sell an item (like a bicycle) to a _**buyer**_ for a _price_.","contractName":"Purchase","contractLongDescription":"Neither trusts each other, but they both trust a _**mediator**_. The _**buyer**_ pays the _price_ into the contract account: if both the _**buyer**_ and the _**seller**_ agree that the _**buyer**_ has received the item, then the _**seller**_ receives the _price_; if not, then the _**mediator**_ ensures that the _**buyer**_ gets their money back.","choiceInfo":[["Confirm problem",{"choiceFormat":{"tag":"DefaultFormat"},"choiceDescription":"Acknowledge there was a problem and a refund must be granted."}],["Dismiss claim",{"choiceFormat":{"tag":"DefaultFormat"},"choiceDescription":"The _**Mediator**_ does not see any problem with the exchange and the _**Seller**_ must be paid."}],["Dispute problem",{"choiceFormat":{"tag":"DefaultFormat"},"choiceDescription":"The _**Seller**_ disagrees with the _**Buyer**_ about the claim that something went wrong."}],["Everything is alright",{"choiceFormat":{"tag":"DefaultFormat"},"choiceDescription":"The transaction was uneventful, _**Buyer**_ agrees to pay the _**Seller**_."}],["Report problem",{"choiceFormat":{"tag":"DefaultFormat"},"choiceDescription":"The _**Buyer**_ claims not having received the product that was paid for as agreed and would like a refund."}]]}
/* 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 buyer: Party = Role("Buyer");
const seller: Party = Role("Seller");
const arbiter: Party = Role("Mediator");
const price: Value = ConstantParam("Price");
const depositTimeout: Timeout = SlotParam("Payment deadline");
const disputeTimeout: Timeout = SlotParam("Complaint response deadline");
const answerTimeout: Timeout = SlotParam("Complaint deadline");
const arbitrageTimeout: Timeout = SlotParam("Mediation deadline");
function choice(choiceName: string, chooser: Party, choiceValue: SomeNumber, continuation: Contract): Case {
return Case(Choice(ChoiceId(choiceName, chooser),
[Bound(choiceValue, choiceValue)]),
continuation);
}
function deposit(timeout: Timeout, timeoutContinuation: Contract, continuation: Contract): Contract {
return When([Case(Deposit(seller, buyer, ada, price), continuation)],
timeout,
timeoutContinuation);
}
function choices(timeout: Timeout, chooser: Party, timeoutContinuation: Contract, list: { value: SomeNumber, name: string, continuation: Contract }[]): Contract {
var caseList: Case[] = new Array(list.length);
list.forEach((element, index) =>
caseList[index] = choice(element.name, chooser, element.value, element.continuation)
);
return When(caseList, timeout, timeoutContinuation);
}
function sellerToBuyer(continuation: Contract): Contract {
return Pay(seller, Account(buyer), ada, price, continuation);
}
function paySeller(continuation: Contract): Contract {
return Pay(buyer, Party(seller), ada, price, continuation);
}
const refundBuyer: Contract = explicitRefunds ? Pay(buyer, Party(buyer), ada, price, Close) : Close;
const refundSeller: Contract = explicitRefunds ? Pay(seller, Party(seller), ada, price, Close) : Close;
const contract: Contract =
deposit(depositTimeout, Close,
choices(disputeTimeout, buyer, refundSeller,
[{ value: 0n, name: "Everything is alright", continuation: refundSeller },
{
value: 1n, name: "Report problem",
continuation:
sellerToBuyer(
choices(answerTimeout, seller, refundBuyer,
[{ value: 1n, name: "Confirm problem", continuation: refundBuyer },
{
value: 0n, name: "Dispute problem", continuation:
choices(arbitrageTimeout, arbiter, refundBuyer,
[{ value: 0n, name: "Dismiss claim", continuation: paySeller(Close) },
{ value: 1n, name: "Confirm problem", continuation: refundBuyer }
])
}]))
}]));
return contract;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment