-
Notifications
You must be signed in to change notification settings - Fork 0
feat: liquidity mining #13
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
pragma solidity 0.8.28; | ||
|
||
import {ERC20, IERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; | ||
import {IERC20Permit} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Permit.sol"; | ||
import {Math} from "@openzeppelin/contracts/utils/math/Math.sol"; | ||
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; | ||
|
||
contract LiquidityMining is ERC20, Ownable { | ||
using SafeERC20 for IERC20; | ||
|
||
uint32 public constant MULTIPLIER_PRECISION = 100; | ||
IERC20 public immutable STAKING_TOKEN; | ||
|
||
struct Tier { | ||
uint32 period; | ||
uint32 multiplier; | ||
} | ||
|
||
struct Stake { | ||
uint256 amount; | ||
uint32 period; | ||
uint32 until; | ||
uint32 multiplier; | ||
} | ||
|
||
bool public miningAllowed; | ||
Tier[] public tiers; | ||
mapping(address user => Stake) public stakes; | ||
|
||
event DisableMining(); | ||
event StakeLocked( | ||
address from, | ||
address to, | ||
uint256 amount, | ||
uint256 totalAmount, | ||
uint32 until, | ||
uint256 addedScore | ||
); | ||
event StakeUnlocked( | ||
address from, | ||
address to, | ||
uint256 amount | ||
); | ||
|
||
error ZeroAddress(); | ||
error EmptyInput(); | ||
error ZeroPeriod(); | ||
error ZeroMultiplier(); | ||
error DecreasingPeriod(); | ||
error InvalidAddedScore(); | ||
error AlreadyDisabled(); | ||
error MiningDisabled(); | ||
error ZeroAmount(); | ||
error Locked(); | ||
|
||
constructor( | ||
string memory name_, | ||
string memory symbol_, | ||
address owner_, | ||
address stakingToken, | ||
Tier[] memory tiers_ | ||
) | ||
ERC20(name_, symbol_) | ||
Ownable(owner_) | ||
{ | ||
require(stakingToken != address(0), ZeroAddress()); | ||
STAKING_TOKEN = IERC20(stakingToken); | ||
miningAllowed = true; | ||
require(tiers_.length > 0, EmptyInput()); | ||
for (uint256 i = 0; i < tiers_.length; ++i) { | ||
require(tiers_[i].period > 0, ZeroPeriod()); | ||
require(tiers_[i].multiplier > 0, ZeroMultiplier()); | ||
if (i > 0) { | ||
require(tiers_[i].period > tiers_[i - 1].period, DecreasingPeriod()); | ||
} | ||
tiers.push(tiers_[i]); | ||
} | ||
} | ||
|
||
function stake(address scoreTo, uint256 amount, uint256 tierId) public { | ||
if (amount > 0) { | ||
STAKING_TOKEN.safeTransferFrom(_msgSender(), address(this), amount); | ||
} | ||
_stake(_msgSender(), scoreTo, amount, tierId); | ||
} | ||
|
||
function stakeWithPermit( | ||
address scoreTo, | ||
uint256 amount, | ||
uint256 tierId, | ||
uint256 deadline, | ||
uint8 v, | ||
bytes32 r, | ||
bytes32 s | ||
) external { | ||
IERC20Permit(address(STAKING_TOKEN)).permit( | ||
_msgSender(), | ||
address(this), | ||
amount, | ||
deadline, | ||
v, | ||
r, | ||
s | ||
); | ||
stake(scoreTo, amount, tierId); | ||
} | ||
|
||
function unstake(address to) external { | ||
uint256 amount = _unstake(_msgSender(), to); | ||
STAKING_TOKEN.safeTransfer(to, amount); | ||
} | ||
|
||
function disableMining() external onlyOwner() { | ||
require(miningAllowed, AlreadyDisabled()); | ||
miningAllowed = false; | ||
emit DisableMining(); | ||
} | ||
|
||
function _stake(address from, address scoreTo, uint256 amount, uint256 tierId) internal { | ||
require(miningAllowed, MiningDisabled()); | ||
Stake memory currentStake = stakes[from]; | ||
Tier memory tier = tiers[tierId]; | ||
uint256 pendingScore = 0; | ||
if (notReached(currentStake.until)) { | ||
uint256 remainingTime = till(currentStake.until); | ||
require(tier.period >= remainingTime, DecreasingPeriod()); | ||
pendingScore = Math.ceilDiv( | ||
currentStake.amount * remainingTime * uint256(currentStake.multiplier), | ||
MULTIPLIER_PRECISION * currentStake.period | ||
); | ||
} | ||
currentStake.amount += amount; | ||
currentStake.period = tier.period; | ||
currentStake.until = timeNow() + tier.period; | ||
currentStake.multiplier = tier.multiplier; | ||
stakes[from] = currentStake; | ||
uint256 newPendingScore = | ||
currentStake.amount * uint256(tier.multiplier) / | ||
uint256(MULTIPLIER_PRECISION); | ||
require(newPendingScore > pendingScore, InvalidAddedScore()); | ||
uint256 addedScore = newPendingScore - pendingScore; | ||
_mint(scoreTo, addedScore); | ||
mpetrunic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
emit StakeLocked(from, scoreTo, amount, currentStake.amount, currentStake.until, addedScore); | ||
} | ||
|
||
function _unstake(address from, address to) internal returns (uint256) { | ||
Stake memory currentStake = stakes[from]; | ||
require(currentStake.amount > 0, ZeroAmount()); | ||
require(reached(currentStake.until), Locked()); | ||
delete stakes[from]; | ||
|
||
emit StakeUnlocked(_msgSender(), to, currentStake.amount); | ||
|
||
return currentStake.amount; | ||
} | ||
|
||
function timeNow() internal view returns (uint32) { | ||
return uint32(block.timestamp); | ||
} | ||
|
||
function reached(uint32 timestamp) internal view returns (bool) { | ||
return timeNow() >= timestamp; | ||
} | ||
|
||
function notReached(uint32 timestamp) internal view returns (bool) { | ||
return !reached(timestamp); | ||
} | ||
|
||
function till(uint32 timestamp) internal view returns (uint32) { | ||
if (reached(timestamp)) { | ||
return 0; | ||
} | ||
return timestamp - timeNow(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
pragma solidity 0.8.28; | ||
|
||
import { | ||
LiquidityMining, | ||
SafeERC20, | ||
IERC20, | ||
IERC20Permit | ||
} from "./LiquidityMining.sol"; | ||
import {ILiquidityHub} from "./interfaces/ILiquidityHub.sol"; | ||
import {IERC4626} from "@openzeppelin/contracts/interfaces/IERC4626.sol"; | ||
|
||
contract SprinterLiquidityMining is LiquidityMining { | ||
using SafeERC20 for IERC20; | ||
|
||
ILiquidityHub public immutable LIQUIDITY_HUB; | ||
|
||
error NotImplemented(); | ||
|
||
constructor(address owner_, address liquidityHub, Tier[] memory tiers_) | ||
LiquidityMining( | ||
"Sprinter USDC LP Score", | ||
"sprUSDC-LP-Score", | ||
owner_, | ||
address(ILiquidityHub(liquidityHub).SHARES()), | ||
tiers_ | ||
) | ||
{ | ||
LIQUIDITY_HUB = ILiquidityHub(liquidityHub); | ||
} | ||
|
||
function depositAndStake(address scoreTo, uint256 amount, uint256 tierId) public { | ||
address from = _msgSender(); | ||
IERC4626 liquidityHub = IERC4626(address(LIQUIDITY_HUB)); | ||
IERC20 asset = IERC20(liquidityHub.asset()); | ||
asset.safeTransferFrom(from, address(this), amount); | ||
asset.approve(address(liquidityHub), amount); | ||
uint256 shares = liquidityHub.deposit(amount, address(this)); | ||
_stake(from, scoreTo, shares, tierId); | ||
} | ||
|
||
function depositAndStakeWithPermit( | ||
address scoreTo, | ||
uint256 amount, | ||
uint256 tierId, | ||
uint256 deadline, | ||
uint8 v, | ||
bytes32 r, | ||
bytes32 s | ||
) external { | ||
IERC20Permit(IERC4626(address(LIQUIDITY_HUB)).asset()).permit( | ||
_msgSender(), | ||
address(this), | ||
amount, | ||
deadline, | ||
v, | ||
r, | ||
s | ||
); | ||
depositAndStake(scoreTo, amount, tierId); | ||
} | ||
|
||
function unstakeAndWithdraw(address to) external { | ||
uint256 shares = _unstake(_msgSender(), address(this)); | ||
IERC4626(address(LIQUIDITY_HUB)).redeem(shares, to, address(this)); | ||
} | ||
|
||
function transfer(address, uint256) public pure override returns (bool) { | ||
revert NotImplemented(); | ||
} | ||
|
||
function allowance(address, address) public pure override returns (uint256) { | ||
// Silences the unreachable code warning from ERC20._spendAllowance(). | ||
return 0; | ||
} | ||
|
||
function approve(address, uint256) public pure override returns (bool) { | ||
revert NotImplemented(); | ||
} | ||
|
||
function transferFrom(address, address, uint256) public pure override returns (bool) { | ||
revert NotImplemented(); | ||
} | ||
|
||
function _update(address from, address to, uint256 value) internal virtual override { | ||
require(from == address(0), NotImplemented()); | ||
super._update(from, to, value); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
pragma solidity 0.8.28; | ||
|
||
import {IManagedToken} from "./IManagedToken.sol"; | ||
|
||
interface ILiquidityHub { | ||
function SHARES() external view returns (IManagedToken); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.