From faaa18add34693e5e8c3cf7f4263d3cb9f5e628a Mon Sep 17 00:00:00 2001 From: Joey Yandle Date: Sun, 29 Jan 2023 21:00:14 -0500 Subject: [PATCH] add accessors for trading-halted; add test for r/w of trading-halted; add test that tries to transfer once trading is halted (currently fails in what appears to be infinite recursion --- .../contracts/sbtc-token-key-admin.clar | 20 +++- .../tests/sbtc-token-key-admin_test.ts | 109 +++++++++++++++++- 2 files changed, 121 insertions(+), 8 deletions(-) diff --git a/contrib/sbtc/token-key-admin/contracts/sbtc-token-key-admin.clar b/contrib/sbtc/token-key-admin/contracts/sbtc-token-key-admin.clar index 152c31d5e4..f755b5bc19 100644 --- a/contrib/sbtc/token-key-admin/contracts/sbtc-token-key-admin.clar +++ b/contrib/sbtc/token-key-admin/contracts/sbtc-token-key-admin.clar @@ -17,6 +17,7 @@ (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 ;; @@ -26,6 +27,7 @@ (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 ;; @@ -68,6 +70,13 @@ ) ) +(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)) @@ -91,14 +100,14 @@ ) ) -(define-public (mint! (amount uint)) +(define-public (mint! (amount uint) (memo (string-ascii 72))) (begin (asserts! (is-coordinator) (err err-invalid-caller)) (ft-mint? sbtc amount tx-sender) ) ) -(define-public (burn! (amount uint)) +(define-public (burn! (amount uint) (memo (string-ascii 72))) (begin (asserts! (is-coordinator) (err err-invalid-caller)) (ft-burn? sbtc amount tx-sender) @@ -108,8 +117,9 @@ (define-public (transfer (amount uint) (sender principal) (recipient principal) (memo (optional (buff 34)))) (begin (asserts! (is-eq tx-sender sender) (err err-not-token-owner)) + (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) +;; (match memo to-print (print to-print) 0x) (ok true) ) ) @@ -132,6 +142,10 @@ (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) ) diff --git a/contrib/sbtc/token-key-admin/tests/sbtc-token-key-admin_test.ts b/contrib/sbtc/token-key-admin/tests/sbtc-token-key-admin_test.ts index 74a0e63db7..b04a94cd81 100644 --- a/contrib/sbtc/token-key-admin/tests/sbtc-token-key-admin_test.ts +++ b/contrib/sbtc/token-key-admin/tests/sbtc-token-key-admin_test.ts @@ -212,7 +212,7 @@ Clarinet.test({ receipt.result.expectOk().expectBool(true); block = chain.mineBlock([ - Tx.contractCall("sbtc-token-key-admin", "mint!", [types.uint(1234)], deployer.address), + Tx.contractCall("sbtc-token-key-admin", "mint!", [types.uint(1234), types.ascii("memo")], deployer.address), ]); [receipt] = block.receipts; @@ -243,7 +243,7 @@ Clarinet.test({ receipt.result.expectOk().expectBool(true); block = chain.mineBlock([ - Tx.contractCall("sbtc-token-key-admin", "mint!", [types.uint(1234)], deployer.address), + Tx.contractCall("sbtc-token-key-admin", "mint!", [types.uint(1234), types.ascii("memo")], deployer.address), ]); [receipt] = block.receipts; @@ -255,7 +255,7 @@ Clarinet.test({ balance.result.expectOk().expectUint(1234); block = chain.mineBlock([ - Tx.contractCall("sbtc-token-key-admin", "burn!", [types.uint(4)], deployer.address), + Tx.contractCall("sbtc-token-key-admin", "burn!", [types.uint(4), types.ascii("memo")], deployer.address), ]); [receipt] = block.receipts; @@ -286,7 +286,7 @@ Clarinet.test({ receipt.result.expectOk().expectBool(true); block = chain.mineBlock([ - Tx.contractCall("sbtc-token-key-admin", "mint!", [types.uint(1234)], deployer.address), + Tx.contractCall("sbtc-token-key-admin", "mint!", [types.uint(1234), types.ascii("memo")], deployer.address), ]); [receipt] = block.receipts; @@ -298,7 +298,7 @@ Clarinet.test({ balance.result.expectOk().expectUint(1234); block = chain.mineBlock([ - Tx.contractCall("sbtc-token-key-admin", "burn!", [types.uint(1235)], deployer.address), + Tx.contractCall("sbtc-token-key-admin", "burn!", [types.uint(1235), types.ascii("memo")], deployer.address), ]); [receipt] = block.receipts; @@ -310,3 +310,102 @@ Clarinet.test({ balance.result.expectOk().expectUint(1234); }, }); + +Clarinet.test({ + name: "Ensure trading-halted can be written then read", + async fn(chain: Chain, accounts: Map) { + let deployer = accounts.get("deployer")!; + + let trading_halted = chain.callReadOnlyFn("sbtc-token-key-admin", "get-trading-halted", [], deployer.address); + + trading_halted.result.expectBool(false); + + let block = chain.mineBlock([ + Tx.contractCall("sbtc-token-key-admin", "set-coordinator-data", [types.tuple({addr: types.principal(deployer.address), key: types.buff(0x000000000000000000000000000000000000000000000000000000000000000000)})], deployer.address), + ]); + + let [receipt] = block.receipts; + + receipt.result.expectOk().expectBool(true); + + block = chain.mineBlock([ + Tx.contractCall("sbtc-token-key-admin", "set-trading-halted", [types.bool(true)], deployer.address), + ]); + + [receipt] = block.receipts; + + receipt.result.expectOk().expectBool(true); + + trading_halted = chain.callReadOnlyFn("sbtc-token-key-admin", "get-trading-halted", [], deployer.address); + + trading_halted.result.expectBool(true); + }, +}); + +Clarinet.test({ + name: "Ensure trading can be halted", + async fn(chain: Chain, accounts: Map) { + let deployer = accounts.get("deployer")!; + let alice = accounts.get("wallet_1")!; + let bob = accounts.get("wallet_2")!; + + let balance = chain.callReadOnlyFn("sbtc-token-key-admin", "get-balance", [types.principal(deployer.address)], deployer.address); + + balance.result.expectOk().expectUint(0); + + let block = chain.mineBlock([ + Tx.contractCall("sbtc-token-key-admin", "set-coordinator-data", [types.tuple({addr: types.principal(deployer.address), key: types.buff(0x000000000000000000000000000000000000000000000000000000000000000000)})], deployer.address), + ]); + + let [receipt] = block.receipts; + + receipt.result.expectOk().expectBool(true); + + console.log(receipt); + + block = chain.mineBlock([ + Tx.contractCall("sbtc-token-key-admin", "mint!", [types.uint(1234), types.ascii("memo")], deployer.address), + ]); + + [receipt] = block.receipts; + + receipt.result.expectOk().expectBool(true); + + console.log(receipt); + + balance = chain.callReadOnlyFn("sbtc-token-key-admin", "get-balance", [types.principal(deployer.address)], deployer.address); + + balance.result.expectOk().expectUint(1234); + + block = chain.mineBlock([ + Tx.contractCall("sbtc-token-key-admin", "transfer", [types.uint(1), types.principal(deployer.address), types.principal(alice.address), types.some(types.buff(0xDEADBEEF))], deployer.address), + ]); + console.log(receipt); + + [receipt] = block.receipts; + + receipt.result.expectOk(); + + balance = chain.callReadOnlyFn("btc-token-key-admin", "get-balance", [types.principal(alice.address)], alice.address); + + balance.result.expectOk().expectUint(1); + + block = chain.mineBlock([ + Tx.contractCall("sbtc-token-key-admin", "set-trading-halted", [types.bool(true)], deployer.address), + ]); + + [receipt] = block.receipts; + + receipt.result.expectOk().expectBool(true); + + block = chain.mineBlock([ + Tx.contractCall("sbtc-token-key-admin", "transfer", [types.uint(1), types.principal(deployer.address), types.principal(bob.address), types.some(types.buff(0xDEADBEEF))], deployer.address), + ]); + console.log(receipt); + + [receipt] = block.receipts; + + receipt.result.expectOk(); + + }, +});