Skip to content

Commit

Permalink
Clarity Setup Continued - Deposit Contract (#140)
Browse files Browse the repository at this point in the history
* feat: initial registry contract and tests setup

* feat: add github action for contract tests

* deposit contract setup kickoff

* calling into registry from deposit

* pre-rebase updates

* fix broken update

* new complete-deposit checks

* updated print & clarigen types

* first test running

* added getter test

* addressed comments, minus removed tests

* added back removed test

* setup two of three remaining tests

* added remaining tests

* finished print test, ready for rereview

* fixed type error

* last items

---------

Co-authored-by: Hank Stoever <[email protected]>
  • Loading branch information
setzeus and hstove authored May 10, 2024
1 parent d51595b commit 996548d
Show file tree
Hide file tree
Showing 9 changed files with 446 additions and 37 deletions.
1 change: 1 addition & 0 deletions contracts/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/clarigen-types.ts
5 changes: 5 additions & 0 deletions contracts/Clarinet.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ path = 'contracts/sbtc-bootstrap-signers.clar'
clarity_version = 2
epoch = 2.5

[contracts.sbtc-deposit]
path = 'contracts/sbtc-deposit.clar'
clarity_version = 2
epoch = 2.5

[contracts.sbtc-registry]
path = 'contracts/sbtc-registry.clar'
clarity_version = 2
Expand Down
59 changes: 59 additions & 0 deletions contracts/contracts/sbtc-deposit.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
;; sBTC Deposit contract

;; constants
(define-constant txid-length u32)

;; error codes
(define-constant ERR_TXID_LEN (err u300))
(define-constant ERR_DEPOSIT_REPLAY (err u301))

;; data vars
;;

;; data maps
;;

;; public functions

;; Accept a new deposit request
;; Note that this function can only be called by the current
;; bootstrap signer set address - it cannot be called by users directly.
;;
;; This function handles the validation & minting of sBTC, it then calls
;; into the sbtc-registry contract to update the state of the protocol
(define-public (complete-deposit-wrapper (txid (buff 32)) (vout-index uint) (amount uint) (recipient principal))
(let
(
(replay-fetch (contract-call? .sbtc-registry get-completed-deposit txid vout-index))
)

;; TODO
;; Check that tx-sender is the bootstrap signer

;; 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

;; Complete the deposit
(ok (contract-call? .sbtc-registry complete-deposit txid vout-index amount recipient))
)
)

;; Accept multiple new deposit requests
;; Note that this function can only be called by the current
;; bootstrap signer set address - it cannot be called by users directly.
;;
;; This function handles the validation & minting of sBTC by handling multiple (up to 1000) deposits at a time,
;; it then calls into the sbtc-registry contract to update the state of the protocol

;; read only functions
;;

;; private functions
;;

68 changes: 56 additions & 12 deletions contracts/contracts/sbtc-registry.clar
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
;; Error codes

;; Contract caller is not authorized
(define-constant ERR_UNAUTHORIZED u400)
(define-constant ERR_UNAUTHORIZED (err u400))

;; Invalid request ID
(define-constant ERR_INVALID_REQUEST_ID (err u401))
Expand Down Expand Up @@ -35,6 +35,37 @@
;; the deposit was accepted.
(define-map withdrawal-status uint bool)

;; Internal data structure to store completed
;; deposit requests & avoid replay attacks.
(define-map completed-deposits {txid: (buff 32), vout-index: uint}
{
amount: uint,
recipient: principal
}
)

;; Read-only functions

;; Get a withdrawal request by its ID.
;;
;; This function returns the fields of the withrawal
;; request, along with its status.
(define-read-only (get-withdrawal-request (id uint))
(match (map-get? withdrawal-requests id)
request (some (merge request {
status: (map-get? withdrawal-status id)
}))
none
)
)

;; Get a completed deposit by its transaction ID & vout index.
;;
;; This function returns the fields of the completed-deposits map.
(define-read-only (get-completed-deposit (txid (buff 32)) (vout-index uint))
(map-get? completed-deposits {txid: txid, vout-index: vout-index})
)

;; Public functions

;; Store a new withdrawal request.
Expand Down Expand Up @@ -79,18 +110,31 @@
)
)

;; Read-only functions

;; Get a withdrawal request by its ID.
;; Store a new insert request.
;; Note that this function can only be called by other sBTC
;; contracts (specifically the current version of the deposit contract)
;; - it cannot be called by users directly.
;;
;; This function returns the fields of the withrawal
;; request, along with it's status.
(define-read-only (get-withdrawal-request (id uint))
(match (map-get? withdrawal-requests id)
request (some (merge request {
status: (map-get? withdrawal-status id)
}))
none
;; This function does not handle validation or moving the funds.
;; Instead, it is purely for the purpose of storing the completed deposit.
(define-public (complete-deposit
(txid (buff 32))
(vout-index uint)
(amount uint)
(recipient principal)
)
(begin
(try! (validate-caller))
(map-insert completed-deposits {txid: txid, vout-index: vout-index} {
amount: amount,
recipient: recipient
})
(print {
topic: "completed-deposit",
txid: txid,
vout-index: vout-index
})
(ok true)
)
)

Expand Down
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,4 +59,9 @@ plan:
emulated-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
path: contracts/sbtc-registry.clar
clarity-version: 2
- emulated-contract-publish:
contract-name: sbtc-deposit
emulated-sender: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM
path: contracts/sbtc-deposit.clar
clarity-version: 2
epoch: "2.5"
Loading

0 comments on commit 996548d

Please sign in to comment.