编译器
0.8.19+commit.7dd6d404
文件 1 的 6:Context.sol
pragma solidity ^0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
}
文件 2 的 6:IERC721A.sol
pragma solidity ^0.8.4;
interface IERC721A {
error ApprovalCallerNotOwnerNorApproved();
error ApprovalQueryForNonexistentToken();
error ApproveToCaller();
error BalanceQueryForZeroAddress();
error MintToZeroAddress();
error MintZeroQuantity();
error OwnerQueryForNonexistentToken();
error TransferCallerNotOwnerNorApproved();
error TransferFromIncorrectOwner();
error TransferToNonERC721ReceiverImplementer();
error TransferToZeroAddress();
error URIQueryForNonexistentToken();
error MintERC2309QuantityExceedsLimit();
error OwnershipNotInitializedForExtraData();
struct TokenOwnership {
address addr;
uint64 startTimestamp;
bool burned;
uint24 extraData;
}
function totalSupply() external view returns (uint256);
function supportsInterface(bytes4 interfaceId) external view returns (bool);
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
function balanceOf(address owner) external view returns (uint256 balance);
function ownerOf(uint256 tokenId) external view returns (address owner);
function safeTransferFrom(
address from,
address to,
uint256 tokenId,
bytes calldata data
) external;
function safeTransferFrom(
address from,
address to,
uint256 tokenId
) external;
function transferFrom(
address from,
address to,
uint256 tokenId
) external;
function approve(address to, uint256 tokenId) external;
function setApprovalForAll(address operator, bool _approved) external;
function getApproved(uint256 tokenId) external view returns (address operator);
function isApprovedForAll(address owner, address operator) external view returns (bool);
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function tokenURI(uint256 tokenId) external view returns (string memory);
event ConsecutiveTransfer(uint256 indexed fromTokenId, uint256 toTokenId, address indexed from, address indexed to);
}
文件 3 的 6:IStaking.sol
pragma solidity ^0.8.4;
interface IStaking {
event TokenStaked(address owner, uint256 indexed tokenId);
event TokenUnstaked(address owner, uint256 indexed tokenId);
function stakeTokens(uint256[] calldata tokenIds) external;
function unstakeTokens(uint256[] calldata tokenIds) external;
function isTokenStaked(uint256 tokenId) external view returns (bool);
function getStakingTier(address wallet) external view returns (uint16);
}
文件 4 的 6:Manageable.sol
pragma solidity ^0.8.2;
import "@openzeppelin/contracts/access/Ownable.sol";
error Manageable__WalletIsNotAManager();
error Manageable__ZeroAddressProhibited();
abstract contract Manageable is Ownable {
mapping(address => bool) private s_managers;
modifier onlyManager() {
_checkManager();
_;
}
constructor() {}
function addManagers(address[] calldata wallets) external onlyOwner {
for (uint256 index = 0; index < wallets.length; index++) {
_addManager(wallets[index]);
}
}
function removeManagers(address[] calldata wallets) external onlyOwner {
for (uint256 index = 0; index < wallets.length; index++) {
_removeManager(wallets[index]);
}
}
function isManager(address wallet) public view returns (bool) {
if (owner() == wallet) return true;
return s_managers[wallet];
}
function _addManager(address wallet) internal {
if (wallet == address(0)) revert Manageable__ZeroAddressProhibited();
s_managers[wallet] = true;
}
function _removeManager(address wallet) internal {
if (!isManager(wallet)) revert Manageable__WalletIsNotAManager();
if (wallet == address(0)) revert Manageable__ZeroAddressProhibited();
s_managers[wallet] = false;
}
function _checkManager() internal view {
if (!isManager(_msgSender())) revert Manageable__WalletIsNotAManager();
}
}
文件 5 的 6:Ownable.sol
pragma solidity ^0.8.0;
import "../utils/Context.sol";
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor() {
_transferOwnership(_msgSender());
}
modifier onlyOwner() {
_checkOwner();
_;
}
function owner() public view virtual returns (address) {
return _owner;
}
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
文件 6 的 6:Staking.sol
pragma solidity ^0.8.4;
import "./IStaking.sol";
import "../library/Manageable.sol";
import "../library/erc721A/IERC721A.sol";
error Staking__ZeroAddressProhibited();
error Staking__InvalidStakingBonusPercent();
error Staking__InvalidMaxCumulativeStakingBonusPercent();
error Staking__NotATokenOwner(address wallet, uint256 tokenId);
error Staking__EmptyTierRequirements();
error Staking__EmptyArray();
error Staking__StakingBatchIsTooBig();
error Staking__TokenIsAlreadyStaked(uint256 tokenId);
error Staking__TokenIsNotStaked(uint256 tokenId);
contract Staking is IStaking, Manageable {
struct WalletStakingInfo {
uint32 lastStakingMilestoneUnixTime;
uint32 savedStakingPoints;
uint16 countOfStakedTokens;
}
IERC721A private immutable i_collectionContract;
uint8 private s_stakingBonusPercent = 2;
uint8 private s_maxCumulativeStakingBonusPercent = 20;
uint32[] private s_stakingTierRequirements;
mapping(uint256 => bool) private s_isTokenStaked;
mapping(address => WalletStakingInfo) private s_walletStakingInfo;
constructor(IERC721A collectionContract) {
if (address(collectionContract) == address(0)) revert Staking__ZeroAddressProhibited();
i_collectionContract = collectionContract;
}
function stakeTokens(uint256[] calldata tokenIds) external {
if (tokenIds.length == 0) revert Staking__EmptyArray();
if (tokenIds.length > 100) revert Staking__StakingBatchIsTooBig();
uint32 currentTime = uint32(block.timestamp);
uint32 currentStakingPoints = getStakingPoints(msg.sender);
uint16 currentCountOfStakedTokens = s_walletStakingInfo[msg.sender].countOfStakedTokens;
s_walletStakingInfo[msg.sender] = WalletStakingInfo({
lastStakingMilestoneUnixTime: currentTime,
savedStakingPoints: currentStakingPoints,
countOfStakedTokens: currentCountOfStakedTokens + uint16(tokenIds.length)
});
for (uint256 i = 0; i < tokenIds.length; i++) {
uint256 tokenId = tokenIds[i];
if (isTokenStaked(tokenId)) revert Staking__TokenIsAlreadyStaked(tokenId);
if (i_collectionContract.ownerOf(tokenId) != msg.sender)
revert Staking__NotATokenOwner(msg.sender, tokenId);
s_isTokenStaked[tokenId] = true;
emit TokenStaked(msg.sender, tokenId);
}
}
function unstakeTokens(uint256[] calldata tokenIds) external {
if (tokenIds.length == 0) revert Staking__EmptyArray();
if (tokenIds.length > 100) revert Staking__StakingBatchIsTooBig();
uint32 currentTime = uint32(block.timestamp);
uint32 currentStakingPoints = getStakingPoints(msg.sender);
uint16 currentCountOfStakedTokens = s_walletStakingInfo[msg.sender].countOfStakedTokens;
s_walletStakingInfo[msg.sender] = WalletStakingInfo({
lastStakingMilestoneUnixTime: currentTime,
savedStakingPoints: currentStakingPoints,
countOfStakedTokens: currentCountOfStakedTokens - uint16(tokenIds.length)
});
for (uint256 i = 0; i < tokenIds.length; i++) {
uint256 tokenId = tokenIds[i];
if (!isTokenStaked(tokenId)) revert Staking__TokenIsNotStaked(tokenId);
if (i_collectionContract.ownerOf(tokenId) != msg.sender)
revert Staking__NotATokenOwner(msg.sender, tokenId);
s_isTokenStaked[tokenId] = false;
emit TokenUnstaked(msg.sender, tokenId);
}
}
function setStakingTierRequirements(uint32[] memory pointsPerTier) external onlyManager {
if (pointsPerTier.length == 0) revert Staking__EmptyTierRequirements();
s_stakingTierRequirements = pointsPerTier;
}
function setStakingBonusPercent(uint8 stakingBonusPercent) external onlyManager {
if (stakingBonusPercent > 100) revert Staking__InvalidStakingBonusPercent();
s_stakingBonusPercent = stakingBonusPercent;
}
function setMaxCumulativeStakingBonusPercent(
uint8 maxCumulativeStakingBonusPercent
) external onlyManager {
if (maxCumulativeStakingBonusPercent > 100)
revert Staking__InvalidMaxCumulativeStakingBonusPercent();
s_maxCumulativeStakingBonusPercent = maxCumulativeStakingBonusPercent;
}
function getStakingTier(address wallet) external view returns (uint16) {
if (wallet == address(0)) revert Staking__ZeroAddressProhibited();
uint32 stakingPoints = getStakingPoints(wallet);
uint32[] memory stakingTierRequirementsInSeconds = s_stakingTierRequirements;
uint16 stakingTier = 0;
for (uint16 i = 0; i < stakingTierRequirementsInSeconds.length; i++) {
if (stakingPoints < stakingTierRequirementsInSeconds[i]) break;
stakingTier++;
}
return stakingTier;
}
function getStakingPoints(address wallet) public view returns (uint32) {
WalletStakingInfo storage walletStakingInfo = s_walletStakingInfo[wallet];
uint32 savedStakingPoints = walletStakingInfo.savedStakingPoints;
uint256 secondsPassed = block.timestamp - walletStakingInfo.lastStakingMilestoneUnixTime;
uint32 basisStakingPointsPerSecond = getBasisStakingPointsPerSecond(wallet);
uint32 stakingPointsGained = uint32((secondsPassed * basisStakingPointsPerSecond) / 100);
return savedStakingPoints + stakingPointsGained;
}
function getBasisStakingPointsPerSecond(address wallet) public view returns (uint32) {
if (wallet == address(0)) revert Staking__ZeroAddressProhibited();
uint16 stakedTokensCount = s_walletStakingInfo[wallet].countOfStakedTokens;
if (stakedTokensCount == 0) return 0;
uint32 stakingBonusPercent = (stakedTokensCount - 1) * s_stakingBonusPercent;
if (stakingBonusPercent > s_maxCumulativeStakingBonusPercent)
stakingBonusPercent = s_maxCumulativeStakingBonusPercent;
return 100 + stakingBonusPercent;
}
function getStakingBonusPercetnt() public view returns (uint32) {
return s_stakingBonusPercent;
}
function getMaxCumulativeStakingBonusPercent() public view returns (uint32) {
return s_maxCumulativeStakingBonusPercent;
}
function getStakingTierRequirements() public view returns (uint32[] memory) {
return s_stakingTierRequirements;
}
function getWalletStakingInfo(address wallet) public view returns (WalletStakingInfo memory) {
return s_walletStakingInfo[wallet];
}
function isTokenStaked(uint256 tokenId) public view returns (bool) {
return s_isTokenStaked[tokenId];
}
}
{
"compilationTarget": {
"contracts/staking/Staking.sol": "Staking"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": false,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"contract IERC721A","name":"collectionContract","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"Manageable__WalletIsNotAManager","type":"error"},{"inputs":[],"name":"Manageable__ZeroAddressProhibited","type":"error"},{"inputs":[],"name":"Staking__EmptyArray","type":"error"},{"inputs":[],"name":"Staking__EmptyTierRequirements","type":"error"},{"inputs":[],"name":"Staking__InvalidMaxCumulativeStakingBonusPercent","type":"error"},{"inputs":[],"name":"Staking__InvalidStakingBonusPercent","type":"error"},{"inputs":[{"internalType":"address","name":"wallet","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"Staking__NotATokenOwner","type":"error"},{"inputs":[],"name":"Staking__StakingBatchIsTooBig","type":"error"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"Staking__TokenIsAlreadyStaked","type":"error"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"Staking__TokenIsNotStaked","type":"error"},{"inputs":[],"name":"Staking__ZeroAddressProhibited","type":"error"},{"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":false,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"TokenStaked","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"TokenUnstaked","type":"event"},{"inputs":[{"internalType":"address[]","name":"wallets","type":"address[]"}],"name":"addManagers","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"wallet","type":"address"}],"name":"getBasisStakingPointsPerSecond","outputs":[{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getMaxCumulativeStakingBonusPercent","outputs":[{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getStakingBonusPercetnt","outputs":[{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"wallet","type":"address"}],"name":"getStakingPoints","outputs":[{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"wallet","type":"address"}],"name":"getStakingTier","outputs":[{"internalType":"uint16","name":"","type":"uint16"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getStakingTierRequirements","outputs":[{"internalType":"uint32[]","name":"","type":"uint32[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"wallet","type":"address"}],"name":"getWalletStakingInfo","outputs":[{"components":[{"internalType":"uint32","name":"lastStakingMilestoneUnixTime","type":"uint32"},{"internalType":"uint32","name":"savedStakingPoints","type":"uint32"},{"internalType":"uint16","name":"countOfStakedTokens","type":"uint16"}],"internalType":"struct Staking.WalletStakingInfo","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"wallet","type":"address"}],"name":"isManager","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"isTokenStaked","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address[]","name":"wallets","type":"address[]"}],"name":"removeManagers","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint8","name":"maxCumulativeStakingBonusPercent","type":"uint8"}],"name":"setMaxCumulativeStakingBonusPercent","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint8","name":"stakingBonusPercent","type":"uint8"}],"name":"setStakingBonusPercent","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint32[]","name":"pointsPerTier","type":"uint32[]"}],"name":"setStakingTierRequirements","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256[]","name":"tokenIds","type":"uint256[]"}],"name":"stakeTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256[]","name":"tokenIds","type":"uint256[]"}],"name":"unstakeTokens","outputs":[],"stateMutability":"nonpayable","type":"function"}]