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

Add base implementation #40

Open
wants to merge 9 commits into
base: fix/session-approval-delegation
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 12 additions & 4 deletions smart-contracts/contracts/delegate/ProvidersDelegator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,26 @@ contract ProvidersDelegator is IProvidersDelegator, OwnableUpgradeable {
revert InsufficientAmount();
}

uint256 feeAmount_ = (amount_ * fee) / PRECISION;
uint256 amountWithFee_ = amount_ - feeAmount_;
FedokDL marked this conversation as resolved.
Show resolved Hide resolved
if (feeAmount_ != 0) {
IERC20(token).safeTransfer(feeTreasury, feeAmount_);

emit FeeClaimed(feeTreasury, feeAmount_);
}

IProviderRegistry(lumerinDiamond).providerRegister(address(this), amountWithFee_, endpoint);

totalRate = currentRate_;
totalStaked += amount_;
totalStaked += amountWithFee_;

lastContractBalance = contractBalance_ - amount_;

staker.rate = currentRate_;
staker.staked += amount_;
staker.staked += amountWithFee_;
staker.claimed += amount_;
staker.pendingRewards = pendingRewards_ - amount_;

IProviderRegistry(lumerinDiamond).providerRegister(address(this), amount_, endpoint);

emit Restaked(staker_, staker.staked, staker.pendingRewards, staker.rate);
}

Expand Down
32 changes: 19 additions & 13 deletions smart-contracts/test/delegate/ProviderDelegator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,45 +325,51 @@ describe('ProvidersDelegator', () => {
await token.transfer(providersDelegator, wei(100));

await providersDelegator.connect(OWNER).restake(KYLE, wei(9999));
expect((await providersDelegator.stakers(KYLE)).staked).to.eq(wei(125));
expect((await providersDelegator.stakers(KYLE)).staked).to.eq(wei(120));
expect(await token.balanceOf(KYLE)).to.eq(wei(900));
expect(await token.balanceOf(TREASURY)).to.eq(wei(0));
expect(await token.balanceOf(TREASURY)).to.eq(wei(5));

await token.transfer(providersDelegator, wei(100));

await providersDelegator.connect(KYLE).claim(KYLE, wei(9999));
expect(await token.balanceOf(KYLE)).to.closeTo(wei(900 + 29.41 * 0.8), wei(0.01));
expect(await token.balanceOf(TREASURY)).to.closeTo(wei(29.41 * 0.2), wei(0.01));
expect(await token.balanceOf(KYLE)).to.closeTo(wei(900 + 28.57 * 0.8), wei(0.01));
expect(await token.balanceOf(TREASURY)).to.closeTo(wei(5 + 28.57 * 0.2), wei(0.01));

await providersDelegator.connect(SHEV).claim(SHEV, wei(9999));
expect(await token.balanceOf(SHEV)).to.closeTo(wei(700 + 75 * 0.8 + 70.58 * 0.8), wei(0.01));
expect(await token.balanceOf(TREASURY)).to.closeTo(wei(29.41 * 0.2 + 75 * 0.2 + 70.58 * 0.2), wei(0.01));
expect(await token.balanceOf(SHEV)).to.closeTo(wei(700 + 75 * 0.8 + 71.42 * 0.8), wei(0.01));
expect(await token.balanceOf(TREASURY)).to.closeTo(wei(5 + 28.57 * 0.2 + 75 * 0.2 + 71.42 * 0.2), wei(0.01));
});

it('should correctly restake, two stakers, partial restake', async () => {
await providersDelegator.connect(KYLE).stake(wei(100));
await providersDelegator.connect(SHEV).stake(wei(300));

await token.transfer(providersDelegator, wei(100));

await providersDelegator.connect(OWNER).restake(KYLE, wei(20));
expect((await providersDelegator.stakers(KYLE)).staked).to.eq(wei(120));
expect((await providersDelegator.stakers(KYLE)).staked).to.eq(wei(116));
expect(await token.balanceOf(KYLE)).to.eq(wei(900));
expect(await token.balanceOf(TREASURY)).to.eq(wei(0));
expect(await token.balanceOf(TREASURY)).to.eq(wei(4));

await token.transfer(providersDelegator, wei(100));

await providersDelegator.connect(KYLE).claim(KYLE, wei(9999));
expect(await token.balanceOf(KYLE)).to.closeTo(wei(900 + 5 * 0.8 + 28.57 * 0.8), wei(0.01));
expect(await token.balanceOf(TREASURY)).to.closeTo(wei(5 * 0.2 + 28.57 * 0.2), wei(0.01));
expect(await token.balanceOf(KYLE)).to.closeTo(wei(900 + 5 * 0.8 + 27.88 * 0.8), wei(0.01));
expect(await token.balanceOf(TREASURY)).to.closeTo(wei(4 + 5 * 0.2 + 27.88 * 0.2), wei(0.01));

await providersDelegator.connect(SHEV).claim(SHEV, wei(9999));
expect(await token.balanceOf(SHEV)).to.closeTo(wei(700 + 75 * 0.8 + 71.42 * 0.8), wei(0.01));
expect(await token.balanceOf(SHEV)).to.closeTo(wei(700 + 75 * 0.8 + 72.11 * 0.8), wei(0.01));
expect(await token.balanceOf(TREASURY)).to.closeTo(
wei(5 * 0.2 + 28.57 * 0.2 + 75 * 0.2 + 71.42 * 0.2),
wei(4 + 5 * 0.2 + 27.88 * 0.2 + 75 * 0.2 + 72.11 * 0.2),
wei(0.01),
);
});
it('should correctly restake with zero fee', async () => {
await providersDelegator.connect(KYLE).stake(wei(100));
await token.transfer(providersDelegator, wei(10));
await providersDelegator.connect(OWNER).restake(KYLE, 1);

expect(await token.balanceOf(TREASURY)).to.eq(wei(0));
});
it('should throw error when restake caller is invalid', async () => {
await expect(providersDelegator.connect(KYLE).restake(SHEV, wei(999))).to.be.revertedWithCustomError(
providersDelegator,
Expand Down