Skip to content

Commit

Permalink
add accessors for trading-halted; add test for r/w of trading-halted;…
Browse files Browse the repository at this point in the history
… add test that tries to transfer once trading is halted (currently fails in what appears to be infinite recursion
  • Loading branch information
xoloki committed Jan 30, 2023
1 parent 9a446fa commit faaa18a
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 8 deletions.
20 changes: 17 additions & 3 deletions contrib/sbtc/token-key-admin/contracts/sbtc-token-key-admin.clar
Original file line number Diff line number Diff line change
Expand Up @@ -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
;;
Expand All @@ -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
;;
Expand Down Expand Up @@ -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))
Expand All @@ -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)
Expand All @@ -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)
)
)
Expand All @@ -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)
)
Expand Down
109 changes: 104 additions & 5 deletions contrib/sbtc/token-key-admin/tests/sbtc-token-key-admin_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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<string, Account>) {
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<string, Account>) {
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();

},
});

0 comments on commit faaa18a

Please sign in to comment.