如何在DAML中扩展默认Party接口,为参与者添加业务属性?
在DAML中关联Party与参与者专属属性的方案
在DAML里,内置的Party类型确实只包含id和name,没法直接扩展属性。要给贷方、借方这类参与者绑定法律实体ID、法定账号等额外信息,最常用的是通过关联合约的方式实现,下面给你两种实用方案:
方案一:创建参与者专属合约
针对贷方、借方这类特定角色,直接定义对应模板,将Party作为核心标识,附加所需属性:
template Lender with lender : Party legalEntityId : Text accountNumber : Text companyId : Text where signatory lender -- 仅允许贷方自己创建/修改该合约,确保数据归属权 key lender : Party maintainer key -- 可选:添加需要查看属性的观察者,比如合作的法务方 observer []
使用方式
其他业务合约(比如贷款合约)可以通过Party引用,再查询或获取对应的属性合约:
template LoanAgreement with lender : Party borrower : Party principal : Decimal where signatory lender, borrower -- 确保贷方已完成属性注册 ensure fetch (Lender lender).accountNumber /= "" -- 业务逻辑中直接调用属性 let lenderLegalId = fetch (Lender lender).legalEntityId
方案二:用接口统一多类型参与者
如果场景中有多种参与者角色(贷方、借方、担保方等),可以定义通用接口来统一属性访问逻辑:
-- 定义通用参与者接口,包含所有角色共享的属性 interface Participant where view type ParticipantBaseInfo = with party : Party legalEntityId : Text -- 贷方实现接口,附加专属属性 template Lender implements Participant with lender : Party legalEntityId : Text accountNumber : Text companyId : Text where signatory lender key lender : Party maintainer key view ParticipantBaseInfo = ParticipantBaseInfo with party = lender, legalEntityId = legalEntityId -- 借方实现接口,附加另一组专属属性 template Borrower implements Participant with borrower : Party legalEntityId : Text taxRegistrationId : Text where signatory borrower key borrower : Party maintainer key view ParticipantBaseInfo = ParticipantBaseInfo with party = borrower, legalEntityId = legalEntityId
使用方式
通过接口可以统一处理不同角色的参与者,无需区分具体模板:
-- 查询所有参与者的基础信息 getAllParticipants : Script [ParticipantBaseInfo] getAllParticipants = do participants <- query @Participant pure $ map view participants
关键注意事项
- 权限控制:属性合约的签名方必须是对应的
Party本身,避免第三方篡改或伪造信息。 - 数据唯一性:通过
key lender : Party绑定,确保每个Party只能对应一份属性合约,避免数据冲突。 - 查询效率:使用
fetch(已知合约存在时)或query(不确定时)获取属性,DAML的键查询能快速定位到对应合约。
内容的提问来源于stack exchange,提问作者Eric Zhu
相关产品推荐
相关产品推荐

