Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
SigismundSchlomo committed Oct 7, 2024
1 parent 80835f2 commit c2119f8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 54 deletions.
14 changes: 9 additions & 5 deletions contracts/staking/token/LimitedTokenPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ contract LimitedTokenPool is Initializable, AccessControl, IOnBlockListener {
LockKeeper public lockKeeper;
RewardsBank public rewardsBank;

MainConfig public mainConfig;
LimitsConfig public limitsConfig;
MainConfig public mainConfig; // immutable
LimitsConfig public limitsConfig; // mutable
Info public info;

mapping(address => Staker) public stakers;
mapping(address => Staker) private stakers;

//EVENTS

Expand Down Expand Up @@ -247,8 +247,12 @@ contract LimitedTokenPool is Initializable, AccessControl, IOnBlockListener {
return mainConfig.name;
}

function getStaker(address user) public view returns (Staker memory) {
return stakers[user];
function getStake(address user) public view returns (uint) {
return stakers[user].stake;
}

function getDeposit(address user) public view returns (uint) {
return stakers[user].deposit;
}

function getUserRewards(address user) public view returns (uint) {
Expand Down
34 changes: 2 additions & 32 deletions contracts/staking/token/TokenPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ contract TokenPool is Initializable, AccessControl, IOnBlockListener {
LimitsConfig public limitsConfig; // mutable
Info public info;

mapping(address => Staker) public stakers;
mapping(address => Staker) private stakers;

//EVENTS

Expand All @@ -63,7 +63,6 @@ contract TokenPool is Initializable, AccessControl, IOnBlockListener {
event UnstakeFast(address indexed user, uint amount, uint penalty);

function initialize(RewardsBank bank_, LockKeeper keeper_, MainConfig calldata mainConfig_, LimitsConfig calldata limitsConfig_) public initializer {
//TODO: Should validate input params
rewardsBank = bank_;
lockKeeper = keeper_;
mainConfig = mainConfig_;
Expand Down Expand Up @@ -123,7 +122,7 @@ contract TokenPool is Initializable, AccessControl, IOnBlockListener {
// lock funds
stakers[msg.sender].lockedWithdrawal = lockKeeper.lockSingle(
msg.sender, address(mainConfig.token), uint64(block.timestamp + limitsConfig.lockPeriod), amount + canceledAmount,
string(abi.encodePacked("TokenStaking unstake: ", _addressToString(address(mainConfig.token))))
string(abi.encodePacked("TokenStaking unstake"))
);

_claimRewards(msg.sender);
Expand Down Expand Up @@ -157,18 +156,6 @@ contract TokenPool is Initializable, AccessControl, IOnBlockListener {

// VIEW METHODS

function getMainConfig() public view returns (MainConfig memory) {
return mainConfig;
}

function getLimitsConfig() public view returns (LimitsConfig memory) {
return limitsConfig;
}

function getInfo() public view returns (Info memory) {
return info;
}

function getStake(address user) public view returns (uint) {
return stakers[user].stake;
}
Expand Down Expand Up @@ -245,21 +232,4 @@ contract TokenPool is Initializable, AccessControl, IOnBlockListener {
if (info.totalStake == 0 && info.totalRewards == 0) return amount;
return amount * info.totalRewards /info.totalStake;
}

function _addressToString(address x) internal pure returns (string memory) {
bytes memory s = new bytes(40);
for (uint i = 0; i < 20; i++) {
uint8 b = uint8(uint(uint160(x)) / (2 ** (8 * (19 - i))));
uint8 hi = (b / 16);
uint8 lo = (b - 16 * hi);
s[2 * i] = _char(hi);
s[2 * i + 1] = _char(lo);
}
return string(s);
}

function _char(uint8 b) internal pure returns (bytes1 c) {
return bytes1(b + (b < 10 ? 0x30 : 0x57));
}

}
37 changes: 20 additions & 17 deletions test/staking/token/LimitedTokenPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ describe("LimitedTokenPool", function () {
const info = await limitedPool.info();
expect(info.totalDeposit).to.equal(depositAmount);

const staker = await limitedPool.getStaker(owner.address);
expect(staker.deposit).to.equal(depositAmount);
const deposit = await limitedPool.getDeposit(owner.address);
expect(deposit).to.equal(depositAmount);
});

it("Should not allow deposit below minimum", async function () {
Expand All @@ -147,8 +147,8 @@ describe("LimitedTokenPool", function () {
const info = await limitedPool.info();
expect(info.totalDeposit).to.equal(ethers.utils.parseEther("500"));

const staker = await limitedPool.getStaker(owner.address);
expect(staker.deposit).to.equal(ethers.utils.parseEther("500"));
const deposit = await limitedPool.getDeposit(owner.address);
expect(deposit).to.equal(ethers.utils.parseEther("500"));
});

it("Should not allow withdrawal more than deposited", async function () {
Expand Down Expand Up @@ -176,8 +176,8 @@ describe("LimitedTokenPool", function () {
const info = await limitedPool.info();
expect(info.totalStake).to.equal(stakeAmount);

const staker = await limitedPool.getStaker(owner.address);
expect(staker.stake).to.equal(stakeAmount);
const stake = await limitedPool.getStake(owner.address);
expect(stake).to.equal(stakeAmount);
});

it("Should not allow staking below minimum", async function () {
Expand Down Expand Up @@ -218,8 +218,8 @@ describe("LimitedTokenPool", function () {
const info = await limitedPool.info();
expect(info.totalStake).to.equal(0);

const staker = await limitedPool.getStaker(owner.address);
expect(staker.stake).to.equal(0);
const stake = await limitedPool.getStake(owner.address);
expect(stake).to.equal(0);
});

it("Should not allow unstaking more than staked", async function () {
Expand Down Expand Up @@ -311,9 +311,10 @@ describe("LimitedTokenPool", function () {
expect(info.totalDeposit).to.equal(depositAmount.mul(2));
expect(info.totalStake).to.equal(stakeAmount.mul(2));

const staker = await limitedPool.getStaker(owner.address);
expect(staker.deposit).to.equal(depositAmount.mul(2));
expect(staker.stake).to.equal(stakeAmount.mul(2));
const deposit = await limitedPool.getDeposit(owner.address);
const stake = await limitedPool.getStake(owner.address);
expect(deposit).to.equal(depositAmount.mul(2));
expect(stake).to.equal(stakeAmount.mul(2));
});

it("Should handle rewards correctly after multiple stakes and unstakes", async function () {
Expand Down Expand Up @@ -399,13 +400,15 @@ describe("LimitedTokenPool", function () {
await limitedPool.connect(user1).stake(ethers.utils.parseEther("200"));
await limitedPool.connect(user2).stake(ethers.utils.parseEther("100"));

const infoUser1 = await limitedPool.getStaker(user1.address);
const infoUser2 = await limitedPool.getStaker(user2.address);
const depositUser1 = await limitedPool.getDeposit(user1.address);
const stakeUser1 = await limitedPool.getStake(user1.address);
const depositUser2 = await limitedPool.getDeposit(user2.address);
const stakeUser2 = await limitedPool.getStake(user2.address);

expect(infoUser1.deposit).to.equal(ethers.utils.parseEther("500"));
expect(infoUser1.stake).to.equal(ethers.utils.parseEther("200"));
expect(infoUser2.deposit).to.equal(ethers.utils.parseEther("300"));
expect(infoUser2.stake).to.equal(ethers.utils.parseEther("100"));
expect(depositUser1).to.equal(ethers.utils.parseEther("500"));
expect(stakeUser1).to.equal(ethers.utils.parseEther("200"));
expect(depositUser2).to.equal(ethers.utils.parseEther("300"));
expect(stakeUser2).to.equal(ethers.utils.parseEther("100"));

const poolInfo = await limitedPool.info();
expect(poolInfo.totalDeposit).to.equal(ethers.utils.parseEther("800"));
Expand Down

0 comments on commit c2119f8

Please sign in to comment.