Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to allow withdrawing without rewards #85

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion contracts/staking/AbstractStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,10 @@ abstract contract AbstractStaking is AbstractValueDistributor, Initializable {
owedValue_ = getOwedValue(msg.sender);

unstake(shares_);
claim(owedValue_);

if (owedValue_ > 0) {
claim(owedValue_);
}
}

/**
Expand Down
48 changes: 48 additions & 0 deletions test/staking/AbstractStaking.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,54 @@ describe("AbstractStaking", () => {
expect(await abstractStaking.getOwedValue(FIRST)).to.equal(0);
expect(await abstractStaking.userOwedValue(FIRST)).to.equal(0);
});

it("should work as expected if withdraw is called right after claiming all the rewards within one block", async () => {
await mintAndApproveTokens(SECOND, sharesToken, wei(200, sharesDecimals));
await mintAndApproveTokens(FIRST, sharesToken, wei(200, sharesDecimals));

await abstractStaking.connect(FIRST).stake(wei(100, sharesDecimals));

// triggering the next block
await mintAndApproveTokens(FIRST, sharesToken, wei(100, sharesDecimals));

await abstractStaking.multicall([
abstractStaking.interface.encodeFunctionData("claim", [await abstractStaking.getOwedValue(FIRST)]),
abstractStaking.interface.encodeFunctionData("withdraw"),
]);

expect(await abstractStaking.getOwedValue(FIRST)).to.equal(0);
expect(await abstractStaking.userOwedValue(FIRST)).to.equal(0);
expect(await abstractStaking.userShares(FIRST)).to.equal(0);
});

it("should withdraw as expected if there are no rewards because of the 0 rate", async () => {
await abstractStaking.setRate(0);

await mintAndApproveTokens(FIRST, sharesToken, wei(100, sharesDecimals));

await abstractStaking.connect(FIRST).stake(wei(100, sharesDecimals));

await time.setNextBlockTimestamp((await time.latest()) + 30);

await abstractStaking.withdraw();

expect(await abstractStaking.getOwedValue(FIRST)).to.equal(0);
expect(await abstractStaking.userOwedValue(FIRST)).to.equal(0);
expect(await abstractStaking.userShares(FIRST)).to.equal(0);
});

it("should not allow to withdraw if there are no shares", async () => {
await mintAndApproveTokens(FIRST, sharesToken, wei(200, sharesDecimals));

await abstractStaking.connect(FIRST).stake(wei(200, sharesDecimals));

await expect(
abstractStaking.multicall([
abstractStaking.interface.encodeFunctionData("unstake", [wei(200, sharesDecimals)]),
abstractStaking.interface.encodeFunctionData("withdraw"),
]),
).to.be.revertedWith("ValueDistributor: amount has to be more than 0");
});
});

describe("claim()", () => {
Expand Down
Loading