嘿,有人能向我解释一下这个智能合约吗?LP股权合约



嘿,所以我在github上寻找一个LP质押智能合约,我发现了这个

// SPDX-License-Identifier: MIT
pragma solidity 0.7.6;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
contract StakingV2 is Ownable {
using SafeMath for uint;
using SafeERC20 for IERC20;
struct Stake {
uint amount;
uint stakeTime;
uint fraction;
uint rewardOut;
}
mapping(uint => mapping(address => Stake)) public stakes;
// Info of each pool.
struct Pool {
uint rewardAmount; // Pool reward tokens limit
uint startTime;
uint endTime;
uint total;
uint freezeTime;
uint freezePercent;
}
Pool[] public pools;
address public stakeToken; // Uniswap LP token from pool MRCH/USDT
address public rewardToken; // MRCH token
event AddPool(uint pid, uint reward, uint startTime, uint endTime, uint freezeTime, uint percent);
event PoolTotal(uint pid, uint oldTotal, uint newTotal);
event StakerFraction(uint pid, address staker, uint oldFraction, uint newFraction);
event Staked(uint pid, address staker, uint amount);
event RewardOut(uint pid, address staker, address token, uint amount);
constructor(
address stakeToken_,
address rewardToken_
) {
require(stakeToken_ != address(0), "MRCHStaking::constructor: stake token address is 0x0");
stakeToken = stakeToken_;
require(rewardToken_ != address(0), "MRCHStaking::constructor: reward token address is 0x0");
rewardToken = rewardToken_;
}
function addPool(uint rewardAmount_, uint startTime_, uint endTime_, uint freezeTime_, uint freezePercent_) public onlyOwner {
require(getTimeStamp() <= startTime_, "MRCHStaking::addPool: bad timing for the request");
require(startTime_ < endTime_, "MRCHStaking::addPool: endTime > startTime");
doTransferIn(msg.sender, rewardToken, rewardAmount_);
pools.push(
Pool({
rewardAmount: rewardAmount_,
startTime: startTime_,
endTime: endTime_,
total: 0,
freezeTime: freezeTime_,
freezePercent: freezePercent_ // scaled by 1e18, for example 5% = 5e18, 0.01% = 1e16
})
);
emit AddPool(pools.length - 1, rewardAmount_, startTime_, endTime_, freezeTime_, freezePercent_);
}
function stake(uint pid, uint amount) public returns (bool) {
require(amount > 0, "MRCHStaking::stake: amount must be positive");
uint timeStamp = getTimeStamp();
require(timeStamp < pools[pid].endTime, "MRCHStaking::stake: bad timing for the request");
address staker = msg.sender;
doTransferIn(staker, stakeToken, amount);
// Transfer is completed
stakes[pid][staker].amount = stakes[pid][staker].amount.add(amount);
uint addition;
if (timeStamp < pools[pid].startTime) {
stakes[pid][staker].stakeTime = pools[pid].startTime;
addition = (pools[pid].endTime.sub(pools[pid].startTime)).mul(amount);
} else {
stakes[pid][staker].stakeTime = timeStamp;
addition = (pools[pid].endTime.sub(timeStamp)).mul(amount);
}
uint oldFraction = stakes[pid][staker].fraction;
stakes[pid][staker].fraction = stakes[pid][staker].fraction.add(addition);
uint oldTotal = pools[pid].total;
pools[pid].total = pools[pid].total.add(addition);
emit Staked(pid, staker, amount);
emit StakerFraction(pid, staker, oldFraction, stakes[pid][staker].fraction);
emit PoolTotal(pid, oldTotal, pools[pid].total);
return true;
}
function withdraw(uint pid) public returns (bool) {
require(claim(pid), "MRCHStaking::withdraw: claim error");
uint amount = stakes[pid][msg.sender].amount;
return withdrawWithoutReward(pid, amount);
}
function withdrawWithoutReward(uint pid, uint amount) public returns (bool) {
return withdrawInternal(pid, msg.sender, amount);
}
function withdrawInternal(uint pid, address staker, uint amount) internal returns (bool) {
require(amount > 0, "MRCHStaking::withdrawInternal: amount must be positive");
require(amount <= stakes[pid][msg.sender].amount, "MRCHStaking::withdrawInternal: not enough balance");
stakes[pid][staker].amount = stakes[pid][staker].amount.sub(amount);
uint freezeTime = stakes[pid][staker].stakeTime.add(pools[pid].freezeTime);
if (getTimeStamp() < freezeTime) {
uint freezeAmount = amount.mul(pools[pid].freezePercent).div(100);
amount = amount.sub(freezeAmount);
}
doTransferOut(stakeToken, staker, amount);
return true;
}
function claim(uint pid) public returns (bool) {
require(getTimeStamp() > pools[pid].endTime, "MRCHStaking::claim: bad timing for the request");
address staker = msg.sender;
uint rewardAmount = currentTotalReward(pid, staker);
if (rewardAmount == 0) {
return true;
}
doTransferOut(rewardToken, staker, rewardAmount);
stakes[pid][staker].rewardOut = stakes[pid][staker].rewardOut.add(rewardAmount);
emit RewardOut(pid, staker, rewardToken, rewardAmount);
return true;
}
function currentTotalReward(uint pid, address staker) public view returns (uint) {
uint totalRewardAmount = pools[pid].rewardAmount;
uint total = pools[pid].total;
if (total == 0) {
return 0;
}
uint fraction = stakes[pid][staker].fraction;
uint rewardOut = stakes[pid][staker].rewardOut;
uint rewardAmount = totalRewardAmount.mul(fraction).div(total);
return rewardAmount.sub(rewardOut);
}
function doTransferOut(address token, address to, uint amount) internal {
if (amount == 0) {
return;
}
IERC20 ERC20Interface = IERC20(token);
ERC20Interface.safeTransfer(to, amount);
}
function doTransferIn(address from, address token, uint amount) internal {
IERC20 ERC20Interface = IERC20(token);
ERC20Interface.safeTransferFrom(from, address(this), amount);
}
function transferTokens(address token, address to, uint amount) public onlyOwner {
doTransferOut(token, to, amount);
}
function getTimeStamp() public view virtual returns (uint) {
return block.timestamp;
}
function getPoolLength() public view returns(uint) {
return pools.length;
}
}

因此,在成功部署到rinkeby测试网之后,我不确定这意味着什么(其中一些(。

所以我知道一个事实,回报Amount_-是每个区块将分配多少奖励。

startTime_是游泳池应该开始的时间(比如你可以使用的日期和时间https://www.epochconverter.com/为此。

startTime_i是指合同将停止分配奖励的时间。

冻结时间就像锁定代币,所以当用户试图在冻结时间之前取出时,他们将无法取出。

冻结百分比是Apy,它随着时间的推移而减少。

我真的不太理解,所以如果我错了,请纠正。我来这里的原因是;如果我对冷冻时间和冷冻百分比的理解是正确的,我如何做到这一点或在字段中输入它。

我知道冷冻百分比可以这样输入5%=5e18。

但是冷冻时间呢?。

如果有人有更好的理解,请与我分享

回答您的问题:

我知道冷冻百分比可以这样输入5%=5e18。-这是正确的。您可以输入一个百分比作为一个整数,并将其视为整数,例如,5将是.05

但是冷冻时间呢?冻结时间是可以以unix时间戳格式发布已锁定LP令牌冻结部分的结束日期。在这种情况下,当有人撤回他们的股份时,如果你使用AddPool创建池,并输入一个值,例如";1679505397(2023年3月(";在冻结时间,它将提取他们的股份减去冻结的金额,在上面的情况下,它被硬编码为5%。例如,我押了10个LP代币。我选择今天提款,但我只会拿回9.5,因为冻结百分比是5,时间戳小于冻结时间。我将不得不等到2023年3月才能退出决赛。

相关内容

  • 没有找到相关文章

最新更新