Joshua Scurr
Joshua Scurr

Reputation: 11

How to create an instance of a specific type in Pact-Lang (Smart Contracts)?

Pact's documentation says you can type objects with schemas. I have been unable to work out how to do this.

When creating an object, eg. in the REPL, with eg. {"a":1.0,"b":2.0}, it is of type object:*.

I have a schema of:

(defschema data
  a : decimal
  b : decimal
)

And a table schema that uses this:

(defschema table-schema
  v : {data}
)
(deftable my-table:{table-schema})

As a note: the above does not type-check (see REPL output below)

I can find no way to insert into this table, as it produces a type error: 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}}

Many thanks in advance.

Full Code:

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

REPL Output/Interaction

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

Upvotes: 1

Views: 75

Answers (2)

It's done like this:

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

Upvotes: 0

georgep
georgep

Reputation: 741

I'm not sure if it's exactly what you want since I left out the explicit type definition on the table. But it allowed me to load your module and call it's function to insert the record in 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"

You can apply the type definition upon reading the object:

(with-read my-table id { "v":= thing:{data} } ..

and then use the properties of the object with (at 'property objectname)

Upvotes: 0

Related Questions