Skip to content
CCPEDIAby Unity Nodes
App Development

How can I read the agreement field of a template

5 posts224 views6 likesLast activity Feb 2024

  1. doron_leviOP

    Hello,
    Can someone tell me how can I read in a script the value stored in the agreement field of a contract?
    I can set the value in it, but how can I print it out to the console, to see that the value I am storing in it is correct.

  2. a_putkov

    I don’t believe there’s any way to do this. As far as I can tell, the only way to examine the text in the agreement clause of a Daml template is by looking at the source code of the template.

  3. gyorgybalazsi 4

    You can extract the text of the agreement from the contract payload using the agreement function, see in the Prelude:

    class HasAgreement t where

    Exposes agreement function. Part of the Template constraint.

    agreement
    : t → Text

    The agreement text of a contract

    This is an example template and script:

    module Main where
    import DA.List (head)
    
    import Daml.Script
    
    type AssetId = ContractId Asset
    
    template Asset
      with
        issuer : Party
        owner  : Party
        name   : Text
      where
        ensure name /= ""
        agreement show issuer <> " has issued an asset of name " <> show name <> " for " <> show owner <> " as owner."
        signatory issuer
        observer owner
        choice Give : AssetId
          with
            newOwner : Party
          controller owner
          do create this with
               owner = newOwner
    
    setup : Script ()
    setup = script do
    -- user_setup_begin
      alice <- allocatePartyWithHint "Alice" (PartyIdHint "Alice")
      bob <- allocatePartyWithHint "Bob" (PartyIdHint "Bob")
      aliceId <- validateUserId "alice"
      bobId <- validateUserId "bob"
      createUser (User aliceId (Some alice)) [CanActAs alice]
      createUser (User bobId (Some bob)) [CanActAs bob]
    -- user_setup_end
    
      aliceTV <- submit alice do
        createCmd Asset with
          issuer = alice
          owner = alice
          name = "TV"
    
      bobTVid <- submit alice do
        exerciseCmd aliceTV Give with newOwner = bob
    
      bobTvInstances <- query @Asset bob
    
      debug $ agreement (head bobTvInstances)._2
    

    See the printed agreement text in the transaction view of the script result:

    Transactions: 
      TX 0 1970-01-01T00:00:00Z (Main:36:14)
      #0:0
      │   consumed by: #1:0
      │   referenced by #1:0
      │   disclosed to (since): 'Alice' (0)
      └─> 'Alice' creates Main:Asset
                  with
                    issuer = 'Alice'; owner = 'Alice'; name = "TV"
      
      TX 1 1970-01-01T00:00:00Z (Main:42:14)
      #1:0
      │   disclosed to (since): 'Alice' (1)
      └─> 'Alice' exercises Give on #0:0 (Main:Asset)
                  with
                    newOwner = 'Bob'
          children:
          #1:1
          │   disclosed to (since): 'Alice' (1), 'Bob' (1)
          └─> 'Alice' creates Main:Asset
                      with
                        issuer = 'Alice'; owner = 'Bob'; name = "TV"
    
    Active contracts:  #1:1
    
    Return value: {}
    
    Trace: 
      "'Alice' has issued an asset of name \"TV\" for 'Bob' as owner."
    
  4. a_putkov

    Thank you @gyorgybalazsi. I stand corrected.

  5. Remy 2

    Hi @doron_levi,

    As @a_putkov explained, there is no way to access the stored value of the agreement text from within Daml. You can only recompute it with the agreement function, as @gyorgybalazsi suggested. Note that, since Daml is deterministic, you can be sure that the recomputed version is equal to the stored value.

    The only way to see the actual value stored is through the ledger API.

    However, be aware that the agreement will be deprecated in 2.9, and it will be gone in 3.0, so it is maybe better to not rely on this feature.