Welcome to this introduction to some more advanced Pact built-in functions.
In this guide, we will go through and explain specific built-in functions that are listed in the pact reference page.
Simple Payment Verification
The quick explanation of the verify-spv
function can be found
here.
verify-spv
takes some blob, a binary data type, provided by the user and runs
code on it that would be too expensive to do in pact. Thus, in the statement
(verify-spv "ETH")
, "ETH" has code in the chainweb-node binary to validate
that the data is well-formed and returns a normal Pact object with all of the
data. It is NOT an oracle; it is a tool that an oracle would use to guarantee
data integrity.
Here
is example code using the chain relay to validate a proof that the sender has
retrieved from infura.
In a REPL script, all you can do is simulate this, as the "ETH" support does not
ship with Pact. The
mock-spv
REPL native allows you to mock a call to verify-spv
(github).
You can simulate any protocol desired. However, getting a protocol added to
Chainweb requires support in the Chainweb binary and is a hard fork. Therefore,
the community would need to spearhead by opening a pull request for a KIP,
Kadena Improvement Process. For instance, to support BTC proofs, a KIP would be
opened to add verify-spv "BTC"
to discuss and specify what is needed.
Afterwards, the Haskell support would need to be implemented and released with a
Chainweb version upgrade. Currently Chainweb supports "ETH" and "TXOUT" only
(github).
;; You write this:
(install-capability (TRANSFER FROM TO PROVIDED))
...
(with-capability (TRANSFER FROM TO REQUESTED) EXPR)
;; ----
;; But what it does internally is more like this:
(install-capability (TRANSFER FROM TO) PROVIDED)
...
(if (already-granted-p (TRANSFER FROM TO))
EXPR
(consume-resource (TRANSFER FROM TO) REQUESTED
(with-capability (TRANSFER FROM TO) EXPR)))
;; You write this:
(install-capability (TRANSFER FROM TO PROVIDED))
...
(with-capability (TRANSFER FROM TO REQUESTED) EXPR)
;; ----
;; But what it does internally is more like this:
(install-capability (TRANSFER FROM TO) PROVIDED)
...
(if (already-granted-p (TRANSFER FROM TO))
EXPR
(consume-resource (TRANSFER FROM TO) REQUESTED
(with-capability (TRANSFER FROM TO) EXPR)))
@managed amount TRANSFER_mgr
@managed amount TRANSFER_mgr
(defun TRANSFER_mgr:decimal (current:decimal requested:decimal)
(defun TRANSFER_mgr:decimal (current:decimal requested:decimal)
The manager function has the job of confirming that sufficient resource exists
and deducting from the resource. It is called whenever with-capability
is used
and the capability has not yet been granted.
Select
The select
built-in function can be found
here.
The select
function is able to pull information from a table under specific
conditions.
This is an example of finding people in a table with a single condition, having "Fatima" as their first or last name.
(select people ['firstName,'lastName] (where 'name (= "Fatima")))
(select people ['firstName,'lastName] (where 'name (= "Fatima")))
But, what if you want to use multiple clauses to get a more specific result.
In this example, you can use the following format to find someone with the name "Fatima" that is older than 40.
(select people ['firstName,'lastName] (and? (where 'name (= "Fatima")) (where 'age (> 40))))
(select people ['firstName,'lastName] (and? (where 'name (= "Fatima")) (where 'age (> 40))))