Skip to content

Instantly share code, notes, and snippets.

@Bucklash
Created April 20, 2021 09:54
Show Gist options
  • Select an option

  • Save Bucklash/a2fda9a4657c2b30aaef3b69774eb5c2 to your computer and use it in GitHub Desktop.

Select an option

Save Bucklash/a2fda9a4657c2b30aaef3b69774eb5c2 to your computer and use it in GitHub Desktop.
New Project
{"valueParameterDescriptions":[["Price","Amount of Lovelace to be paid by the \"Buyer\" for the item."]],"slotParameterDescriptions":[["Buyer's deposit timeout","Deadline by which the \"Buyer\" must deposit the selling \"Price\" in the contract."],["Buyer's dispute timeout","Deadline by which, if the \"Buyer\" has not opened a dispute, the \"Seller\" will be paid."],["Seller's response timeout","Deadline by which, if the \"Seller\" has not responded to the dispute, the \"Buyer\" will be refunded."],["Timeout for arbitrage","Deadline by which, if the \"Arbiter\" has not resolved the dispute, the \"Buyer\" will be refunded."]],"roleDescriptions":[["Arbiter","The party that will choose who gets the money in the event of a disagreement between the \"Buyer\" and the \"Seller\" about the outcome."],["Buyer","The party that wants to buy the item. Payment is made to the \"Seller\" if they acknowledge receiving the item."],["Seller","The party that wants to sell the item. They receive the payment if the exchange is uneventful."]],"contractType":"ES","contractName":"Simple escrow","contractDescription":"Regulates a money exchange between a \"Buyer\" and a \"Seller\". If there is a disagreement, an \"Arbiter\" will decide whether the money is refunded or paid to the \"Seller\".","choiceDescriptions":[["Confirm problem","Acknowledge there was a problem and a refund must be granted."],["Dismiss claim","The \"Arbiter\" does not see any problem with the exchange and the \"Seller\" must be paid."],["Dispute problem","The \"Seller\" disagrees with the \"Buyer\" about the claim that something went wrong."],["Everything is alright","The transaction was uneventful, \"Buyer\" agrees to pay the \"Seller\"."],["Report problem","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("Arbiter");
const price: Value = ConstantParam("Price");
const depositTimeout: Timeout = SlotParam("Buyer's deposit timeout");
const disputeTimeout: Timeout = SlotParam("Buyer's dispute timeout");
const answerTimeout: Timeout = SlotParam("Seller's response timeout");
const arbitrageTimeout: Timeout = SlotParam("Timeout for arbitrage");
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