Creating contracts via their interface in Daml Script

Hello,

I was building a test on Daml Scripts to create a contract via its interface, much like you can within choices. But then I got an error, because the interface does not extend the HasAgreement type class.
My question is, does the Daml.Script createCmd function require this type constraint, since neither the regular create or the Daml.Trigger createCmd need it?

Thank you

Hello David,

I was building a test on Daml Scripts to create a contract via its interface, much like you can within choices.

Daml Script commands are designed to closely match ledger API commands rather than Daml updates. Since interface values are not serializable, it’s not possible to create an interface directly through the ledger API. Therefore, there’s no real point in trying to test a create by interface with a Script command. This is different from “exercise by interface” that can actually be done through the ledger API.

If you really want to test the creation by interface, you will have to create a helper template, something like this:

template Helper
  with
    p : Party
  where
    signatory p
    choice CreateByInterface : ()
      controller p
      do
        let asset = Asset with
          issuer = p
          owner = p
          amount = 15
        let token = toInterface @Token asset
        cidToken <- create token
        pure ()

assuming Asset is a template implementing the interface Token.

Does the Daml.Script createCmd function require this type constraint, since neither the regular create or the Daml.Trigger createCmd need it?

The HasAgreement restriction is not arbitrary but is there to ensure that the type being created is indeed a template (not an interface), aligning with the ledger API’s requirements.

I do not think the comparison with Daml Update (like the regular create) is very relevant, as the script has to interact with the ledger through the ledger API.

In the context of Daml Triggers, the Trigger runner works around this limitation by internally converting the “create by interface” into a “create by template”. However, I’m not sure if this was a step forward as it creates confusion with Daml script and it is probably not so useful.

I hope this helps!

Cheers

2 Likes

In fact, it appears that Daml Triggers createCmd does require the HasAgreement type constraint. Trying to create an interface with it, will result in a crash.

Thank you for highlighting the discrepancy between Script and Triggers, this has led us to the discovery of this bug.

I pushed I bug fix.

2 Likes