Created
May 18, 2021 00:46
-
-
Save devonartis/fe76d4eea614de4933de36e5709a1cb6 to your computer and use it in GitHub Desktop.
Escrow with collateral
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
{"valueParameterDescriptions":[["Collateral amount","The amount of Lovelace to be deposited by both parties at the start of the contract to serve as an incentive for collaboration."],["Price","The amount of Lovelace to be paid by the *Buyer* as part of the exchange."]],"slotParameterDescriptions":[["Collateral deposit by seller timeout","The deadline by which the *Seller* must deposit the *Collateral amount* in the contract."],["Deposit of collateral by buyer timeout","The deadline by which the *Buyer* must deposit the *Collateral amount* in the contract."],["Deposit of price by buyer timeout","The deadline by which the *Buyer* must deposit the *Price* in the contract."],["Dispute by buyer timeout","The deadline by which, if the *Buyer* has not opened a dispute, the *Seller* will be paid."],["Seller's response timeout","The deadline by which, if the *Seller* has not responded to the dispute, the *Buyer* will be refunded."]],"roleDescriptions":[["Buyer","The party that pays for the item on sale."],["Seller","The party that sells the item and gets the money if the exchange is successful."]],"contractType":"ES","contractName":"Escrow with collateral","contractDescription":"Regulates a money exchange between a *Buyer* and a *Seller* using a collateral from both parties to incentivize collaboration. If there is a disagreement the collateral is burned.","choiceDescriptions":[["Confirm problem","Acknowledge that there was a problem and a refund must be granted."],["Dispute problem","The *Seller* disagrees with the *Buyer* about the claim that something went wrong and the collateral will be burnt."],["Everything is alright","The exchange was successful and the *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."]]} |
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
/* 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 burnAddress: Party = PK("0000000000000000000000000000000000000000000000000000000000000000"); | |
const price: Value = ConstantParam("Price"); | |
const collateral: Value = ConstantParam("Collateral amount"); | |
const sellerCollateralTimeout: Timeout = SlotParam("Collateral deposit by seller timeout"); | |
const buyerCollateralTimeout: Timeout = SlotParam("Deposit of collateral by buyer timeout"); | |
const depositTimeout: Timeout = SlotParam("Deposit of price by buyer timeout"); | |
const disputeTimeout: Timeout = SlotParam("Dispute by buyer timeout"); | |
const answerTimeout: Timeout = SlotParam("Seller's response timeout"); | |
function depositCollateral(party: Party, timeout: Timeout, timeoutContinuation: Contract, continuation: Contract): Contract { | |
return When([Case(Deposit(party, party, ada, collateral), continuation)], | |
timeout, | |
timeoutContinuation); | |
} | |
function burnCollaterals(continuation: Contract): Contract { | |
return Pay(seller, Party(burnAddress), ada, collateral, | |
Pay(buyer, Party(burnAddress), ada, collateral, | |
continuation)); | |
} | |
function deposit(timeout: Timeout, timeoutContinuation: Contract, continuation: Contract): Contract { | |
return When([Case(Deposit(seller, buyer, ada, price), continuation)], | |
timeout, | |
timeoutContinuation); | |
} | |
function choice(choiceName: string, chooser: Party, choiceValue: SomeNumber, continuation: Contract): Case { | |
return Case(Choice(ChoiceId(choiceName, chooser), | |
[Bound(choiceValue, choiceValue)]), | |
continuation); | |
} | |
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 refundSellerCollateral(continuation: Contract): Contract { | |
if (explicitRefunds) { | |
return Pay(seller, Party(seller), ada, collateral, continuation); | |
} else { | |
return continuation; | |
} | |
} | |
function refundBuyerCollateral(continuation: Contract): Contract { | |
if (explicitRefunds) { | |
return Pay(buyer, Party(buyer), ada, collateral, continuation); | |
} else { | |
return continuation; | |
} | |
} | |
function refundCollaterals(continuation: Contract): Contract { | |
return refundSellerCollateral(refundBuyerCollateral(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 = | |
depositCollateral(seller, sellerCollateralTimeout, Close, | |
depositCollateral(buyer, buyerCollateralTimeout, refundSellerCollateral(Close), | |
deposit(depositTimeout, refundCollaterals(Close), | |
choices(disputeTimeout, buyer, refundCollaterals(refundSeller), | |
[{ value: 0n, name: "Everything is alright", continuation: refundCollaterals(refundSeller) }, | |
{ | |
value: 1n, name: "Report problem", | |
continuation: | |
sellerToBuyer( | |
choices(answerTimeout, seller, refundCollaterals(refundBuyer), | |
[{ value: 1n, name: "Confirm problem", continuation: refundCollaterals(refundBuyer) }, | |
{ value: 0n, name: "Dispute problem", continuation: burnCollaterals(refundBuyer) }])) | |
}])))); | |
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
{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment