文件 1 的 5:IERC20.sol
pragma solidity >=0.6.0 <0.8.0;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, 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 sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
文件 2 的 5:IMerkleDistributor.sol
pragma solidity ^0.7.0;
interface IMerkleDistributor {
function token() external view returns (address);
function merkleRoot() external view returns (bytes32);
function isClaimed(uint256 index) external view returns (bool);
function claim(
uint256 index,
address account,
uint256 amount,
bytes32[] calldata merkleProof
) external;
event Claimed(uint256 index, address account, uint256 amount);
}
文件 3 的 5:MerkleDistributor.sol
pragma solidity ^0.7.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/cryptography/MerkleProof.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
import "./interfaces/IMerkleDistributor.sol";
contract MerkleDistributor is IMerkleDistributor {
using SafeMath for uint256;
address public immutable override token;
bytes32 public immutable override merkleRoot;
mapping(uint256 => uint256) private claimedBitMap;
uint256 public constant MAX_BONUS = 0.999e18;
uint256 public constant PERCENTAGE_BASE = 1e18;
uint256 public totalClaims;
uint256 public initialPoolSize;
uint256 public currentPoolSize;
uint256 public bonusSum;
uint256 public claimed;
uint256 public percentageIndex;
uint256 public bonusStart;
uint256 public bonusEnd;
uint256 public emergencyTimeout;
address public emergencyReceiver;
constructor(
address token_,
bytes32 merkleRoot_,
uint256 _totalClaims,
uint256 _initialPoolSize,
uint256 _bonusStart,
uint256 _bonusEnd,
uint256 _emergencyTimeout,
address _emergencyReceiver
) {
token = token_;
merkleRoot = merkleRoot_;
totalClaims = _totalClaims;
initialPoolSize = _initialPoolSize;
currentPoolSize = _initialPoolSize;
percentageIndex = PERCENTAGE_BASE;
bonusStart = _bonusStart;
bonusEnd = _bonusEnd;
emergencyTimeout = _emergencyTimeout;
emergencyReceiver = _emergencyReceiver;
require(bonusStart < bonusEnd, "WRONG_BONUS_TIME");
require(emergencyTimeout > bonusEnd, "WRONG_EMERGENCY_TIMEOUT");
}
function isClaimed(uint256 index) public 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 _setClaimed(uint256 index) private {
uint256 claimedWordIndex = index / 256;
uint256 claimedBitIndex = index % 256;
claimedBitMap[claimedWordIndex] =
claimedBitMap[claimedWordIndex] |
(1 << claimedBitIndex);
}
function claim(
uint256 index,
address account,
uint256 amount,
bytes32[] calldata merkleProof
) external override {
require(!isClaimed(index), "MerkleDistributor: Drop already claimed.");
require(msg.sender == account, "Only owner can claim");
bytes32 node = keccak256(abi.encodePacked(index, account, amount));
require(
MerkleProof.verify(merkleProof, merkleRoot, node),
"MerkleDistributor: Invalid proof."
);
_setClaimed(index);
uint256 adjustedAmount = _applyAdjustment(amount);
require(
IERC20(token).transfer(account, adjustedAmount),
"MerkleDistributor: Transfer failed."
);
emit Claimed(index, account, adjustedAmount);
}
function getBonus() public view returns (uint256) {
uint256 timeRemaining =
block.timestamp > bonusEnd ? 0 : bonusEnd.sub(block.timestamp);
return MAX_BONUS.mul(timeRemaining).div(bonusEnd.sub(bonusStart));
}
function calculateAdjustedAmount(uint256 amount)
public
view
returns (
uint256 adjustedAmount,
uint256 bonus,
uint256 bonusPart
)
{
if (claimed + 1 == totalClaims) {
return (amount.add(bonusSum), 0, 0);
}
uint256 adjustedPercentage =
amount.mul(PERCENTAGE_BASE).div(initialPoolSize).mul(percentageIndex).div(
PERCENTAGE_BASE
);
bonusPart = adjustedPercentage.mul(bonusSum).div(PERCENTAGE_BASE);
uint256 totalToClaim = amount.add(bonusPart);
bonus = totalToClaim.mul(getBonus()).div(PERCENTAGE_BASE);
adjustedAmount = totalToClaim.sub(bonus);
}
function _applyAdjustment(uint256 amount) private returns (uint256) {
(uint256 adjustedAmount, uint256 bonus, uint256 bonusPart) =
calculateAdjustedAmount(amount);
claimed += 1;
if (claimed == totalClaims) {
return adjustedAmount;
}
uint256 newPoolSize = currentPoolSize.sub(amount);
percentageIndex = percentageIndex
.mul(currentPoolSize.mul(PERCENTAGE_BASE).div(newPoolSize))
.div(PERCENTAGE_BASE);
currentPoolSize = newPoolSize;
bonusSum = bonusSum.sub(bonusPart).add(bonus);
return adjustedAmount;
}
function emergencyWithdrawal() public {
require(block.timestamp > emergencyTimeout, "TIMEOUT_NOT_EXPIRED");
IERC20(token).transfer(
emergencyReceiver,
IERC20(token).balanceOf(address(this))
);
}
}
文件 4 的 5:MerkleProof.sol
pragma solidity >=0.6.0 <0.8.0;
library MerkleProof {
function verify(bytes32[] memory proof, bytes32 root, bytes32 leaf) internal pure returns (bool) {
bytes32 computedHash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
bytes32 proofElement = proof[i];
if (computedHash <= proofElement) {
computedHash = keccak256(abi.encodePacked(computedHash, proofElement));
} else {
computedHash = keccak256(abi.encodePacked(proofElement, computedHash));
}
}
return computedHash == root;
}
}
文件 5 的 5:SafeMath.sol
pragma solidity >=0.6.0 <0.8.0;
library SafeMath {
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b > a) return (false, 0);
return (true, a - b);
}
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (a == 0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b == 0) return (false, 0);
return (true, a / b);
}
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
if (b == 0) return (false, 0);
return (true, a % b);
}
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
return a - b;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) return 0;
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: division by zero");
return a / b;
}
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: modulo by zero");
return a % b;
}
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
return a - b;
}
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
return a / b;
}
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
return a % b;
}
}
{
"compilationTarget": {
"contracts/MerkleDistributor.sol": "MerkleDistributor"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"token_","type":"address"},{"internalType":"bytes32","name":"merkleRoot_","type":"bytes32"},{"internalType":"uint256","name":"_totalClaims","type":"uint256"},{"internalType":"uint256","name":"_initialPoolSize","type":"uint256"},{"internalType":"uint256","name":"_bonusStart","type":"uint256"},{"internalType":"uint256","name":"_bonusEnd","type":"uint256"},{"internalType":"uint256","name":"_emergencyTimeout","type":"uint256"},{"internalType":"address","name":"_emergencyReceiver","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"index","type":"uint256"},{"indexed":false,"internalType":"address","name":"account","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Claimed","type":"event"},{"inputs":[],"name":"MAX_BONUS","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"PERCENTAGE_BASE","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"bonusEnd","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"bonusStart","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"bonusSum","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"calculateAdjustedAmount","outputs":[{"internalType":"uint256","name":"adjustedAmount","type":"uint256"},{"internalType":"uint256","name":"bonus","type":"uint256"},{"internalType":"uint256","name":"bonusPart","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"index","type":"uint256"},{"internalType":"address","name":"account","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"bytes32[]","name":"merkleProof","type":"bytes32[]"}],"name":"claim","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"claimed","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"currentPoolSize","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"emergencyReceiver","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"emergencyTimeout","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"emergencyWithdrawal","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"getBonus","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"initialPoolSize","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":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"percentageIndex","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"token","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"totalClaims","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]