Skip to content

Instantly share code, notes, and snippets.

@thomashoneyman
Created January 25, 2023 01:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomashoneyman/bb9007cb8a986693b76a43820ec50998 to your computer and use it in GitHub Desktop.
Save thomashoneyman/bb9007cb8a986693b76a43820ec50998 to your computer and use it in GitHub Desktop.
Mutual reference in Pact modules
(interface gov-iface
(defun is-allowed:bool (token:string))
(defun multiplier:integer ()))
(interface token-iface
(defun supply:integer ()))
(module gov GOV
(implements gov-iface)
(defcap GOV () true)
; A table from token types to module references
(defschema token-refs-schema ref:module{token-iface})
(deftable token-refs-table:{token-refs-schema})
(defun init (token:string ref:module{token-iface})
(insert token-refs-table token { "ref": ref }))
; A function that, given a token, uses its reference
; while determining if an action is allowed
(defun is-allowed:bool (token:string)
(with-read token-refs-table token { "ref" := modref:module{token-iface} }
(> (modref::supply) 0)))
; A function that doesn't use a token module.
(defun multiplier:integer () 2))
(module token GOV
(implements token-iface)
(defcap GOV () true)
; a table with one key holding a reference to the governance module
(defschema gov-ref-schema ref:module{gov-iface})
(deftable gov-ref-table:{gov-ref-schema})
(defun init (ref:module{gov-iface})
(insert gov-ref-table "gov" { "ref": ref }))
(defun supply:integer ()
(with-read gov-ref-table "gov" { "ref" := modref:module{gov-iface} }
(modref::is-allowed "KDA")
(* (modref::multiplier) 10))))
(create-table gov.token-refs-table)
(create-table token.gov-ref-table)
(token.init gov)
(gov.init "KDA" token)
(load "../refs.pact")
(print (token.supply))
(print (gov.is-allowed "KDA"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment