Skip to content

Commit eec432f

Browse files
author
Mark Brivvins
committed
63-consolidate stake withdrawal tests
1 parent d40229d commit eec432f

1 file changed

Lines changed: 26 additions & 97 deletions

File tree

test/UTT.test.ts

Lines changed: 26 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,27 @@ describe("UTT", function () {
288288

289289
it("should allow a user to withdraw part of their stake", async function () {
290290
const { utt, user1, service1 } = await deployWithUserStake();
291+
const balanceBefore = await utt.balanceOf(user1.address);
292+
const stakeBefore = await utt.previousEndorserStakes(
293+
service1.address,
294+
user1.address
295+
);
296+
const totalStakeBefore = await utt.totalStake(service1.address);
291297

292-
await utt
298+
const withdrawP = utt
293299
.connect(user1)
294300
.withdrawStake(service1.address, 400, mockTransactionId);
295301

302+
await expect(withdrawP)
303+
.to.emit(utt, "WithdrawStake")
304+
.withArgs(user1.address, service1.address, 400, mockTransactionId);
305+
expect(await utt.balanceOf(user1.address)).to.eq(balanceBefore + 400n);
296306
expect(
297307
await utt.previousEndorserStakes(service1.address, user1.address)
298-
).to.eq(600);
299-
expect(await utt.totalStake(service1.address)).to.eq(600);
308+
).to.eq(stakeBefore - 400n);
309+
expect(await utt.totalStake(service1.address)).to.eq(
310+
totalStakeBefore - 400n
311+
);
300312
});
301313

302314
it("should allow a user to withdraw all of their stake", async function () {
@@ -312,69 +324,12 @@ describe("UTT", function () {
312324
expect(await utt.totalStake(service1.address)).to.eq(0);
313325
});
314326

315-
it("should increase the user's UTT balance by the withdrawn amount", async function () {
316-
const { utt, user1, service1 } = await deployWithUserStake();
317-
const balanceBefore = await utt.balanceOf(user1.address);
318-
319-
await utt
320-
.connect(user1)
321-
.withdrawStake(service1.address, 250, mockTransactionId);
322-
323-
expect(await utt.balanceOf(user1.address)).to.eq(balanceBefore + 250n);
324-
});
325-
326-
it("should decrease the user's stake for the target by the withdrawn amount", async function () {
327-
const { utt, user1, service1 } = await deployWithUserStake();
328-
const stakeBefore = await utt.previousEndorserStakes(
329-
service1.address,
330-
user1.address
331-
);
332-
333-
await utt
334-
.connect(user1)
335-
.withdrawStake(service1.address, 250, mockTransactionId);
336-
337-
expect(
338-
await utt.previousEndorserStakes(service1.address, user1.address)
339-
).to.eq(stakeBefore - 250n);
340-
});
341-
342-
it("should decrease total stake for the target by the withdrawn amount", async function () {
343-
const { utt, user1, service1 } = await deployWithUserStake();
344-
const totalStakeBefore = await utt.totalStake(service1.address);
345-
346-
await utt
347-
.connect(user1)
348-
.withdrawStake(service1.address, 250, mockTransactionId);
349-
350-
expect(await utt.totalStake(service1.address)).to.eq(
351-
totalStakeBefore - 250n
352-
);
353-
});
354-
355-
it("should emit a WithdrawStake event with correct parameters", async function () {
356-
const { utt, user1, service1 } = await deployWithUserStake();
357-
358-
await expect(
359-
utt
360-
.connect(user1)
361-
.withdrawStake(service1.address, 250, mockTransactionId)
362-
)
363-
.to.emit(utt, "WithdrawStake")
364-
.withArgs(user1.address, service1.address, 250, mockTransactionId);
365-
});
366-
367-
it("should revert when withdrawing zero stake", async function () {
327+
it("should revert when withdrawing an invalid amount", async function () {
368328
const { utt, user1, service1 } = await deployWithUserStake();
369329

370330
await expect(
371331
utt.connect(user1).withdrawStake(service1.address, 0, mockTransactionId)
372332
).to.be.revertedWith("UTT: withdraw amount must be greater than zero");
373-
});
374-
375-
it("should revert when withdrawing more than the user's current stake", async function () {
376-
const { utt, user1, service1 } = await deployWithUserStake();
377-
378333
await expect(
379334
utt
380335
.connect(user1)
@@ -390,57 +345,31 @@ describe("UTT", function () {
390345
).to.be.revertedWith("UTT: withdraw amount exceeds stake");
391346
});
392347

393-
it("should not emit RewardPreviousEndorserLevel1", async function () {
348+
it("should not emit reward events", async function () {
394349
const { utt, user1, service1 } = await deployWithUserStake();
395350

396-
await expect(
397-
utt
398-
.connect(user1)
399-
.withdrawStake(service1.address, 250, mockTransactionId)
400-
).to.not.emit(utt, "RewardPreviousEndorserLevel1");
401-
});
402-
403-
it("should not emit RewardPreviousEndorserLevel2", async function () {
404-
const { utt, user1, service1 } = await deployWithUserStake();
405-
406-
await expect(
407-
utt
408-
.connect(user1)
409-
.withdrawStake(service1.address, 250, mockTransactionId)
410-
).to.not.emit(utt, "RewardPreviousEndorserLevel2");
411-
});
412-
413-
it("should not emit RewardUTUCoin", async function () {
414-
const { utt, user1, service1 } = await deployWithUserStake();
351+
const withdrawP = utt
352+
.connect(user1)
353+
.withdrawStake(service1.address, 250, mockTransactionId);
415354

416-
await expect(
417-
utt
418-
.connect(user1)
419-
.withdrawStake(service1.address, 250, mockTransactionId)
420-
).to.not.emit(utt, "RewardUTUCoin");
355+
await expect(withdrawP)
356+
.to.not.emit(utt, "RewardPreviousEndorserLevel1")
357+
.to.not.emit(utt, "RewardPreviousEndorserLevel2")
358+
.to.not.emit(utt, "RewardUTUCoin");
421359
});
422360

423361
it("should not change claimable UTU Coin", async function () {
424362
const { utt, user1, service1 } = await deployWithClaimableRewards();
425363
const claimableBefore = await utt.claimableUTUCoin(user1.address);
426-
expect(claimableBefore).to.be.gt(0);
427-
428-
await utt
429-
.connect(user1)
430-
.withdrawStake(service1.address, 50, mockTransactionId);
431-
432-
expect(await utt.claimableUTUCoin(user1.address)).to.eq(claimableBefore);
433-
});
434-
435-
it("should not change total claimable UTU Coin", async function () {
436-
const { utt, user1, service1 } = await deployWithClaimableRewards();
437364
const totalClaimableBefore = await utt.totalClaimableUTUCoin();
365+
expect(claimableBefore).to.be.gt(0);
438366
expect(totalClaimableBefore).to.be.gt(0);
439367

440368
await utt
441369
.connect(user1)
442370
.withdrawStake(service1.address, 50, mockTransactionId);
443371

372+
expect(await utt.claimableUTUCoin(user1.address)).to.eq(claimableBefore);
444373
expect(await utt.totalClaimableUTUCoin()).to.eq(totalClaimableBefore);
445374
});
446375

0 commit comments

Comments
 (0)