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

sBTC Alpha Testnet Contract #3499

Closed
wants to merge 43 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
9257369
first pass sbtc key admin contract
xoloki Jan 17, 2023
88005e6
get-coordinator-key was returning a signer key
xoloki Jan 18, 2023
1f4b94b
add unit tests
xoloki Jan 18, 2023
f91263c
add ft trait
xoloki Jan 18, 2023
661112d
remove trait-impl since it isn't working
xoloki Jan 18, 2023
b04c477
start renaming
xoloki Jan 18, 2023
ab52440
keep renaming
xoloki Jan 18, 2023
333264a
keep renaming
xoloki Jan 18, 2023
301747f
finish renaming
xoloki Jan 18, 2023
46a83d2
keep renaming
xoloki Jan 18, 2023
c659730
finish renaming
xoloki Jan 18, 2023
219d11d
add delete-signer-key and a test for it
xoloki Jan 18, 2023
3870a44
impl ft trait
xoloki Jan 18, 2023
e80af39
remove mint func in favor of trait token-credit
xoloki Jan 18, 2023
ee66bf3
still need mint
xoloki Jan 18, 2023
f4bd17a
add test for mint
xoloki Jan 18, 2023
cc61cb6
check for 0 balance before mint
xoloki Jan 18, 2023
3dac929
check result from mint
xoloki Jan 18, 2023
59b818e
enforce BTC max satoshi limit
xoloki Jan 20, 2023
03e7d47
add token uri
xoloki Jan 20, 2023
5887bdd
add burn! function; store principal and pubkey for coordinator and si…
xoloki Jan 20, 2023
13c61d2
define err constants to just be the uints, and explicitly add the err…
xoloki Jan 20, 2023
02cfb9d
add burn test to make sure it fails if balance is insufficient
xoloki Jan 20, 2023
6956d7b
make 1 sBTC = 1 BTC; assert valid caller don't use if/else; add separ…
xoloki Jan 20, 2023
c5c106f
fix is-coordinator to use optional match syntax
xoloki Jan 20, 2023
b5bf535
gate mint/burn on coordinator not contract owner; set coordinator in …
xoloki Jan 20, 2023
68fa718
update token-uri to point to whitepaper not repo
xoloki Jan 20, 2023
ef0b46b
add vars num-keys num-parties and threshold; add setters
xoloki Jan 20, 2023
be99984
add getters and tests for num-keys/num-parties/threshold
xoloki Jan 20, 2023
a91e677
add func to test for valid signer id; assert valid signer id in set/d…
xoloki Jan 23, 2023
eacdaba
make contract owner var not const, and allow setting it by current co…
xoloki Jan 23, 2023
a956ae6
use different error code for invalid-signer-id
xoloki Jan 23, 2023
f48546c
add bitcoin-payout-address with accesors and a test
xoloki Jan 24, 2023
b2473b0
change btc addr name to bitcoin-wallet-address
xoloki Jan 26, 2023
9a446fa
gate btc wallet addr setter on coordinator
xoloki Jan 26, 2023
faaa18a
add accessors for trading-halted; add test for r/w of trading-halted;…
xoloki Jan 30, 2023
23b3e95
rename memo field for mint/burn and log it
xoloki Jan 30, 2023
21d527e
add dst/src principals to mint/burn; update mint/burn/transfer tests
xoloki Jan 30, 2023
c22af4a
rewrote test and now it's passing
xoloki Jan 30, 2023
55ce020
add more checks in trading halted test
xoloki Jan 30, 2023
c4d0f45
uncomment memo print
xoloki Jan 30, 2023
0791bf8
check bob balance after transfer
xoloki Jan 30, 2023
d419542
Merge branch 'next' into sbt-key-admin
jcnelson Feb 21, 2023
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
5 changes: 5 additions & 0 deletions contrib/sbtc/token-key-admin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

**/settings/Mainnet.toml
**/settings/Testnet.toml
.cache/**
history.txt
5 changes: 5 additions & 0 deletions contrib/sbtc/token-key-admin/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

{
"deno.enable": true,
"files.eol": "\n"
}
18 changes: 18 additions & 0 deletions contrib/sbtc/token-key-admin/.vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

{
"version": "2.0.0",
"tasks": [
{
"label": "check contracts",
"group": "test",
"type": "shell",
"command": "clarinet check"
},
{
"label": "test contracts",
"group": "test",
"type": "shell",
"command": "clarinet test"
}
]
}
23 changes: 23 additions & 0 deletions contrib/sbtc/token-key-admin/Clarinet.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[project]
name = 'sbtc-token-key-admin'
description = ''
authors = []
telemetry = true
cache_dir = './.cache'

[[project.requirements]]
contract_id = "SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard"

[contracts.sbtc-token-key-admin]
path = 'contracts/sbtc-token-key-admin.clar'
clarity_version = 1
epoch = 2.0

[repl.analysis]
passes = ['check_checker']

[repl.analysis.check_checker]
strict = false
trusted_sender = false
trusted_caller = false
callee_filter = false
202 changes: 202 additions & 0 deletions contrib/sbtc/token-key-admin/contracts/sbtc-token-key-admin.clar
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@

;; title: sbtc-token-key-admin
;; version:
;; summary:
;; description:

;; traits
;;
(impl-trait 'SP3FBR2AGK5H9QBDH3EEN6DF8EK8JY7RX8QJ5SVTE.sip-010-trait-ft-standard.sip-010-trait)

;; token definitions
;;
(define-fungible-token sbtc u21000000)

;; constants
;;
(define-constant err-invalid-caller u1)
(define-constant err-invalid-signer-id u2)
(define-constant err-not-token-owner u3)
(define-constant err-trading-halted u4)

;; data vars
;;
(define-data-var contract-owner principal tx-sender)
(define-data-var coordinator (optional {addr: principal, key: (buff 33)}) none)
(define-data-var num-keys uint u4000)
(define-data-var num-parties uint u4000)
(define-data-var threshold uint u2800)
(define-data-var bitcoin-wallet-address (optional (string-ascii 72)) none)
(define-data-var trading-halted bool false)

;; data maps
;;
(define-map signers uint {addr: principal, key: (buff 33)})

;; public functions
;;
(define-public (set-contract-owner (owner principal))
(begin
(asserts! (is-contract-owner) (err err-invalid-caller))
(ok (var-set contract-owner owner))
)
)

(define-public (set-coordinator-data (data {addr: principal, key: (buff 33)}))
(begin
(asserts! (is-contract-owner) (err err-invalid-caller))
(ok (var-set coordinator (some data)))
)
)

(define-public (set-num-keys (n uint))
(begin
(asserts! (is-contract-owner) (err err-invalid-caller))
(ok (var-set num-keys n))
)
)

(define-public (set-num-parties (n uint))
(begin
(asserts! (is-contract-owner) (err err-invalid-caller))
(ok (var-set num-parties n))
)
)

(define-public (set-threshold (n uint))
(begin
(asserts! (is-contract-owner) (err err-invalid-caller))
(ok (var-set threshold n))
)
)

(define-public (set-trading-halted (b bool))
(begin
(asserts! (is-coordinator) (err err-invalid-caller))
(ok (var-set trading-halted b))
)
)

(define-public (set-signer-data (id uint) (data {addr: principal, key: (buff 33)}))
(begin
(asserts! (is-contract-owner) (err err-invalid-caller))
(asserts! (is-valid-signer-id id) (err err-invalid-signer-id))
(ok (map-set signers id data))
)
)

(define-public (delete-signer-data (id uint))
(begin
(asserts! (is-contract-owner) (err err-invalid-caller))
(asserts! (is-valid-signer-id id) (err err-invalid-signer-id))
(ok (map-delete signers id))
)
)

(define-public (set-bitcoin-wallet-address (addr (string-ascii 72)))
(begin
(asserts! (is-coordinator) (err err-invalid-caller))
(ok (var-set bitcoin-wallet-address (some addr)))
)
)

(define-public (mint! (amount uint) (dst principal) (peg-in-txid (string-ascii 72)))
(begin
(asserts! (is-coordinator) (err err-invalid-caller))
(print peg-in-txid)
(ft-mint? sbtc amount dst)
)
)

(define-public (burn! (amount uint) (src principal) (peg-out-txid (string-ascii 72)))
(begin
(asserts! (is-coordinator) (err err-invalid-caller))
(print peg-out-txid)
(ft-burn? sbtc amount src)
)
)

(define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34))))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add a trading halt boolean member variable gated to contract owner if we need to disable trading for any reason.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this var with a r/w test, and another test that actually tries to transfer when halted. The first test passes, but the second test goes into what appears to be an infinite recursion loop when we try to do the first transfer.

Can you look and see if I've done something obviously wrong @igorsyl @jcnelson ?

(begin
(asserts! (is-eq tx-sender sender) (err err-not-token-owner))
igorsyl marked this conversation as resolved.
Show resolved Hide resolved
(asserts! (is-eq (var-get trading-halted) false) (err err-trading-halted))
(try! (ft-transfer? sbtc amount sender recipient))
(match memo to-print (print to-print) 0x)
(ok true)
)
)

;; read only functions
;;
(define-read-only (get-coordinator-data)
(var-get coordinator)
)

(define-read-only (get-num-keys)
(var-get num-keys)
)

(define-read-only (get-num-parties)
(var-get num-parties)
)

(define-read-only (get-threshold)
(var-get threshold)
)

(define-read-only (get-trading-halted)
(var-get trading-halted)
)

(define-read-only (get-bitcoin-wallet-address)
(var-get bitcoin-wallet-address)
)

(define-read-only (get-signer-data (signer uint))
(map-get? signers signer)
)

;;(define-read-only (get-signers)
jcnelson marked this conversation as resolved.
Show resolved Hide resolved
;; (map-get? signers)
;;)

(define-read-only (get-name)
(ok "sBTC")
)

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

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

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

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

(define-read-only (get-token-uri)
(ok (some u"https://assets.stacks.co/sbtc.pdf"))
)

;; private functions
;;
(define-private (is-contract-owner)
(is-eq (var-get contract-owner) tx-sender)
)

(define-private (is-coordinator)
(match (var-get coordinator) cdata
(is-eq (get addr cdata) tx-sender)
false
)
)

(define-private (is-valid-signer-id (id uint))
(and (>= id u0) (< id (var-get num-keys)))
)
Loading