Skip to content

Instantly share code, notes, and snippets.

@marco-martins
Last active June 24, 2022 10:12
Show Gist options
  • Save marco-martins/d201817387bda63ff7efb5cb5a72ed74 to your computer and use it in GitHub Desktop.
Save marco-martins/d201817387bda63ff7efb5cb5a72ed74 to your computer and use it in GitHub Desktop.
homework04-refactoring.js
{"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":[]}
// 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();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment