文件 1 的 9:Address.sol
pragma solidity ^0.8.0;
library Address {
function isContract(address account) internal view returns (bool) {
uint256 size;
assembly {
size := extcodesize(account)
}
return size > 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 的 9: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 的 9:IERC20.sol
pragma solidity ^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);
}
文件 4 的 9:IERC677Receiver.sol
pragma solidity ^0.8.0;
interface IERC677Receiver {
function onTokenTransfer(address _sender, uint _value, bytes calldata _data) external;
}
文件 5 的 9:IVoters.sol
pragma solidity ^0.8.0;
interface IVoters {
function snapshot() external returns (uint);
function totalSupplyAt(uint snapshotId) external view returns (uint);
function votesAt(address account, uint snapshotId) external view returns (uint);
function balanceOf(address account) external view returns (uint);
function balanceOfAt(address account, uint snapshotId) external view returns (uint);
function donate(uint amount) external;
}
文件 6 的 9: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() {
_setOwner(_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 {
_setOwner(address(0));
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_setOwner(newOwner);
}
function _setOwner(address newOwner) private {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
文件 7 的 9:ReentrancyGuard.sol
pragma solidity ^0.8.0;
abstract contract ReentrancyGuard {
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
uint256 private _status;
constructor() {
_status = _NOT_ENTERED;
}
modifier nonReentrant() {
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
_status = _ENTERED;
_;
_status = _NOT_ENTERED;
}
}
文件 8 的 9:SafeERC20.sol
pragma solidity ^0.8.0;
import "../IERC20.sol";
import "../../../utils/Address.sol";
library SafeERC20 {
using Address for address;
function safeTransfer(
IERC20 token,
address to,
uint256 value
) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(
IERC20 token,
address from,
address to,
uint256 value
) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
function safeApprove(
IERC20 token,
address spender,
uint256 value
) internal {
require(
(value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function safeIncreaseAllowance(
IERC20 token,
address spender,
uint256 value
) internal {
uint256 newAllowance = token.allowance(address(this), spender) + value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(
IERC20 token,
address spender,
uint256 value
) internal {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
uint256 newAllowance = oldAllowance - value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
}
function _callOptionalReturn(IERC20 token, bytes memory data) private {
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
if (returndata.length > 0) {
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
文件 9 的 9:SaleBatch.sol
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "./interfaces/IVoters.sol";
import "./interfaces/IERC677Receiver.sol";
contract SaleBatch is IERC677Receiver, Ownable, ReentrancyGuard {
using SafeERC20 for IERC20;
struct UserInfo {
uint amount;
bool claimedRefund;
bool claimedTokens;
}
IERC20 public paymentToken;
IERC20 public offeringToken;
uint public startTime;
uint public endTime;
uint public raisingAmount;
uint public offeringAmount;
uint public perUserCap;
uint public votingMinimum;
IVoters public votingToken;
uint public votingSnapshotId;
bool public paused;
bool public finalized;
uint public totalAmount;
mapping(address => UserInfo) public userInfo;
address[] public addressList;
event Deposit(address indexed user, uint amount);
event HarvestRefund(address indexed user, uint amount);
event HarvestTokens(address indexed user, uint amount);
constructor(
address _paymentToken,
address _offeringToken,
uint _startTime,
uint _endTime,
uint _offeringAmount,
uint _raisingAmount,
uint _perUserCap
) Ownable() {
paymentToken = IERC20(_paymentToken);
offeringToken = IERC20(_offeringToken);
startTime = _startTime;
endTime = _endTime;
offeringAmount = _offeringAmount;
raisingAmount = _raisingAmount;
perUserCap = _perUserCap;
require(_paymentToken != address(0) && _offeringToken != address(0), "!zero");
require(_paymentToken != _offeringToken, "payment != offering");
require(_offeringAmount > 0, "offering > 0");
require(_raisingAmount > 0, "raising > 0");
require(_startTime > block.timestamp, "start > now");
require(_startTime < _endTime, "start < end");
require(_startTime < 10000000000, "start time not unix");
require(_endTime < 10000000000, "start time not unix");
}
function configureVotingToken(
uint minimum,
address token,
uint snapshotId
) public onlyOwner {
votingMinimum = minimum;
votingToken = IVoters(token);
votingSnapshotId = snapshotId;
}
function setRaisingAmount(uint amount) public onlyOwner {
require(block.timestamp < startTime && totalAmount == 0, "sale started");
raisingAmount = amount;
}
function setPerUserCap(uint cap) public onlyOwner {
require(block.timestamp < startTime && totalAmount == 0, "sale started");
perUserCap = cap;
}
function togglePaused() public onlyOwner {
paused = !paused;
}
function finalize() public {
require(msg.sender == owner() || block.timestamp > endTime + 14 days, "not allowed");
finalized = true;
}
function getAddressListLength() external view returns (uint) {
return addressList.length;
}
function getParams() external view returns (uint, uint, uint, uint, uint, uint, bool, bool) {
return (startTime, endTime, raisingAmount, offeringAmount, perUserCap, totalAmount, paused, finalized);
}
function getVotingParams() external view returns (uint, address, uint) {
return (votingMinimum, address(votingToken), votingSnapshotId);
}
function _deposit(address user, uint amount) private nonReentrant {
require(!paused, "paused");
require(block.timestamp >= startTime && block.timestamp <= endTime, "sale not active");
require(amount > 0, "need amount > 0");
require(perUserCap == 0 || userInfo[user].amount + amount <= perUserCap, "over per user cap");
if (votingMinimum > 0) {
if (votingSnapshotId == 0) {
require(votingToken.balanceOf(user) >= votingMinimum, "under minimum locked");
} else {
require(votingToken.balanceOfAt(user, votingSnapshotId) >= votingMinimum, "under minimum locked");
}
}
if (userInfo[user].amount == 0) {
addressList.push(address(user));
}
userInfo[user].amount = userInfo[user].amount + amount;
totalAmount = totalAmount + amount;
emit Deposit(user, amount);
}
function deposit(uint amount) public {
_transferFrom(msg.sender, amount);
_deposit(msg.sender, amount);
}
function onTokenTransfer(address user, uint amount, bytes calldata _data) public override {
require(msg.sender == address(paymentToken), "onTokenTransfer: not paymentToken");
_deposit(user, amount);
}
function harvestRefund() public nonReentrant {
require(!paused, "paused");
require(block.timestamp > endTime, "sale not ended");
require(userInfo[msg.sender].amount > 0, "have you participated?");
require(!userInfo[msg.sender].claimedRefund, "nothing to harvest");
userInfo[msg.sender].claimedRefund = true;
uint amount = getRefundingAmount(msg.sender);
if (amount > 0) {
paymentToken.safeTransfer(address(msg.sender), amount);
}
emit HarvestRefund(msg.sender, amount);
}
function harvestTokens() public nonReentrant {
require(!paused, "paused");
require(block.timestamp > endTime, "sale not ended");
require(finalized, "not finalized");
require(userInfo[msg.sender].amount > 0, "have you participated?");
require(!userInfo[msg.sender].claimedTokens, "nothing to harvest");
userInfo[msg.sender].claimedTokens = true;
uint amount = getOfferingAmount(msg.sender);
if (amount > 0) {
offeringToken.safeTransfer(address(msg.sender), amount);
}
emit HarvestTokens(msg.sender, amount);
}
function harvestAll() public {
harvestRefund();
harvestTokens();
}
function getUserAllocation(address _user) public view returns (uint) {
return (userInfo[_user].amount * 1e12) / totalAmount / 1e6;
}
function getOfferingAmount(address _user) public view returns (uint) {
if (totalAmount > raisingAmount) {
uint allocation = getUserAllocation(_user);
return (offeringAmount * allocation) / 1e6;
} else {
return (userInfo[_user].amount * offeringAmount) / raisingAmount;
}
}
function getRefundingAmount(address _user) public view returns (uint) {
if (totalAmount <= raisingAmount) {
return 0;
}
uint allocation = getUserAllocation(_user);
uint payAmount = (raisingAmount * allocation) / 1e6;
return userInfo[_user].amount - payAmount;
}
function withdrawToken(address token, uint amount) public onlyOwner {
IERC20(token).safeTransfer(msg.sender, amount);
}
function _transferFrom(address from, uint amount) private {
uint balanceBefore = paymentToken.balanceOf(address(this));
paymentToken.safeTransferFrom(from, address(this), amount);
uint balanceAfter = paymentToken.balanceOf(address(this));
require(balanceAfter - balanceBefore == amount, "_transferFrom: balance change does not match amount");
}
}
{
"compilationTarget": {
"contracts/SaleBatch.sol": "SaleBatch"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"address","name":"_paymentToken","type":"address"},{"internalType":"address","name":"_offeringToken","type":"address"},{"internalType":"uint256","name":"_startTime","type":"uint256"},{"internalType":"uint256","name":"_endTime","type":"uint256"},{"internalType":"uint256","name":"_offeringAmount","type":"uint256"},{"internalType":"uint256","name":"_raisingAmount","type":"uint256"},{"internalType":"uint256","name":"_perUserCap","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"HarvestRefund","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"HarvestTokens","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"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"addressList","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"minimum","type":"uint256"},{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"snapshotId","type":"uint256"}],"name":"configureVotingToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"deposit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"endTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"finalize","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"finalized","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getAddressListLength","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_user","type":"address"}],"name":"getOfferingAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getParams","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"bool","name":"","type":"bool"},{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_user","type":"address"}],"name":"getRefundingAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_user","type":"address"}],"name":"getUserAllocation","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getVotingParams","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"harvestAll","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"harvestRefund","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"harvestTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"offeringAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"offeringToken","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"user","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"bytes","name":"_data","type":"bytes"}],"name":"onTokenTransfer","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"paused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"paymentToken","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"perUserCap","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"raisingAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"cap","type":"uint256"}],"name":"setPerUserCap","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"setRaisingAmount","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"startTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"togglePaused","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"totalAmount","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":"address","name":"","type":"address"}],"name":"userInfo","outputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"bool","name":"claimedRefund","type":"bool"},{"internalType":"bool","name":"claimedTokens","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"votingMinimum","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"votingSnapshotId","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"votingToken","outputs":[{"internalType":"contract IVoters","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"withdrawToken","outputs":[],"stateMutability":"nonpayable","type":"function"}]