Skip to content

Commit

Permalink
fresh branch, updated withdrawal, deposits & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
setzeus committed Nov 7, 2024
1 parent 0d52435 commit 873329b
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 8 deletions.
12 changes: 8 additions & 4 deletions contracts/contracts/sbtc-deposit.clar
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
(amount uint)
(recipient principal)
(burn-hash (buff 32))
(burn-height uint))
(burn-height uint)
(sweep-txid (buff 32)))
(let
(
(current-signer-data (contract-call? .sbtc-registry get-current-signer-data))
Expand All @@ -49,6 +50,9 @@
;; Check that txid is the correct length
(asserts! (is-eq (len txid) txid-length) ERR_TXID_LEN)

;; Check that sweep txid is the correct length
(asserts! (is-eq (len sweep-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)

Expand All @@ -75,7 +79,7 @@
;; 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.
(define-public (complete-deposits-wrapper
(deposits (list 650 {txid: (buff 32), vout-index: uint, amount: uint, recipient: principal, burn-hash: (buff 32), burn-height: uint}))
(deposits (list 650 {txid: (buff 32), vout-index: uint, amount: uint, recipient: principal, burn-hash: (buff 32), burn-height: uint, sweep-txid: (buff 32)}))
)
(begin
;; Check that the caller is the current signer principal
Expand All @@ -90,11 +94,11 @@

;; private functions
;; #[allow(unchecked_data)]
(define-private (complete-individual-deposits-helper (deposit {txid: (buff 32), vout-index: uint, amount: uint, recipient: principal, burn-hash: (buff 32), burn-height: uint}) (helper-response (response uint uint)))
(define-private (complete-individual-deposits-helper (deposit {txid: (buff 32), vout-index: uint, amount: uint, recipient: principal, burn-hash: (buff 32), burn-height: uint, sweep-txid: (buff 32)}) (helper-response (response uint uint)))
(match helper-response
index
(begin
(try! (unwrap! (complete-deposit-wrapper (get txid deposit) (get vout-index deposit) (get amount deposit) (get recipient deposit) (get burn-hash deposit) (get burn-height deposit)) (err (+ ERR_DEPOSIT_INDEX_PREFIX (+ u10 index)))))
(try! (unwrap! (complete-deposit-wrapper (get txid deposit) (get vout-index deposit) (get amount deposit) (get recipient deposit) (get burn-hash deposit) (get burn-height deposit) (get sweep-txid deposit)) (err (+ ERR_DEPOSIT_INDEX_PREFIX (+ u10 index)))))
(ok (+ index u1))
)
err-response
Expand Down
11 changes: 7 additions & 4 deletions contracts/contracts/sbtc-withdrawal.clar
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@
(output-index uint)
(fee uint)
(burn-hash (buff 32))
(burn-height uint))
(burn-height uint)
(sweep-txid (buff 32)))
(let
(
(current-signer-data (contract-call? .sbtc-registry get-current-signer-data))
Expand Down Expand Up @@ -220,7 +221,8 @@
output-index: (optional uint),
fee: (optional uint),
burn-hash: (buff 32),
burn-height: uint})))
burn-height: uint,
sweep-txid: (optional (buff 32))})))
(let
(
(current-signer-data (contract-call? .sbtc-registry get-current-signer-data))
Expand All @@ -242,7 +244,8 @@
output-index: (optional uint),
fee: (optional uint),
burn-hash: (buff 32),
burn-height: uint})
burn-height: uint,
sweep-txid: (optional (buff 32))})
(helper-response (response uint uint)))
(match helper-response
index
Expand All @@ -260,7 +263,7 @@
(asserts!
(and (is-some current-bitcoin-txid) (is-some current-output-index) (is-some current-fee))
(err (+ ERR_WITHDRAWAL_INDEX_PREFIX (+ u10 index))))
(unwrap! (accept-withdrawal-request (get request-id withdrawal) (unwrap-panic current-bitcoin-txid) current-signer-bitmap (unwrap-panic current-output-index) (unwrap-panic current-fee) (get burn-hash withdrawal) (get burn-height withdrawal)) (err (+ ERR_WITHDRAWAL_INDEX_PREFIX (+ u10 index))))
(unwrap! (accept-withdrawal-request (get request-id withdrawal) (unwrap-panic current-bitcoin-txid) current-signer-bitmap (unwrap-panic current-output-index) (unwrap-panic current-fee) (get burn-hash withdrawal) (get burn-height withdrawal) (unwrap-panic (get sweep-txid withdrawal))) (err (+ ERR_WITHDRAWAL_INDEX_PREFIX (+ u10 index))))
)
;; rejected
(unwrap! (reject-withdrawal-request (get request-id withdrawal) current-signer-bitmap) (err (+ ERR_WITHDRAWAL_INDEX_PREFIX (+ u10 index))))
Expand Down
18 changes: 18 additions & 0 deletions contracts/tests/clarigen-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ export const contracts = {
{ name: "burn-hash", type: { buffer: { length: 32 } } },
{ name: "burn-height", type: "uint128" },
{ name: "recipient", type: "principal" },
{ name: "sweep-txid", type: { buffer: { length: 32 } } },
{ name: "txid", type: { buffer: { length: 32 } } },
{ name: "vout-index", type: "uint128" },
],
Expand All @@ -522,6 +523,7 @@ export const contracts = {
burnHash: Uint8Array;
burnHeight: number | bigint;
recipient: string;
sweepTxid: Uint8Array;
txid: Uint8Array;
voutIndex: number | bigint;
},
Expand All @@ -544,6 +546,7 @@ export const contracts = {
{ name: "recipient", type: "principal" },
{ name: "burn-hash", type: { buffer: { length: 32 } } },
{ name: "burn-height", type: "uint128" },
{ name: "sweep-txid", type: { buffer: { length: 32 } } },
],
outputs: {
type: {
Expand All @@ -561,6 +564,7 @@ export const contracts = {
recipient: TypedAbiArg<string, "recipient">,
burnHash: TypedAbiArg<Uint8Array, "burnHash">,
burnHeight: TypedAbiArg<number | bigint, "burnHeight">,
sweepTxid: TypedAbiArg<Uint8Array, "sweepTxid">,
],
Response<Response<boolean, bigint>, bigint>
>,
Expand All @@ -578,6 +582,7 @@ export const contracts = {
{ name: "burn-hash", type: { buffer: { length: 32 } } },
{ name: "burn-height", type: "uint128" },
{ name: "recipient", type: "principal" },
{ name: "sweep-txid", type: { buffer: { length: 32 } } },
{ name: "txid", type: { buffer: { length: 32 } } },
{ name: "vout-index", type: "uint128" },
],
Expand All @@ -596,6 +601,7 @@ export const contracts = {
burnHash: Uint8Array;
burnHeight: number | bigint;
recipient: string;
sweepTxid: Uint8Array;
txid: Uint8Array;
voutIndex: number | bigint;
}[],
Expand Down Expand Up @@ -1569,6 +1575,10 @@ export const contracts = {
{ name: "request-id", type: "uint128" },
{ name: "signer-bitmap", type: "uint128" },
{ name: "status", type: "bool" },
{
name: "sweep-txid",
type: { optional: { buffer: { length: 32 } } },
},
],
},
},
Expand All @@ -1590,6 +1600,7 @@ export const contracts = {
requestId: number | bigint;
signerBitmap: number | bigint;
status: boolean;
sweepTxid: Uint8Array | null;
},
"withdrawal"
>,
Expand All @@ -1611,6 +1622,7 @@ export const contracts = {
{ name: "fee", type: "uint128" },
{ name: "burn-hash", type: { buffer: { length: 32 } } },
{ name: "burn-height", type: "uint128" },
{ name: "sweep-txid", type: { buffer: { length: 32 } } },
],
outputs: { type: { response: { ok: "bool", error: "uint128" } } },
} as TypedAbiFunction<
Expand All @@ -1622,6 +1634,7 @@ export const contracts = {
fee: TypedAbiArg<number | bigint, "fee">,
burnHash: TypedAbiArg<Uint8Array, "burnHash">,
burnHeight: TypedAbiArg<number | bigint, "burnHeight">,
sweepTxid: TypedAbiArg<Uint8Array, "sweepTxid">,
],
Response<boolean, bigint>
>,
Expand All @@ -1646,6 +1659,10 @@ export const contracts = {
{ name: "request-id", type: "uint128" },
{ name: "signer-bitmap", type: "uint128" },
{ name: "status", type: "bool" },
{
name: "sweep-txid",
type: { optional: { buffer: { length: 32 } } },
},
],
},
length: 600,
Expand All @@ -1666,6 +1683,7 @@ export const contracts = {
requestId: number | bigint;
signerBitmap: number | bigint;
status: boolean;
sweepTxid: Uint8Array | null;
}[],
"withdrawals"
>,
Expand Down
18 changes: 18 additions & 0 deletions contracts/tests/sbtc-deposit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ describe("sBTC deposit contract", () => {
recipient: deployer,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(1),
}),
deployer
);
Expand All @@ -41,6 +42,7 @@ describe("sBTC deposit contract", () => {
recipient: deployer,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(1),
}),
deployer
);
Expand All @@ -60,6 +62,7 @@ describe("sBTC deposit contract", () => {
recipient: deployer,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(1),
}),
deployer
);
Expand All @@ -72,6 +75,7 @@ describe("sBTC deposit contract", () => {
recipient: deployer,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(1),
}),
deployer
);
Expand All @@ -89,6 +93,7 @@ describe("sBTC deposit contract", () => {
recipient: deployer,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(1),
}),
deployer
);
Expand All @@ -102,6 +107,7 @@ describe("sBTC deposit contract", () => {
recipient: deployer,
burnHash: new Uint8Array(32).fill(1),
burnHeight,
sweepTxid: new Uint8Array(32).fill(2),
}),
deployer
);
Expand All @@ -119,6 +125,7 @@ describe("sBTC deposit contract", () => {
recipient: deployer,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(1),
}),
deployer
);
Expand Down Expand Up @@ -154,6 +161,7 @@ describe("sBTC deposit contract", () => {
recipient: deployer,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(2),
}),
deployer
);
Expand Down Expand Up @@ -184,6 +192,7 @@ describe("sBTC deposit contract", () => {
recipient: deployer,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(1),
},
{
txid: new Uint8Array(32).fill(1),
Expand All @@ -192,6 +201,7 @@ describe("sBTC deposit contract", () => {
recipient: deployer,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(1),
},
],
...getCurrentBurnInfo(),
Expand All @@ -214,6 +224,7 @@ describe("sBTC deposit contract", () => {
recipient: deployer,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(1),
},
{
txid: new Uint8Array(32).fill(1),
Expand All @@ -222,6 +233,7 @@ describe("sBTC deposit contract", () => {
recipient: deployer,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(1),
},
],
...getCurrentBurnInfo(),
Expand All @@ -244,6 +256,7 @@ describe("sBTC deposit contract", () => {
recipient: deployer,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(1),
},
{
txid: new Uint8Array(32).fill(1),
Expand All @@ -252,6 +265,7 @@ describe("sBTC deposit contract", () => {
recipient: deployer,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(1),
},
{
txid: new Uint8Array(32).fill(2),
Expand All @@ -260,6 +274,7 @@ describe("sBTC deposit contract", () => {
recipient: alice,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(1),
},
],
...getCurrentBurnInfo(),
Expand All @@ -282,6 +297,7 @@ describe("sBTC deposit contract", () => {
recipient: deployer,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(1),
},
{
txid: new Uint8Array(32).fill(1),
Expand All @@ -290,6 +306,7 @@ describe("sBTC deposit contract", () => {
recipient: deployer,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(1),
},
],
...getCurrentBurnInfo(),
Expand Down Expand Up @@ -317,6 +334,7 @@ describe("optimization tests", () => {
recipient: deployer,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(1),
})),
...getCurrentBurnInfo(),
}),
Expand Down
2 changes: 2 additions & 0 deletions contracts/tests/sbtc-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe("sBTC token contract", () => {
recipient: alice,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(0),
}),
deployer
);
Expand Down Expand Up @@ -56,6 +57,7 @@ describe("sBTC token contract", () => {
recipient: alice,
burnHash,
burnHeight,
sweepTxid: new Uint8Array(32).fill(1),
}),
deployer
);
Expand Down
Loading

0 comments on commit 873329b

Please sign in to comment.