如何在pact-lang(智能合约)中创建特定类型的实例? PACT的文档说您可以使用架构键入对象。我一直无法确定如何做到这一点。 创建对象时,例如。在repl中,带有例如。 {“ A”:1.0,“ B”:2.0},...

问题描述 投票:0回答:1
object:*

。 我有一个架构:

(defschema data a : decimal b : decimal )

使用以下方式:
(defschema table-schema
  v : {data}
)
(deftable my-table:{table-schema})

请注意:以上不键入检查(请参见下面的depp upputs)

我找不到将其插入该表的方法,因为它会产生类型错误:
Type error: expected (defschema data  [a:decimal, b:<a>]), found object:* at : (insert (deftable my-table:(defschema table-schema  [v:(de... "123" {"v": {"value": 100.0,"rate": 0.5}}

先前谢谢。
Full代码:

(define-keyset 'admin-keyset (read-keyset "admin-keyset")) (module complex-schema 'admin-keyset (defschema data a : decimal b ) (defschema table-schema v : {data} ) (deftable my-table:{table-schema}) (defun new-thing (id:string) (insert my-table id { "v" : {"value":100.0,"rate":0.5} }) ) )

取消输出/相互作用

pact> (load "complex-schema.repl")
"Loading complex-schema.repl..."
"Setting transaction keys"
"Setting transaction data"
"Begin Tx 0"
"Loading complex-schema.pact..."
"Keyset defined"
"Loaded module complex-schema, hash tGl1sYgL1VWh8zBJCU3KmMGbofkzK0gBEyXwbRnp7sI"
"Typecheck complex-schema: Unable to resolve all types"
:OutputFailure: Unable to unify field type: (v, {complex-schema.data}, (object:*, Object object5::object:* {"rate": Prim decimal6::decimal = LDecimal {_lDecimal = 0.5},"value": Prim decimal7::decimal = LDecimal {_lDecimal = 100.0}}))
"Verification of complex-schema failed"
:OutputFailure: Unable to unify field type: (v, {complex-schema.data}, (object:*, Object object4::object:* {"rate": Prim decimal5::decimal = LDecimal {_lDecimal = 0.5},"value": Prim decimal6::decimal = LDecimal {_lDecimal = 100.0}}))
"Commit Tx 0"
"Begin Tx 1"
"Using complex-schema"
"TableCreated"
"Commit Tx 1"
pact> (use complex-schema)
"Using complex-schema"
pact> (new-thing "123")
<interactive>:1:0: Type error: expected (defschema data  [a:decimal, b:<a>]), found object:*
 at : (insert (deftable my-table:(defschema table-schema  [v:(de... "123" {"v": {"value": 100.0,"rate": 0.5}})
 at <interactive>:0:0: (new-thing "123")

我不确定这是否正是您想要的,因为我在表上遗漏了显式类型的定义。但是它使我可以加载您的模块并调用其功能,以将记录插入

my-table

(module complex-schema 'admin-keyset
   (defschema data
   a : decimal
   b   )
   (defschema table-schema
    v   
   )   
   (deftable my-table:{table-schema}) 
   (defun new-thing (id:string) 
   (insert my-table id 
    {       
    "v" : {"a":100.0,"b":0.5}     
    })
   )   
   (defun read-thing (id:string) 
    (with-read my-table id { "v":= thing:{data} }
    (format "Property a is {} b is {}" [(at 'a thing) (at 'b thing)])) 
   ) 
)
"Loaded module complex-schema, hash BuS1-Ez49mdvVuflRmWmJBJMmgq0nOl3XqAeSGLx_fM"
pact>
(create-table my-table)
"TableCreated"
pact>
(new-thing "test")
"Write succeeded"
pact> (read-thing "test")
"Property a is 100.0 b is 0.5"

您可以在阅读对象时应用类型的定义:
(with-read my-table id { "v":= thing:{data} } ..
smartcontracts pact-lang kadena
1个回答
0
投票
然后将对象的属性与

(at 'property objectname)

一起使用
    

这样做是这样的:

(defschema table-schema v : object{data} )

	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.