编译器
0.8.15+commit.e14f2714
文件 1 的 13:Address.sol
pragma solidity ^0.8.1;
library Address {
function isContract(address account) internal view returns (bool) {
return account.code.length > 0;
}
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{value: amount}("");
require(success, "Address: unable to send value, recipient may have reverted");
}
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCall(target, data, "Address: low-level call failed");
}
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
function functionCallWithValue(
address target,
bytes memory data,
uint256 value
) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
function functionCallWithValue(
address target,
bytes memory data,
uint256 value,
string memory errorMessage
) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
require(isContract(target), "Address: call to non-contract");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResult(success, returndata, errorMessage);
}
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
function functionStaticCall(
address target,
bytes memory data,
string memory errorMessage
) internal view returns (bytes memory) {
require(isContract(target), "Address: static call to non-contract");
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResult(success, returndata, errorMessage);
}
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
return functionDelegateCall(target, data, "Address: low-level delegate call failed");
}
function functionDelegateCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
require(isContract(target), "Address: delegate call to non-contract");
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResult(success, returndata, errorMessage);
}
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
文件 2 的 13: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;
}
}
文件 3 的 13:ECDSA.sol
pragma solidity ^0.8.0;
pragma abicoder v1;
import "@openzeppelin/contracts/interfaces/IERC1271.sol";
library ECDSA {
function recover(bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal view returns(address signer) {
assembly {
let ptr := mload(0x40)
mstore(ptr, hash)
mstore(add(ptr, 0x20), v)
mstore(add(ptr, 0x40), r)
mstore(add(ptr, 0x60), s)
if staticcall(gas(), 0x1, ptr, 0x80, 0, 0x20) {
signer := mload(0)
}
}
}
function recover(bytes32 hash, bytes32 r, bytes32 vs) internal view returns(address signer) {
assembly {
let ptr := mload(0x40)
mstore(ptr, hash)
mstore(add(ptr, 0x20), add(27, shr(255, vs)))
mstore(add(ptr, 0x40), r)
mstore(add(ptr, 0x60), shr(1, shl(1, vs)))
if staticcall(gas(), 0x1, ptr, 0x80, 0, 0x20) {
signer := mload(0)
}
}
}
function recover(bytes32 hash, bytes calldata signature) internal view returns(address signer) {
assembly {
let ptr := mload(0x40)
switch signature.length
case 65 {
mstore(add(ptr, 0x20), byte(0, calldataload(add(signature.offset, 0x40))))
calldatacopy(add(ptr, 0x40), signature.offset, 0x40)
}
case 64 {
let vs := calldataload(add(signature.offset, 0x20))
mstore(add(ptr, 0x20), add(27, shr(255, vs)))
calldatacopy(add(ptr, 0x40), signature.offset, 0x20)
mstore(add(ptr, 0x60), shr(1, shl(1, vs)))
}
default {
ptr := 0
}
if ptr {
if gt(mload(add(ptr, 0x60)), 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
ptr := 0
}
if ptr {
mstore(ptr, hash)
if staticcall(gas(), 0x1, ptr, 0x80, 0, 0x20) {
signer := mload(0)
}
}
}
}
}
function recoverOrIsValidSignature(address signer, bytes32 hash, bytes calldata signature) internal view returns(bool success) {
if ((signature.length == 64 || signature.length == 65) && recover(hash, signature) == signer) {
return true;
}
return isValidSignature(signer, hash, signature);
}
function recoverOrIsValidSignature(address signer, bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal view returns(bool success) {
if (recover(hash, v, r, s) == signer) {
return true;
}
return isValidSignature(signer, hash, v, r, s);
}
function recoverOrIsValidSignature(address signer, bytes32 hash, bytes32 r, bytes32 vs) internal view returns(bool success) {
if (recover(hash, r, vs) == signer) {
return true;
}
return isValidSignature(signer, hash, r, vs);
}
function recoverOrIsValidSignature65(address signer, bytes32 hash, bytes32 r, bytes32 vs) internal view returns(bool success) {
if (recover(hash, r, vs) == signer) {
return true;
}
return isValidSignature65(signer, hash, r, vs);
}
function isValidSignature(address signer, bytes32 hash, bytes calldata signature) internal view returns(bool success) {
bytes4 selector = IERC1271.isValidSignature.selector;
assembly {
let ptr := mload(0x40)
let len := add(0x64, signature.length)
mstore(ptr, selector)
mstore(add(ptr, 0x04), hash)
mstore(add(ptr, 0x24), 0x40)
mstore(add(ptr, 0x44), signature.length)
calldatacopy(add(ptr, 0x64), signature.offset, signature.length)
mstore(0, 0)
if staticcall(gas(), signer, ptr, len, 0, 0x20) {
success := eq(selector, mload(0))
}
}
}
function isValidSignature(address signer, bytes32 hash, uint8 v, bytes32 r, bytes32 s) internal view returns(bool success) {
bytes4 selector = IERC1271.isValidSignature.selector;
assembly {
let ptr := mload(0x40)
let len := add(0x64, 65)
mstore(ptr, selector)
mstore(add(ptr, 0x04), hash)
mstore(add(ptr, 0x24), 0x40)
mstore(add(ptr, 0x44), 65)
mstore(add(ptr, 0x64), r)
mstore(add(ptr, 0x84), s)
mstore8(add(ptr, 0xa4), v)
mstore(0, 0)
if staticcall(gas(), signer, ptr, len, 0, 0x20) {
success := eq(selector, mload(0))
}
}
}
function isValidSignature(address signer, bytes32 hash, bytes32 r, bytes32 vs) internal view returns(bool success) {
bytes4 selector = IERC1271.isValidSignature.selector;
assembly {
let ptr := mload(0x40)
let len := add(0x64, 64)
mstore(ptr, selector)
mstore(add(ptr, 0x04), hash)
mstore(add(ptr, 0x24), 0x40)
mstore(add(ptr, 0x44), 64)
mstore(add(ptr, 0x64), r)
mstore(add(ptr, 0x84), vs)
mstore(0, 0)
if staticcall(gas(), signer, ptr, len, 0, 0x20) {
success := eq(selector, mload(0))
}
}
}
function isValidSignature65(address signer, bytes32 hash, bytes32 r, bytes32 vs) internal view returns(bool success) {
bytes4 selector = IERC1271.isValidSignature.selector;
assembly {
let ptr := mload(0x40)
let len := add(0x64, 65)
mstore(ptr, selector)
mstore(add(ptr, 0x04), hash)
mstore(add(ptr, 0x24), 0x40)
mstore(add(ptr, 0x44), 65)
mstore(add(ptr, 0x64), r)
mstore(add(ptr, 0x84), shr(1, shl(1, vs)))
mstore8(add(ptr, 0xa4), add(27, shr(255, vs)))
mstore(0, 0)
if staticcall(gas(), signer, ptr, len, 0, 0x20) {
success := eq(selector, mload(0))
}
}
}
function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32 res) {
assembly {
mstore(0, 0x19457468657265756d205369676e6564204d6573736167653a0a333200000000)
mstore(28, hash)
res := keccak256(0, 60)
}
}
function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32 res) {
assembly {
let ptr := mload(0x40)
mstore(ptr, 0x1901000000000000000000000000000000000000000000000000000000000000)
mstore(add(ptr, 0x02), domainSeparator)
mstore(add(ptr, 0x22), structHash)
res := keccak256(ptr, 66)
}
}
}
文件 4 的 13:IDaiLikePermit.sol
pragma solidity ^0.8.0;
pragma abicoder v1;
interface IDaiLikePermit {
function permit(address holder, address spender, uint256 nonce, uint256 expiry, bool allowed, uint8 v, bytes32 r, bytes32 s) external;
}
文件 5 的 13:IERC1271.sol
pragma solidity ^0.8.0;
interface IERC1271 {
function isValidSignature(bytes32 hash, bytes memory signature) external view returns (bytes4 magicValue);
}
文件 6 的 13: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);
}
文件 7 的 13:ISignatureMerkleDrop128.sol
pragma solidity ^0.8.0;
pragma abicoder v1;
interface ISignatureMerkleDrop128 {
function token() external view returns (address);
function merkleRoot() external view returns (bytes16);
function depth() external view returns (uint256);
function claim(address receiver, uint256 amount, bytes calldata merkleProof, bytes calldata signature) external;
function verify(bytes calldata proof, bytes16 root, bytes16 leaf) external view returns (bool valid, uint256 index);
function isClaimed(uint256 index) external view returns (bool);
}
文件 8 的 13:Math.sol
pragma solidity ^0.8.0;
library Math {
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a >= b ? a : b;
}
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
function average(uint256 a, uint256 b) internal pure returns (uint256) {
return (a & b) + (a ^ b) / 2;
}
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
return a / b + (a % b == 0 ? 0 : 1);
}
}
文件 9 的 13: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);
}
}
文件 10 的 13:RevertReasonForwarder.sol
pragma solidity ^0.8.0;
pragma abicoder v1;
library RevertReasonForwarder {
function reRevert() internal pure {
assembly {
let ptr := mload(0x40)
returndatacopy(ptr, 0, returndatasize())
revert(ptr, returndatasize())
}
}
}
文件 11 的 13:SafeERC20.sol
pragma solidity ^0.8.0;
pragma abicoder v1;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/extensions/draft-IERC20Permit.sol";
import "../interfaces/IDaiLikePermit.sol";
import "../libraries/RevertReasonForwarder.sol";
library SafeERC20 {
error SafeTransferFailed();
error SafeTransferFromFailed();
error ForceApproveFailed();
error SafeIncreaseAllowanceFailed();
error SafeDecreaseAllowanceFailed();
error SafePermitBadLength();
function safeTransferFrom(IERC20 token, address from, address to, uint256 amount) internal {
bytes4 selector = token.transferFrom.selector;
bool success;
assembly {
let data := mload(0x40)
mstore(data, selector)
mstore(add(data, 0x04), from)
mstore(add(data, 0x24), to)
mstore(add(data, 0x44), amount)
let status := call(gas(), token, 0, data, 100, 0x0, 0x20)
success := and(status, or(iszero(returndatasize()), and(gt(returndatasize(), 31), eq(mload(0), 1))))
}
if (!success) {
revert SafeTransferFromFailed();
}
}
function safeTransfer(IERC20 token, address to, uint256 value) internal {
if (!_makeCall(token, token.transfer.selector, to, value)) {
revert SafeTransferFailed();
}
}
function forceApprove(IERC20 token, address spender, uint256 value) internal {
if (!_makeCall(token, token.approve.selector, spender, value)) {
if (!_makeCall(token, token.approve.selector, spender, 0) ||
!_makeCall(token, token.approve.selector, spender, value))
{
revert ForceApproveFailed();
}
}
}
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 allowance = token.allowance(address(this), spender);
if (value > type(uint256).max - allowance) revert SafeIncreaseAllowanceFailed();
forceApprove(token, spender, allowance + value);
}
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 allowance = token.allowance(address(this), spender);
if (value > allowance) revert SafeDecreaseAllowanceFailed();
forceApprove(token, spender, allowance - value);
}
function safePermit(IERC20 token, bytes calldata permit) internal {
bool success;
if (permit.length == 32 * 7) {
success = _makeCalldataCall(token, IERC20Permit.permit.selector, permit);
} else if (permit.length == 32 * 8) {
success = _makeCalldataCall(token, IDaiLikePermit.permit.selector, permit);
} else {
revert SafePermitBadLength();
}
if (!success) {
RevertReasonForwarder.reRevert();
}
}
function _makeCall(IERC20 token, bytes4 selector, address to, uint256 amount) private returns(bool done) {
assembly {
let data := mload(0x40)
mstore(data, selector)
mstore(add(data, 0x04), to)
mstore(add(data, 0x24), amount)
let success := call(gas(), token, 0, data, 68, 0x0, 0x20)
done := and(
success,
or(
iszero(returndatasize()),
and(gt(returndatasize(), 31), eq(mload(0), 1))
)
)
}
}
function _makeCalldataCall(IERC20 token, bytes4 selector, bytes calldata args) private returns(bool done) {
assembly {
let len := add(4, args.length)
let data := mload(0x40)
mstore(data, selector)
calldatacopy(add(data, 0x04), args.offset, args.length)
let success := call(gas(), token, 0, data, len, 0x0, 0x20)
done := and(
success,
or(
iszero(returndatasize()),
and(gt(returndatasize(), 31), eq(mload(0), 1))
)
)
}
}
}
文件 12 的 13:SignatureMerkleDrop128.sol
pragma solidity 0.8.15;
pragma abicoder v1;
import "@openzeppelin/contracts/utils/math/Math.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@1inch/solidity-utils/contracts/libraries/SafeERC20.sol";
import "@1inch/solidity-utils/contracts/libraries/ECDSA.sol";
import "./interfaces/ISignatureMerkleDrop128.sol";
contract SignatureMerkleDrop128 is ISignatureMerkleDrop128, Ownable {
using Address for address payable;
using SafeERC20 for IERC20;
address public immutable override token;
bytes16 public immutable override merkleRoot;
uint256 public immutable override depth;
mapping(uint256 => uint256) private _claimedBitMap;
uint256 private constant _CLAIM_GAS_COST = 60000;
receive() external payable {}
constructor(address token_, bytes16 merkleRoot_, uint256 depth_) {
token = token_;
merkleRoot = merkleRoot_;
depth = depth_;
}
function claim(address receiver, uint256 amount, bytes calldata merkleProof, bytes calldata signature) external override {
bytes32 signedHash = ECDSA.toEthSignedMessageHash(keccak256(abi.encodePacked(receiver)));
address account = ECDSA.recover(signedHash, signature);
bytes16 node = bytes16(keccak256(abi.encodePacked(account, amount)));
(bool valid, uint256 index) = _verifyAsm(merkleProof, merkleRoot, node);
require(valid, "MD: Invalid proof");
_invalidate(index);
IERC20(token).safeTransfer(receiver, amount);
_cashback();
}
function verify(bytes calldata proof, bytes16 root, bytes16 leaf) external view returns (bool valid, uint256 index) {
return _verifyAsm(proof, root, leaf);
}
function verify(bytes calldata proof, bytes16 leaf) external view returns (bool valid, uint256 index) {
return _verifyAsm(proof, merkleRoot, leaf);
}
function isClaimed(uint256 index) external view override returns (bool) {
uint256 claimedWordIndex = index / 256;
uint256 claimedBitIndex = index % 256;
uint256 claimedWord = _claimedBitMap[claimedWordIndex];
uint256 mask = (1 << claimedBitIndex);
return claimedWord & mask == mask;
}
function _cashback() private {
uint256 balance = address(this).balance;
if (balance > 0) {
payable(tx.origin).sendValue(Math.min(block.basefee * _CLAIM_GAS_COST, balance));
}
}
function _invalidate(uint256 index) private {
uint256 claimedWordIndex = index >> 8;
uint256 claimedBitIndex = index & 0xff;
uint256 claimedWord = _claimedBitMap[claimedWordIndex];
uint256 newClaimedWord = claimedWord | (1 << claimedBitIndex);
require(claimedWord != newClaimedWord, "MD: Drop already claimed");
_claimedBitMap[claimedWordIndex] = newClaimedWord;
}
function _verifyAsm(bytes calldata proof, bytes16 root, bytes16 leaf) private view returns (bool valid, uint256 index) {
assembly {
let ptr := proof.offset
let mask := 1
for { let end := add(ptr, proof.length) } lt(ptr, end) { ptr := add(ptr, 0x10) } {
let node := calldataload(ptr)
switch lt(leaf, node)
case 1 {
mstore(0x00, leaf)
mstore(0x10, node)
}
default {
mstore(0x00, node)
mstore(0x10, leaf)
index := or(mask, index)
}
leaf := keccak256(0x00, 0x20)
mask := shl(1, mask)
}
valid := iszero(shr(128, xor(root, leaf)))
}
unchecked {
index <<= depth - proof.length / 16;
}
}
function rescueFunds(address token_, uint256 amount) external onlyOwner {
if (token_ == address(0)) {
payable(msg.sender).sendValue(amount);
} else {
IERC20(token_).safeTransfer(msg.sender, amount);
}
}
}
文件 13 的 13:draft-IERC20Permit.sol
pragma solidity ^0.8.0;
interface IERC20Permit {
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
function nonces(address owner) external view returns (uint256);
function DOMAIN_SEPARATOR() external view returns (bytes32);
}
{
"compilationTarget": {
"contracts/SignatureMerkleDrop128.sol": "SignatureMerkleDrop128"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": true,
"runs": 1000000
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"token_","type":"address"},{"internalType":"bytes16","name":"merkleRoot_","type":"bytes16"},{"internalType":"uint256","name":"depth_","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[],"name":"SafeTransferFailed","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"},{"inputs":[{"internalType":"address","name":"receiver","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"bytes","name":"merkleProof","type":"bytes"},{"internalType":"bytes","name":"signature","type":"bytes"}],"name":"claim","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"depth","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"index","type":"uint256"}],"name":"isClaimed","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"merkleRoot","outputs":[{"internalType":"bytes16","name":"","type":"bytes16"}],"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":[{"internalType":"address","name":"token_","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"rescueFunds","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"token","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes","name":"proof","type":"bytes"},{"internalType":"bytes16","name":"root","type":"bytes16"},{"internalType":"bytes16","name":"leaf","type":"bytes16"}],"name":"verify","outputs":[{"internalType":"bool","name":"valid","type":"bool"},{"internalType":"uint256","name":"index","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes","name":"proof","type":"bytes"},{"internalType":"bytes16","name":"leaf","type":"bytes16"}],"name":"verify","outputs":[{"internalType":"bool","name":"valid","type":"bool"},{"internalType":"uint256","name":"index","type":"uint256"}],"stateMutability":"view","type":"function"},{"stateMutability":"payable","type":"receive"}]