DAML:在需要 ContractId 作为输入的场景中创建模板,并在提交后自动执行选择

DAML: Create a template in scenario requiring a ContractId as input and exercise a choice automatically after submit

我有 2 个关于 DAML 自动选择和场景的可能性的问题。 我有这个模板,需要输入 ContractId:

template Create_Creation
  with     
    current_login     : Party
    artist            : Party
    title             : Text
    votingRight       : Set Party
    observers_list_id : ContractId Observers
  where 
    signatory current_login

我需要在场景中创建其中一些模板,但无法指定 ContractId(如 #0:0),给我这样的错误:Couldn't match expected type 'ContractId Observers' with actual type 'Text'Is it possible to specify a ContractId in设想?

接下来,在上面的模板中,我定义了一个名为 Load_all_creation_observerschoice,它创建了一个模板 Creation 并将 template Observers 中指定的观察者作为观察者加载到其中:

 choice Load_all_creation_observers : ContractId Creation 
      controller current_login
      do
        observers_list <- fetch observers_list_id
        create Creation with created_by = current_login; artist = artist; title = title;
        votingRight = votingRight; observers_list_id = observers_list_id; observers = observers_list.observers

template Observers
  with 
    superuser : Party
    observers : Set Party
  where 
    signatory superuser
    observer observers

按照现在的代码,当用户创建 Create_Creation template 时,他需要执行 Load_all_creation_observers 选择以创建 Creation 模板,并加载所有观察者。是否可以在用户提交 Create_Creation 模板时自动执行此选择?或者可能根本不选择它并将其定义为自动化功能,就像您在普通编程语言(if 语句)中所做的那样。您似乎只能在选项中定义 do 函数。

在一个场景中,分类账上唯一存在的合同是迄今为止在该场景中创建的合同。因此,如果有一个 Observers contractId,则必须在场景中的某个先前点创建一个 Observers 合同。

ContractIds 是不透明的,绝对不可预测,所以考虑 contract-id 文字是没有意义的。相反,当创建合约时,在此时绑定生成的 id。即

  test = scenario do
    su <- getParty "Super User"
    obsId <- su submit create Observers with ...
    p1 <- getParty "Party 1"
    ccId <- p1 submit create Create_Creation with ...; observers_list_id = obsId

鉴于关于合约 ID 的问题已经得到解答,我将重点关注您的第二个问题。

你不能自动执行选择(你可以有一些账外自动化,例如,一个 DAML 触发器可以做到这一点,但在这种情况下你得不到任何原子性保证)。我解决这个问题的方法是定义一个只有一个选择的新模板,然后在分类帐 API 上使用 CreateAndExercise 调用该选择。这几乎等同于定义一个顶级函数。对于您的示例,这看起来类似于以下内容

template CreateCreationTemplate
  with
    p : Party
  where
   signatory p
   choice CreateCreation : ContractId Creation
     with
      observers_list_id : ContractId Observers
      artist : Party
      title : Text
      votingRight : Set Party
     do observers_list <- fetch observers_list_id
        create Creation with
          created_by = p
          artist = artist
          title = title
          votingRight = votingRight
          observers_list_id = observers_list_id
          observers = observers_list.observers

您可以将选择的某些字段作为模板的字段,但作为一般准则,我倾向于在模拟顶级函数时仅将派对作为模板的字段。

根据您的使用情况,也可以使用具有非消耗性 CreateCreation 选择的单个“工厂”模板。