编译器
0.8.22+commit.4fc1097e
文件 1 的 29: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 的 29:ArbitrageSearch.sol
pragma solidity =0.8.22;
import "openzeppelin-contracts/contracts/utils/math/Math.sol";
import "../interfaces/IExchangeConfig.sol";
import "../pools/PoolUtils.sol";
abstract contract ArbitrageSearch
{
IERC20 immutable public weth;
IERC20 immutable public usdc;
IERC20 immutable public usdt;
constructor( IExchangeConfig _exchangeConfig )
{
weth = _exchangeConfig.weth();
usdc = _exchangeConfig.usdc();
usdt = _exchangeConfig.usdt();
}
function _arbitragePath( IERC20 swapTokenIn, IERC20 swapTokenOut ) internal view returns (IERC20 arbToken2, IERC20 arbToken3)
{
if ( address(swapTokenIn) == address(usdc))
if ( address(swapTokenOut) == address(weth))
return (usdc, usdt);
if ( address(swapTokenIn) == address(weth))
if ( address(swapTokenOut) == address(usdc))
return (usdt, usdc);
if ( address(swapTokenIn) == address(weth))
return (usdc, swapTokenOut);
if ( address(swapTokenOut) == address(weth))
return (swapTokenIn, usdc);
return (swapTokenOut, swapTokenIn);
}
function _mostSignificantBit(uint256 x) internal pure returns (uint256 msb)
{
unchecked
{
if (x >= 2**128) { x >>= 128; msb += 128; }
if (x >= 2**64) { x >>= 64; msb += 64; }
if (x >= 2**32) { x >>= 32; msb += 32; }
if (x >= 2**16) { x >>= 16; msb += 16; }
if (x >= 2**8) { x >>= 8; msb += 8; }
if (x >= 2**4) { x >>= 4; msb += 4; }
if (x >= 2**2) { x >>= 2; msb += 2; }
if (x >= 2**1) { x >>= 1; msb += 1; }
}
}
function _shiftRequired( uint256 x, uint256 y, uint256 z ) internal pure returns (uint256)
{
unchecked
{
uint256 requiredBits0 = _mostSignificantBit(x) + _mostSignificantBit(y) + _mostSignificantBit(z);
if ( requiredBits0 < 240 )
return 0;
return Math.ceilDiv( requiredBits0 - 240, 3 );
}
}
function _determineShift( uint256 a0, uint256 b0, uint256 c0, uint256 a1, uint256 b1, uint256 c1 ) internal pure returns (uint256)
{
uint256 shift0 = _shiftRequired(a0, b0, c0);
uint256 shift1 = _shiftRequired(a1, b1, c1);
return shift0 > shift1 ? shift0 : shift1;
}
function _bestArbitrageIn( uint256 a0, uint256 a1, uint256 b0, uint256 b1, uint256 c0, uint256 c1 ) internal pure returns (uint256 bestArbAmountIn)
{
unchecked
{
uint256 shift = _determineShift( a0, b0, c0, a1, b1, c1 );
if ( shift > 0 )
{
a0 = a0 >> shift;
a1 = a1 >> shift;
b0 = b0 >> shift;
b1 = b1 >> shift;
c0 = c0 >> shift;
c1 = c1 >> shift;
}
uint256 n0 = a0 * b0 * c0;
uint256 n1 = a1 * b1 * c1;
if (n1 <= n0)
return 0;
uint256 m = a1 * b1 + c0 * ( b0 + a1 );
uint256 z = Math.sqrt(n0) * Math.sqrt(n1);
bestArbAmountIn = ( z - n0 ) / m;
if ( bestArbAmountIn == 0 )
return 0;
if ( shift > 0 )
{
bestArbAmountIn = bestArbAmountIn << shift;
a0 = a0 << shift;
a1 = a1 << shift;
b0 = b0 << shift;
b1 = b1 << shift;
c0 = c0 << shift;
c1 = c1 << shift;
}
uint256 amountOut = (a1 * bestArbAmountIn) / (a0 + bestArbAmountIn);
amountOut = (b1 * amountOut) / (b0 + amountOut);
amountOut = (c1 * amountOut) / (c0 + amountOut);
if ( amountOut < bestArbAmountIn )
return 0;
}
}
}
文件 3 的 29: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 的 29:IAccessManager.sol
pragma solidity =0.8.22;
interface IAccessManager
{
function excludedCountriesUpdated() external;
function grantAccess(bytes calldata signature) external;
function geoVersion() external view returns (uint256);
function walletHasAccess(address wallet) external view returns (bool);
}
文件 5 的 29:IAirdrop.sol
pragma solidity =0.8.22;
interface IAirdrop
{
function authorizeWallet( address wallet, uint256 saltAmount ) external;
function allowClaiming() external;
function claim() external;
function claimedByUser( address wallet) external view returns (uint256);
function claimingAllowed() external view returns (bool);
function claimingStartTimestamp() external view returns (uint256);
function claimableAmount(address wallet) external view returns (uint256);
function airdropForUser( address wallet ) external view returns (uint256);
}
文件 6 的 29:IBootstrapBallot.sol
pragma solidity =0.8.22;
interface IBootstrapBallot
{
function vote( bool voteStartExchangeYes, uint256 saltAmount, bytes calldata signature ) external;
function finalizeBallot() external;
function authorizeAirdrop2( uint256 saltAmount, bytes calldata signature ) external;
function finalizeAirdrop2() external;
function claimableTimestamp1() external view returns (uint256);
function claimableTimestamp2() external view returns (uint256);
function hasVoted(address user) external view returns (bool);
function ballotFinalized() external view returns (bool);
function startExchangeYes() external view returns (uint256);
function startExchangeNo() external view returns (uint256);
}
文件 7 的 29:IDAO.sol
pragma solidity =0.8.22;
import "../../rewards/interfaces/ISaltRewards.sol";
import "../../pools/interfaces/IPools.sol";
import "../../interfaces/ISalt.sol";
interface IDAO
{
function finalizeBallot( uint256 ballotID ) external;
function manuallyRemoveBallot( uint256 ballotID ) external;
function withdrawFromDAO( IERC20 token ) external returns (uint256 withdrawnAmount);
function pools() external view returns (IPools);
function websiteURL() external view returns (string memory);
function countryIsExcluded( string calldata country ) external view returns (bool);
}
文件 8 的 29: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 的 29: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 的 29:IEmissions.sol
pragma solidity =0.8.22;
interface IEmissions
{
function performUpkeep( uint256 timeSinceLastUpkeep ) external;
}
文件 11 的 29:IExchangeConfig.sol
pragma solidity =0.8.22;
import "openzeppelin-contracts/contracts/finance/VestingWallet.sol";
import "../staking/interfaces/ILiquidity.sol";
import "../launch/interfaces/IInitialDistribution.sol";
import "../rewards/interfaces/IRewardsEmitter.sol";
import "../rewards/interfaces/ISaltRewards.sol";
import "../rewards/interfaces/IEmissions.sol";
import "../interfaces/IAccessManager.sol";
import "../launch/interfaces/IAirdrop.sol";
import "../dao/interfaces/IDAO.sol";
import "../interfaces/ISalt.sol";
import "./IUpkeep.sol";
interface IExchangeConfig
{
function setContracts( IDAO _dao, IUpkeep _upkeep, IInitialDistribution _initialDistribution, VestingWallet _teamVestingWallet, VestingWallet _daoVestingWallet ) external;
function setAccessManager( IAccessManager _accessManager ) external;
function salt() external view returns (ISalt);
function wbtc() external view returns (IERC20);
function weth() external view returns (IERC20);
function usdc() external view returns (IERC20);
function usdt() external view returns (IERC20);
function daoVestingWallet() external view returns (VestingWallet);
function teamVestingWallet() external view returns (VestingWallet);
function initialDistribution() external view returns (IInitialDistribution);
function accessManager() external view returns (IAccessManager);
function dao() external view returns (IDAO);
function upkeep() external view returns (IUpkeep);
function teamWallet() external view returns (address);
function walletHasAccess( address wallet ) external view returns (bool);
}
文件 12 的 29:IInitialDistribution.sol
pragma solidity =0.8.22;
import "./IBootstrapBallot.sol";
import "./IAirdrop.sol";
interface IInitialDistribution
{
function distributionApproved( IAirdrop airdrop1, IAirdrop airdrop2 ) external;
function bootstrapBallot() external view returns (IBootstrapBallot);
}
文件 13 的 29:ILiquidity.sol
pragma solidity =0.8.22;
import "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import "./IStakingRewards.sol";
interface ILiquidity is IStakingRewards
{
function depositLiquidityAndIncreaseShare( IERC20 tokenA, IERC20 tokenB, uint256 maxAmountA, uint256 maxAmountB, uint256 minAddedAmountA, uint256 minAddedAmountB, uint256 minAddedLiquidity, uint256 deadline, bool useZapping ) external returns (uint256 addedLiquidity);
function withdrawLiquidityAndClaim( IERC20 tokenA, IERC20 tokenB, uint256 liquidityToWithdraw, uint256 minReclaimedA, uint256 minReclaimedB, uint256 deadline ) external returns (uint256 reclaimedA, uint256 reclaimedB);
}
文件 14 的 29:IPoolStats.sol
pragma solidity =0.8.22;
interface IPoolStats
{
struct ArbitrageIndicies
{
uint64 index1;
uint64 index2;
uint64 index3;
}
function clearProfitsForPools() external;
function updateArbitrageIndicies() external;
function profitsForWhitelistedPools() external view returns (uint256[] memory _calculatedProfits);
function arbitrageIndicies(bytes32 poolID) external view returns (ArbitrageIndicies memory);
}
文件 15 的 29:IPools.sol
pragma solidity =0.8.22;
import "../../staking/interfaces/ILiquidity.sol";
import "../../dao/interfaces/IDAO.sol";
import "./IPoolStats.sol";
interface IPools is IPoolStats
{
function startExchangeApproved() external;
function setContracts( IDAO _dao, ILiquidity _liquidity ) external;
function addLiquidity( IERC20 tokenA, IERC20 tokenB, uint256 maxAmountA, uint256 maxAmountB, uint256 minAddedAmountA, uint256 minAddedAmountB, uint256 totalLiquidity ) external returns (uint256 addedAmountA, uint256 addedAmountB, uint256 addedLiquidity);
function removeLiquidity( IERC20 tokenA, IERC20 tokenB, uint256 liquidityToRemove, uint256 minReclaimedA, uint256 minReclaimedB, uint256 totalLiquidity ) external returns (uint256 reclaimedA, uint256 reclaimedB);
function deposit( IERC20 token, uint256 amount ) external;
function withdraw( IERC20 token, uint256 amount ) external;
function swap( IERC20 swapTokenIn, IERC20 swapTokenOut, uint256 swapAmountIn, uint256 minAmountOut, uint256 deadline ) external returns (uint256 swapAmountOut);
function depositSwapWithdraw(IERC20 swapTokenIn, IERC20 swapTokenOut, uint256 swapAmountIn, uint256 minAmountOut, uint256 deadline ) external returns (uint256 swapAmountOut);
function depositDoubleSwapWithdraw( IERC20 swapTokenIn, IERC20 swapTokenMiddle, IERC20 swapTokenOut, uint256 swapAmountIn, uint256 minAmountOut, uint256 deadline ) external returns (uint256 swapAmountOut);
function depositZapSwapWithdraw(IERC20 swapTokenIn, IERC20 swapTokenOut, uint256 swapAmountIn ) external returns (uint256 swapAmountOut);
function exchangeIsLive() external view returns (bool);
function getPoolReserves(IERC20 tokenA, IERC20 tokenB) external view returns (uint256 reserveA, uint256 reserveB);
function depositedUserBalance(address user, IERC20 token) external view returns (uint256);
}
文件 16 的 29:IPoolsConfig.sol
pragma solidity =0.8.22;
import "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import "./IPools.sol";
interface IPoolsConfig
{
function whitelistPool( IERC20 tokenA, IERC20 tokenB ) external;
function unwhitelistPool( IERC20 tokenA, IERC20 tokenB ) external;
function changeMaximumWhitelistedPools(bool increase) external;
function maximumWhitelistedPools() external view returns (uint256);
function numberOfWhitelistedPools() external view returns (uint256);
function isWhitelisted( bytes32 poolID ) external view returns (bool);
function whitelistedPools() external view returns (bytes32[] calldata);
function underlyingTokenPair( bytes32 poolID ) external view returns (IERC20 tokenA, IERC20 tokenB);
function tokenHasBeenWhitelisted( IERC20 token, IERC20 weth, IERC20 usdc ) external view returns (bool);
}
文件 17 的 29:IRewardsEmitter.sol
pragma solidity =0.8.22;
import "../../staking/interfaces/IStakingRewards.sol";
interface IRewardsEmitter
{
function addSALTRewards( AddedReward[] calldata addedRewards ) external;
function performUpkeep( uint256 timeSinceLastUpkeep ) external;
function pendingRewardsForPools( bytes32[] calldata pools ) external view returns (uint256[] calldata);
}
文件 18 的 29:ISalt.sol
pragma solidity =0.8.22;
import "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
interface ISalt is IERC20
{
function burnTokensInContract() external;
function totalBurned() external view returns (uint256);
}
文件 19 的 29:ISaltRewards.sol
pragma solidity =0.8.22;
import "./IRewardsEmitter.sol";
interface ISaltRewards
{
function sendInitialSaltRewards( uint256 liquidityBootstrapAmount, bytes32[] calldata poolIDs ) external;
function performUpkeep( bytes32[] calldata poolIDs, uint256[] calldata profitsForPools ) external;
function stakingRewardsEmitter() external view returns (IRewardsEmitter);
function liquidityRewardsEmitter() external view returns (IRewardsEmitter);
}
文件 20 的 29:IStakingRewards.sol
pragma solidity =0.8.22;
struct AddedReward
{
bytes32 poolID;
uint256 amountToAdd;
}
struct UserShareInfo
{
uint256 userShare;
uint256 virtualRewards;
uint256 cooldownExpiration;
}
interface IStakingRewards
{
function claimAllRewards( bytes32[] calldata poolIDs ) external returns (uint256 rewardsAmount);
function addSALTRewards( AddedReward[] calldata addedRewards ) external;
function totalShares(bytes32 poolID) external view returns (uint256);
function totalSharesForPools( bytes32[] calldata poolIDs ) external view returns (uint256[] calldata shares);
function totalRewardsForPools( bytes32[] calldata poolIDs ) external view returns (uint256[] calldata rewards);
function userRewardForPool( address wallet, bytes32 poolID ) external view returns (uint256);
function userShareForPool( address wallet, bytes32 poolID ) external view returns (uint256);
function userVirtualRewardsForPool( address wallet, bytes32 poolID ) external view returns (uint256);
function userRewardsForPools( address wallet, bytes32[] calldata poolIDs ) external view returns (uint256[] calldata rewards);
function userShareForPools( address wallet, bytes32[] calldata poolIDs ) external view returns (uint256[] calldata shares);
function userCooldowns( address wallet, bytes32[] calldata poolIDs ) external view returns (uint256[] calldata cooldowns);
}
文件 21 的 29:IUpkeep.sol
pragma solidity =0.8.22;
interface IUpkeep
{
function performUpkeep() external;
function currentRewardsForCallingPerformUpkeep() external view returns (uint256);
function lastUpkeepTimeEmissions() external view returns (uint256);
function lastUpkeepTimeRewardsEmitters() external view returns (uint256);
}
文件 22 的 29: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);
}
}
}
文件 23 的 29: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() {
_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);
}
}
文件 24 的 29:PoolStats.sol
pragma solidity =0.8.22;
import "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import "../interfaces/IExchangeConfig.sol";
import "./interfaces/IPoolsConfig.sol";
import "./interfaces/IPoolStats.sol";
import "./PoolUtils.sol";
abstract contract PoolStats is IPoolStats
{
uint64 constant INVALID_POOL_ID = type(uint64).max;
IExchangeConfig immutable public exchangeConfig;
IPoolsConfig immutable public poolsConfig;
IERC20 immutable public _weth;
mapping(bytes32=>uint256) public _arbitrageProfits;
mapping(bytes32=>ArbitrageIndicies) public _arbitrageIndicies;
constructor( IExchangeConfig _exchangeConfig, IPoolsConfig _poolsConfig )
{
exchangeConfig = _exchangeConfig;
poolsConfig = _poolsConfig;
_weth = exchangeConfig.weth();
}
function _updateProfitsFromArbitrage( IERC20 arbToken2, IERC20 arbToken3, uint256 arbitrageProfit ) internal
{
bytes32 poolID = PoolUtils._poolID( arbToken2, arbToken3 );
_arbitrageProfits[poolID] += arbitrageProfit;
}
function clearProfitsForPools() external
{
require(msg.sender == address(exchangeConfig.upkeep()), "PoolStats.clearProfitsForPools is only callable from the Upkeep contract" );
bytes32[] memory poolIDs = poolsConfig.whitelistedPools();
for( uint256 i = 0; i < poolIDs.length; i++ )
_arbitrageProfits[ poolIDs[i] ] = 1;
}
function _poolIndex( IERC20 tokenA, IERC20 tokenB, bytes32[] memory poolIDs ) internal pure returns (uint64 index)
{
bytes32 poolID = PoolUtils._poolID( tokenA, tokenB );
for( uint256 i = 0; i < poolIDs.length; i++ )
{
if (poolID == poolIDs[i])
return uint64(i);
}
return INVALID_POOL_ID;
}
function updateArbitrageIndicies() public
{
bytes32[] memory poolIDs = poolsConfig.whitelistedPools();
for( uint256 i = 0; i < poolIDs.length; i++ )
{
bytes32 poolID = poolIDs[i];
(IERC20 arbToken2, IERC20 arbToken3) = poolsConfig.underlyingTokenPair(poolID);
if ( (arbToken2 != _weth) && (arbToken3 != _weth) )
{
uint64 poolIndex1 = _poolIndex( _weth, arbToken2, poolIDs );
uint64 poolIndex2 = _poolIndex( arbToken2, arbToken3, poolIDs );
uint64 poolIndex3 = _poolIndex( arbToken3, _weth, poolIDs );
ArbitrageIndicies memory indicies = _arbitrageIndicies[poolID];
if ( ( poolIndex1 != indicies.index1 ) || ( poolIndex2 != indicies.index2 ) || ( poolIndex3 != indicies.index3 ) )
_arbitrageIndicies[poolID] = ArbitrageIndicies(poolIndex1, poolIndex2, poolIndex3);
}
}
}
function _calculateArbitrageProfits( bytes32[] memory poolIDs, uint256[] memory _calculatedProfits ) internal view
{
for( uint256 i = 0; i < poolIDs.length; i++ )
{
bytes32 poolID = poolIDs[i];
uint256 arbitrageProfit = _arbitrageProfits[poolID] / 3;
if ( arbitrageProfit > 0 )
{
ArbitrageIndicies memory indicies = _arbitrageIndicies[poolID];
if ( indicies.index1 != INVALID_POOL_ID )
_calculatedProfits[indicies.index1] += arbitrageProfit;
if ( indicies.index2 != INVALID_POOL_ID )
_calculatedProfits[indicies.index2] += arbitrageProfit;
if ( indicies.index3 != INVALID_POOL_ID )
_calculatedProfits[indicies.index3] += arbitrageProfit;
}
}
}
function profitsForWhitelistedPools() external view returns (uint256[] memory _calculatedProfits)
{
bytes32[] memory poolIDs = poolsConfig.whitelistedPools();
_calculatedProfits = new uint256[](poolIDs.length);
_calculateArbitrageProfits( poolIDs, _calculatedProfits );
}
function arbitrageIndicies(bytes32 poolID) external view returns (ArbitrageIndicies memory)
{
return _arbitrageIndicies[poolID];
}
}
文件 25 的 29:PoolUtils.sol
pragma solidity =0.8.22;
import "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
library PoolUtils
{
uint256 constant public DUST = 100;
bytes32 constant public STAKED_SALT = bytes32(0);
function _poolID( IERC20 tokenA, IERC20 tokenB ) internal pure returns (bytes32 poolID)
{
if ( uint160(address(tokenB)) < uint160(address(tokenA)) )
return keccak256(abi.encodePacked(address(tokenB), address(tokenA)));
return keccak256(abi.encodePacked(address(tokenA), address(tokenB)));
}
function _poolIDAndFlipped( IERC20 tokenA, IERC20 tokenB ) internal pure returns (bytes32 poolID, bool flipped)
{
if ( uint160(address(tokenB)) < uint160(address(tokenA)) )
return (keccak256(abi.encodePacked(address(tokenB), address(tokenA))), true);
return (keccak256(abi.encodePacked(address(tokenA), address(tokenB))), false);
}
}
文件 26 的 29:Pools.sol
pragma solidity =0.8.22;
import "openzeppelin-contracts/contracts/security/ReentrancyGuard.sol";
import "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
import "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import "openzeppelin-contracts/contracts/access/Ownable.sol";
import "../interfaces/IExchangeConfig.sol";
import "../arbitrage/ArbitrageSearch.sol";
import "./interfaces/IPoolsConfig.sol";
import "./interfaces/IPools.sol";
import "./PoolStats.sol";
import "./PoolUtils.sol";
contract Pools is IPools, ReentrancyGuard, PoolStats, ArbitrageSearch, Ownable
{
event LiquidityAdded(IERC20 indexed tokenA, IERC20 indexed tokenB, uint256 addedAmountA, uint256 addedAmountB, uint256 addedLiquidity);
event LiquidityRemoved(IERC20 indexed tokenA, IERC20 indexed tokenB, uint256 reclaimedA, uint256 reclaimedB, uint256 removedLiquidity);
event TokenDeposit(address indexed user, IERC20 indexed token, uint256 amount);
event TokenWithdrawal(address indexed user, IERC20 indexed token, uint256 amount);
event SwapAndArbitrage(address indexed user, IERC20 indexed swapTokenIn, IERC20 indexed swapTokenOut, uint256 swapAmountIn, uint256 swapAmountOut, uint256 arbitrageProfit);
using SafeERC20 for IERC20;
struct PoolReserves
{
uint128 reserve0;
uint128 reserve1;
}
IDAO public dao;
ILiquidity public liquidity;
ISalt public salt;
bool public exchangeIsLive;
mapping(bytes32=>PoolReserves) private _poolReserves;
mapping(address=>mapping(IERC20=>uint256)) private _userDeposits;
mapping(address => uint) private lastSwappedBlocks;
constructor( IExchangeConfig _exchangeConfig, IPoolsConfig _poolsConfig )
ArbitrageSearch(_exchangeConfig)
PoolStats(_exchangeConfig, _poolsConfig)
{
salt = _exchangeConfig.salt();
}
modifier oneUserSwapPerBlock()
{
require(lastSwappedBlocks[msg.sender] != block.number, "User already swapped in this block");
_;
lastSwappedBlocks[msg.sender] = block.number;
}
modifier ensureNotExpired(uint256 deadline)
{
require(block.timestamp <= deadline, "TX EXPIRED");
_;
}
function setContracts( IDAO _dao, ILiquidity _liquidity ) external onlyOwner
{
dao = _dao;
liquidity = _liquidity;
renounceOwnership();
}
function startExchangeApproved() external nonReentrant
{
require( msg.sender == address(exchangeConfig.initialDistribution().bootstrapBallot()), "Pools.startExchangeApproved can only be called from the BootstrapBallot contract" );
updateArbitrageIndicies();
exchangeIsLive = true;
}
function _addLiquidity( bytes32 poolID, uint256 maxAmount0, uint256 maxAmount1, uint256 totalLiquidity ) internal returns(uint256 addedAmount0, uint256 addedAmount1, uint256 addedLiquidity)
{
PoolReserves storage reserves = _poolReserves[poolID];
uint256 reserve0 = reserves.reserve0;
uint256 reserve1 = reserves.reserve1;
if ( ( reserve0 == 0 ) || ( reserve1 == 0 ) )
{
reserves.reserve0 += uint128(maxAmount0);
reserves.reserve1 += uint128(maxAmount1);
return ( maxAmount0, maxAmount1, (maxAmount0 + maxAmount1) );
}
uint256 proportionalB = ( maxAmount0 * reserve1 ) / reserve0;
if ( proportionalB > maxAmount1 )
{
addedAmount0 = ( maxAmount1 * reserve0 ) / reserve1;
addedAmount1 = maxAmount1;
}
else
{
addedAmount0 = maxAmount0;
addedAmount1 = proportionalB;
}
require( addedAmount0 > PoolUtils.DUST, "Added liquidity for token 0 less than DUST" );
require( addedAmount1 > PoolUtils.DUST, "Added liquidity for token 1 less than DUST" );
reserves.reserve0 += uint128(addedAmount0);
reserves.reserve1 += uint128(addedAmount1);
addedLiquidity = (totalLiquidity * (addedAmount0+addedAmount1) ) / (reserve0+reserve1);
}
function addLiquidity( IERC20 tokenA, IERC20 tokenB, uint256 maxAmountA, uint256 maxAmountB, uint256 minAddedAmountA, uint256 minAddedAmountB, uint256 totalLiquidity ) external nonReentrant returns (uint256 addedAmountA, uint256 addedAmountB, uint256 addedLiquidity)
{
require( msg.sender == address(liquidity), "Pools.addLiquidity is only callable from the Liquidity contract" );
require( exchangeIsLive, "The exchange is not yet live" );
require( address(tokenA) != address(tokenB), "Cannot add liquidity for duplicate tokens" );
require( maxAmountA > PoolUtils.DUST, "The amount of tokenA to add is too small" );
require( maxAmountB > PoolUtils.DUST, "The amount of tokenB to add is too small" );
(bytes32 poolID, bool flipped) = PoolUtils._poolIDAndFlipped(tokenA, tokenB);
if ( flipped )
(addedAmountB, addedAmountA, addedLiquidity) = _addLiquidity( poolID, maxAmountB, maxAmountA, totalLiquidity );
else
(addedAmountA, addedAmountB, addedLiquidity) = _addLiquidity( poolID, maxAmountA, maxAmountB, totalLiquidity );
require( addedAmountA >= minAddedAmountA, "Insufficient tokenA added to liquidity" );
require( addedAmountB >= minAddedAmountB, "Insufficient tokenB added to liquidity" );
tokenA.safeTransferFrom(msg.sender, address(this), addedAmountA );
tokenB.safeTransferFrom(msg.sender, address(this), addedAmountB );
emit LiquidityAdded(tokenA, tokenB, addedAmountA, addedAmountB, addedLiquidity);
}
function removeLiquidity( IERC20 tokenA, IERC20 tokenB, uint256 liquidityToRemove, uint256 minReclaimedA, uint256 minReclaimedB, uint256 totalLiquidity ) external nonReentrant returns (uint256 reclaimedA, uint256 reclaimedB)
{
require( msg.sender == address(liquidity), "Pools.removeLiquidity is only callable from the Liquidity contract" );
require( liquidityToRemove > 0, "The amount of liquidityToRemove cannot be zero" );
(bytes32 poolID, bool flipped) = PoolUtils._poolIDAndFlipped(tokenA, tokenB);
PoolReserves storage reserves = _poolReserves[poolID];
if (reserves.reserve0 <= reserves.reserve1 )
{
reclaimedA = ( reserves.reserve0 * liquidityToRemove ) / totalLiquidity;
reclaimedB = ( reserves.reserve1 * reclaimedA ) / reserves.reserve0;
}
else
{
reclaimedB = ( reserves.reserve1 * liquidityToRemove ) / totalLiquidity;
reclaimedA = ( reserves.reserve0 * reclaimedB ) / reserves.reserve1;
}
reserves.reserve0 -= uint128(reclaimedA);
reserves.reserve1 -= uint128(reclaimedB);
require((reserves.reserve0 >= PoolUtils.DUST) && (reserves.reserve1 >= PoolUtils.DUST), "Insufficient reserves after liquidity removal");
if (flipped)
(reclaimedA,reclaimedB) = (reclaimedB,reclaimedA);
require( (reclaimedA >= minReclaimedA) && (reclaimedB >= minReclaimedB), "Insufficient underlying tokens returned" );
tokenA.safeTransfer( msg.sender, reclaimedA );
tokenB.safeTransfer( msg.sender, reclaimedB );
emit LiquidityRemoved(tokenA, tokenB, reclaimedA, reclaimedB, liquidityToRemove);
}
function deposit( IERC20 token, uint256 amount ) external nonReentrant
{
require( amount > PoolUtils.DUST, "Deposit amount too small");
_userDeposits[msg.sender][token] += amount;
token.safeTransferFrom(msg.sender, address(this), amount );
emit TokenDeposit(msg.sender, token, amount);
}
function withdraw( IERC20 token, uint256 amount ) external nonReentrant
{
require( _userDeposits[msg.sender][token] >= amount, "Insufficient balance to withdraw specified amount" );
require( amount > PoolUtils.DUST, "Withdraw amount too small");
_userDeposits[msg.sender][token] -= amount;
token.safeTransfer( msg.sender, amount );
emit TokenWithdrawal(msg.sender, token, amount);
}
function _adjustReservesForSwap( PoolReserves storage reserves, bool flipped, uint256 amountIn ) internal returns (uint256 amountOut)
{
uint256 reserve0 = reserves.reserve0;
uint256 reserve1 = reserves.reserve1;
if (flipped)
{
reserve1 += amountIn;
amountOut = reserve0 * amountIn / reserve1;
reserve0 -= amountOut;
}
else
{
reserve0 += amountIn;
amountOut = reserve1 * amountIn / reserve0;
reserve1 -= amountOut;
}
require( (reserve0 > PoolUtils.DUST) && (reserve1 > PoolUtils.DUST), "Insufficient reserves after swap");
require( (reserve0 <= type(uint128).max) && (reserve1 <= type(uint128).max), "Reserves overflow after swap" );
reserves.reserve0 = uint128(reserve0);
reserves.reserve1 = uint128(reserve1);
}
function _arbitrage(uint256 arbitrageAmountIn, PoolReserves storage reservesA, PoolReserves storage reservesB, PoolReserves storage reservesC, bool flippedA, bool flippedB, bool flippedC ) internal returns (uint256 arbitrageProfit)
{
uint256 amountOut = _adjustReservesForSwap( reservesA, flippedA, arbitrageAmountIn );
amountOut = _adjustReservesForSwap( reservesB, flippedB, amountOut );
amountOut = _adjustReservesForSwap( reservesC, flippedC, amountOut );
arbitrageProfit = amountOut - arbitrageAmountIn;
(bytes32 poolID, bool flipped) = PoolUtils._poolIDAndFlipped(weth, salt);
PoolReserves storage reserves = _poolReserves[poolID];
if ( ( reserves.reserve0 > PoolUtils.DUST ) && ( reserves.reserve1 > PoolUtils.DUST ) )
{
uint256 saltOut = _adjustReservesForSwap(reserves, flipped, arbitrageProfit);
_userDeposits[address(dao)][salt] += saltOut;
}
}
function _attemptArbitrage( IERC20 arbToken2, IERC20 arbToken3 ) internal returns (uint256 arbitrageProfit)
{
bytes32 poolID;
bool flippedA;
bool flippedB;
bool flippedC;
PoolReserves storage reservesA;
PoolReserves storage reservesB;
PoolReserves storage reservesC;
uint256 arbitrageAmountIn;
{
(poolID, flippedA) = PoolUtils._poolIDAndFlipped(weth, arbToken2);
reservesA = _poolReserves[poolID];
(uint256 a0, uint256 a1) = (reservesA.reserve0, reservesA.reserve1 );
if (flippedA)
(a0, a1) = (a1, a0);
(poolID, flippedB) = PoolUtils._poolIDAndFlipped(arbToken2, arbToken3);
reservesB = _poolReserves[poolID];
(uint256 b0, uint256 b1) = (reservesB.reserve0, reservesB.reserve1 );
if (flippedB)
(b0, b1) = (b1, b0);
(poolID, flippedC) = PoolUtils._poolIDAndFlipped(arbToken3, weth);
reservesC = _poolReserves[poolID];
(uint256 c0, uint256 c1) = (reservesC.reserve0, reservesC.reserve1 );
if (flippedC)
(c0, c1) = (c1, c0);
if ( a0 > PoolUtils.DUST && a1 > PoolUtils.DUST && b0 > PoolUtils.DUST && b1 > PoolUtils.DUST && c0 > PoolUtils.DUST && c1 > PoolUtils.DUST )
arbitrageAmountIn = _bestArbitrageIn(a0, a1, b0, b1, c0, c1 );
}
if (arbitrageAmountIn > 0)
{
arbitrageProfit = _arbitrage(arbitrageAmountIn, reservesA, reservesB, reservesC, flippedA, flippedB, flippedC);
_updateProfitsFromArbitrage( arbToken2, arbToken3, arbitrageProfit );
}
}
function _adjustReservesForSwapAndAttemptArbitrage( IERC20 swapTokenIn, IERC20 swapTokenOut, uint256 swapAmountIn, uint256 minAmountOut ) internal returns (uint256 swapAmountOut)
{
(bytes32 poolID, bool flipped) = PoolUtils._poolIDAndFlipped(swapTokenIn, swapTokenOut);
PoolReserves storage reserves = _poolReserves[poolID];
require((reserves.reserve0 > PoolUtils.DUST) && (reserves.reserve1 > PoolUtils.DUST), "Insufficient reserves before swap");
swapAmountOut = _adjustReservesForSwap( reserves, flipped, swapAmountIn );
require( swapAmountOut >= minAmountOut, "Insufficient resulting token amount" );
(IERC20 arbToken2, IERC20 arbToken3) = _arbitragePath( swapTokenIn, swapTokenOut );
uint256 arbitrageProfit = _attemptArbitrage( arbToken2, arbToken3 );
emit SwapAndArbitrage(msg.sender, swapTokenIn, swapTokenOut, swapAmountIn, swapAmountOut, arbitrageProfit);
}
function swap( IERC20 swapTokenIn, IERC20 swapTokenOut, uint256 swapAmountIn, uint256 minAmountOut, uint256 deadline ) external oneUserSwapPerBlock nonReentrant ensureNotExpired(deadline) returns (uint256 swapAmountOut)
{
mapping(IERC20=>uint256) storage userDeposits = _userDeposits[msg.sender];
require( userDeposits[swapTokenIn] >= swapAmountIn, "Insufficient deposited token balance of initial token" );
userDeposits[swapTokenIn] -= swapAmountIn;
swapAmountOut = _adjustReservesForSwapAndAttemptArbitrage(swapTokenIn, swapTokenOut, swapAmountIn, minAmountOut );
userDeposits[swapTokenOut] += swapAmountOut;
}
function depositSwapWithdraw(IERC20 swapTokenIn, IERC20 swapTokenOut, uint256 swapAmountIn, uint256 minAmountOut, uint256 deadline ) external oneUserSwapPerBlock nonReentrant ensureNotExpired(deadline) returns (uint256 swapAmountOut)
{
swapTokenIn.safeTransferFrom(msg.sender, address(this), swapAmountIn );
swapAmountOut = _adjustReservesForSwapAndAttemptArbitrage(swapTokenIn, swapTokenOut, swapAmountIn, minAmountOut );
swapTokenOut.safeTransfer( msg.sender, swapAmountOut );
}
function depositZapSwapWithdraw(IERC20 zapSwapTokenIn, IERC20 zapSwapTokenOut, uint256 zapSwapAmountIn ) external returns (uint256 zapSwapAmountOut)
{
require( msg.sender == address(liquidity), "Pools.depositZapSwapWithdraw is only callable from the Liquidity contract" );
zapSwapTokenIn.safeTransferFrom(msg.sender, address(this), zapSwapAmountIn );
(bytes32 poolID, bool flipped) = PoolUtils._poolIDAndFlipped(zapSwapTokenIn, zapSwapTokenOut);
PoolReserves storage reserves = _poolReserves[poolID];
if ( flipped )
require( zapSwapAmountIn < reserves.reserve1 / 100, "Cannot zap more than 1% of the reserves" );
else
require( zapSwapAmountIn < reserves.reserve0 / 100, "Cannot zap more than 1% of the reserves" );
zapSwapAmountOut = _adjustReservesForSwap( reserves, flipped, zapSwapAmountIn );
zapSwapTokenOut.safeTransfer( msg.sender, zapSwapAmountOut );
}
function depositDoubleSwapWithdraw( IERC20 swapTokenIn, IERC20 swapTokenMiddle, IERC20 swapTokenOut, uint256 swapAmountIn, uint256 minAmountOut, uint256 deadline ) external oneUserSwapPerBlock nonReentrant ensureNotExpired(deadline) returns (uint256 swapAmountOut)
{
swapTokenIn.safeTransferFrom(msg.sender, address(this), swapAmountIn );
uint256 middleAmountOut = _adjustReservesForSwapAndAttemptArbitrage(swapTokenIn, swapTokenMiddle, swapAmountIn, 0 );
swapAmountOut = _adjustReservesForSwapAndAttemptArbitrage(swapTokenMiddle, swapTokenOut, middleAmountOut, minAmountOut );
swapTokenOut.safeTransfer( msg.sender, swapAmountOut );
}
function getPoolReserves(IERC20 tokenA, IERC20 tokenB) public view returns (uint256 reserveA, uint256 reserveB)
{
(bytes32 poolID, bool flipped) = PoolUtils._poolIDAndFlipped(tokenA, tokenB);
PoolReserves memory reserves = _poolReserves[poolID];
reserveA = reserves.reserve0;
reserveB = reserves.reserve1;
if (flipped)
(reserveA, reserveB) = (reserveB, reserveA);
}
function depositedUserBalance(address user, IERC20 token) public view returns (uint256)
{
return _userDeposits[user][token];
}
}
文件 27 的 29: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() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
_status = _ENTERED;
}
function _nonReentrantAfter() private {
_status = _NOT_ENTERED;
}
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == _ENTERED;
}
}
文件 28 的 29: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));
}
}
文件 29 的 29:VestingWallet.sol
pragma solidity ^0.8.0;
import "../token/ERC20/utils/SafeERC20.sol";
import "../utils/Address.sol";
import "../utils/Context.sol";
contract VestingWallet is Context {
event EtherReleased(uint256 amount);
event ERC20Released(address indexed token, uint256 amount);
uint256 private _released;
mapping(address => uint256) private _erc20Released;
address private immutable _beneficiary;
uint64 private immutable _start;
uint64 private immutable _duration;
constructor(address beneficiaryAddress, uint64 startTimestamp, uint64 durationSeconds) payable {
require(beneficiaryAddress != address(0), "VestingWallet: beneficiary is zero address");
_beneficiary = beneficiaryAddress;
_start = startTimestamp;
_duration = durationSeconds;
}
receive() external payable virtual {}
function beneficiary() public view virtual returns (address) {
return _beneficiary;
}
function start() public view virtual returns (uint256) {
return _start;
}
function duration() public view virtual returns (uint256) {
return _duration;
}
function released() public view virtual returns (uint256) {
return _released;
}
function released(address token) public view virtual returns (uint256) {
return _erc20Released[token];
}
function releasable() public view virtual returns (uint256) {
return vestedAmount(uint64(block.timestamp)) - released();
}
function releasable(address token) public view virtual returns (uint256) {
return vestedAmount(token, uint64(block.timestamp)) - released(token);
}
function release() public virtual {
uint256 amount = releasable();
_released += amount;
emit EtherReleased(amount);
Address.sendValue(payable(beneficiary()), amount);
}
function release(address token) public virtual {
uint256 amount = releasable(token);
_erc20Released[token] += amount;
emit ERC20Released(token, amount);
SafeERC20.safeTransfer(IERC20(token), beneficiary(), amount);
}
function vestedAmount(uint64 timestamp) public view virtual returns (uint256) {
return _vestingSchedule(address(this).balance + released(), timestamp);
}
function vestedAmount(address token, uint64 timestamp) public view virtual returns (uint256) {
return _vestingSchedule(IERC20(token).balanceOf(address(this)) + released(token), timestamp);
}
function _vestingSchedule(uint256 totalAllocation, uint64 timestamp) internal view virtual returns (uint256) {
if (timestamp < start()) {
return 0;
} else if (timestamp > start() + duration()) {
return totalAllocation;
} else {
return (totalAllocation * (timestamp - start())) / duration();
}
}
}
{
"compilationTarget": {
"src/pools/Pools.sol": "Pools"
},
"evmVersion": "paris",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 10000
},
"remappings": [
":chainlink/=lib/chainlink/",
":ds-test/=lib/openzeppelin-contracts/lib/forge-std/lib/ds-test/src/",
":erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
":forge-std/=lib/openzeppelin-contracts/lib/forge-std/src/",
":openzeppelin-contracts/=lib/openzeppelin-contracts/",
":openzeppelin/=lib/openzeppelin-contracts/contracts/",
":v3-core/=lib/v3-core/contracts/"
]
}
[{"inputs":[{"internalType":"contract IExchangeConfig","name":"_exchangeConfig","type":"address"},{"internalType":"contract IPoolsConfig","name":"_poolsConfig","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract IERC20","name":"tokenA","type":"address"},{"indexed":true,"internalType":"contract IERC20","name":"tokenB","type":"address"},{"indexed":false,"internalType":"uint256","name":"addedAmountA","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"addedAmountB","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"addedLiquidity","type":"uint256"}],"name":"LiquidityAdded","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract IERC20","name":"tokenA","type":"address"},{"indexed":true,"internalType":"contract IERC20","name":"tokenB","type":"address"},{"indexed":false,"internalType":"uint256","name":"reclaimedA","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"reclaimedB","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"removedLiquidity","type":"uint256"}],"name":"LiquidityRemoved","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":"contract IERC20","name":"swapTokenIn","type":"address"},{"indexed":true,"internalType":"contract IERC20","name":"swapTokenOut","type":"address"},{"indexed":false,"internalType":"uint256","name":"swapAmountIn","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"swapAmountOut","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"arbitrageProfit","type":"uint256"}],"name":"SwapAndArbitrage","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":true,"internalType":"contract IERC20","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"TokenDeposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":true,"internalType":"contract IERC20","name":"token","type":"address"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"TokenWithdrawal","type":"event"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"_arbitrageIndicies","outputs":[{"internalType":"uint64","name":"index1","type":"uint64"},{"internalType":"uint64","name":"index2","type":"uint64"},{"internalType":"uint64","name":"index3","type":"uint64"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"_arbitrageProfits","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"_weth","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"tokenA","type":"address"},{"internalType":"contract IERC20","name":"tokenB","type":"address"},{"internalType":"uint256","name":"maxAmountA","type":"uint256"},{"internalType":"uint256","name":"maxAmountB","type":"uint256"},{"internalType":"uint256","name":"minAddedAmountA","type":"uint256"},{"internalType":"uint256","name":"minAddedAmountB","type":"uint256"},{"internalType":"uint256","name":"totalLiquidity","type":"uint256"}],"name":"addLiquidity","outputs":[{"internalType":"uint256","name":"addedAmountA","type":"uint256"},{"internalType":"uint256","name":"addedAmountB","type":"uint256"},{"internalType":"uint256","name":"addedLiquidity","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"poolID","type":"bytes32"}],"name":"arbitrageIndicies","outputs":[{"components":[{"internalType":"uint64","name":"index1","type":"uint64"},{"internalType":"uint64","name":"index2","type":"uint64"},{"internalType":"uint64","name":"index3","type":"uint64"}],"internalType":"struct IPoolStats.ArbitrageIndicies","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"clearProfitsForPools","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"dao","outputs":[{"internalType":"contract IDAO","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"deposit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"swapTokenIn","type":"address"},{"internalType":"contract IERC20","name":"swapTokenMiddle","type":"address"},{"internalType":"contract IERC20","name":"swapTokenOut","type":"address"},{"internalType":"uint256","name":"swapAmountIn","type":"uint256"},{"internalType":"uint256","name":"minAmountOut","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"depositDoubleSwapWithdraw","outputs":[{"internalType":"uint256","name":"swapAmountOut","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"swapTokenIn","type":"address"},{"internalType":"contract IERC20","name":"swapTokenOut","type":"address"},{"internalType":"uint256","name":"swapAmountIn","type":"uint256"},{"internalType":"uint256","name":"minAmountOut","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"depositSwapWithdraw","outputs":[{"internalType":"uint256","name":"swapAmountOut","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"zapSwapTokenIn","type":"address"},{"internalType":"contract IERC20","name":"zapSwapTokenOut","type":"address"},{"internalType":"uint256","name":"zapSwapAmountIn","type":"uint256"}],"name":"depositZapSwapWithdraw","outputs":[{"internalType":"uint256","name":"zapSwapAmountOut","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"user","type":"address"},{"internalType":"contract IERC20","name":"token","type":"address"}],"name":"depositedUserBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"exchangeConfig","outputs":[{"internalType":"contract IExchangeConfig","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"exchangeIsLive","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"tokenA","type":"address"},{"internalType":"contract IERC20","name":"tokenB","type":"address"}],"name":"getPoolReserves","outputs":[{"internalType":"uint256","name":"reserveA","type":"uint256"},{"internalType":"uint256","name":"reserveB","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"liquidity","outputs":[{"internalType":"contract ILiquidity","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"poolsConfig","outputs":[{"internalType":"contract IPoolsConfig","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"profitsForWhitelistedPools","outputs":[{"internalType":"uint256[]","name":"_calculatedProfits","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"tokenA","type":"address"},{"internalType":"contract IERC20","name":"tokenB","type":"address"},{"internalType":"uint256","name":"liquidityToRemove","type":"uint256"},{"internalType":"uint256","name":"minReclaimedA","type":"uint256"},{"internalType":"uint256","name":"minReclaimedB","type":"uint256"},{"internalType":"uint256","name":"totalLiquidity","type":"uint256"}],"name":"removeLiquidity","outputs":[{"internalType":"uint256","name":"reclaimedA","type":"uint256"},{"internalType":"uint256","name":"reclaimedB","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"salt","outputs":[{"internalType":"contract ISalt","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IDAO","name":"_dao","type":"address"},{"internalType":"contract ILiquidity","name":"_liquidity","type":"address"}],"name":"setContracts","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"startExchangeApproved","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"swapTokenIn","type":"address"},{"internalType":"contract IERC20","name":"swapTokenOut","type":"address"},{"internalType":"uint256","name":"swapAmountIn","type":"uint256"},{"internalType":"uint256","name":"minAmountOut","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"}],"name":"swap","outputs":[{"internalType":"uint256","name":"swapAmountOut","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"updateArbitrageIndicies","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"usdc","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"usdt","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"weth","outputs":[{"internalType":"contract IERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"token","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"}],"name":"withdraw","outputs":[],"stateMutability":"nonpayable","type":"function"}]