编译器
0.8.14+commit.80d49f37
文件 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:IERC165.sol
pragma solidity ^0.8.0;
interface IERC165 {
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
文件 3 的 6:IERC721.sol
pragma solidity ^0.8.0;
import "../../utils/introspection/IERC165.sol";
interface IERC721 is IERC165 {
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);
}
文件 4 的 6:IERC721Receiver.sol
pragma solidity ^0.8.0;
interface IERC721Receiver {
function onERC721Received(
address operator,
address from,
uint256 tokenId,
bytes calldata data
) external returns (bytes4);
}
文件 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());
}
function owner() public view virtual returns (address) {
return _owner;
}
modifier onlyOwner() {
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:WhiteSandsStaking.sol
pragma solidity ^0.8.14;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
contract WhiteSandsStaking is IERC721Receiver, Ownable {
struct TokenInfo {
uint32 collectionId;
uint32 id;
uint32 timestamp;
address owner;
}
mapping(IERC721 => bool) public _acceptedCollections;
mapping(IERC721 => uint32) public _collectionToId;
mapping(IERC721 => mapping(uint256 => mapping(address => uint256)))
public _indexOfTokens;
mapping(IERC721 => mapping(uint256 => mapping(address => uint256)))
public _indexOfTokensByOwners;
mapping(IERC721 => mapping(uint256 => mapping(address => bool)))
public _isStaked;
mapping(address => TokenInfo[]) public _stakedByOwners;
TokenInfo[] public _staked;
IERC721[] public _collections;
constructor(address[] memory collections) {
for (uint256 i = 0; i < collections.length; i++) {
acceptCollection(collections[i]);
}
}
function acceptCollection(address collection_) public onlyOwner {
IERC721 collection = IERC721(collection_);
_collections.push(collection);
_collectionToId[collection] = uint32(_collections.length) - 1;
_acceptedCollections[collection] = true;
}
function removeCollection(address collection) external onlyOwner {
delete _acceptedCollections[IERC721(collection)];
}
function stake(address[] calldata collections, uint256[] calldata ids)
external
{
require(collections.length == ids.length, "!params");
for (uint256 i = 0; i < collections.length; i++) {
IERC721 collection = IERC721(collections[i]);
uint256 id = ids[i];
require(_acceptedCollections[collection], "!collection");
_isStaked[collection][id][msg.sender] = true;
TokenInfo memory ti = TokenInfo(
_collectionToId[collection],
uint32(id),
uint32(block.timestamp),
msg.sender
);
_staked.push(ti);
_indexOfTokens[collection][id][msg.sender] = _staked.length - 1;
_stakedByOwners[msg.sender].push(ti);
_indexOfTokensByOwners[collection][id][msg.sender] =
_stakedByOwners[msg.sender].length -
1;
collection.safeTransferFrom(
msg.sender,
address(this),
id,
abi.encodePacked(WhiteSandsStaking.stake.selector)
);
}
}
function unstake(address[] calldata collections, uint256[] calldata ids)
external
{
require(collections.length == ids.length, "!params");
for (uint256 i = 0; i < collections.length; i++) {
IERC721 collection = IERC721(collections[i]);
uint256 id = ids[i];
require(_isStaked[collection][id][msg.sender], "!staked");
if (_stakedByOwners[msg.sender].length > 1) {
uint256 index = _indexOfTokensByOwners[collection][id][
msg.sender
];
TokenInfo memory last = _stakedByOwners[msg.sender][
_stakedByOwners[msg.sender].length - 1
];
_stakedByOwners[msg.sender][index] = last;
_indexOfTokensByOwners[_collections[last.collectionId]][
last.id
][msg.sender] = index;
}
_stakedByOwners[msg.sender].pop();
if (_staked.length > 1) {
uint256 index = _indexOfTokens[collection][id][msg.sender];
TokenInfo memory last = _staked[_staked.length - 1];
_staked[index] = last;
_indexOfTokens[_collections[last.collectionId]][last.id][
last.owner
] = index;
}
_staked.pop();
delete _indexOfTokens[collection][id][msg.sender];
delete _indexOfTokensByOwners[collection][id][msg.sender];
delete _isStaked[collection][id][msg.sender];
collection.safeTransferFrom(address(this), msg.sender, id);
}
}
function getStakedByOwner(address owner)
external
view
returns (
address[] memory,
uint256[] memory,
uint256[] memory
)
{
uint256 count = _stakedByOwners[owner].length;
address[] memory collections = new address[](count);
uint256[] memory ids = new uint256[](count);
uint256[] memory timestamps = new uint256[](count);
for (uint256 i = 0; i < count; i++) {
TokenInfo memory ti = _stakedByOwners[owner][i];
collections[i] = address(_collections[ti.collectionId]);
ids[i] = ti.id;
timestamps[i] = ti.timestamp;
}
return (collections, ids, timestamps);
}
function getStaked()
external
view
returns (
address[] memory,
uint256[] memory,
address[] memory,
uint256[] memory
)
{
return getStakedFrom(0, _staked.length);
}
function getStakedFrom(uint256 from, uint256 count)
public
view
returns (
address[] memory,
uint256[] memory,
address[] memory,
uint256[] memory
)
{
address[] memory collections = new address[](count);
uint256[] memory ids = new uint256[](count);
address[] memory owners = new address[](count);
uint256[] memory timestamps = new uint256[](count);
for (uint256 i = from; i < count; i++) {
TokenInfo memory ti = _staked[i];
collections[i] = address(_collections[ti.collectionId]);
ids[i] = ti.id;
owners[i] = ti.owner;
timestamps[i] = ti.timestamp;
}
return (collections, ids, owners, timestamps);
}
function onERC721Received(
address,
address,
uint256,
bytes calldata data
) external pure override returns (bytes4) {
require(
keccak256(data) ==
keccak256(abi.encodePacked(WhiteSandsStaking.stake.selector)),
"!invalid"
);
return IERC721Receiver.onERC721Received.selector;
}
}
{
"compilationTarget": {
"contracts/WhiteSandsStaking.sol": "WhiteSandsStaking"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 10000
},
"remappings": []
}
[{"inputs":[{"internalType":"address[]","name":"collections","type":"address[]"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"inputs":[{"internalType":"contract IERC721","name":"","type":"address"}],"name":"_acceptedCollections","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC721","name":"","type":"address"}],"name":"_collectionToId","outputs":[{"internalType":"uint32","name":"","type":"uint32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"_collections","outputs":[{"internalType":"contract IERC721","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC721","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"address","name":"","type":"address"}],"name":"_indexOfTokens","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC721","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"address","name":"","type":"address"}],"name":"_indexOfTokensByOwners","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC721","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"address","name":"","type":"address"}],"name":"_isStaked","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"_staked","outputs":[{"internalType":"uint32","name":"collectionId","type":"uint32"},{"internalType":"uint32","name":"id","type":"uint32"},{"internalType":"uint32","name":"timestamp","type":"uint32"},{"internalType":"address","name":"owner","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"_stakedByOwners","outputs":[{"internalType":"uint32","name":"collectionId","type":"uint32"},{"internalType":"uint32","name":"id","type":"uint32"},{"internalType":"uint32","name":"timestamp","type":"uint32"},{"internalType":"address","name":"owner","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"collection_","type":"address"}],"name":"acceptCollection","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"getStaked","outputs":[{"internalType":"address[]","name":"","type":"address[]"},{"internalType":"uint256[]","name":"","type":"uint256[]"},{"internalType":"address[]","name":"","type":"address[]"},{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"getStakedByOwner","outputs":[{"internalType":"address[]","name":"","type":"address[]"},{"internalType":"uint256[]","name":"","type":"uint256[]"},{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"from","type":"uint256"},{"internalType":"uint256","name":"count","type":"uint256"}],"name":"getStakedFrom","outputs":[{"internalType":"address[]","name":"","type":"address[]"},{"internalType":"uint256[]","name":"","type":"uint256[]"},{"internalType":"address[]","name":"","type":"address[]"},{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"onERC721Received","outputs":[{"internalType":"bytes4","name":"","type":"bytes4"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"collection","type":"address"}],"name":"removeCollection","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address[]","name":"collections","type":"address[]"},{"internalType":"uint256[]","name":"ids","type":"uint256[]"}],"name":"stake","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address[]","name":"collections","type":"address[]"},{"internalType":"uint256[]","name":"ids","type":"uint256[]"}],"name":"unstake","outputs":[],"stateMutability":"nonpayable","type":"function"}]