DAML Script not working
- Iqra_Mustafa1OP
Hi, this DAML Script is not working. Can please take a look to fix it for daml version 2, what is the problem with it.
setup : Script ()
setup = script do
bank ← allocateParty “AIB Bank”
martin ← allocateParty “Alice”
carshop ← allocateParty “CarShop”
let
cash = Cash
with
issuer = bank
owner = alice
currency = CHF
amount = 2000.0
cashCid ← submit bank do
createCmd cash
let
bikeRepair = CarRepair with
carShop = carshop
carOwner = alice
description = “car repair and maintainance”
price = 200.0
paymentDue = date 27 Jun 2022
proposalCid ← submit shop do
createCmd CarRepairProposal
with
proposer = carshop
receiver = alice
proposal = CarRepair
repairCid ← submit alice do
exerciseCmd proposalCid Accept
submit alice do
exerciseCmd repairCid Pay with cashCid - cocreature 1
Please encode your code in a markdown code block to make it easier to read:
``` yourcodehere ```It would also be great if you could share the error you’re receiving.
- Iqra_Mustafa1
Blockquote
setup : Script ()
setup = script do
bank ← allocateParty “AIB Bank”
martin ← allocateParty “Alice”
carshop ← allocateParty “CarShop”
let
cash = Cash
with
issuer = bank
owner = alice
currency = CHF
amount = 2000.0
cashCid ← submit bank do
createCmd cash
let
bikeRepair = CarRepair with
carShop = carshop
carOwner = alice
description = “car repair and maintainance”
price = 200.0
paymentDue = date 27 Jun 2022
proposalCid ← submit shop do
createCmd CarRepairProposal
with
proposer = carshop
receiver = alice
proposal = CarRepair
repairCid ← submit alice do
exerciseCmd proposalCid Accept
submit alice do
exerciseCmd repairCid Pay with cashCid
Blockquote
- a_putkov
@Iqra_Mustafa1
I assume you wanted the whole code snippet you provided to be part of the function named “setup”, which returns Script (). If this is the case, the parsing error is due to missing indentation. In this case the whole body of the function falls within the “do” block and should be indented.
As I attempted to fix your code I found a bunch of other errors, like incorrect date format etc. The setup function below is free from syntax errors and works for me after creating a skeleton for the templates utilized in this function. I can’t be certain it will work for you, as your templates must be different from mine. But it should bring you closer to the working Daml Script.setup : Script () setup = script do bank <- allocateParty "AIB Bank" alice <- allocateParty "Alice" carshop <- allocateParty "CarShop" let cash = Cash with issuer = bank owner = alice currency = "CHF" amount = 2000.0 cashCid <- submit bank do createCmd cash let carRepair = CarRepair with carShop = carshop carOwner = alice description = "car repair and maintainance" price = 200.0 paymentDue = date 2022 Jun 27 proposalCid <- submit carshop do createCmd CarRepairProposal with proposer = carshop receiver = alice proposal = carRepair repairCid <- submit alice do exerciseCmd proposalCid Accept submit alice do exerciseCmd repairCid Pay with cashCid return () - Iqra_Mustafa1
This is the DAML Contract. I have checked the DAML Script but still its giving error on Script().Error/ServiceCenter.daml:58:7: error:
Not in scope: type constructor or class ‘Script’typecheckBlockquote
module ServiceCenter wheredata Currency = USD | EUR | GBP
deriving (Eq, Show)
template Cash
with
issuer: Party
owner: Party
currency: Currency
amount: Decimal
where
signatory issuer
observer ownernonconsuming choice Transfer : ContractId Cash with newOwner: Party controller owner do archive self create this with owner=newOwnertemplate CarRepair
with
carShop: Party
carOwner: Party
description: Text
price: Decimal
paymentDue: Date
where
signatory carShop
observer carOwnernonconsuming choice Pay : ContractId Cash with cashCid: ContractId Cash controller carOwner do cash <- fetch cashCid assert ( cash.currency == USD && cash.amount == price) exercise cashCid Transfer with newOwner=carShoptemplate CarRepairProposal
with
proposer: Party
receiver: Party
proposal: CarRepair
where
signatory proposer
observer receivernonconsuming choice Accept : ContractId CarRepair controller receiver do create proposalBlockquote
- a_putkov
@Iqra_Mustafa1
With these templates, replacingcurrency = "CHF"with saycurrency = USDwhen creating thecashrecord would resolve the compile error. But the script would still fail because of the mismatch in the amount of theCashcontract and the value of thepricefield inCarRepaircontract, and because of the assertion that checks that these two values must match in thePaychoice of theCarRepairtemplate.
Here’s the Daml Script code that works with the templates you provided.setup : Script () setup = script do bank <- allocateParty "AIB Bank" alice <- allocateParty "Alice" carshop <- allocateParty "CarShop" let cash = Cash with issuer = bank owner = alice currency = USD amount = 200.0 cashCid <- submit bank do createCmd cash let carRepair = CarRepair with carShop = carshop carOwner = alice description = "car repair and maintainance" price = 200.0 paymentDue = date 2022 Jun 27 proposalCid <- submit carshop do createCmd CarRepairProposal with proposer = carshop receiver = alice proposal = carRepair repairCid <- submit alice do exerciseCmd proposalCid Accept submit alice do exerciseCmd repairCid Pay with cashCid return () - Iqra_Mustafa1
I was making some changes in the contract in the CarRepair Choice PAY:
nonconsuming choice Pay : ContractId Cash with cashCid: ContractId Cash cash: Cash controller carOwner do assert (cash.currency == USD && cash.amount == price) exercise cashCid Transfer with newOwner = carShopIn this case, in the above code I replaced the fetchID statement after do (Please see correct contract code above in this thread(carRepair Template)). And added cash = Cash. When I tested that by making changes then I observe following behaviour on DAML HUB
The green box represents the output of correct model which is above in this thread, which is asking for only contractID Cash to process the payment. However, the red box depicts the output of this amended approach. Actually when I try to access the Cash template arguments in carRepair choice PAY, it is asking again to enter all arguments of cash template to process cash transfer into carRepair.
I wanted to ask by doing cash = Cash is also right , and removing fetchID after do is ok. Because the contract is working fine. However, this approach again loaded the cash template. This approach can lead a logical vulnerability. any thoughts on this???
- a_putkov
@Iqra_Mustafa1
Your latest implementation of thePaychoice introduces a logical error. In Daml the contract ID and the contract payload are not interchangeable.
The fieldcashthat you introduced in thePaychoice has no relation to the fieldcashCid. The values in these two fields can represent two completely different contracts. Moreover, the value of thecashfield can be a Daml record of typeCash, which does not exist on the ledger at all.
Let me illustrate. Suppose the contract ID you pass to thecashCidfield refers to aCashcontract representing 20 EUR. Then, according to the business logic you’re looking to implement, the exercise of thePaychoice should fail because the currency and the amount of theCashcontract does not match the currency and the price of theCarRepaircontract, right? Well, with your latest implementation of thePaychoice, I can pass aCashcontract representing 20 EUR to thecashCidfield. And I can pass to thecashfield of thePaychoice a Daml record of typeCash, which does not exist on the ledger, and which has thecurrencyand theamountfield values that match the currency and the price in theCarRepaircontract, so the transaction will succeed. In the following Daml script the value of thedummyCashRecordvariable is a Daml record, which does not exist on the ledger. By passing this variable to thePaychoice I successfully transfer 20 EURCashcontract as payment for car repair that car repair shop wants 200 USD for.setup : Script () setup = script do bank <- allocateParty "AIB Bank" alice <- allocateParty "Alice" carshop <- allocateParty "CarShop" let cash = Cash with issuer = bank owner = alice currency = EUR amount = 20.0 cashCid <- submit bank do createCmd cash let carRepair = CarRepair with carShop = carshop carOwner = alice description = "car repair and maintainance" price = 200.0 paymentDue = date 2022 Jun 27 proposalCid <- submit carshop do createCmd CarRepairProposal with proposer = carshop receiver = alice proposal = carRepair repairCid <- submit alice do exerciseCmd proposalCid Accept let dummyCashRecord = Cash with issuer = bank owner = alice currency = USD amount = 200.0 submit alice do exerciseCmd repairCid Pay with cashCid cash = dummyCashRecord return ()If you want to use a contract in a choice (e.g. archive the contract or exercise a choice on it), you need to pass the contract ID to the choice. And if you need to use the contract data in the choice, then you need to fetch the contract using contract ID.

