Skip to content

Commit 43e96d5

Browse files
authored
Merge pull request #18 from sprintertech/feat-liq-mining-unique
feat: Staking unique positions
2 parents 52a5a82 + 509873f commit 43e96d5

File tree

3 files changed

+107
-189
lines changed

3 files changed

+107
-189
lines changed

contracts/LiquidityMining.sol

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ contract LiquidityMining is ERC20, Ownable {
2727

2828
bool public miningAllowed;
2929
Tier[] public tiers;
30-
mapping(address user => Stake) public stakes;
30+
mapping(address user => Stake[]) public stakes;
3131

3232
event DisableMining();
3333
event StakeLocked(
@@ -79,10 +79,12 @@ contract LiquidityMining is ERC20, Ownable {
7979
}
8080
}
8181

82+
function getStakes(address user) public view returns(Stake[] memory) {
83+
return stakes[user];
84+
}
85+
8286
function stake(address scoreTo, uint256 amount, uint256 tierId) public {
83-
if (amount > 0) {
84-
STAKING_TOKEN.safeTransferFrom(_msgSender(), address(this), amount);
85-
}
87+
STAKING_TOKEN.safeTransferFrom(_msgSender(), address(this), amount);
8688
_stake(_msgSender(), scoreTo, amount, tierId);
8789
}
8890

@@ -107,8 +109,8 @@ contract LiquidityMining is ERC20, Ownable {
107109
stake(scoreTo, amount, tierId);
108110
}
109111

110-
function unstake(address to) external {
111-
uint256 amount = _unstake(_msgSender(), to);
112+
function unstake(uint256 id, address to) external {
113+
uint256 amount = _unstake(_msgSender(), id, to);
112114
STAKING_TOKEN.safeTransfer(to, amount);
113115
}
114116

@@ -119,38 +121,28 @@ contract LiquidityMining is ERC20, Ownable {
119121
}
120122

121123
function _stake(address from, address scoreTo, uint256 amount, uint256 tierId) internal {
124+
require(amount > 0, ZeroAmount());
122125
require(miningAllowed, MiningDisabled());
123-
Stake memory currentStake = stakes[from];
126+
Stake memory currentStake;
124127
Tier memory tier = tiers[tierId];
125-
uint256 pendingScore = 0;
126-
if (notReached(currentStake.until)) {
127-
uint256 remainingTime = till(currentStake.until);
128-
require(tier.period >= remainingTime, DecreasingPeriod());
129-
pendingScore = Math.ceilDiv(
130-
currentStake.amount * remainingTime * uint256(currentStake.multiplier),
131-
MULTIPLIER_PRECISION * currentStake.period
132-
);
133-
}
134-
currentStake.amount += amount;
128+
currentStake.amount = amount;
135129
currentStake.period = tier.period;
136130
currentStake.until = timeNow() + tier.period;
137131
currentStake.multiplier = tier.multiplier;
138-
stakes[from] = currentStake;
139-
uint256 newPendingScore =
132+
stakes[from].push(currentStake);
133+
uint256 addedScore =
140134
currentStake.amount * uint256(tier.multiplier) /
141135
uint256(MULTIPLIER_PRECISION);
142-
require(newPendingScore > pendingScore, InvalidAddedScore());
143-
uint256 addedScore = newPendingScore - pendingScore;
144136
_mint(scoreTo, addedScore);
145137

146-
emit StakeLocked(from, scoreTo, amount, currentStake.amount, currentStake.until, addedScore);
138+
emit StakeLocked(from, scoreTo, amount, amount, currentStake.until, addedScore);
147139
}
148140

149-
function _unstake(address from, address to) internal returns (uint256) {
150-
Stake memory currentStake = stakes[from];
141+
function _unstake(address from, uint256 id, address to) internal returns (uint256) {
142+
Stake memory currentStake = stakes[from][id];
151143
require(currentStake.amount > 0, ZeroAmount());
152144
require(reached(currentStake.until), Locked());
153-
delete stakes[from];
145+
delete stakes[from][id];
154146

155147
emit StakeUnlocked(_msgSender(), to, currentStake.amount);
156148

@@ -164,15 +156,4 @@ contract LiquidityMining is ERC20, Ownable {
164156
function reached(uint32 timestamp) internal view returns (bool) {
165157
return timeNow() >= timestamp;
166158
}
167-
168-
function notReached(uint32 timestamp) internal view returns (bool) {
169-
return !reached(timestamp);
170-
}
171-
172-
function till(uint32 timestamp) internal view returns (uint32) {
173-
if (reached(timestamp)) {
174-
return 0;
175-
}
176-
return timestamp - timeNow();
177-
}
178159
}

contracts/SprinterLiquidityMining.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ contract SprinterLiquidityMining is LiquidityMining {
6060
depositAndStake(scoreTo, amount, tierId);
6161
}
6262

63-
function unstakeAndWithdraw(address to) external {
64-
uint256 shares = _unstake(_msgSender(), address(this));
63+
function unstakeAndWithdraw(uint256 id, address to) external {
64+
uint256 shares = _unstake(_msgSender(), id, address(this));
6565
IERC4626(address(LIQUIDITY_HUB)).redeem(shares, to, address(this));
6666
}
6767

0 commit comments

Comments
 (0)