编译器
0.8.18+commit.87f61d96
文件 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:IERC20.sol
pragma solidity ^0.8.0;
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 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 from, address to, uint256 amount) external returns (bool);
}
文件 3 的 6:ILoveRoles.sol
pragma solidity 0.8.18;
interface ILoveRoles {
function grantRole(address account, string calldata role) external;
function revokeRole(address account, string calldata role) external;
function checkRole(address accountToCheck, string calldata role) external view returns (bool);
}
文件 4 的 6:LoveAirdrop.sol
pragma solidity 0.8.18;
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {Ownable} from '@openzeppelin/contracts/access/Ownable.sol';
import {MerkleProof} from '@openzeppelin/contracts/utils/cryptography/MerkleProof.sol';
import {ILoveRoles} from './interfaces/ILoveRoles.sol';
contract LoveAirdrop is Ownable {
struct LoveDrop {
address creator;
bytes32 root;
uint256 total;
uint256 claimed;
bool cancelled;
}
mapping(bytes32 nameHash => LoveDrop drop) public drops;
mapping(bytes32 nameHash => mapping(address recipient => bool isClaimed)) public isClaimed;
IERC20 private immutable _token;
ILoveRoles private immutable _marketplace;
uint256 public reservedBalance;
event LoveDropCreated(string dropName, address indexed creator, bytes32 root, uint256 total);
event LoveClaimed(string indexed dropName, address indexed recipient, uint256 amount);
event LoveDropCancelled(string dropName);
event TokensWithdrawn(uint256 amount);
constructor(address tokenAddress, address marketplace, address admin) {
_marketplace = ILoveRoles(marketplace);
_token = IERC20(tokenAddress);
transferOwnership(admin);
}
modifier onlyAdmin() {
bool isAdmin = _marketplace.checkRole(msg.sender, 'admin');
require(isAdmin || msg.sender == owner(), 'only admin');
_;
}
function createLoveDrop(string calldata dropName, bytes32 root, uint256 total) external onlyAdmin {
uint256 reservedTotal = reservedBalance + total;
require(_getLoveBalance() >= reservedTotal, 'insufficient balance');
bytes32 nameHash = keccak256(bytes(dropName));
require(drops[nameHash].creator == address(0), 'name already exists');
drops[nameHash] = LoveDrop({creator: msg.sender, root: root, total: total, claimed: 0, cancelled: false});
reservedBalance = reservedTotal;
emit LoveDropCreated(dropName, msg.sender, root, total);
}
function cancelLoveDrop(string calldata dropName) external onlyAdmin {
bytes32 nameHash = keccak256(bytes(dropName));
LoveDrop storage drop = drops[nameHash];
require(drop.creator != address(0), 'LoveDrop does not exist');
require(!drop.cancelled, 'LoveDrop is cancelled');
reservedBalance -= drop.total - drop.claimed;
drop.cancelled = true;
emit LoveDropCancelled(dropName);
}
function claim(string memory dropName, address recipient, uint256 amount, bytes32[] calldata proof) external {
bytes32 nameHash = keccak256(bytes(dropName));
require(drops[nameHash].creator != address(0), 'LoveDrop does not exist');
LoveDrop storage drop = drops[nameHash];
require(!drop.cancelled, 'LoveDrop is cancelled');
require(!isClaimed[nameHash][recipient], 'already claimed');
require(verifyClaim(proof, drop.root, recipient, amount), 'invalid proof');
isClaimed[nameHash][recipient] = true;
drop.claimed += amount;
reservedBalance -= amount;
require(_token.transfer(recipient, amount), 'transfer failed');
emit LoveClaimed(dropName, recipient, amount);
}
function withdrawTokens(uint256 amount) external onlyOwner {
require(amount > 0, 'Invalid amount');
require(_getLoveBalance() - reservedBalance >= amount, 'Insufficient balance (reserved)');
require(_token.transfer(owner(), amount), 'Transfer failed');
emit TokensWithdrawn(amount);
}
function verifyClaim(
bytes32[] memory proof,
bytes32 root,
address recipient,
uint256 amount
) public pure returns (bool) {
bytes32 leaf = keccak256(bytes.concat(keccak256(abi.encode(recipient, amount))));
return MerkleProof.verify(proof, root, leaf);
}
function _getLoveBalance() internal view returns (uint256) {
return _token.balanceOf(address(this));
}
}
文件 5 的 6:MerkleProof.sol
pragma solidity ^0.8.0;
library MerkleProof {
function verify(bytes32[] memory proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
return processProof(proof, leaf) == root;
}
function verifyCalldata(bytes32[] calldata proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
return processProofCalldata(proof, leaf) == root;
}
function processProof(bytes32[] memory proof, bytes32 leaf) internal pure returns (bytes32) {
bytes32 computedHash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
computedHash = _hashPair(computedHash, proof[i]);
}
return computedHash;
}
function processProofCalldata(bytes32[] calldata proof, bytes32 leaf) internal pure returns (bytes32) {
bytes32 computedHash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
computedHash = _hashPair(computedHash, proof[i]);
}
return computedHash;
}
function multiProofVerify(
bytes32[] memory proof,
bool[] memory proofFlags,
bytes32 root,
bytes32[] memory leaves
) internal pure returns (bool) {
return processMultiProof(proof, proofFlags, leaves) == root;
}
function multiProofVerifyCalldata(
bytes32[] calldata proof,
bool[] calldata proofFlags,
bytes32 root,
bytes32[] memory leaves
) internal pure returns (bool) {
return processMultiProofCalldata(proof, proofFlags, leaves) == root;
}
function processMultiProof(
bytes32[] memory proof,
bool[] memory proofFlags,
bytes32[] memory leaves
) internal pure returns (bytes32 merkleRoot) {
uint256 leavesLen = leaves.length;
uint256 proofLen = proof.length;
uint256 totalHashes = proofFlags.length;
require(leavesLen + proofLen - 1 == totalHashes, "MerkleProof: invalid multiproof");
bytes32[] memory hashes = new bytes32[](totalHashes);
uint256 leafPos = 0;
uint256 hashPos = 0;
uint256 proofPos = 0;
for (uint256 i = 0; i < totalHashes; i++) {
bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
bytes32 b = proofFlags[i]
? (leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++])
: proof[proofPos++];
hashes[i] = _hashPair(a, b);
}
if (totalHashes > 0) {
require(proofPos == proofLen, "MerkleProof: invalid multiproof");
unchecked {
return hashes[totalHashes - 1];
}
} else if (leavesLen > 0) {
return leaves[0];
} else {
return proof[0];
}
}
function processMultiProofCalldata(
bytes32[] calldata proof,
bool[] calldata proofFlags,
bytes32[] memory leaves
) internal pure returns (bytes32 merkleRoot) {
uint256 leavesLen = leaves.length;
uint256 proofLen = proof.length;
uint256 totalHashes = proofFlags.length;
require(leavesLen + proofLen - 1 == totalHashes, "MerkleProof: invalid multiproof");
bytes32[] memory hashes = new bytes32[](totalHashes);
uint256 leafPos = 0;
uint256 hashPos = 0;
uint256 proofPos = 0;
for (uint256 i = 0; i < totalHashes; i++) {
bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
bytes32 b = proofFlags[i]
? (leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++])
: proof[proofPos++];
hashes[i] = _hashPair(a, b);
}
if (totalHashes > 0) {
require(proofPos == proofLen, "MerkleProof: invalid multiproof");
unchecked {
return hashes[totalHashes - 1];
}
} else if (leavesLen > 0) {
return leaves[0];
} else {
return proof[0];
}
}
function _hashPair(bytes32 a, bytes32 b) private pure returns (bytes32) {
return a < b ? _efficientHash(a, b) : _efficientHash(b, a);
}
function _efficientHash(bytes32 a, bytes32 b) private pure returns (bytes32 value) {
assembly {
mstore(0x00, a)
mstore(0x20, b)
value := keccak256(0x00, 0x40)
}
}
}
文件 6 的 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);
}
}
{
"compilationTarget": {
"contracts/LoveAirdrop.sol": "LoveAirdrop"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"tokenAddress","type":"address"},{"internalType":"address","name":"marketplace","type":"address"},{"internalType":"address","name":"admin","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"string","name":"dropName","type":"string"},{"indexed":true,"internalType":"address","name":"recipient","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"LoveClaimed","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"string","name":"dropName","type":"string"}],"name":"LoveDropCancelled","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"string","name":"dropName","type":"string"},{"indexed":true,"internalType":"address","name":"creator","type":"address"},{"indexed":false,"internalType":"bytes32","name":"root","type":"bytes32"},{"indexed":false,"internalType":"uint256","name":"total","type":"uint256"}],"name":"LoveDropCreated","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":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"TokensWithdrawn","type":"event"},{"inputs":[{"internalType":"string","name":"dropName","type":"string"}],"name":"cancelLoveDrop","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"dropName","type":"string"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"bytes32[]","name":"proof","type":"bytes32[]"}],"name":"claim","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"dropName","type":"string"},{"internalType":"bytes32","name":"root","type":"bytes32"},{"internalType":"uint256","name":"total","type":"uint256"}],"name":"createLoveDrop","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"nameHash","type":"bytes32"}],"name":"drops","outputs":[{"internalType":"address","name":"creator","type":"address"},{"internalType":"bytes32","name":"root","type":"bytes32"},{"internalType":"uint256","name":"total","type":"uint256"},{"internalType":"uint256","name":"claimed","type":"uint256"},{"internalType":"bool","name":"cancelled","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"nameHash","type":"bytes32"},{"internalType":"address","name":"recipient","type":"address"}],"name":"isClaimed","outputs":[{"internalType":"bool","name":"isClaimed","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"reservedBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32[]","name":"proof","type":"bytes32[]"},{"internalType":"bytes32","name":"root","type":"bytes32"},{"internalType":"address","name":"recipient","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"verifyClaim","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"withdrawTokens","outputs":[],"stateMutability":"nonpayable","type":"function"}]