Skip to content

Commit

Permalink
fix: withdrawal tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hstove committed Oct 11, 2024
1 parent 3a761e5 commit 4d0739f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
2 changes: 1 addition & 1 deletion contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"@clarigen/cli": "2.1.1",
"@clarigen/core": "2.1.1",
"@clarigen/test": "2.1.1",
"@hirosystems/clarinet-sdk": "^2.8.1",
"@hirosystems/clarinet-sdk": "^2.9.0",
"@noble/secp256k1": "^2.1.0",
"@scure/base": "^1.1.6",
"@scure/btc-signer": "^1.3.1",
Expand Down
55 changes: 36 additions & 19 deletions contracts/tests/sbtc-withdrawal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
deployer,
deposit,
errors,
getCurrentBurnInfo,
randomPublicKeys,
registry,
stxAddressToPoxAddress,
Expand Down Expand Up @@ -106,7 +107,7 @@ describe("initiating a withdrawal request", () => {
recipient: alicePoxAddr,
amount: defaultAmount,
maxFee: defaultMaxFee,
blockHeight: 2n,
blockHeight: BigInt(simnet.blockHeight - 2),
status: null,
});

Expand All @@ -131,7 +132,7 @@ describe("initiating a withdrawal request", () => {
recipient: alicePoxAddr,
amount: defaultAmount,
maxFee: defaultMaxFee,
blockHeight: 2n,
blockHeight: BigInt(simnet.blockHeight - 2),
topic: "withdrawal-create",
requestId: 1n,
});
Expand All @@ -147,7 +148,9 @@ describe("initiating a withdrawal request", () => {
}),
deployer
);
expect(rovOk(token.getBalance(alice))).toEqual(defaultAmount + defaultMaxFee);
expect(rovOk(token.getBalance(alice))).toEqual(
defaultAmount + defaultMaxFee
);
const receipt = txOk(
withdrawal.initiateWithdrawalRequest({
amount: defaultAmount,
Expand All @@ -165,7 +168,9 @@ describe("initiating a withdrawal request", () => {
expect(mintEvent.data.asset_identifier).toEqual(
`${token.identifier}::${token.fungible_tokens[1].name}`
);
expect(mintEvent.data.amount).toEqual((defaultAmount + defaultMaxFee).toString());
expect(mintEvent.data.amount).toEqual(
(defaultAmount + defaultMaxFee).toString()
);
expect(rovOk(token.getBalanceAvailable(alice))).toEqual(0n);
});

Expand Down Expand Up @@ -264,7 +269,6 @@ test("max-fee must be accounted for", () => {
expect(receipt.value).toEqual(1n);
});


describe("Accepting a withdrawal request", () => {
test("Fails with non-existant request-id", () => {
// Alice initiates withdrawalrequest
Expand Down Expand Up @@ -452,7 +456,7 @@ describe("Accepting a withdrawal request", () => {
requestId: 1n,
bitcoinTxid: new Uint8Array(32).fill(0),
signerBitmap: 2n,
outputIndex : 10n,
outputIndex: 10n,
topic: "withdrawal-accept",
fee: defaultMaxFee + 10n,
});
Expand Down Expand Up @@ -499,7 +503,7 @@ describe("Accepting a withdrawal request", () => {
recipient: alicePoxAddr,
amount: defaultAmount,
maxFee: defaultMaxFee,
blockHeight: 2n,
blockHeight: BigInt(simnet.blockHeight - 3),
status: true,
});
});
Expand All @@ -518,8 +522,12 @@ describe("Accepting a withdrawal request", () => {
}),
deployer
);
expect(rovOk(token.getBalance(alice))).toEqual(defaultAmount + defaultMaxFee);
expect(rovOk(token.getBalanceAvailable(alice))).toEqual(defaultAmount + defaultMaxFee);
expect(rovOk(token.getBalance(alice))).toEqual(
defaultAmount + defaultMaxFee
);
expect(rovOk(token.getBalanceAvailable(alice))).toEqual(
defaultAmount + defaultMaxFee
);
expect(rovOk(token.getBalanceLocked(alice))).toEqual(0n);
txOk(
withdrawal.initiateWithdrawalRequest({
Expand All @@ -531,9 +539,13 @@ describe("Accepting a withdrawal request", () => {
);
// Initiating a withdrawal request doesn't change the "balance", but
// does change how much is available.
expect(rovOk(token.getBalance(alice))).toEqual(defaultAmount + defaultMaxFee);
expect(rovOk(token.getBalance(alice))).toEqual(
defaultAmount + defaultMaxFee
);
expect(rovOk(token.getBalanceAvailable(alice))).toEqual(0n);
expect(rovOk(token.getBalanceLocked(alice))).toEqual(defaultAmount + defaultMaxFee);
expect(rovOk(token.getBalanceLocked(alice))).toEqual(
defaultAmount + defaultMaxFee
);
const receipt = txOk(
withdrawal.rejectWithdrawalRequest({
requestId: 1n,
Expand All @@ -542,8 +554,12 @@ describe("Accepting a withdrawal request", () => {
deployer
);
// This is the original balance, rejecting the request restores it.
expect(rovOk(token.getBalance(alice))).toEqual(defaultAmount + defaultMaxFee);
expect(rovOk(token.getBalanceAvailable(alice))).toEqual(defaultAmount + defaultMaxFee);
expect(rovOk(token.getBalance(alice))).toEqual(
defaultAmount + defaultMaxFee
);
expect(rovOk(token.getBalanceAvailable(alice))).toEqual(
defaultAmount + defaultMaxFee
);
expect(rovOk(token.getBalanceLocked(alice))).toEqual(0n);

// Check that the request was stored correctly with the correct status
Expand All @@ -556,7 +572,7 @@ describe("Accepting a withdrawal request", () => {
recipient: alicePoxAddr,
amount: defaultAmount,
maxFee: defaultMaxFee,
blockHeight: 2n,
blockHeight: BigInt(simnet.blockHeight - 3),
status: false,
});

Expand Down Expand Up @@ -739,7 +755,7 @@ describe("Reject a withdrawal request", () => {
bitcoinTxid: new Uint8Array(32).fill(1),
outputIndex: 10n,
fee: 10n,
}
},
],
burnHeight: 10n,
burnHash: new Uint8Array(32).fill(0),
Expand Down Expand Up @@ -852,7 +868,7 @@ describe("Complete multiple withdrawals", () => {
describe("optimization tests for completing withdrawals", () => {
test("maximizing the number of withdrawal completions in one tx", () => {
const totalAmount = 1000000n;
const runs = 500;
const runs = 300;
const perAmount = totalAmount / BigInt(runs);
const maxFee = 10n;
const txids = randomPublicKeys(runs).map((pk) => pk.slice(0, 32));
Expand All @@ -876,7 +892,8 @@ describe("optimization tests for completing withdrawals", () => {
alice
);
}
txErr(
const { burnHeight, burnHash } = getCurrentBurnInfo();
txOk(
withdrawal.completeWithdrawals({
withdrawals: txids.map((txid, index) => {
return {
Expand All @@ -888,8 +905,8 @@ describe("optimization tests for completing withdrawals", () => {
fee: 10n,
};
}),
burnHeight: 10n,
burnHash: new Uint8Array(32).fill(0)
burnHeight,
burnHash: burnHash!,
}),
deployer
);
Expand Down

0 comments on commit 4d0739f

Please sign in to comment.