编译器
0.8.24+commit.e11b9ed9
文件 1 的 7:Address.sol
pragma solidity ^0.8.20;
library Address {
error AddressInsufficientBalance(address account);
error AddressEmptyCode(address target);
error FailedInnerCall();
function sendValue(address payable recipient, uint256 amount) internal {
if (address(this).balance < amount) {
revert AddressInsufficientBalance(address(this));
}
(bool success, ) = recipient.call{value: amount}("");
if (!success) {
revert FailedInnerCall();
}
}
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0);
}
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
if (address(this).balance < value) {
revert AddressInsufficientBalance(address(this));
}
(bool success, bytes memory returndata) = target.call{value: value}(data);
return verifyCallResultFromTarget(target, success, returndata);
}
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
(bool success, bytes memory returndata) = target.staticcall(data);
return verifyCallResultFromTarget(target, success, returndata);
}
function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
(bool success, bytes memory returndata) = target.delegatecall(data);
return verifyCallResultFromTarget(target, success, returndata);
}
function verifyCallResultFromTarget(
address target,
bool success,
bytes memory returndata
) internal view returns (bytes memory) {
if (!success) {
_revert(returndata);
} else {
if (returndata.length == 0 && target.code.length == 0) {
revert AddressEmptyCode(target);
}
return returndata;
}
}
function verifyCallResult(bool success, bytes memory returndata) internal pure returns (bytes memory) {
if (!success) {
_revert(returndata);
} else {
return returndata;
}
}
function _revert(bytes memory returndata) private pure {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert FailedInnerCall();
}
}
}
文件 2 的 7:Balance.sol
pragma solidity 0.8.24;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
interface IWETH9 is IERC20 {
function deposit() external payable;
function withdraw(uint256) external;
}
interface ISwapRouter02 {
struct ExactInputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint256 amountIn;
uint256 amountOutMinimum;
uint160 sqrtPriceLimitX96;
}
struct ExactInputParams {
bytes path;
address recipient;
uint256 amountIn;
uint256 amountOutMinimum;
}
function exactInput(
ExactInputParams calldata params
) external payable returns (uint256 amountOut);
function exactInputSingle(
ExactInputSingleParams calldata params
) external payable returns (uint256 amountOut);
struct ExactOutputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint256 amountOut;
uint256 amountInMaximum;
uint160 sqrtPriceLimitX96;
}
function exactOutputSingle(
ExactOutputSingleParams calldata params
) external payable returns (uint256 amountIn);
}
contract ProBalance is Ownable {
using SafeERC20 for IERC20;
address public i_USDT;
address public i_USDC;
address public i_WETH;
bool public acceptNative;
ISwapRouter02 public swapRouter;
mapping(address => bool) public allowedDepositTokens;
address public operator;
bool public acceptWithdraw;
uint256 public withdrawBufferPeriod = 3 seconds;
mapping(address => uint256) public maxSingleWithdraw;
mapping(address => uint256) public protectedBalances;
mapping(address => uint256) public userLastWithdraw;
event InjectTreasury(uint256 amount);
event BalanceAdded(address indexed user, uint256 indexed amount);
event TokenBalanceAdded(
address indexed user,
uint256 indexed amount,
address indexed token
);
modifier withdrawAllowed() {
require(acceptWithdraw, "not allowed");
_;
}
modifier onlyOperator() {
require(msg.sender == operator, "op");
_;
}
modifier protectedBalanceCheck(address token) {
if (token == address(0))
require(
address(this).balance > protectedBalances[token],
"protected contact admin"
);
else
require(
IERC20(token).balanceOf(address(this)) >
protectedBalances[token],
"protected contact admin"
);
_;
}
constructor(
bool _acceptWithdraw,
bool _acceptNative,
address _weth,
address _usdt,
address _usdc,
address _router,
address _operator
) Ownable(msg.sender) {
i_WETH = _weth;
i_USDT = _usdt;
i_USDC = _usdc;
swapRouter = ISwapRouter02(_router);
acceptWithdraw = _acceptWithdraw;
acceptNative = _acceptNative;
operator = _operator;
allowedDepositTokens[_usdt] = true;
allowedDepositTokens[_usdc] = true;
}
function addBalance() external payable {
require(acceptNative, "native not accepted");
emit BalanceAdded(msg.sender, msg.value);
}
function addBalanceWithSwap(
address targetToken,
uint256 amountOutMinimum,
uint24 poolFee
) external payable {
require(
targetToken == i_USDT || targetToken == i_USDC,
"cant convert to target"
);
ISwapRouter02.ExactInputSingleParams memory params = ISwapRouter02
.ExactInputSingleParams({
tokenIn: i_WETH,
tokenOut: targetToken,
fee: poolFee,
recipient: address(this),
amountIn: msg.value,
amountOutMinimum: amountOutMinimum,
sqrtPriceLimitX96: 0
});
uint256 targetAmount = swapRouter.exactInputSingle{value: msg.value}(
params
);
emit TokenBalanceAdded(msg.sender, targetAmount, targetToken);
}
function addTokenBalance(address token, uint256 amount) external {
require(allowedDepositTokens[token], "token not accepted");
IERC20(token).safeTransferFrom(msg.sender, address(this), amount);
emit TokenBalanceAdded(msg.sender, amount, token);
}
function addTokenBalanceWithSwap(
address token,
address targetToken,
uint256 amount,
uint256 amountOutMinimum,
uint24 poolFee
) external {
IERC20(token).safeTransferFrom(msg.sender, address(this), amount);
require(
targetToken == i_USDT || targetToken == i_USDC,
"cant convert to target"
);
IERC20(token).approve(address(swapRouter), amount);
ISwapRouter02.ExactInputSingleParams memory params = ISwapRouter02
.ExactInputSingleParams({
tokenIn: token,
tokenOut: targetToken,
fee: poolFee,
recipient: address(this),
amountIn: amount,
amountOutMinimum: amountOutMinimum,
sqrtPriceLimitX96: 0
});
uint targetAmount = swapRouter.exactInputSingle(params);
emit TokenBalanceAdded(msg.sender, targetAmount, targetToken);
}
function addTokenBalanceWithMultihop(
address token,
address tokenHop,
address targetToken,
uint256 amount,
uint256 amountOutMinimum,
uint24 poolFee1,
uint24 poolFee2
) external {
IERC20(token).safeTransferFrom(msg.sender, address(this), amount);
require(
targetToken == i_USDT || targetToken == i_USDC,
"cant convert to target"
);
IERC20(token).approve(address(swapRouter), amount);
ISwapRouter02.ExactInputParams memory params = ISwapRouter02
.ExactInputParams({
path: abi.encodePacked(
token,
poolFee1,
tokenHop,
poolFee2,
targetToken
),
recipient: address(this),
amountIn: amount,
amountOutMinimum: amountOutMinimum
});
uint targetAmount = swapRouter.exactInput(params);
emit TokenBalanceAdded(msg.sender, targetAmount, targetToken);
}
function injectTreasury() external payable {
emit InjectTreasury(msg.value);
}
function setAcceptNative(bool _acceptNative) external onlyOwner {
acceptNative = _acceptNative;
}
function setAcceptWithdraw(bool _acceptWithdraw) external onlyOwner {
acceptWithdraw = _acceptWithdraw;
}
function setSwapRouter(address _router) external onlyOwner {
swapRouter = ISwapRouter02(_router);
}
function setOperator(address _op) external onlyOwner {
require(_op != address(0), "cant be address 0");
operator = _op;
}
function setAllowedDepositToken(
address token,
bool status
) external onlyOwner {
require(token != address(0), "cant be address 0");
allowedDepositTokens[token] = status;
}
function setProtectedBalance(
address _token,
uint256 _amount
) external onlyOwner {
protectedBalances[_token] = _amount;
}
function setMaxSingleWithdraw(
address _token,
uint256 _amount
) external onlyOwner {
maxSingleWithdraw[_token] = _amount;
}
function recoverAmount(uint256 amount) external onlyOwner {
_transferAmount(msg.sender, amount);
}
function recoverToken(
address tokenAddress,
uint256 amount
) external onlyOwner {
_transferEthOrToken(tokenAddress, owner(), amount);
}
function sendBalanceAmount(
address tokenAddress,
address user,
uint256 amount
) external onlyOwner {
_transferEthOrToken(tokenAddress, user, amount);
}
function _transferEthOrToken(
address tokenAddress,
address _to,
uint256 _amount
) internal {
if (tokenAddress == address(0)) _transferAmount(_to, _amount);
else IERC20(tokenAddress).safeTransfer(_to, _amount);
}
function _transferAmount(address _to, uint256 _amount) internal {
(bool success, ) = _to.call{value: _amount}("");
require(success, "TransferHelper: TRANSFER_FAILED");
}
function canSend(
address tokenAddress,
address user,
uint256 amount
) public view returns (bool) {
if (!acceptWithdraw) return false;
if (msg.sender != operator) return false;
if (userLastWithdraw[user] + withdrawBufferPeriod > block.timestamp)
return false;
if (
maxSingleWithdraw[tokenAddress] > 0 &&
amount > maxSingleWithdraw[tokenAddress]
) return false;
return true;
}
function sendBalance(
address tokenAddress,
address user,
uint256 amount
)
external
onlyOperator
withdrawAllowed
protectedBalanceCheck(tokenAddress)
{
require(
userLastWithdraw[user] + withdrawBufferPeriod < block.timestamp,
"waitForBuffer"
);
userLastWithdraw[user] = block.timestamp;
require(
maxSingleWithdraw[tokenAddress] > 0 &&
amount < maxSingleWithdraw[tokenAddress],
">max"
);
_transferEthOrToken(tokenAddress, user, amount);
}
}
文件 3 的 7:Context.sol
pragma solidity ^0.8.20;
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
文件 4 的 7:IERC20.sol
pragma solidity ^0.8.20;
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 value) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 value) external returns (bool);
function transferFrom(address from, address to, uint256 value) external returns (bool);
}
文件 5 的 7:IERC20Permit.sol
pragma solidity ^0.8.20;
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);
}
文件 6 的 7:Ownable.sol
pragma solidity ^0.8.20;
import {Context} from "../utils/Context.sol";
abstract contract Ownable is Context {
address private _owner;
error OwnableUnauthorizedAccount(address account);
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
modifier onlyOwner() {
_checkOwner();
_;
}
function owner() public view virtual returns (address) {
return _owner;
}
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
文件 7 的 7:SafeERC20.sol
pragma solidity ^0.8.20;
import {IERC20} from "../IERC20.sol";
import {IERC20Permit} from "../extensions/IERC20Permit.sol";
import {Address} from "../../../utils/Address.sol";
library SafeERC20 {
using Address for address;
error SafeERC20FailedOperation(address token);
error SafeERC20FailedDecreaseAllowance(address spender, uint256 currentAllowance, uint256 requestedDecrease);
function safeTransfer(IERC20 token, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeCall(token.transfer, (to, value)));
}
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeCall(token.transferFrom, (from, to, value)));
}
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 oldAllowance = token.allowance(address(this), spender);
forceApprove(token, spender, oldAllowance + value);
}
function safeDecreaseAllowance(IERC20 token, address spender, uint256 requestedDecrease) internal {
unchecked {
uint256 currentAllowance = token.allowance(address(this), spender);
if (currentAllowance < requestedDecrease) {
revert SafeERC20FailedDecreaseAllowance(spender, currentAllowance, requestedDecrease);
}
forceApprove(token, spender, currentAllowance - requestedDecrease);
}
}
function forceApprove(IERC20 token, address spender, uint256 value) internal {
bytes memory approvalCall = abi.encodeCall(token.approve, (spender, value));
if (!_callOptionalReturnBool(token, approvalCall)) {
_callOptionalReturn(token, abi.encodeCall(token.approve, (spender, 0)));
_callOptionalReturn(token, approvalCall);
}
}
function _callOptionalReturn(IERC20 token, bytes memory data) private {
bytes memory returndata = address(token).functionCall(data);
if (returndata.length != 0 && !abi.decode(returndata, (bool))) {
revert SafeERC20FailedOperation(address(token));
}
}
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(token).code.length > 0;
}
}
{
"compilationTarget": {
"contracts/Balance.sol": "ProBalance"
},
"evmVersion": "shanghai",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": []
}
[{"inputs":[{"internalType":"bool","name":"_acceptWithdraw","type":"bool"},{"internalType":"bool","name":"_acceptNative","type":"bool"},{"internalType":"address","name":"_weth","type":"address"},{"internalType":"address","name":"_usdt","type":"address"},{"internalType":"address","name":"_usdc","type":"address"},{"internalType":"address","name":"_router","type":"address"},{"internalType":"address","name":"_operator","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"address","name":"target","type":"address"}],"name":"AddressEmptyCode","type":"error"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"AddressInsufficientBalance","type":"error"},{"inputs":[],"name":"FailedInnerCall","type":"error"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"OwnableInvalidOwner","type":"error"},{"inputs":[{"internalType":"address","name":"account","type":"address"}],"name":"OwnableUnauthorizedAccount","type":"error"},{"inputs":[{"internalType":"address","name":"token","type":"address"}],"name":"SafeERC20FailedOperation","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":true,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"BalanceAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"InjectTreasury","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":"user","type":"address"},{"indexed":true,"internalType":"uint256","name":"amount","type":"uint256"},{"indexed":true,"internalType":"address","name":"token","type":"address"}],"name":"TokenBalanceAdded","type":"event"},{"inputs":[],"name":"acceptNative","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"acceptWithdraw","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"addBalance","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"targetToken","type":"address"},{"internalType":"uint256","name":"amountOutMinimum","type":"uint256"},{"internalType":"uint24","name":"poolFee","type":"uint24"}],"name":"addBalanceWithSwap","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"addTokenBalance","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"address","name":"tokenHop","type":"address"},{"internalType":"address","name":"targetToken","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"amountOutMinimum","type":"uint256"},{"internalType":"uint24","name":"poolFee1","type":"uint24"},{"internalType":"uint24","name":"poolFee2","type":"uint24"}],"name":"addTokenBalanceWithMultihop","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"address","name":"targetToken","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"uint256","name":"amountOutMinimum","type":"uint256"},{"internalType":"uint24","name":"poolFee","type":"uint24"}],"name":"addTokenBalanceWithSwap","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"allowedDepositTokens","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"tokenAddress","type":"address"},{"internalType":"address","name":"user","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"canSend","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"i_USDC","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"i_USDT","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"i_WETH","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"injectTreasury","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"maxSingleWithdraw","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"operator","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"protectedBalances","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"recoverAmount","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenAddress","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"recoverToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenAddress","type":"address"},{"internalType":"address","name":"user","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"sendBalance","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"tokenAddress","type":"address"},{"internalType":"address","name":"user","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"sendBalanceAmount","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bool","name":"_acceptNative","type":"bool"}],"name":"setAcceptNative","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bool","name":"_acceptWithdraw","type":"bool"}],"name":"setAcceptWithdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"token","type":"address"},{"internalType":"bool","name":"status","type":"bool"}],"name":"setAllowedDepositToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_token","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"setMaxSingleWithdraw","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_op","type":"address"}],"name":"setOperator","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_token","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"setProtectedBalance","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_router","type":"address"}],"name":"setSwapRouter","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"swapRouter","outputs":[{"internalType":"contract ISwapRouter02","name":"","type":"address"}],"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":"userLastWithdraw","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"withdrawBufferPeriod","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]