可以在创建 daml 合同时生成日期

问题描述

我想在创建 daml 合同时获取系统日期。有没有办法做到这一点。

示例:-

module ExampleTemplateModule where

模板 ExampleTemplate

with

    admin: Party 

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

    signatory admin

我知道我可以在脚本执行块中执行此操作,但是我想在必须创建合同时执行此操作。如果这是不可能的,我是否可以通过其他方式在创建 daml 合同时获取系统日期。

解决方法

您无法直接在创建中获取时间。但是,您可以在选择中获得时间,然后从中创建合同。该选择可以是非消耗性的并且您只创建一个单独的辅助合约来调用该选项,或者它可以是消耗性的并且您可以通过 createAndExercise 调用该选项。以下是说明这两个选项的完整示例:

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 ()

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...