Hi! Thank you to your help, I wrote my first smart contract!
Quick question: Is this syntax deprecated?
newToken ← create Token
with
…
The two dots to state that we keep the same arguments are they still valid? Is there a new syntax to keep the same arguments? I cannot find them in the documentation (only in the tutorials), but I don’t how to replace them, as I get an error when I try to use this syntax.
Thank you everyone in advance!
Claudia
@claudia_giannoni
This syntax is not deprecated. The two dots after the with
keyword specify that the fields in the template record or regular Daml record are bound to the namesake local variables. Assuming the template named Token
has two fields named owner
and amount
, the following three notations to create a contract based on Token
are equivalent.
create Token with ..
create Token with
owner
amount
create Token with
owner = owner
amount = amount
The latter syntax is the most verbose and it best illustrates what happens when the code is compiled. The fields named owner
and amount
in the Token
contract being created are respectively bound to the local variables named ‘owner’ and ‘amount’. For this code to compile local variables named ‘owner’ and ‘amount’ must exist and have the same type as namesake fields in Token
. E.g.
template Token
with
owner : Party
amount : Decimal
where
signatory owner
template MyTemplate
with
creator : Party
field1 : Party
field2 : Decimal
where
signatory creator
let
owner = field1
amount = field2
nonconsuming choice CreateToken : ContractId Token
controller owner
do
create Token with ..
2 Likes
Hi Putkov,
Thank you ever so much for your prompt reply!
I have been trying all day but still have a few problems. I have copied the code below.
1- I want to make sure that the asset I offer has all the same arguments that the asset created. Do I have to repeat all of them?
2- As you can see from the code, I have created choices to update the asset. How can I ensure the asset offered is the one with the last updates?
Thank you in advance if you want to help a bit more!
In my defence, I am working hard…
module Asset where
import Daml.Script
import DA.List
import DA.Time
type AssetId = ContractId Asset
type AssetDocOfferId = ContractId AssetDocOffer
template Asset
with
ownerSolicitor : Party
owner : Party
auctionHouse :Party --also makes sure that all parties are legit
issued: Time
--assetIDCode: Text --To check how to create an asset ID
assetDescription : Text
assetDocuments : [Text]
assetDocLink : Text --to be reviwed to create a link to the documents
valuableDescription : Text
valuableDocuments : [Text]
valuableDocLink : Text --to be reviwed to create a link to the documents
where
signatory ownerSolicitor --userAdmin
observer owner, auctionHouse
--update and add more documents
choice AddDocuments
: AssetId
with
--assetIDCode:Text AssetIDCode cannot change. How can make sure it's always the same
newAssetDescription: Text
newAssetDocuments:[Text]
newAssetDocLink : Text
newValuableDescription : Text
newValuableDocuments : [Text]
newValuableDocLink : Text
controller ownerSolicitor
do
create this with
assetDescription = newAssetDescription
assetDocuments = assetDocuments ++ newAssetDocuments
assetDocLink = newAssetDocLink
valuableDescription = newValuableDescription
valuableDocuments = valuableDocuments ++ newValuableDocuments
valuableDocLink = newValuableDocLink
--update and remove more documents
choice RemoveDocuments
: AssetId
with
removeAssetDescription: Text
assetDocumentsToRemove:Text
removeAssetDocLink : Text
removeValuableDescription : Text
valuableDocumentsToRemove : Text
removeValuableDocLink : Text
controller ownerSolicitor
do
create this with
assetDescription = removeAssetDescription
assetDocuments = delete assetDocumentsToRemove assetDocuments
assetDocLink = removeAssetDocLink
valuableDescription = removeValuableDescription
valuableDocuments = delete valuableDocumentsToRemove valuableDocuments
valuableDocLink = removeValuableDocLink
-------------------------------------------------------------------------
template AssetDocOffer
with
ownerSolicitor : Party
owner : Party
auctionHouse :Party
issued: Time
newOwnerSolicitor:Party
newOwner:Party
notes: Text
assetDescription : Text
assetDocuments : [Text]
assetDocLink : Text
valuableDescription : Text
valuableDocuments : [Text]
valuableDocLink : Text
where
signatory ownerSolicitor
observer owner,newOwner, auctionHouse
choice Accept : ContractId AssetDocOffer
controller ownerSolicitor, newOwnerSolicitor
do create this with
owner = newOwner
ownerSolicitor= newOwnerSolicitor
@claudia_giannoni
When asking a new question on the forum, please always start a new thread. If you need to reference an existing thread, include a link in your post. Lengthy threads with multiple unrelated or loosely related discussions make a poor forum experience because they’re hard to navigate and because they make the forum less searchable.
To answer your questions,
- Instead of defining in AssetDocOffer template the fields that you defined in Asset template, you can include in AssetDocOffer a field of the type Asset, which will contain a record with the fields defined in Asset template. Then you use this field of the type Asset to get to the values in the fields inside the Asset record. See for example the definition for signatories and observers in the template below. Note that I purposely omitted the Accept choice from this template, as I couldn’t figure out from your code what workflow you’re looking to implement or how this choice fits into it.
template AssetDocOffer
with
asset : Asset
newOwnerSolicitor:Party
newOwner:Party
notes: Text
where
signatory asset.ownerSolicitor
observer asset.owner, newOwner, asset.auctionHouse
- If I understand correctly, you’re looking to establish a persistent exclusive relationship between an AssetDocOffer contract and an Asset contract, right? So that, if you have an AssetDocOffer contract, you can always fetch corresponding Asset contract (which implies there must be no more than one Asset contract corresponding to AssetDocOffer contract). And you’re looking to persist this relationship when what is represented by the Asset contract is mutated (existing Asset contract is archived and in its stead a new Asset contract is created with the values of some fields modified from the original). If this is what you’re looking to achieve, I’d suggest including some sort of a unique identifier in Asset template, and use this identifier as a contract key. E.g. include field named assetIdCode and populate it with off the ledger generated GUID whenever new Asset contract is created. Then you can use this key in AssetDocOffer template to fetch the corresponding Asset contract.
template Asset
with
assetIdCode : Text
ownerSolicitor : Party
...
where
signatory ownerSolicitor --userAdmin
observer owner, auctionHouse
key (ownerSolicitor, assetIdCode) : (Party, Text)
maintainer key._1
...
template AssetDocOffer
with
asset : Asset
newOwnerSolicitor:Party
newOwner:Party
notes: Text
where
signatory asset.ownerSolicitor
observer asset.owner, newOwner, asset.auctionHouse
choice AssetDocOffer_GetAsset : Asset
controller asset.ownerSolicitor
do
(_, asset') <- fetchByKey @Asset (key asset)
return asset'
I hope this helps
Sorry, from now onwards I’ll always start a new topic even for further questions.
Thank you ever so much, what you sent is very very helpful!!
Claudia