Skip to content

Commit

Permalink
Add trait import and trait implementation contracts
Browse files Browse the repository at this point in the history
This commit adds the following to the example project:

- `ft-transfer-many`: A contract that imports a trait and references it in a
function parameter.
- `rendezvous-token`: A contract that implements the referenced trait.

Additionally, it includes an invariant and a property test for the
ft-transfer-many contract to demonstrate the random selection of trait
implementations in action.
  • Loading branch information
BowTiedRadone committed Jan 24, 2025
1 parent 0868451 commit 33432a1
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 14 deletions.
12 changes: 10 additions & 2 deletions example/Clarinet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ cache_dir = "./.cache"
[[project.requirements]]
contract_id = 'SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG.ststx-token'

[[project.requirements]]
contract_id = 'SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG.sip-010-trait-ft-standard'

[contracts.rendezvous-token]
path = 'contracts/rendezvous-token.clar'
clarity_version = 2
epoch = 2.5

[contracts.cargo]
path = "contracts/cargo.clar"
clarity_version = 3
Expand All @@ -26,8 +34,8 @@ path = "contracts/slice.clar"
clarity_version = 3
epoch = 3.0

[contracts.trait]
path = 'contracts/trait.clar'
[contracts.ft-transfer-many]
path = 'contracts/ft-transfer-many.clar'
clarity_version = 3
epoch = 3.0

Expand Down
21 changes: 21 additions & 0 deletions example/contracts/ft-transfer-many.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(use-trait ft-trait 'SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG.sip-010-trait-ft-standard.sip-010-trait)

(define-public (transfer-many
(transfers-list
(list 5 {token: <ft-trait>, recipient: principal, amount: uint})
)
)
(ok (map transfer transfers-list))
)

(define-private (transfer
(one-transfer {token: <ft-trait>, recipient: principal, amount: uint})
)
(let
(
(ft (get token one-transfer))
(recipient (get recipient one-transfer))
(amount (get amount one-transfer))
)
(contract-call? ft transfer amount tx-sender recipient none))
)
47 changes: 47 additions & 0 deletions example/contracts/ft-transfer-many.tests.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
;; Invariants

(define-read-only (invariant-trait (address principal))
(let
(
(total-supply
(unwrap-panic (contract-call? .rendezvous-token get-total-supply)))
(user-balance
(unwrap-panic
(contract-call?
.rendezvous-token
get-balance
address
)
)
)
)
(<= user-balance total-supply)
)
)

;; Properties

(define-public (test-transfer
(token <ft-trait>) (recipient principal) (amount uint)
)
(let
(
(sender-balance-before
(unwrap-panic (contract-call? token get-balance tx-sender)))
(transfer-result
(transfer {token: token, recipient: recipient, amount: amount}))
(sender-balance-after
(unwrap-panic (contract-call? token get-balance tx-sender)))
)
(match transfer-result
result
(ok
(is-eq
sender-balance-after
(- sender-balance-before amount)
)
)
error (ok false)
)
)
)
41 changes: 41 additions & 0 deletions example/contracts/rendezvous-token.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
(impl-trait 'SP4SZE494VC2YC5JYG7AYFQ44F5Q4PYV7DVMDPBG.sip-010-trait-ft-standard.sip-010-trait)

(define-fungible-token rendezvous)

;; SIP-010 methods.

(define-read-only (get-total-supply)
(ok (ft-get-supply rendezvous))
)

(define-read-only (get-name)
(ok "Rendezvous Token")
)

(define-read-only (get-symbol)
(ok "rendezvous")
)

(define-read-only (get-decimals)
(ok u6)
)

(define-read-only (get-balance (account principal))
(ok (ft-get-balance rendezvous account))
)

(define-read-only (get-token-uri)
(ok (some u""))
)

(define-public (transfer
(amount uint)
(sender principal)
(recipient principal)
(memo (optional (buff 34)))
)
(match (ft-transfer? rendezvous amount sender recipient)
response (ok response)
error (err error)
)
)
3 changes: 0 additions & 3 deletions example/contracts/trait.clar

This file was deleted.

9 changes: 0 additions & 9 deletions example/contracts/trait.tests.clar

This file was deleted.

0 comments on commit 33432a1

Please sign in to comment.