Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 policy examples #183

Merged
merged 41 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
e144239
Fixed-Issuante Toeken tests
Sep 4, 2023
1c07172
Fix typo
Sep 4, 2023
1236dba
Merge branch 'v2' into v2-policies-test
Sep 7, 2023
9708ce4
fix syntax errors
Sep 7, 2023
8f52d60
update onchain-manifest-test
Aug 30, 2023
5d22c3e
add fip to gh workflow
Sep 7, 2023
dadab6c
Revert "enforce that royalty-policy only registers royalty_spec with …
Sep 7, 2023
49d2bdd
enforce precision of max-supply and min-amount
Sep 7, 2023
0c18066
update onchain-manifest INIT-CALL
Sep 7, 2023
ad8c895
merge v2
Sep 15, 2023
702674e
update example policies
Sep 15, 2023
c74009d
add fip readme
Oct 13, 2023
f9c2076
merge v2, update example-policies, add timed-mint-policy
Oct 20, 2023
67e0635
Merge branch 'v2' of https://github.com/kadena-io/marmalade into v2-e…
jermaine150 Dec 21, 2023
66edff5
chore: added .md files and more tests
wooglie Jan 8, 2024
2753df6
feat: soul bound policy example
wooglie Jan 8, 2024
633bf1e
fix: typo in docs
wooglie Jan 8, 2024
e1ba09d
chore: use real accounts in tests
wooglie Jan 9, 2024
713fc65
chore: use real accounts in tests
wooglie Jan 9, 2024
239da63
chore: fixed max-supply enforcment and added more tests
wooglie Jan 9, 2024
58b0108
chore: add more tests to onchain-manifest-policy
wooglie Jan 9, 2024
237ae80
fix: make the token mintable to only once
wooglie Jan 10, 2024
4aed35e
Merge branch 'main' into v2-examples
wooglie Jan 11, 2024
7d4ed78
Merge branch 'v2-examples' into v2-examples-addition
wooglie Jan 11, 2024
f26e2c4
fix: use real account keys
wooglie Jan 11, 2024
b7668d1
Merge branch 'v2-examples' into v2-examples-soul-bound-policy
wooglie Jan 11, 2024
13b64b5
fix: use real account keys
wooglie Jan 11, 2024
855bdd1
chore: require concrete guard policy to be present
wooglie Jan 11, 2024
3ca5c02
fix: typo in docs
wooglie Jan 11, 2024
7af3ad9
Merge pull request #176 from kadena-io/v2-examples-addition
wooglie Jan 16, 2024
9192262
chore: added types and simplified some logic
wooglie Jan 16, 2024
47b0de4
Merge branch 'main' into v2-examples
wooglie Jan 16, 2024
3b82df3
chore: use util functions
wooglie Jan 17, 2024
0c8adec
chore: use custom governance per policy
wooglie Jan 18, 2024
12286d4
Merge branch 'main' into v2-examples
wooglie Jan 18, 2024
29ccf54
feat: add gitub action to test example policies
wooglie Jan 18, 2024
a5820cb
Merge branch 'main' into v2-examples
wooglie Jan 24, 2024
bc92020
chore: rename main github workflow
wooglie Jan 24, 2024
517159e
Merge branch 'v2-examples' into v2-examples-soul-bound-policy
wooglie Jan 25, 2024
9f43bb4
chore: add deployment guard to soul bound policy
wooglie Jan 25, 2024
b63663a
Merge pull request #177 from kadena-io/v2-examples-soul-bound-policy
wooglie Jan 25, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/test-example-policies.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Test example policies

on:
push:
paths:
- "examples/**"
pull_request:
paths:
- "examples/**"

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
test-pact:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Setup Pact
uses: ./.github/actions/setup-pact
with:
version: "4.10.0"

- name: Test fixed-issuance-policy-v1
uses: ./.github/actions/repl
with:
target: examples/policies/fixed-issuance-policy/fixed-issuance-policy-v1.pact

- name: Test onchain-manifest-policy-v1
uses: ./.github/actions/repl
with:
target: examples/policies/onchain-manifest-policy/onchain-manifest-policy-v1.repl

- name: Test timed-mint-policy-v1
uses: ./.github/actions/repl
with:
target: examples/policies/timed-mint-policy/timed-mint-policy-v1.repl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CI
name: Test Marmalade protocol

on:
push:
Expand Down
125 changes: 125 additions & 0 deletions examples/policies/fixed-issuance-policy/fixed-issuance-policy-v1.pact
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
(namespace (read-msg 'ns))

(module fixed-issuance-policy-v1 GOVERNANCE

@doc "Policy for minting with a fixed issuance"

(defconst ADMIN-KS:string "marmalade-examples.fixed-issuance-policy")

(defcap GOVERNANCE ()
(enforce-guard ADMIN-KS))

(implements kip.token-policy-v2)
(use kip.token-policy-v2 [token-info])
(use marmalade-v2.policy-manager)

(defconst FIXED-ISSUANCE-SPEC:string "fixed_issuance_spec")

(defschema supply-schema
max-supply:decimal
min-amount:decimal
precision:integer
)

(deftable supplies:{supply-schema})

(defun get-supply:object{supply-schema} (token:object{token-info})
(read supplies (at 'id token))
)

(defun enforce-init:bool
( token:object{token-info}
)
@doc "The function is run at `create-token` step of marmalade-v2.ledger. \
\ Required msg-data keys: \
\ * fixed_issuance_spec:object{supply-schema} - registers minimum mint \
\ amount, max-supply, and precision information of the created token"
(require-capability (INIT-CALL (at "id" token) (at "precision" token) (at "uri" token) fixed-issuance-policy-v1))
(let* (
(fixed-issuance-spec:object{supply-schema} (read-msg FIXED-ISSUANCE-SPEC))
(min-amount:decimal (at 'min-amount fixed-issuance-spec))
(max-supply:decimal (at 'max-supply fixed-issuance-spec))
(precision:integer (at 'precision fixed-issuance-spec))
)
(enforce (= (at 'precision token) precision) "Invalid Precision")
(enforce (and (= (floor min-amount precision) min-amount) (> min-amount 0.0)) "Invalid min-amount")
(enforce (and (= (floor max-supply precision) max-supply) (>= max-supply 0.0)) "Invalid max-supply")
(insert supplies (at 'id token) {
"min-amount": min-amount
,"max-supply": max-supply
,"precision": precision
}))
true
)

(defun enforce-mint:bool
( token:object{token-info}
account:string
guard:guard
amount:decimal
)
(require-capability (MINT-CALL (at "id" token) account amount fixed-issuance-policy-v1))
(bind (get-supply token)
{ 'min-amount:=min-amount:decimal
, 'max-supply:=max-supply:decimal
}
(enforce (>= amount min-amount) "mint amount < min-amount")
(if (> max-supply 0.0)
(enforce (<= (+ amount (at 'supply token)) max-supply) "Exceeds max supply")
true
)
))

(defun enforce-burn:bool
( token:object{token-info}
account:string
amount:decimal
)
true
)

(defun enforce-offer:bool
( token:object{token-info}
seller:string
amount:decimal
timeout:integer
sale-id:string
)
true
)

(defun enforce-buy:bool
( token:object{token-info}
seller:string
buyer:string
buyer-guard:guard
amount:decimal
sale-id:string )
true
)

(defun enforce-withdraw:bool
( token:object{token-info}
seller:string
amount:decimal
timeout:integer
sale-id:string )
true
)

(defun enforce-transfer:bool
( token:object{token-info}
sender:string
guard:guard
receiver:string
amount:decimal )
true
)
)


(if (read-msg 'upgrade)
["upgrade complete"]
[(create-table supplies) ])

(enforce-guard ADMIN-KS)
158 changes: 158 additions & 0 deletions examples/policies/fixed-issuance-policy/fixed-issuance-policy-v1.repl
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@

;;load policy manager, ledger
(load "../../../pact/marmalade.repl")


(begin-tx "load fixed issuance policy")
(env-data {
"ns": "marmalade-examples"
, "fixed-issuance-policy": ["fixed-issuance-policy"]
, "upgrade": false}
)
(env-sigs [
{ 'key: 'fixed-issuance-policy
,'caps: []
}])

(ns.write-registry (read-msg 'ns) (read-keyset 'fixed-issuance-policy) true)
(define-namespace
(read-msg 'ns)
(read-keyset 'fixed-issuance-policy) (read-keyset 'fixed-issuance-policy)
)

(namespace (read-msg 'ns))

(define-keyset (+ (read-msg 'ns) ".fixed-issuance-policy") (read-keyset 'fixed-issuance-policy))

(load "fixed-issuance-policy-v1.pact")
(typecheck "marmalade-examples.fixed-issuance-policy-v1")

(commit-tx)

(begin-tx "Create token with fixed issuance policy")
(use marmalade-v2.ledger)
(use marmalade-v2.policy-manager)
(use marmalade-examples.fixed-issuance-policy-v1)
(use mini-guard-utils)
(env-data {
"token-id": (create-token-id { 'uri: "test-fixed-issuance-policy-v1-0", 'precision: 0, 'policies: [marmalade-examples.fixed-issuance-policy-v1] } ALWAYS-TRUE),
"token-id-wrong-precision": (create-token-id { 'uri: "test-fixed-issuance-policy-v1-0", 'precision: 1, 'policies: [marmalade-examples.fixed-issuance-policy-v1] } ALWAYS-TRUE ),
"fixed_issuance_spec": {
'max-supply:2.0,
'min-amount:1.0,
'precision:0}
}
)

(env-sigs [])

(expect "create a token with fixed-issuance-policy"
true
(create-token (read-msg 'token-id) 0 "test-fixed-issuance-policy-v1-0" [marmalade-examples.fixed-issuance-policy-v1] ALWAYS-TRUE))

(expect "create-token events"
[ {"name": "marmalade-v2.ledger.TOKEN","params": [(read-msg 'token-id) 0 [marmalade-examples.fixed-issuance-policy-v1] "test-fixed-issuance-policy-v1-0" ALWAYS-TRUE]} ]
(map (remove "module-hash") (env-events true)))

(expect-failure "Precision is incorrect"
"Invalid Precision"
(create-token (read-msg 'token-id-wrong-precision) 1 "test-fixed-issuance-policy-v1-0" [marmalade-examples.fixed-issuance-policy-v1] ALWAYS-TRUE))

(env-data {
"token-id": (create-token-id { 'uri: "test-fixed-issuance-policy-v1-1", 'precision: 0, 'policies: [marmalade-examples.fixed-issuance-policy-v1] } ALWAYS-TRUE),
"fixed_issuance_spec": {
'max-supply:2.0,
'min-amount:0.0,
'precision:0
}
}
)

(expect-failure "min amount is not valid"
"Invalid min-amount"
(create-token (read-msg 'token-id) 0 "test-fixed-issuance-policy-v1-1" [marmalade-examples.fixed-issuance-policy-v1] ALWAYS-TRUE))

(env-data {
"token-id": (create-token-id { 'uri: "test-fixed-issuance-policy-v1-1", 'precision: 1, 'policies: [marmalade-examples.fixed-issuance-policy-v1] } ALWAYS-TRUE),
"fixed_issuance_spec": {
'max-supply:0.0,
'min-amount:1.0,
'precision:1
}
}
)

(expect "Create token succeeds with max-supply 0"
true
(create-token (read-msg 'token-id) 1 "test-fixed-issuance-policy-v1-1" [marmalade-examples.fixed-issuance-policy-v1] ALWAYS-TRUE))

(expect "create-token events"
[ {"name": "marmalade-v2.ledger.TOKEN","params": [(read-msg 'token-id) 1 [marmalade-examples.fixed-issuance-policy-v1] "test-fixed-issuance-policy-v1-1" ALWAYS-TRUE]} ]
(map (remove "module-hash") (env-events true)))

(commit-tx)

(begin-tx "Mint token")

(use marmalade-v2.ledger)
(use marmalade-v2.policy-manager)
(use marmalade-examples.fixed-issuance-policy-v1)
(use mini-guard-utils)

(env-data {
"token-id": (create-token-id { 'uri: "test-fixed-issuance-policy-v1-2", 'precision: 1, 'policies: [marmalade-examples.fixed-issuance-policy-v1] } ALWAYS-TRUE),
"token-id-wrong-precision": (create-token-id { 'uri: "test-fixed-issuance-policy-v1-2", 'precision: 1, 'policies: [marmalade-examples.fixed-issuance-policy-v1] } ALWAYS-TRUE ),
"fixed_issuance_spec": {
'max-supply:1.0,
'min-amount:0.5,
'precision:1}
}
)

(env-sigs [])

(expect "create a token with fixed-issuance-policy"
true
(create-token (read-msg 'token-id) 1 "test-fixed-issuance-policy-v1-2" [marmalade-examples.fixed-issuance-policy-v1] ALWAYS-TRUE))

(expect "create-token events"
[ {"name": "marmalade-v2.ledger.TOKEN","params": [(read-msg 'token-id) 1 [marmalade-examples.fixed-issuance-policy-v1] "test-fixed-issuance-policy-v1-2" ALWAYS-TRUE]} ]
(map (remove "module-hash") (env-events true)))

(env-data {
"token-id": (create-token-id { 'uri: "test-fixed-issuance-policy-v1-2", 'precision: 1, 'policies: [marmalade-examples.fixed-issuance-policy-v1] } ALWAYS-TRUE)
,"account": "k:e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3"
,"account-guard": {"keys": ["e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3"], "pred": "keys-all"}
}
)

(env-sigs [
{ 'key: 'account
,'caps: [(marmalade-v2.ledger.MINT (read-msg 'token-id) (read-msg 'account) 2.0)]
}])

(expect-failure "Fail if minting more than max-supply"
"Exceeds max supply"
(mint (read-msg 'token-id) (read-msg 'account) (read-keyset 'account-guard ) 2.0))

(env-sigs [
{ 'key: 'account
,'caps: [(marmalade-v2.ledger.MINT (read-msg 'token-id) (read-msg 'account) 1.0)]
}])

(expect-failure "Fail if minting less than min-amount"
"mint amount < min-amount"
(mint (read-msg 'token-id) (read-msg 'account) (read-keyset 'account-guard ) 0.2))


(expect "Minted successfully"
true
(mint (read-msg 'token-id) (read-msg 'account) (read-keyset 'account-guard ) 1.0))

(expect "mint events"
[ {"name": "marmalade-v2.ledger.MINT","params": [(read-msg 'token-id) (read-msg 'account) 1.0]}
{"name": "marmalade-v2.ledger.ACCOUNT_GUARD","params": [(read-msg 'token-id) (read-msg 'account) (read-keyset 'account-guard)]}
{"name": "marmalade-v2.ledger.RECONCILE","params": [(read-msg 'token-id) 1.0 {"account": "","current": 0.0,"previous": 0.0} {"account": "k:e4c6807d79d8bf4695e10e5678ebf72862f59b71f971d39dd3349f4beeacd6e3","current": 1.0,"previous": 0.0}]} {"name": "marmalade-v2.ledger.SUPPLY","params": [(read-msg 'token-id) 1.0]}]
(map (remove "module-hash") (env-events true)))

(rollback-tx)
37 changes: 37 additions & 0 deletions examples/policies/fixed-issuance-policy/fixed-issuance-policy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

# Fixed Issuance Policy

Fixed Issuance Policy is an example module of Marmalade which provides a simple method to program a fractional token. Because there are various ways to program fractional tokens, the contract only provides the simplest feature that is required for fractional tokens, and encourages users to extend upon the
contract.

## Specification, tables, capabilities:

**Schemas**: `supply-schema` is a schema that store information related to the issuance of the token.
- `max-supply`: refers to the maximum number of supply that tokens can be minted to. If max-supply equals `0.0`, then the token is considered to have unlimited supply .
- `min-amount`: min-amount is the minimum amount that the token can be minted with.
- `precision`: precision is the same precision used in `marmalade-v2.ledger`. It is enforced that the two precisions equal each other.

**Tables**: `supplies` table stores token supply information.

**Capabilities**:
- `GOVERNANCE`: enforces access control of contract upgrade.

## Policy Functions

**`enforce-init`:** Run during `marmalade-v2.ledger.create-token`, and registers the token's supply information.

**`enforce-mint`:** Enforced during `marmalade-v2.ledger.mint`, and enforces the mint logic following the provided supply information

**`enforce-burn`:** Enabled without limitation. In order to program burn, the token must incorporate a different policy that programs burn.

**`enforce-offer`:** Enabled without limitation. In order to program offer, the token must incorporate a different policy that programs offer.

**`enforce-buy`:** Enabled without limitation. In order to program buy, the token must incorporate a different policy that programs buy.

**`enforce-withdraw`:** Enabled without limitation. In order to program withdraw, the token must incorporate a different policy that programs withdraw.

**`enforce-transfer`:** Enabled without limitation. In order to program transfer, the token must incorporate a different policy that programs transfer.

## Custom functions

**`get-supply:`** This function is used to retrieve supply information from the `token_spec`
Loading
Loading