moduleinterfacepactkadena

Storing and retrieving a module reference in PACT


I have an interface which is implemented by a module.

(interface my_interface
    (defun balance:decimal())
)

(module IMPL 'admin
  (implements my_interface)

  (defun balance:decimal() 1.1)
)

In another module, I have a schema which includes a module reference. I have some functions to store and read that reference.

(defschema schema
    token:module{my_interface}
)

(deftable table:{schema})

(defun i (m:module{my_interface})
    (insert table "key" {
        "token":m
      }
    )
    (m::balance)
)
(defun r:decimal ()
    (with-read table "key" {
      "token":=token
     }
     (token::balance)
    )
)

The first dereference (with the param being inserted) works but the second one (reading from database) fails with error:

"resolveRef: dynamic ref not found: token::balance"

But, the PACT lang docs states:

Module references can be used as normal pact values, which includes storage in the database.

What am I doing wrong? Any help will be appreciated!

Full code:

(define-keyset 'admin (read-keyset "admin-2596680724"))


(interface my_interface
    (defun balance:decimal())
)

(module IMPL 'admin
  (implements my_interface)

  (defun balance:decimal() 1.1)
)


(module MY_MODULE 'admin

    (defschema schema
        token:module{my_interface}
    )
    
    (deftable table:{schema})
    
    (defun i (m:module{my_interface})
        (insert table "key" {
            "token":m
          }
        )
        (m::balance)
    )
    (defun r:decimal ()
        (with-read table "key" {
          "token":=token
         }
         (token::balance)
        )
    )
)

(create-table table)

Solution

  • To solve the issue I added a function balance to the module so I can specify the type exactly:

    (defun r:decimal ()
        (with-read table "key" {
          "token":=token
         }
         (balance token)
        )
    )
    
    (defun balance (t:module{my_interface})
        (t::balance)
    )
    

    Source: "Successful typechecking is usually a matter of providing schemas for all tables, and argument types for ancillary functions that call ambiguous or overloaded native functions."

    https://pact-language.readthedocs.io/en/latest/pact-reference.html?highlight=select#static-type-inference-on-modules