Enthusiast Coder
Enthusiast Coder

Reputation: 51

Can date be generated while creating a daml contract

I want to get the system date when the daml contract is created. Is there and way to do that.

Example :-

module ExampleTemplateModule where

template ExampleTemplate

with

    admin: Party 

    todayDate: Date     --- In place of this can I use getDate and get today's date
                       
where

    signatory admin

I know I can do this inside a script-do block, but I want to do it when I have to create a contract. If this is not possible, is there some other way through which I can take system's date while creating daml contracts.

Upvotes: 1

Views: 154

Answers (1)

cocreature
cocreature

Reputation: 811

You cannot get the time directly in a create. However, you can get the time in a choice and then create the contract from that. That choice can either be non-consuming and you only create a single helper contract that you call the choice on or it can be consuming and you can call the choice via createAndExercise. Here is a full example illustrating both options:

module ExampleTemplateModule where

import DA.Date
import Daml.Script

template ExampleTemplate
  with
    admin: Party
    todayDate: Date     --- In place of this can I use getDate and get today's date
  where
    signatory admin

template Helper
  with
    admin : Party
  where
    signatory admin
    nonconsuming choice CreateExampleTemplate : ContractId ExampleTemplate
      controller admin
      do time <- getTime
         create ExampleTemplate with admin = admin, todayDate = toDateUTC time
    choice CreateExampleTemplate' : ContractId ExampleTemplate
      controller admin
      do time <- getTime
         create ExampleTemplate with admin = admin, todayDate = toDateUTC time


test = script do
  p <- allocateParty "p"
  helper <- submit p $ createCmd (Helper p)
  ex1 <- submit p $ exerciseCmd helper CreateExampleTemplate
  e2 <- submit p $ createAndExerciseCmd (Helper p) CreateExampleTemplate'
  pure ()

Upvotes: 1

Related Questions