Skip to content

Commit

Permalink
Import sbtc-token contract & update sbtc-deposit (#185)
Browse files Browse the repository at this point in the history
* successfully imported & updated registry with protocol map

* added sbtc-token logic to sbtc-deposit contract

* updated token test

* added more tests

* removed unused imports

* addressed comments

* additional coverage

* import-sbtc-token

* removed unused

* fix: remove contract auto-generated docs

* updated token error order

* fix: re-run clarigen to fix tests

---------

Co-authored-by: Hank Stoever <[email protected]>
  • Loading branch information
setzeus and hstove authored May 28, 2024
1 parent 0277a7f commit b285d91
Show file tree
Hide file tree
Showing 11 changed files with 730 additions and 675 deletions.
4 changes: 4 additions & 0 deletions contracts/Clarinet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ path = 'contracts/sbtc-withdrawal.clar'
clarity_version = 2
epoch = 2.5

[contracts.sbtc-token]
path = 'contracts/sbtc-token.clar'
clarity_version = 2
epoch = 2.5
[repl.analysis]
passes = ['check_checker']

Expand Down
7 changes: 6 additions & 1 deletion contracts/contracts/sbtc-deposit.clar
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

;; The required length of a txid
(define-constant txid-length u32)
(define-constant dust-limit u546)

;; error codes

;; TXID used in deposit is not the correct length
(define-constant ERR_TXID_LEN (err u300))
;; Deposit has already been completed
(define-constant ERR_DEPOSIT_REPLAY (err u301))
(define-constant ERR_LOWER_THAN_DUST (err u302))

;; data vars

Expand All @@ -32,14 +34,17 @@
;; TODO
;; Check that tx-sender is the bootstrap signer

;; Check that amount is greater than dust limit
(asserts! (> amount dust-limit) ERR_LOWER_THAN_DUST)

;; Check that txid is the correct length
(asserts! (is-eq (len txid) txid-length) ERR_TXID_LEN)

;; Assert that the deposit has not already been completed (no replay)
(asserts! (is-none replay-fetch) ERR_DEPOSIT_REPLAY)

;; TODO
;; Mint the sBTC to the recipient
(try! (contract-call? .sbtc-token protocol-mint amount recipient))

;; Complete the deposit
(ok (contract-call? .sbtc-registry complete-deposit txid vout-index amount recipient))
Expand Down
19 changes: 17 additions & 2 deletions contracts/contracts/sbtc-registry.clar
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@
;; stored to avoid replay
(define-map multi-sig-address principal bool)

;; Data structure to store the active protocol contracts
(define-map protocol-contracts principal bool)
(map-set protocol-contracts .sbtc-bootstrap-signers true)
(map-set protocol-contracts .sbtc-deposit true)
(if (not is-in-mainnet) (map-set protocol-contracts tx-sender true) true)

;; Read-only functions
;; Get a withdrawal request by its ID.
;; This function returns the fields of the withrawal
Expand Down Expand Up @@ -171,7 +177,8 @@
(print {
topic: "completed-deposit",
txid: txid,
vout-index: vout-index
vout-index: vout-index,
amount: amount
})
(ok true)
)
Expand Down Expand Up @@ -218,4 +225,12 @@
;; wont be hit
;; (if (is-eq contract-caller .controller) (ok true) (err ERR_UNAUTHORIZED))
(if false ERR_UNAUTHORIZED (ok true))
)
)

;; Checks whether the contract-caller is a protocol contract
(define-read-only (is-protocol-caller (principal-checked principal))
(is-some (map-get? protocol-contracts principal-checked))
)

;; TODO: Add a function to add a protocol contract
;; TODO: Add a function to remove a protocol contract
146 changes: 146 additions & 0 deletions contracts/contracts/sbtc-token.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
(define-constant ERR_NOT_OWNER (err u4)) ;; `tx-sender` or `contract-caller` tried to move a token it does not own.
(define-constant ERR_NOT_AUTH (err u5)) ;; `tx-sender` or `contract-caller` is not the protocol caller

(define-fungible-token sbtc-token)
(define-fungible-token sbtc-token-locked)

(define-data-var token-name (string-ascii 32) "sBTC Mini")
(define-data-var token-symbol (string-ascii 10) "sBTC")
(define-data-var token-uri (optional (string-utf8 256)) none)
(define-constant token-decimals u8)

(define-read-only (is-protocol-caller)
(ok (asserts! (contract-call? .sbtc-registry is-protocol-caller contract-caller) ERR_NOT_AUTH))
)

;; --- Protocol functions

;; #[allow(unchecked_data)]
(define-public (protocol-transfer (amount uint) (sender principal) (recipient principal))
(begin
(try! (is-protocol-caller))
(ft-transfer? sbtc-token amount sender recipient)
)
)

;; #[allow(unchecked_data)]
(define-public (protocol-lock (amount uint) (owner principal))
(begin
(try! (is-protocol-caller))
(try! (ft-burn? sbtc-token amount owner))
(ft-mint? sbtc-token-locked amount owner)
)
)

;; #[allow(unchecked_data)]
(define-public (protocol-unlock (amount uint) (owner principal))
(begin
(try! (is-protocol-caller))
(try! (ft-burn? sbtc-token-locked amount owner))
(ft-mint? sbtc-token amount owner)
)
)

;; #[allow(unchecked_data)]
(define-public (protocol-mint (amount uint) (recipient principal))
(begin
(try! (is-protocol-caller))
(ft-mint? sbtc-token amount recipient)
)
)

;; #[allow(unchecked_data)]
(define-public (protocol-burn (amount uint) (owner principal))
(begin
(try! (is-protocol-caller))
(ft-burn? sbtc-token amount owner)
)
)

;; #[allow(unchecked_data)]
(define-public (protocol-burn-locked (amount uint) (owner principal))
(begin
(try! (is-protocol-caller))
(ft-burn? sbtc-token-locked amount owner)
)
)

;; #[allow(unchecked_data)]
(define-public (protocol-set-name (new-name (string-ascii 32)))
(begin
(try! (is-protocol-caller))
(ok (var-set token-name new-name))
)
)

;; #[allow(unchecked_data)]
(define-public (protocol-set-symbol (new-symbol (string-ascii 10)))
(begin
(try! (is-protocol-caller))
(ok (var-set token-symbol new-symbol))
)
)

;; #[allow(unchecked_data)]
(define-public (protocol-set-token-uri (new-uri (optional (string-utf8 256))))
(begin
(try! (is-protocol-caller))
(ok (var-set token-uri new-uri))
)
)

(define-private (protocol-mint-many-iter (item {amount: uint, recipient: principal}))
(ft-mint? sbtc-token (get amount item) (get recipient item))
)

;; #[allow(unchecked_data)]
(define-public (protocol-mint-many (recipients (list 200 {amount: uint, recipient: principal})))
(begin
(try! (is-protocol-caller))
(ok (map protocol-mint-many-iter recipients))
)
)

;; --- Public functions

;; sip-010-trait

;; #[allow(unchecked_data)]
(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34))))
(begin
(asserts! (or (is-eq tx-sender sender) (is-eq contract-caller sender)) ERR_NOT_OWNER)
(ft-transfer? sbtc-token amount sender recipient)
)
)

(define-read-only (get-name)
(ok (var-get token-name))
)

(define-read-only (get-symbol)
(ok (var-get token-symbol))
)

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

(define-read-only (get-balance (who principal))
(ok (+ (ft-get-balance sbtc-token who) (ft-get-balance sbtc-token-locked who)))
)

(define-read-only (get-balance-available (who principal))
(ok (ft-get-balance sbtc-token who))
)

(define-read-only (get-balance-locked (who principal))
(ok (ft-get-balance sbtc-token-locked who))
)

(define-read-only (get-total-supply)
(ok (+ (ft-get-supply sbtc-token) (ft-get-supply sbtc-token-locked)))
)

(define-read-only (get-token-uri)
(ok (var-get token-uri))
)
5 changes: 5 additions & 0 deletions contracts/deployments/default.simnet-plan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ plan:
emulated-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
path: contracts/sbtc-bootstrap-signers.clar
clarity-version: 2
- emulated-contract-publish:
contract-name: sbtc-token
emulated-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
path: contracts/sbtc-token.clar
clarity-version: 2
- emulated-contract-publish:
contract-name: sbtc-deposit
emulated-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
Expand Down
112 changes: 0 additions & 112 deletions contracts/docs/sbtc-deposit.md

This file was deleted.

Loading

0 comments on commit b285d91

Please sign in to comment.