Last active
June 24, 2022 10:12
-
-
Save marco-martins/d201817387bda63ff7efb5cb5a72ed74 to your computer and use it in GitHub Desktop.
homework04-refactoring.js
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
{"valueParameterInfo":[["Bank Deposit Amount",{"valueParameterFormat":{"contents":[6,"ADA"],"tag":"DecimalFormat"},"valueParameterDescription":"Bank deposit in ADA"}],["Client Deposit Amount",{"valueParameterFormat":{"contents":[6,"ADA"],"tag":"DecimalFormat"},"valueParameterDescription":"Client deposit in ADA"}]],"timeParameterDescriptions":[["Bank Deposit Deadline","Bank deposit deadline"],["Client Deposit Deadline","Client deposit deadline"]],"roleDescriptions":[],"contractType":"Other","contractShortDescription":"Unknown","contractName":"Unknown","contractLongDescription":"We couldn't find information about this contract","choiceInfo":[]} |
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
// Params | |
const bankDepositAmount: Value = ConstantParam("Bank Deposit Amount") | |
const clientDepositAmount: Value = ConstantParam("Client Deposit Amount") | |
const bankDepositDeadline: Timeout = TimeParam("Bank Deposit Deadline") | |
const clientDepositDeadline: Timeout = TimeParam("Client Deposit Deadline") | |
// Parties | |
const bank: Party = Role("Bank") | |
const client: Party = Role("Client") | |
// Settings | |
const depositsNumber: number = 5 | |
// Functions | |
const deposit = (from: Party, to: Party, amount: Value): Action => Deposit(to, from, ada, amount) | |
const payment = (from: Party, to: Party, amount: Value): Contract => Pay(from, Party(to), ada, amount, Close) | |
const clientMakeDeposits = (i: number): Contract => { | |
i-- | |
return When([ | |
Case( | |
deposit(client, bank, clientDepositAmount), | |
// Recursive call, IF client deposits pending THEN make client deposit ELSE make the final payment | |
i > 0 ? clientMakeDeposits(i) : payment(bank, client, AvailableMoney(ada, bank)) | |
) | |
], clientDepositDeadline, Close) | |
} | |
const contract = (): Contract => { | |
return When([ | |
Case( | |
deposit(bank, bank, bankDepositAmount), | |
clientMakeDeposits(depositsNumber) | |
) | |
], bankDepositDeadline, Close) | |
} | |
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