问题描述
我想在创建 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 ()