Hi @krmmalik,
Struggling through building your own application is always going to be much more enlightening than reading documentation or following in predetermined footsteps. That said, if you do have any suggestion for improving either the documentation or the tutorials, we’re always happy to receive those.
Regarding data modelling, there are a few key concepts at play.
A data declaration is the primary way to define a composite data structure in Daml. That is, any time you think two (or more) pieces of information should be consider “together”, the go-to solution for that is data
. You want to store information about a person?
data Person = Person with age: Int, name: Text
A template is a representation of a contract. When you define a template, Daml will automatically create for you an equivalent data declaration, which I will refer to in the following as a contract payload. The template defines what kind of contracts you can store on the ledger; contracts are always on the ledger and never in your code. However, you can read data from the ledger and store a copy in your code in the form of a contract payload.
The issue is that the existence of a piece of data in your code that happens to have the right shape to be the payload of a contract in no way indicates the existence of a contract with corresponding data on the ledger.
There is also, in the general case, no guarantee that, for a given contract that exists on the ledger, there is no other contract with the exact same payload. (Contract keys are an exception to that.) If you want to refer to a specific contract (on the ledger) from your code, you need to use a reference to that contract, which we call a contract ID. There is no way, in Daml code, to create a contract ID from nothing, so when you have a contract ID there is a good chance there was, at some point, a contract with that ID on the ledger. You can, from your Daml code, verify the existence of that contract and get the corresponding payload (fetch
).
The issue I was pointing out in your code is that you seem to be using template
declarations to define, say, Agent
, but then you only use the Agent
data type, with no relation to the ledger.
Specifically, in:
template Agent with
agentname: Party
where
signatory agentname
template Investor with
agent: Agent
investorname: Party
where
signatory investorname
the field agent
in Investor
is of the data type that corresponds to the payload that a contract of type Agent
could have, but does not necessarily correspond to any Agent
contract on the ledger, so you may believe you have a lot more consistency guarantees than you actually do.
Is this any clearer? I guess the main point of confusion I can see here is that there are multiple “namespaces” at play, i.e. the data type Agent
is only tangentially related to the template type Agent
. This ambiguity is very useful in practice once you understand how all the pieces fit together, but I can see how it could be an obstacle to getting to that understanding in the first place.