编译器
0.8.18+commit.87f61d96
文件 1 的 13:AccessControl.sol
pragma solidity ^0.8.0;
import "./IAccessControl.sol";
import "../utils/Context.sol";
import "../utils/Strings.sol";
import "../utils/introspection/ERC165.sol";
abstract contract AccessControl is Context, IAccessControl, ERC165 {
struct RoleData {
mapping(address => bool) members;
bytes32 adminRole;
}
mapping(bytes32 => RoleData) private _roles;
bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
modifier onlyRole(bytes32 role) {
_checkRole(role);
_;
}
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return interfaceId == type(IAccessControl).interfaceId || super.supportsInterface(interfaceId);
}
function hasRole(bytes32 role, address account) public view virtual override returns (bool) {
return _roles[role].members[account];
}
function _checkRole(bytes32 role) internal view virtual {
_checkRole(role, _msgSender());
}
function _checkRole(bytes32 role, address account) internal view virtual {
if (!hasRole(role, account)) {
revert(
string(
abi.encodePacked(
"AccessControl: account ",
Strings.toHexString(account),
" is missing role ",
Strings.toHexString(uint256(role), 32)
)
)
);
}
}
function getRoleAdmin(bytes32 role) public view virtual override returns (bytes32) {
return _roles[role].adminRole;
}
function grantRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
_grantRole(role, account);
}
function revokeRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
_revokeRole(role, account);
}
function renounceRole(bytes32 role, address account) public virtual override {
require(account == _msgSender(), "AccessControl: can only renounce roles for self");
_revokeRole(role, account);
}
function _setupRole(bytes32 role, address account) internal virtual {
_grantRole(role, account);
}
function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
bytes32 previousAdminRole = getRoleAdmin(role);
_roles[role].adminRole = adminRole;
emit RoleAdminChanged(role, previousAdminRole, adminRole);
}
function _grantRole(bytes32 role, address account) internal virtual {
if (!hasRole(role, account)) {
_roles[role].members[account] = true;
emit RoleGranted(role, account, _msgSender());
}
}
function _revokeRole(bytes32 role, address account) internal virtual {
if (hasRole(role, account)) {
_roles[role].members[account] = false;
emit RoleRevoked(role, account, _msgSender());
}
}
}
文件 2 的 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 functionCallWithValue(target, data, 0, "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");
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, 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) {
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, 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) {
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResultFromTarget(target, success, returndata, errorMessage);
}
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata,
string memory errorMessage
) internal view returns (bytes memory) {
if (success) {
if (returndata.length == 0) {
require(isContract(target), "Address: call to non-contract");
}
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
_revert(returndata, errorMessage);
}
}
function _revert(bytes memory returndata, string memory errorMessage) private pure {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
文件 3 的 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;
}
}
文件 4 的 13:ERC165.sol
pragma solidity ^0.8.0;
import "./IERC165.sol";
abstract contract ERC165 is IERC165 {
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
return interfaceId == type(IERC165).interfaceId;
}
}
文件 5 的 13:FixedMode.sol
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/access/AccessControl.sol";
interface IWETH {
function deposit() external payable;
function transfer(address to, uint256 value) external returns (bool);
function withdraw(uint256) external;
}
contract FixedMode is AccessControl {
event Refund(address buyer, uint256 _donationId, uint256 refundNumber, address refundToken);
event Donate(address buyer, uint256 _donationId, uint256 donateNumber, address donateToken);
address constant TO_ADDRESS = 0xDB3e36f751471C0507F3EC3E7d28dabD10A3e311;
address public WETH;
using SafeERC20 for IERC20;
bytes32 public constant ADMIN_ROLE = keccak256("ADMIN_ROLE");
struct DonationToken {
address donationToken;
address paymentToken;
uint256 totalSupply;
uint256 minimumPurchase;
uint256 startTime;
uint256 deadline;
string remark;
uint256 extractTimes;
uint256 extractIntervalSeconds;
uint256[] extractRatios;
uint256 donatedAmount;
uint256 targetAmount;
}
struct ExtractInfo {
uint256 donationsNumber;
uint256 extractNumber;
uint256 extractTimes;
uint256 extractTime;
bool refundFlag;
}
mapping(uint256 => DonationToken) public donationTokens;
uint256 public tokenCount;
mapping(uint256 => mapping(address => ExtractInfo)) public extractInfos;
constructor() {
_setupRole(ADMIN_ROLE, msg.sender);
}
receive() external payable {}
function setWETH(address _WETH) external {
require(hasRole(ADMIN_ROLE, msg.sender), "Caller is not an admin");
WETH = _WETH;
}
function addToken(
address _donationToken,
address _paymentToken,
uint256 _totalSupply,
uint256 _minimumPurchase,
uint256 _startTime,
uint256 _deadline,
string memory _remark,
uint256 _extractTimes,
uint256 _extractIntervalSeconds,
uint256[] memory extractRatios,
uint256 _targetAmount
) external {
require(hasRole(ADMIN_ROLE, msg.sender), "Caller is not an admin");
require(_totalSupply > 0, "TotalSupply must be greater than zero");
require(block.timestamp < _deadline, "deadline error");
require(_paymentToken != address(0), "paymentToken address cannot be zero");
require(_targetAmount > 0, "targetAmount must be greater than zero");
uint256 length = extractRatios.length;
require(length > 0, "Please enter a ratio");
require(_extractTimes == length, "Please complete the extraction ratio");
tokenCount++;
donationTokens[tokenCount] = DonationToken({
donationToken: _donationToken,
paymentToken: _paymentToken,
totalSupply: _totalSupply,
minimumPurchase: _minimumPurchase,
startTime: _startTime,
deadline: _deadline,
remark: _remark,
extractTimes: _extractTimes,
extractIntervalSeconds: _extractIntervalSeconds,
extractRatios: extractRatios,
donatedAmount: 0,
targetAmount: _targetAmount
});
}
function exist(uint256 _donationId) public view {
require(_donationId > 0, "ID must be greater than 0");
require(_donationId <= tokenCount, "ID does not exist");
}
function getToken(uint256 _donationId) external view returns (DonationToken memory) {
exist(_donationId);
return donationTokens[_donationId];
}
function getTokenRange(uint256 startId, uint256 endId) external view returns (DonationToken[] memory) {
require(startId <= endId, "Invalid range");
require(endId <= tokenCount, "End ID exceeds token count");
uint256 count = endId - startId + 1;
DonationToken[] memory result = new DonationToken[](count);
for (uint256 i = 0; i < count; i++) {
result[i] = donationTokens[startId + i];
}
return result;
}
function setExtract(uint256 _donationId, uint256 _extractTimes, uint256 _extractIntervalSeconds, uint256[] memory extractRatios_) external {
require(hasRole(ADMIN_ROLE, msg.sender), "Caller is not an admin");
exist(_donationId);
uint256 length = extractRatios_.length;
require(length > 0, "Please enter a ratio");
require(_extractTimes == length, "Please complete the extraction ratio");
for (uint256 i = 0; i < extractRatios_.length; i++) {
uint256 currentRatio = extractRatios_[i];
require(currentRatio > 0 && currentRatio <= 100, "Scale must be greater than 0 and less than or equal to 100");
}
donationTokens[_donationId].extractTimes = _extractTimes;
donationTokens[_donationId].extractIntervalSeconds = _extractIntervalSeconds;
donationTokens[_donationId].extractRatios = extractRatios_;
}
function setTotalSupply(uint256 _donationId, uint256 _totalSupply) external {
require(hasRole(ADMIN_ROLE, msg.sender), "Caller is not an admin");
exist(_donationId);
require(_totalSupply > 0, "TotalSupply must be greater than zero");
donationTokens[_donationId].totalSupply = _totalSupply;
}
function setStartTime(uint256 _donationId, uint256 _startTime) external {
require(hasRole(ADMIN_ROLE, msg.sender), "Caller is not an admin");
exist(_donationId);
donationTokens[_donationId].startTime = _startTime;
}
function setRemark(uint256 _donationId, string memory _remark) external {
require(hasRole(ADMIN_ROLE, msg.sender), "Caller is not an admin");
exist(_donationId);
donationTokens[_donationId].remark = _remark;
}
function setDeadline(uint256 _donationId, uint256 _deadline) external {
require(hasRole(ADMIN_ROLE, msg.sender), "Caller is not an admin");
exist(_donationId);
donationTokens[_donationId].deadline = _deadline;
}
function setDonationToken(uint256 _donationId, address _donationToken) external {
require(hasRole(ADMIN_ROLE, msg.sender), "Caller is not an admin");
exist(_donationId);
require(_donationToken != address(0), "donationToken address cannot be zero");
donationTokens[_donationId].donationToken = _donationToken;
}
function setPaymentToken(uint256 _donationId, address _paymentToken) external {
require(hasRole(ADMIN_ROLE, msg.sender), "Caller is not an admin");
exist(_donationId);
require(_paymentToken != address(0), "paymentToken address cannot be zero");
donationTokens[_donationId].paymentToken = _paymentToken;
}
function setMinimumPurchase(uint256 _donationId, uint256 _minimumPurchase) external {
require(hasRole(ADMIN_ROLE, msg.sender), "Caller is not an admin");
exist(_donationId);
donationTokens[_donationId].minimumPurchase = _minimumPurchase;
}
function setTargetAmount(uint256 _donationId, uint256 _targetAmount) external {
require(hasRole(ADMIN_ROLE, msg.sender), "Caller is not an admin");
exist(_donationId);
donationTokens[_donationId].targetAmount = _targetAmount;
}
function donateCondition(DonationToken memory donationToken, uint256 _amount) private view {
require(donationToken.paymentToken != address(0), "paymentToken does not exist");
require(block.timestamp >= donationToken.startTime, "Donation has not started");
require(block.timestamp < donationToken.deadline, "Donation period has ended");
require(_amount > 0, "Amount must be greater than zero");
require(_amount >= donationToken.minimumPurchase, "Must not be less than the minimum purchase amount");
}
function donateAdd(uint256 _donationId, uint256 _amount) private {
DonationToken memory donationToken = donationTokens[_donationId];
extractInfos[_donationId][msg.sender].donationsNumber += _amount;
donationTokens[_donationId].donatedAmount += _amount;
emit Donate(msg.sender, _donationId, _amount, donationToken.donationToken);
}
function donateETH(uint256 _donationId) external payable {
DonationToken memory donationToken = donationTokens[_donationId];
donateCondition(donationToken, msg.value);
require(donationToken.paymentToken == WETH, "Only ETH");
donateAdd(_donationId, msg.value);
}
function donate(uint256 _donationId, uint256 _amount) public {
DonationToken memory donationToken = donationTokens[_donationId];
donateCondition(donationToken, _amount);
require(donationToken.paymentToken != WETH, "Cannot be ETH");
IERC20(donationToken.paymentToken).safeTransferFrom(msg.sender, address(this), _amount);
donateAdd(_donationId, _amount);
}
function withdraw(uint256 _donationId) external {
DonationToken memory donationTokenObj = donationTokens[_donationId];
require(donationTokenObj.donationToken != address(0), "donationToken does not exist");
require(block.timestamp >= donationTokenObj.deadline, "Donation period has not ended");
ExtractInfo memory extractInfo = extractInfos[_donationId][msg.sender];
require(extractInfo.extractTimes < donationTokenObj.extractTimes, "Fetch times exceeded");
uint256 availableTime = extractInfo.extractTime + donationTokenObj.extractIntervalSeconds;
require(block.timestamp >= availableTime, "Not yet pickup time");
uint256 rate = (extractInfo.donationsNumber * 100) / donationTokenObj.donatedAmount;
uint256 allNumber = (donationTokenObj.totalSupply * rate) / 100;
require(allNumber > 0, "Insufficient balance");
uint256 extractNumber = allNumber - extractInfo.extractNumber;
uint256 amountPer = (allNumber * donationTokenObj.extractRatios[extractInfo.extractTimes]) / 100;
require(extractNumber > 0, "Insufficient quantity available");
if (amountPer >= extractNumber) {
_payment(extractNumber, donationTokenObj.donationToken, _donationId);
} else {
_payment(amountPer, donationTokenObj.donationToken, _donationId);
}
uint256 refundAmount = donationTokenObj.donatedAmount - donationTokenObj.targetAmount;
if (refundAmount > 0 && !extractInfos[_donationId][msg.sender].refundFlag) {
uint256 addressRefundAmount = (refundAmount * rate) / 100;
if (addressRefundAmount > 0) {
extractInfos[_donationId][msg.sender].refundFlag = true;
if (donationTokenObj.paymentToken == WETH) {
payable(msg.sender).transfer(addressRefundAmount);
} else {
IERC20(donationTokenObj.paymentToken).safeTransfer(msg.sender, addressRefundAmount);
}
emit Refund(msg.sender, _donationId, addressRefundAmount, donationTokenObj.paymentToken);
}
}
}
function _payment(uint256 extractNumber, address _donationToken0, uint256 _donationId) private {
IERC20(_donationToken0).safeTransfer(msg.sender, extractNumber);
extractInfos[_donationId][msg.sender].extractNumber += extractNumber;
extractInfos[_donationId][msg.sender].extractTimes += 1;
extractInfos[_donationId][msg.sender].extractTime = block.timestamp;
}
function transferTokens(address _tokenAddress, uint256 _amount) external {
require(hasRole(ADMIN_ROLE, msg.sender), "Caller is not an admin");
require(_tokenAddress != address(0), "Token address cannot be zero");
require(_amount > 0, "Amount must be greater than zero");
IERC20(_tokenAddress).safeTransfer(TO_ADDRESS, _amount);
}
function transferETH(uint256 _amount) public {
require(hasRole(ADMIN_ROLE, msg.sender), "Caller is not an admin");
payable(TO_ADDRESS).transfer(_amount);
}
}
文件 6 的 13:IAccessControl.sol
pragma solidity ^0.8.0;
interface IAccessControl {
event RoleAdminChanged(bytes32 indexed role, bytes32 indexed previousAdminRole, bytes32 indexed newAdminRole);
event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender);
event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender);
function hasRole(bytes32 role, address account) external view returns (bool);
function getRoleAdmin(bytes32 role) external view returns (bytes32);
function grantRole(bytes32 role, address account) external;
function revokeRole(bytes32 role, address account) external;
function renounceRole(bytes32 role, address account) external;
}
文件 7 的 13:IERC165.sol
pragma solidity ^0.8.0;
interface IERC165 {
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
文件 8 的 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);
}
文件 9 的 13: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);
}
文件 10 的 13:Math.sol
pragma solidity ^0.8.0;
library Math {
enum Rounding {
Down,
Up,
Zero
}
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 == 0 ? 0 : (a - 1) / b + 1;
}
function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
uint256 prod0;
uint256 prod1;
assembly {
let mm := mulmod(x, y, not(0))
prod0 := mul(x, y)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
if (prod1 == 0) {
return prod0 / denominator;
}
require(denominator > prod1, "Math: mulDiv overflow");
uint256 remainder;
assembly {
remainder := mulmod(x, y, denominator)
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
uint256 twos = denominator & (~denominator + 1);
assembly {
denominator := div(denominator, twos)
prod0 := div(prod0, twos)
twos := add(div(sub(0, twos), twos), 1)
}
prod0 |= prod1 * twos;
uint256 inverse = (3 * denominator) ^ 2;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
inverse *= 2 - denominator * inverse;
result = prod0 * inverse;
return result;
}
}
function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
uint256 result = mulDiv(x, y, denominator);
if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
result += 1;
}
return result;
}
function sqrt(uint256 a) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 result = 1 << (log2(a) >> 1);
unchecked {
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
result = (result + a / result) >> 1;
return min(result, a / result);
}
}
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = sqrt(a);
return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
}
}
function log2(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 128;
}
if (value >> 64 > 0) {
value >>= 64;
result += 64;
}
if (value >> 32 > 0) {
value >>= 32;
result += 32;
}
if (value >> 16 > 0) {
value >>= 16;
result += 16;
}
if (value >> 8 > 0) {
value >>= 8;
result += 8;
}
if (value >> 4 > 0) {
value >>= 4;
result += 4;
}
if (value >> 2 > 0) {
value >>= 2;
result += 2;
}
if (value >> 1 > 0) {
result += 1;
}
}
return result;
}
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log2(value);
return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
}
}
function log10(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >= 10 ** 64) {
value /= 10 ** 64;
result += 64;
}
if (value >= 10 ** 32) {
value /= 10 ** 32;
result += 32;
}
if (value >= 10 ** 16) {
value /= 10 ** 16;
result += 16;
}
if (value >= 10 ** 8) {
value /= 10 ** 8;
result += 8;
}
if (value >= 10 ** 4) {
value /= 10 ** 4;
result += 4;
}
if (value >= 10 ** 2) {
value /= 10 ** 2;
result += 2;
}
if (value >= 10 ** 1) {
result += 1;
}
}
return result;
}
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log10(value);
return result + (rounding == Rounding.Up && 10 ** result < value ? 1 : 0);
}
}
function log256(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
unchecked {
if (value >> 128 > 0) {
value >>= 128;
result += 16;
}
if (value >> 64 > 0) {
value >>= 64;
result += 8;
}
if (value >> 32 > 0) {
value >>= 32;
result += 4;
}
if (value >> 16 > 0) {
value >>= 16;
result += 2;
}
if (value >> 8 > 0) {
result += 1;
}
}
return result;
}
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
unchecked {
uint256 result = log256(value);
return result + (rounding == Rounding.Up && 1 << (result << 3) < value ? 1 : 0);
}
}
}
文件 11 的 13:SafeERC20.sol
pragma solidity ^0.8.0;
import "../IERC20.sol";
import "../extensions/IERC20Permit.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 oldAllowance = token.allowance(address(this), spender);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance + value));
}
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");
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, oldAllowance - value));
}
}
function forceApprove(IERC20 token, address spender, uint256 value) internal {
bytes memory approvalCall = abi.encodeWithSelector(token.approve.selector, spender, value);
if (!_callOptionalReturnBool(token, approvalCall)) {
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, 0));
_callOptionalReturn(token, approvalCall);
}
}
function safePermit(
IERC20Permit token,
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
uint256 nonceBefore = token.nonces(owner);
token.permit(owner, spender, value, deadline, v, r, s);
uint256 nonceAfter = token.nonces(owner);
require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
}
function _callOptionalReturn(IERC20 token, bytes memory data) private {
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
require(returndata.length == 0 || abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
(bool success, bytes memory returndata) = address(token).call(data);
return
success && (returndata.length == 0 || abi.decode(returndata, (bool))) && Address.isContract(address(token));
}
}
文件 12 的 13:SignedMath.sol
pragma solidity ^0.8.0;
library SignedMath {
function max(int256 a, int256 b) internal pure returns (int256) {
return a > b ? a : b;
}
function min(int256 a, int256 b) internal pure returns (int256) {
return a < b ? a : b;
}
function average(int256 a, int256 b) internal pure returns (int256) {
int256 x = (a & b) + ((a ^ b) >> 1);
return x + (int256(uint256(x) >> 255) & (a ^ b));
}
function abs(int256 n) internal pure returns (uint256) {
unchecked {
return uint256(n >= 0 ? n : -n);
}
}
}
文件 13 的 13:Strings.sol
pragma solidity ^0.8.0;
import "./math/Math.sol";
import "./math/SignedMath.sol";
library Strings {
bytes16 private constant _SYMBOLS = "0123456789abcdef";
uint8 private constant _ADDRESS_LENGTH = 20;
function toString(uint256 value) internal pure returns (string memory) {
unchecked {
uint256 length = Math.log10(value) + 1;
string memory buffer = new string(length);
uint256 ptr;
assembly {
ptr := add(buffer, add(32, length))
}
while (true) {
ptr--;
assembly {
mstore8(ptr, byte(mod(value, 10), _SYMBOLS))
}
value /= 10;
if (value == 0) break;
}
return buffer;
}
}
function toString(int256 value) internal pure returns (string memory) {
return string(abi.encodePacked(value < 0 ? "-" : "", toString(SignedMath.abs(value))));
}
function toHexString(uint256 value) internal pure returns (string memory) {
unchecked {
return toHexString(value, Math.log256(value) + 1);
}
}
function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
bytes memory buffer = new bytes(2 * length + 2);
buffer[0] = "0";
buffer[1] = "x";
for (uint256 i = 2 * length + 1; i > 1; --i) {
buffer[i] = _SYMBOLS[value & 0xf];
value >>= 4;
}
require(value == 0, "Strings: hex length insufficient");
return string(buffer);
}
function toHexString(address addr) internal pure returns (string memory) {
return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH);
}
function equal(string memory a, string memory b) internal pure returns (bool) {
return keccak256(bytes(a)) == keccak256(bytes(b));
}
}
{
"compilationTarget": {
"contracts/FixedMode.sol": "FixedMode"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"buyer","type":"address"},{"indexed":false,"internalType":"uint256","name":"_donationId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"donateNumber","type":"uint256"},{"indexed":false,"internalType":"address","name":"donateToken","type":"address"}],"name":"Donate","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"buyer","type":"address"},{"indexed":false,"internalType":"uint256","name":"_donationId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"refundNumber","type":"uint256"},{"indexed":false,"internalType":"address","name":"refundToken","type":"address"}],"name":"Refund","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"previousAdminRole","type":"bytes32"},{"indexed":true,"internalType":"bytes32","name":"newAdminRole","type":"bytes32"}],"name":"RoleAdminChanged","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"RoleGranted","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"bytes32","name":"role","type":"bytes32"},{"indexed":true,"internalType":"address","name":"account","type":"address"},{"indexed":true,"internalType":"address","name":"sender","type":"address"}],"name":"RoleRevoked","type":"event"},{"inputs":[],"name":"ADMIN_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"DEFAULT_ADMIN_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"WETH","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_donationToken","type":"address"},{"internalType":"address","name":"_paymentToken","type":"address"},{"internalType":"uint256","name":"_totalSupply","type":"uint256"},{"internalType":"uint256","name":"_minimumPurchase","type":"uint256"},{"internalType":"uint256","name":"_startTime","type":"uint256"},{"internalType":"uint256","name":"_deadline","type":"uint256"},{"internalType":"string","name":"_remark","type":"string"},{"internalType":"uint256","name":"_extractTimes","type":"uint256"},{"internalType":"uint256","name":"_extractIntervalSeconds","type":"uint256"},{"internalType":"uint256[]","name":"extractRatios","type":"uint256[]"},{"internalType":"uint256","name":"_targetAmount","type":"uint256"}],"name":"addToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_donationId","type":"uint256"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"donate","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_donationId","type":"uint256"}],"name":"donateETH","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"donationTokens","outputs":[{"internalType":"address","name":"donationToken","type":"address"},{"internalType":"address","name":"paymentToken","type":"address"},{"internalType":"uint256","name":"totalSupply","type":"uint256"},{"internalType":"uint256","name":"minimumPurchase","type":"uint256"},{"internalType":"uint256","name":"startTime","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"string","name":"remark","type":"string"},{"internalType":"uint256","name":"extractTimes","type":"uint256"},{"internalType":"uint256","name":"extractIntervalSeconds","type":"uint256"},{"internalType":"uint256","name":"donatedAmount","type":"uint256"},{"internalType":"uint256","name":"targetAmount","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_donationId","type":"uint256"}],"name":"exist","outputs":[],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"address","name":"","type":"address"}],"name":"extractInfos","outputs":[{"internalType":"uint256","name":"donationsNumber","type":"uint256"},{"internalType":"uint256","name":"extractNumber","type":"uint256"},{"internalType":"uint256","name":"extractTimes","type":"uint256"},{"internalType":"uint256","name":"extractTime","type":"uint256"},{"internalType":"bool","name":"refundFlag","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"}],"name":"getRoleAdmin","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_donationId","type":"uint256"}],"name":"getToken","outputs":[{"components":[{"internalType":"address","name":"donationToken","type":"address"},{"internalType":"address","name":"paymentToken","type":"address"},{"internalType":"uint256","name":"totalSupply","type":"uint256"},{"internalType":"uint256","name":"minimumPurchase","type":"uint256"},{"internalType":"uint256","name":"startTime","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"string","name":"remark","type":"string"},{"internalType":"uint256","name":"extractTimes","type":"uint256"},{"internalType":"uint256","name":"extractIntervalSeconds","type":"uint256"},{"internalType":"uint256[]","name":"extractRatios","type":"uint256[]"},{"internalType":"uint256","name":"donatedAmount","type":"uint256"},{"internalType":"uint256","name":"targetAmount","type":"uint256"}],"internalType":"struct FixedMode.DonationToken","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"startId","type":"uint256"},{"internalType":"uint256","name":"endId","type":"uint256"}],"name":"getTokenRange","outputs":[{"components":[{"internalType":"address","name":"donationToken","type":"address"},{"internalType":"address","name":"paymentToken","type":"address"},{"internalType":"uint256","name":"totalSupply","type":"uint256"},{"internalType":"uint256","name":"minimumPurchase","type":"uint256"},{"internalType":"uint256","name":"startTime","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"string","name":"remark","type":"string"},{"internalType":"uint256","name":"extractTimes","type":"uint256"},{"internalType":"uint256","name":"extractIntervalSeconds","type":"uint256"},{"internalType":"uint256[]","name":"extractRatios","type":"uint256[]"},{"internalType":"uint256","name":"donatedAmount","type":"uint256"},{"internalType":"uint256","name":"targetAmount","type":"uint256"}],"internalType":"struct FixedMode.DonationToken[]","name":"","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"grantRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"hasRole","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"renounceRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"role","type":"bytes32"},{"internalType":"address","name":"account","type":"address"}],"name":"revokeRole","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_donationId","type":"uint256"},{"internalType":"uint256","name":"_deadline","type":"uint256"}],"name":"setDeadline","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_donationId","type":"uint256"},{"internalType":"address","name":"_donationToken","type":"address"}],"name":"setDonationToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_donationId","type":"uint256"},{"internalType":"uint256","name":"_extractTimes","type":"uint256"},{"internalType":"uint256","name":"_extractIntervalSeconds","type":"uint256"},{"internalType":"uint256[]","name":"extractRatios_","type":"uint256[]"}],"name":"setExtract","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_donationId","type":"uint256"},{"internalType":"uint256","name":"_minimumPurchase","type":"uint256"}],"name":"setMinimumPurchase","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_donationId","type":"uint256"},{"internalType":"address","name":"_paymentToken","type":"address"}],"name":"setPaymentToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_donationId","type":"uint256"},{"internalType":"string","name":"_remark","type":"string"}],"name":"setRemark","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_donationId","type":"uint256"},{"internalType":"uint256","name":"_startTime","type":"uint256"}],"name":"setStartTime","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_donationId","type":"uint256"},{"internalType":"uint256","name":"_targetAmount","type":"uint256"}],"name":"setTargetAmount","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_donationId","type":"uint256"},{"internalType":"uint256","name":"_totalSupply","type":"uint256"}],"name":"setTotalSupply","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_WETH","type":"address"}],"name":"setWETH","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"interfaceId","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"tokenCount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"transferETH","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_tokenAddress","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"transferTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_donationId","type":"uint256"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]