Verified contractExactUNLICENSED
TokenStaking
Address0x940fb21c8d673e9afc69af643e5820cb4026c7fa
- Balance
0.02581 ETH
- Transactions
2,109
- Compiler
solc 0.8.20
- Same code
Only this contract
Details
- Compiler
- solc 0.8.20+commit.a1b79de6
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- paris
- License
- UNLICENSED
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 20,679,475
- Creation transaction
- 0x214c50a6e0…b9fb0f046f
2 files
- contracts/TokenStaking.solmain4.75 KB
- contracts/utils/IERC20.sol796 B
contracts/TokenStaking.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.20;
/*
┌─────────────────────────────────────────────────────────────────────────┐
│ │
│ _______ ______ _ │
│ |__ __| | ____| (_) │
│ | | __ ___ _| |__ __ _ _ __ _ __ ___ _ _ __ __ _ │
│ | |/ _` \ \/ / __/ _` | '__| '_ ` _ \ | | '_ \ / _` | │
│ | | (_| |> <| | | (_| | | | | | | | | | | | | | (_| | │
│ |_|\__,_/_/\_\_| \__,_|_| |_| |_| |_|(_)_|_| |_|\__, | │
│ __/ | │
│ |___/ │
│ │
│ taxfarm.ing │
│ │
└─────────────────────────────────────────────────────────────────────────┘
*/
import {IERC20} from "./utils/IERC20.sol";
contract TokenStaking {
IERC20 public taxFarmingToken;
uint public totalStaked; // total token staked
uint public totalRewards; // total rewards received by the contract
uint private accumulatedRewardsPerToken = 1; // act as a price entry for new staker, the delta between this variable entry and exit is the effective rewards per token
uint private constant DECIMALS = 1e12; // floor precision protection decimals
struct Stake {
uint tokensStaked;
uint accumulatedRewardsPerToken;
}
mapping (address => Stake) public stakersInfos;
mapping (address => uint) public lastTx; // last tx block of stakers, used to avoid reentrancy and flashloans attacks
event TokenStaked(address indexed user, uint amount, uint accumulatedRewardsPerToken);
event TokenUnstaked(address indexed user, uint amount, uint accumulatedRewardsPerToken);
constructor(address _taxFarmingToken) {
taxFarmingToken = IERC20(_taxFarmingToken);
}
// prevent user to use any function again in this block
modifier blockUser(address user) {
require(lastTx[user] != block.number, "User blocked");
lastTx[user] = block.number;
_;
}
function stake(uint amount) external blockUser(msg.sender) {
if (amount == 0) return;
address user = msg.sender;
if (stakersInfos[user].tokensStaked == 0) stakersInfos[user].accumulatedRewardsPerToken = accumulatedRewardsPerToken;
else _claim(user);
taxFarmingToken.transferFrom(user, address(this), amount);
totalStaked += amount;
stakersInfos[user].tokensStaked += amount;
emit TokenStaked(user, amount, accumulatedRewardsPerToken);
}
function unstake(uint amount) external blockUser(msg.sender) {
address user = msg.sender;
if (amount == 0 || stakersInfos[user].tokensStaked == 0) return;
_claim(user);
require(stakersInfos[user].tokensStaked >= amount, "Not enough staked tokens");
stakersInfos[user].tokensStaked -= amount;
totalStaked -= amount;
taxFarmingToken.transfer(user, amount);
emit TokenUnstaked(user, amount, accumulatedRewardsPerToken);
}
function claim() external blockUser(msg.sender) {
_claim(msg.sender);
}
function _claim(address user) private {
uint rewards = getUserRewards(user);
stakersInfos[user].accumulatedRewardsPerToken = accumulatedRewardsPerToken;
if (rewards == 0) return;
(bool success, ) = payable(user).call{value: rewards}("");
require(success, "Unable to claim rewards");
}
function getUserRewards(address user) public view returns (uint) {
uint rewardsPerToken = accumulatedRewardsPerToken - stakersInfos[user].accumulatedRewardsPerToken;
uint rewards = (rewardsPerToken * stakersInfos[user].tokensStaked) / DECIMALS;
return rewards;
}
receive() payable external {
require(totalStaked != 0, "No stakers");
uint rewardsPerToken = (msg.value * DECIMALS) / totalStaked;
accumulatedRewardsPerToken += rewardsPerToken;
totalRewards += msg.value;
}
}
contracts/utils/IERC20.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(
address sender,
address recipient,
uint256 amount
) external returns (bool);
function burn(uint256 value) external;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}Compiler settingsShow
{
"metadata": {
"bytecodeHash": "ipfs"
},
"libraries": {},
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "paris",
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.