编译器
0.8.19+commit.7dd6d404
文件 1 的 9: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);
}
}
}
文件 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 {
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);
}
文件 4 的 9:IERC20Metadata.sol
pragma solidity ^0.8.0;
import "IERC20.sol";
interface IERC20Metadata is IERC20 {
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view returns (uint8);
}
文件 5 的 9:IERC20Mint.sol
pragma solidity 0.8.19;
import "IERC20Metadata.sol";
interface IERC20Mint is IERC20Metadata {
function mint(address _for, uint256 _amount) external;
}
文件 6 的 9:Ownable.sol
pragma solidity ^0.8.0;
import "Context.sol";
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor() {
_transferOwnership(_msgSender());
}
modifier onlyOwner() {
_checkOwner();
_;
}
function owner() public view virtual returns (address) {
return _owner;
}
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
文件 7 的 9:SafeERC20.sol
pragma solidity ^0.8.0;
import "IERC20.sol";
import "draft-IERC20Permit.sol";
import "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 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");
if (returndata.length > 0) {
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
文件 8 的 9:UBDNLockerDistributor.sol
pragma solidity 0.8.19;
import "SafeERC20.sol";
import "Ownable.sol";
import "IERC20Mint.sol";
contract UBDNLockerDistributor is Ownable {
using SafeERC20 for IERC20Mint;
struct Lock {
uint256 amount;
uint256 lockedUntil;
}
uint256 constant public START_PRICE = 1;
uint256 constant public PRICE_INCREASE_STEP = 1;
uint256 constant public INCREASE_FROM_ROUND = 1;
uint256 constant public ROUND_VOLUME = 1_000_000e18;
uint256 constant public ADD_NEW_PAYMENT_TOKEN_TIMELOCK = 48 hours;
uint256 constant public EMERGENCY_PAYMENT_PAUSE = 1 hours;
uint256 public LOCK_PERIOD = 90 days;
uint256 public distributedAmount;
IERC20Mint public distributionToken;
mapping (address => uint256) public paymentTokens;
mapping (address => Lock[]) public userLocks;
mapping (address => bool) public isGuardian;
event DistributionTokenSet(address indexed Token);
event PaymentTokenStatus(address indexed Token, bool Status);
event Purchase(
address indexed User,
uint256 indexed PurchaseAmount,
address indexed PaymentToken,
uint256 PaymentAmount
);
event Claimed(address User, uint256 Amount, uint256 Timestamp);
event PaymentTokenPaused(address indexed Token, uint256 Until);
constructor (uint256 _lockPeriod) {
if (_lockPeriod > 0) {
LOCK_PERIOD = _lockPeriod;
}
}
function buyTokensForExactStableWithSlippage(
address _paymentToken,
uint256 _inAmount,
uint256 _outNotLess
)
external
{
require(
_calcTokensForExactStable(_paymentToken,_inAmount) >= _outNotLess,
"Slippage occur"
);
buyTokensForExactStable(_paymentToken, _inAmount);
}
function buyTokensForExactStable(address _paymentToken, uint256 _inAmount)
public
{
require(address(distributionToken) != address(0), 'Distribution not Define');
require(_isValidForPayment(_paymentToken), 'This payment token not supported');
uint256 outAmount = _calcTokensForExactStable(_paymentToken,_inAmount);
require(outAmount > 0, 'Cant buy zero');
_newLock(msg.sender, outAmount);
distributedAmount += outAmount;
distributionToken.mint(address(this), outAmount);
emit Purchase(msg.sender, outAmount, _paymentToken, _inAmount);
IERC20Mint(_paymentToken).safeTransferFrom(msg.sender, owner(),_inAmount);
}
function claimTokens() external {
uint256 claimAmount;
for (uint256 i = 0; i < userLocks[msg.sender].length; ++i){
if (block.timestamp >= userLocks[msg.sender][i].lockedUntil){
claimAmount += userLocks[msg.sender][i].amount;
userLocks[msg.sender][i].amount = 0;
}
}
require(claimAmount > 0, 'Nothing to claim');
distributionToken.safeTransfer(msg.sender, claimAmount);
emit Claimed(msg.sender, claimAmount, block.timestamp);
}
function emergencyPause(address _paymentToken) external {
require(isGuardian[msg.sender], "Only for approved guardians");
if (
paymentTokens[_paymentToken] > 0
&& paymentTokens[_paymentToken] <= block.timestamp
)
{
paymentTokens[_paymentToken] = block.timestamp + EMERGENCY_PAYMENT_PAUSE;
emit PaymentTokenPaused(_paymentToken, paymentTokens[_paymentToken]);
}
}
function setPaymentTokenStatus(address _token, bool _state)
external
onlyOwner
{
if (_state ) {
paymentTokens[_token] = block.timestamp + ADD_NEW_PAYMENT_TOKEN_TIMELOCK;
} else {
paymentTokens[_token] = 0;
}
emit PaymentTokenStatus(_token, _state);
}
function setDistributionToken(address _token)
external
onlyOwner
{
require(address(distributionToken) == address(0), "Can call only once");
distributionToken = IERC20Mint(_token);
emit DistributionTokenSet(_token);
}
function setGuardianStatus(address _guardian, bool _state)
external
onlyOwner
{
isGuardian[_guardian] = _state;
}
function calcTokensForExactStable(address _paymentToken, uint256 _inAmount)
external
view
returns(uint256)
{
return _calcTokensForExactStable(_paymentToken, _inAmount);
}
function calcStableForExactTokens(address _paymentToken, uint256 _outAmount)
external
view
returns(uint256)
{
return _calcStableForExactTokens(_paymentToken, _outAmount);
}
function priceInUnitsAndRemainByRound(uint256 _round)
external
view
returns(uint256, uint256)
{
return _priceInUnitsAndRemainByRound(_round);
}
function getUserLocks(address _user)
public
view
returns(Lock[] memory)
{
return userLocks[_user];
}
function getUserAvailableAmount(address _user)
public
view
returns(uint256 total, uint256 availableNow)
{
for (uint256 i = 0; i < userLocks[_user].length; ++i){
total += userLocks[_user][i].amount;
if (block.timestamp >= userLocks[_user][i].lockedUntil){
availableNow += userLocks[_user][i].amount;
}
}
}
function getCurrentRound() external view returns(uint256){
return _currenRound();
}
function _newLock(address _user, uint256 _lockAmount) internal {
userLocks[_user].push(
Lock(_lockAmount, block.timestamp + LOCK_PERIOD)
);
}
function _calcStableForExactTokens(address _paymentToken, uint256 _outAmount)
internal
virtual
view
returns(uint256 inAmount)
{
uint256 outA = _outAmount;
uint256 curR = _currenRound();
uint256 curPrice;
uint256 curRest;
uint8 payTokenDecimals = IERC20Mint(_paymentToken).decimals();
uint8 dstTokenDecimals = distributionToken.decimals();
while (outA > 0) {
(curPrice, curRest) = _priceInUnitsAndRemainByRound(curR);
if (outA > curRest) {
inAmount += curRest
* curPrice * 10**payTokenDecimals
/ (10**dstTokenDecimals);
outA -= curRest;
++ curR;
} else {
inAmount += outA
* curPrice * 10**payTokenDecimals
/ (10**dstTokenDecimals);
return inAmount;
}
}
}
function _calcTokensForExactStable(address _paymentToken, uint256 _inAmount)
internal
virtual
view
returns(uint256 outAmount)
{
uint256 inA = _inAmount;
uint256 curR = _currenRound();
uint256 curPrice;
uint256 curRest;
uint8 payTokenDecimals = IERC20Mint(_paymentToken).decimals();
uint8 dstTokenDecimals = distributionToken.decimals();
while (inA > 0) {
(curPrice, curRest) = _priceInUnitsAndRemainByRound(curR);
if (
inA
* (10**dstTokenDecimals)
/ (curPrice * 10**payTokenDecimals)
> curRest
)
{
outAmount += curRest;
inA -= curRest
* curPrice * 10**payTokenDecimals
/ (10**dstTokenDecimals);
++ curR;
} else {
outAmount += inA
* 10**dstTokenDecimals
/ (curPrice * 10**payTokenDecimals);
return outAmount;
}
}
}
function _priceInUnitsAndRemainByRound(uint256 _round)
internal
view
virtual
returns(uint256 price, uint256 rest)
{
if (_round < INCREASE_FROM_ROUND){
price = START_PRICE;
} else {
price = START_PRICE + PRICE_INCREASE_STEP * (_round - INCREASE_FROM_ROUND + 1);
}
if (_round < _currenRound()){
rest = 0;
} else if (_round == _currenRound()){
if (_round == 1){
rest = ROUND_VOLUME - distributedAmount;
} else {
rest = ROUND_VOLUME - (distributedAmount % ROUND_VOLUME);
}
} else {
rest = ROUND_VOLUME;
}
}
function _currenRound() internal view virtual returns(uint256){
return distributedAmount / ROUND_VOLUME + 1;
}
function _isValidForPayment(address _paymentToken) internal view returns(bool){
if (paymentTokens[_paymentToken] == 0) {
return false;
}
require(
paymentTokens[_paymentToken] < block.timestamp,
"Token paused or timelocked"
);
return true;
}
}
文件 9 的 9: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": {
"UBDNLockerDistributor.sol": "UBDNLockerDistributor"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"uint256","name":"_lockPeriod","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"User","type":"address"},{"indexed":false,"internalType":"uint256","name":"Amount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"Timestamp","type":"uint256"}],"name":"Claimed","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"Token","type":"address"}],"name":"DistributionTokenSet","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"Token","type":"address"},{"indexed":false,"internalType":"uint256","name":"Until","type":"uint256"}],"name":"PaymentTokenPaused","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"Token","type":"address"},{"indexed":false,"internalType":"bool","name":"Status","type":"bool"}],"name":"PaymentTokenStatus","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"User","type":"address"},{"indexed":true,"internalType":"uint256","name":"PurchaseAmount","type":"uint256"},{"indexed":true,"internalType":"address","name":"PaymentToken","type":"address"},{"indexed":false,"internalType":"uint256","name":"PaymentAmount","type":"uint256"}],"name":"Purchase","type":"event"},{"inputs":[],"name":"ADD_NEW_PAYMENT_TOKEN_TIMELOCK","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"EMERGENCY_PAYMENT_PAUSE","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"INCREASE_FROM_ROUND","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"LOCK_PERIOD","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"PRICE_INCREASE_STEP","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"ROUND_VOLUME","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"START_PRICE","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_paymentToken","type":"address"},{"internalType":"uint256","name":"_inAmount","type":"uint256"}],"name":"buyTokensForExactStable","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_paymentToken","type":"address"},{"internalType":"uint256","name":"_inAmount","type":"uint256"},{"internalType":"uint256","name":"_outNotLess","type":"uint256"}],"name":"buyTokensForExactStableWithSlippage","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_paymentToken","type":"address"},{"internalType":"uint256","name":"_outAmount","type":"uint256"}],"name":"calcStableForExactTokens","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_paymentToken","type":"address"},{"internalType":"uint256","name":"_inAmount","type":"uint256"}],"name":"calcTokensForExactStable","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"claimTokens","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"distributedAmount","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"distributionToken","outputs":[{"internalType":"contract IERC20Mint","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_paymentToken","type":"address"}],"name":"emergencyPause","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"getCurrentRound","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_user","type":"address"}],"name":"getUserAvailableAmount","outputs":[{"internalType":"uint256","name":"total","type":"uint256"},{"internalType":"uint256","name":"availableNow","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_user","type":"address"}],"name":"getUserLocks","outputs":[{"components":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"lockedUntil","type":"uint256"}],"internalType":"struct UBDNLockerDistributor.Lock[]","name":"","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"isGuardian","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"paymentTokens","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"_round","type":"uint256"}],"name":"priceInUnitsAndRemainByRound","outputs":[{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_token","type":"address"}],"name":"setDistributionToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_guardian","type":"address"},{"internalType":"bool","name":"_state","type":"bool"}],"name":"setGuardianStatus","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_token","type":"address"},{"internalType":"bool","name":"_state","type":"bool"}],"name":"setPaymentTokenStatus","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"uint256","name":"","type":"uint256"}],"name":"userLocks","outputs":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"lockedUntil","type":"uint256"}],"stateMutability":"view","type":"function"}]