From 6510dfd44adc27161644605b5ace8047508c68ef Mon Sep 17 00:00:00 2001 From: Sander Looijenga Date: Tue, 7 Nov 2023 11:46:59 +0100 Subject: [PATCH] Updates to the Faucet contract --- .../tools/src/pact/faucet/testnet-faucet.pact | 65 ++++++++++++++++++- .../tools/src/pact/faucet/testnet-faucet.repl | 4 +- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/packages/apps/tools/src/pact/faucet/testnet-faucet.pact b/packages/apps/tools/src/pact/faucet/testnet-faucet.pact index c58876cda16..86b561c5bba 100644 --- a/packages/apps/tools/src/pact/faucet/testnet-faucet.pact +++ b/packages/apps/tools/src/pact/faucet/testnet-faucet.pact @@ -1,11 +1,69 @@ (namespace "user") -(module coin-faucet GOVERNANCE +(module coin-faucet-v2 GOVERNANCE "'coin-faucet' represents Kadena's Coin Faucet Contract." + (implements gas-payer-v1) + ;; TODO - use hashed import (use coin) + ; -------------------------------------------------------------------------- + ; Gas station + ; -------------------------------------------------------------------------- + + (defcap ALLOW_GAS () true) + + (defun chain-gas-price () + (at 'gas-price (chain-data)) + ) + + (defun enforce-below-or-at-gas-price:bool (gasPrice:decimal) + (enforce (<= (chain-gas-price) gasPrice) + (format "Gas Price must be smaller than or equal to {}" [gasPrice])) + ) + + (defcap GAS_PAYER:bool + ( user:string + limit:integer + price:decimal + ) + @doc + " Provide a capability indicating that declaring module supports \ + \ gas payment for USER for gas LIMIT and PRICE. Functionality \ + \ should require capability (coin.FUND_TX), and should validate \ + \ the spend of (limit * price), possibly updating some database \ + \ entry. \ + \ Should compose capability required for 'create-gas-payer-guard'." + ; @model + ; [ (property (user != "")) + ; (property (limit > 0)) + ; (property (price > 0.0)) + ; ] + (enforce (= "exec" (at "tx-type" (read-msg))) "Can only be used inside an exec") + (enforce (= 1 (length (at "exec-code" (read-msg)))) "Can only be used to call one pact function") + (enforce (= "(user.coin-faucet-v2." (take 21 (at 0 (at "exec-code" (read-msg))))) "only coin faucet smart contract") + (enforce-below-or-at-gas-price 0.0000001) + (compose-capability (ALLOW_GAS)) + ) + + (defun create-gas-payer-guard:guard () + @doc + " Provide a guard suitable for controlling a coin account that can \ + \ pay gas via GAS_PAYER mechanics. Generally this is accomplished \ + \ by having GAS_PAYER compose an unparameterized, unmanaged capability \ + \ that is required in this guard. Thus, if coin contract is able to \ + \ successfully acquire GAS_PAYER, the composed 'anonymous' cap required \ + \ here will be in scope, and gas buy will succeed." + (create-capability-guard (ALLOW_GAS)) + ) + + (defconst GAS_STATION_ACCOUNT (create-principal (create-gas-payer-guard))) + + (defun init () + (coin.create-account GAS_STATION_ACCOUNT (create-gas-payer-guard)) + ) + ; -------------------------------------------------------------------------- ; Governance ; -------------------------------------------------------------------------- @@ -111,4 +169,7 @@ ) (if (read-msg 'upgrade) ["Upgrade successful"] - [(create-table history-table)]) + [ + (create-table history-table) + (init) + ]) diff --git a/packages/apps/tools/src/pact/faucet/testnet-faucet.repl b/packages/apps/tools/src/pact/faucet/testnet-faucet.repl index 6780bac4e33..07d41ea3b9b 100644 --- a/packages/apps/tools/src/pact/faucet/testnet-faucet.repl +++ b/packages/apps/tools/src/pact/faucet/testnet-faucet.repl @@ -1,5 +1,6 @@ (begin-tx "Load modules") +(load "root/gas-payer-v1.pact") (load "root/fungible-v2.pact") (load "root/fungible-xchain-v1.pact") (load "root/coin-v5.pact") @@ -41,12 +42,11 @@ (env-data {}) (env-sigs [{"key": "whatever", "caps": []}]) -(env-data {"contract-admins": ["whatever"]}) +(env-data {"contract-admins": ["whatever"], "upgrade": false}) (begin-tx "Load the Faucet pact") (load "testnet-faucet.pact") -(create-table history-table) (commit-tx)