编译器
0.8.20+commit.a1b79de6
文件 1 的 7:Context.sol
pragma solidity ^0.8.20;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
文件 2 的 7:IAccessControl.sol
pragma solidity ^0.8.20;
interface IAccessControl {
error AccessControlUnauthorizedAccount(address account, bytes32 neededRole);
error AccessControlBadConfirmation();
event RoleAdminChanged(bytes32 indexed role, bytes32 indexed previousAdminRole, bytes32 indexed newAdminRole);
event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender);
event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender);
function hasRole(bytes32 role, address account) external view returns (bool);
function getRoleAdmin(bytes32 role) external view returns (bytes32);
function grantRole(bytes32 role, address account) external;
function revokeRole(bytes32 role, address account) external;
function renounceRole(bytes32 role, address callerConfirmation) external;
}
文件 3 的 7:IERC20.sol
pragma solidity ^0.8.20;
interface IERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address to, uint256 value) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
}
文件 4 的 7:ILingoToken.sol
pragma solidity 0.8.20;
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IAccessControl} from "@openzeppelin/contracts/access/IAccessControl.sol";
interface ILingoToken is IERC20, IAccessControl {
function INTERNAL_ROLE() external view returns (bytes32);
function MINTER_ROLE() external view returns (bytes32);
}
文件 5 的 7:Ownable.sol
pragma solidity ^0.8.20;
import {Context} from "../utils/Context.sol";
abstract contract Ownable is Context {
address private _owner;
error OwnableUnauthorizedAccount(address account);
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
modifier onlyOwner() {
_checkOwner();
_;
}
function owner() public view virtual returns (address) {
return _owner;
}
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
文件 6 的 7:Ownable2Step.sol
pragma solidity ^0.8.20;
import {Ownable} from "./Ownable.sol";
abstract contract Ownable2Step is Ownable {
address private _pendingOwner;
event OwnershipTransferStarted(address indexed previousOwner, address indexed newOwner);
function pendingOwner() public view virtual returns (address) {
return _pendingOwner;
}
function transferOwnership(address newOwner) public virtual override onlyOwner {
_pendingOwner = newOwner;
emit OwnershipTransferStarted(owner(), newOwner);
}
function _transferOwnership(address newOwner) internal virtual override {
delete _pendingOwner;
super._transferOwnership(newOwner);
}
function acceptOwnership() public virtual {
address sender = _msgSender();
if (pendingOwner() != sender) {
revert OwnableUnauthorizedAccount(sender);
}
_transferOwnership(sender);
}
}
文件 7 的 7:TokenStaking.sol
pragma solidity 0.8.20;
import {ILingoToken} from "./ILingoToken.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {Ownable2Step} from "@openzeppelin/contracts/access/Ownable2Step.sol";
contract TokenStaking is Ownable2Step {
struct Position {
uint128 amount;
uint128 unlockBlock;
}
ILingoToken public immutable LINGO_TOKEN;
uint256 public constant MIN_DEPOSIT = 10 ** 18;
uint256[] public lockDurations;
uint256 public lockDurationsCount;
mapping(address => Position[]) private userPositions;
event Staked(address indexed user, uint256 amount, uint256 duration);
event Unstaked(address indexed user, uint256 amount, uint256 unlockBlock);
event LockDurationsUpdated(uint256[] durations);
error InvalidDuration();
error StakeStillLocked();
error MissingInternalRole();
error InsufficientAmount();
error UnauthorizedStakingOnBehalf();
constructor(
address _initialOwner,
ILingoToken _lingoToken,
uint256[] memory _lockDurations
) Ownable(_initialOwner) {
LINGO_TOKEN = ILingoToken(_lingoToken);
lockDurations = _lockDurations;
lockDurationsCount = _lockDurations.length;
}
function stake(
uint256 _amount,
uint256 _durationIndex,
uint256 _expectedDuration,
address _user
) external {
if (_amount < MIN_DEPOSIT) revert InsufficientAmount();
if (!LINGO_TOKEN.hasRole(LINGO_TOKEN.INTERNAL_ROLE(), address(this)))
revert MissingInternalRole();
if (lockDurations.length < _durationIndex) revert InvalidDuration();
if (msg.sender != _user) {
if (!LINGO_TOKEN.hasRole(LINGO_TOKEN.MINTER_ROLE(), msg.sender)) {
revert UnauthorizedStakingOnBehalf();
}
}
uint256 duration = lockDurations[_durationIndex];
if (duration != _expectedDuration) revert InvalidDuration();
uint256 unlockBlock = block.number + duration;
userPositions[_user].push(
Position(uint128(_amount), uint128(unlockBlock))
);
emit Staked(_user, _amount, duration);
LINGO_TOKEN.transferFrom(msg.sender, address(this), _amount);
}
function unstake(uint256 _stakeIndex) external {
Position memory stakeDetails = userPositions[msg.sender][_stakeIndex];
if (block.number < stakeDetails.unlockBlock) revert StakeStillLocked();
uint256 amount = stakeDetails.amount;
uint256 positionLength = userPositions[msg.sender].length;
if (_stakeIndex < positionLength - 1) {
userPositions[msg.sender][_stakeIndex] = userPositions[msg.sender][
positionLength - 1
];
}
userPositions[msg.sender].pop();
emit Unstaked(msg.sender, amount, stakeDetails.unlockBlock);
LINGO_TOKEN.transfer(msg.sender, amount);
}
function updateLockDurations(
uint256[] calldata _durations
) external onlyOwner {
lockDurations = _durations;
lockDurationsCount = _durations.length;
emit LockDurationsUpdated(_durations);
}
function getStakes(
address _user
) external view returns (Position[] memory) {
return userPositions[_user];
}
}
{
"compilationTarget": {
"contracts/TokenStaking.sol": "TokenStaking"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": false,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_initialOwner","type":"address"},{"internalType":"contract ILingoToken","name":"_lingoToken","type":"address"},{"internalType":"uint256[]","name":"_lockDurations","type":"uint256[]"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"InsufficientAmount","type":"error"},{"inputs":[],"name":"InvalidDuration","type":"error"},{"inputs":[],"name":"MissingInternalRole","type":"error"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"OwnableInvalidOwner","type":"error"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"OwnableUnauthorizedAccount","type":"error"},{"inputs":[],"name":"StakeStillLocked","type":"error"},{"inputs":[],"name":"UnauthorizedStakingOnBehalf","type":"error"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256[]","name":"durations","type":"uint256[]"}],"name":"LockDurationsUpdated","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferStarted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"duration","type":"uint256"}],"name":"Staked","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"unlockBlock","type":"uint256"}],"name":"Unstaked","type":"event"},{"inputs":[],"name":"LINGO_TOKEN","outputs":[{"internalType":"contract ILingoToken","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"MIN_DEPOSIT","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"acceptOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_user","type":"address"}],"name":"getStakes","outputs":[{"components":[{"internalType":"uint128","name":"amount","type":"uint128"},{"internalType":"uint128","name":"unlockBlock","type":"uint128"}],"internalType":"struct TokenStaking.Position[]","name":"","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"lockDurations","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"lockDurationsCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pendingOwner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"},{"internalType":"uint256","name":"_durationIndex","type":"uint256"},{"internalType":"uint256","name":"_expectedDuration","type":"uint256"},{"internalType":"address","name":"_user","type":"address"}],"name":"stake","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_stakeIndex","type":"uint256"}],"name":"unstake","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256[]","name":"_durations","type":"uint256[]"}],"name":"updateLockDurations","outputs":[],"stateMutability":"nonpayable","type":"function"}]