Spaces:
No application file
No application file
Create Efikcoin mining pool staking
Browse filesnew EfikcoinStakingPool(0xCDdA7382C645c368231bca9AFCE80F7184854444)
- Efikcoin mining pool staking +84 -0
Efikcoin mining pool staking
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// SPDX-License-Identifier: MIT
|
| 2 |
+
pragma solidity ^0.8.0;
|
| 3 |
+
|
| 4 |
+
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
| 5 |
+
import "@openzeppelin/contracts/access/Ownable.sol";
|
| 6 |
+
|
| 7 |
+
contract EfikcoinStakingPool is Ownable {
|
| 8 |
+
IERC20 public efikToken;
|
| 9 |
+
|
| 10 |
+
uint256 public rewardRate = 10; // 10 EFIK per day per 1000 staked
|
| 11 |
+
uint256 public totalStaked;
|
| 12 |
+
|
| 13 |
+
struct StakeInfo {
|
| 14 |
+
uint256 amount;
|
| 15 |
+
uint256 startTime;
|
| 16 |
+
uint256 rewardClaimed;
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
mapping(address => StakeInfo) public stakes;
|
| 20 |
+
|
| 21 |
+
event Staked(address indexed user, uint256 amount);
|
| 22 |
+
event RewardClaimed(address indexed user, uint256 reward);
|
| 23 |
+
event Withdrawn(address indexed user, uint256 amount);
|
| 24 |
+
|
| 25 |
+
constructor(address _efikTokenAddress) {
|
| 26 |
+
efikToken = IERC20(_efikTokenAddress);
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
function stake(uint256 _amount) external {
|
| 30 |
+
require(_amount > 0, "Stake amount must be greater than zero");
|
| 31 |
+
efikToken.transferFrom(msg.sender, address(this), _amount);
|
| 32 |
+
|
| 33 |
+
StakeInfo storage user = stakes[msg.sender];
|
| 34 |
+
user.amount += _amount;
|
| 35 |
+
user.startTime = block.timestamp;
|
| 36 |
+
|
| 37 |
+
totalStaked += _amount;
|
| 38 |
+
|
| 39 |
+
emit Staked(msg.sender, _amount);
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
function calculateReward(address _user) public view returns (uint256) {
|
| 43 |
+
StakeInfo memory user = stakes[_user];
|
| 44 |
+
uint256 duration = block.timestamp - user.startTime;
|
| 45 |
+
uint256 daysStaked = duration / 1 days;
|
| 46 |
+
return (user.amount * rewardRate * daysStaked) / 1000;
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
function claimReward() external {
|
| 50 |
+
uint256 reward = calculateReward(msg.sender);
|
| 51 |
+
require(reward > 0, "No reward available");
|
| 52 |
+
|
| 53 |
+
stakes[msg.sender].rewardClaimed += reward;
|
| 54 |
+
stakes[msg.sender].startTime = block.timestamp;
|
| 55 |
+
|
| 56 |
+
efikToken.transfer(msg.sender, reward);
|
| 57 |
+
|
| 58 |
+
emit RewardClaimed(msg.sender, reward);
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
function withdrawStake() external {
|
| 62 |
+
StakeInfo storage user = stakes[msg.sender];
|
| 63 |
+
require(user.amount > 0, "No staked amount");
|
| 64 |
+
|
| 65 |
+
uint256 reward = calculateReward(msg.sender);
|
| 66 |
+
uint256 totalAmount = user.amount + reward;
|
| 67 |
+
|
| 68 |
+
efikToken.transfer(msg.sender, totalAmount);
|
| 69 |
+
|
| 70 |
+
totalStaked -= user.amount;
|
| 71 |
+
delete stakes[msg.sender];
|
| 72 |
+
|
| 73 |
+
emit Withdrawn(msg.sender, totalAmount);
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
function updateRewardRate(uint256 _newRate) external onlyOwner {
|
| 77 |
+
rewardRate = _newRate;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
function emergencyWithdraw() external onlyOwner {
|
| 81 |
+
uint256 balance = efikToken.balanceOf(address(this));
|
| 82 |
+
efikToken.transfer(owner(), balance);
|
| 83 |
+
}
|
| 84 |
+
}
|