Address Contract Verified
Address
0x2f67158859Fe0f69f5773570eC60444Fe0c1693c
Balance
0 ETH
Nonce
1
Code Size
7660 bytes
Creator
0xa308DE21...559c at tx 0xb2131e17...c977cc
Indexed Transactions
0
Contract Bytecode
7660 bytes
0x608060405234801561001057600080fd5b50600436106101ae5760003560e01c8063715018a6116100ee5780638596f71211610097578063b95ec5f211610071578063b95ec5f214610389578063c2ee3a08146103a7578063e33fd580146103af578063f2fde38b146103b857600080fd5b80638596f7121461034b5780638d8c3de7146103585780638da5cb5b1461037857600080fd5b80637d7c2a1c116100c85780637d7c2a1c146103285780637dc0d1d0146103305780638486747d1461034357600080fd5b8063715018a6146102f15780637535d246146102f95780637bdab7fa1461032057600080fd5b80632e0f26251161015b578063556f3c4e11610135578063556f3c4e1461029957806367175c45146102bc57806369e96d2d146102c55780636c6d201a146102e857600080fd5b80632e0f262514610243578063313ce5671461024b578063411557d11461025a57600080fd5b80631cb44dfc1161018c5780631cb44dfc1461020a57806324b02e431461021d578063263d48ba1461023057600080fd5b806302c333b3146101b357806302f6be6d146101c857806316332843146101e4575b600080fd5b6101c66101c1366004611932565b6103cb565b005b6101d160055481565b6040519081526020015b60405180910390f35b6101f76101f2366004611954565b61043e565b60405160029190910b81526020016101db565b6101c6610218366004611982565b61049c565b6101c661022b36600461199f565b610587565b6101c661023e366004611954565b61059d565b6101d1601281565b604051601281526020016101db565b6102817f0000000000000000000000002dcaff0f75765d7867887fc402b71c841b3a4bfb81565b6040516001600160a01b0390911681526020016101db565b6002546003546102a7919082565b604080519283526020830191909152016101db565b6101f761bb8081565b6102d86102d3366004611954565b6105aa565b60405190151581526020016101db565b6101d160065481565b6101c66105c9565b6102817f000000000000000000000000898adc9aa0c23dce3fed6456c34dbe2b5778432581565b6102d86105dd565b6101c6610616565b600154610281906001600160a01b031681565b6101d1610869565b6007546102d89060ff1681565b61036b6103663660046119fc565b61091d565b6040516101db9190611a6e565b6000546001600160a01b0316610281565b610393620f424081565b60405162ffffff90911681526020016101db565b6101d1610a00565b6101d160045481565b6101c66103c6366004611982565b610a0f565b6103d3610a88565b6103df6012600a611bb7565b8111156104335760405162461bcd60e51b815260206004820152600b60248201527f496e76616c69645065726300000000000000000000000000000000000000000060448201526064015b60405180910390fd5b600591909155600655565b60008061045b6127106104536012600a611bb7565b859190610ae2565b905061046860c882611bd9565b6104729082611bed565b905060c88110156104845760c8610495565b61049561049082610bd3565b610c6f565b9392505050565b6104a4610a88565b806001600160a01b031663313ce5676040518163ffffffff1660e01b8152600401602060405180830381865afa1580156104e2573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906105069190611c00565b60ff166012146105585760405162461bcd60e51b815260206004820152601260248201527f556e6578706563746564446563696d616c730000000000000000000000000000604482015260640161042a565b6001805473ffffffffffffffffffffffffffffffffffffffff19166001600160a01b0392909216919091179055565b61058f610a88565b805160025560200151600355565b6105a5610a88565b600455565b600081600260000154111580156105c357506003548211155b92915050565b6105d1610a88565b6105db6000610cd7565b565b60006106117f0000000000000000000000002dcaff0f75765d7867887fc402b71c841b3a4bfb6001600160a01b0316610d34565b905090565b60015460408051636664499b60e01b8152815160009384936001600160a01b0390911692636664499b9260048083019392829003018187875af1158015610661573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906106859190611c23565b91509150600081801561069c575061069c836105aa565b60075490915060ff1615158115151415816106be576106b9610e96565b6106c7565b6106c784611004565b80610744577f0000000000000000000000002dcaff0f75765d7867887fc402b71c841b3a4bfb6001600160a01b0316637d7c2a1c6040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561072757600080fd5b505af115801561073b573d6000803e3d6000fd5b50505050610776565b6107767f0000000000000000000000002dcaff0f75765d7867887fc402b71c841b3a4bfb6001600160a01b031661120c565b8161084e576107fb7f000000000000000000000000898adc9aa0c23dce3fed6456c34dbe2b577843256107a7610869565b6107b36012600a611bb7565b6107bd9190611bed565b6107c96012600a611bb7565b6001600160a01b037f0000000000000000000000002dcaff0f75765d7867887fc402b71c841b3a4bfb16929190611383565b61084e6001600160a01b037f0000000000000000000000002dcaff0f75765d7867887fc402b71c841b3a4bfb167f000000000000000000000000898adc9aa0c23dce3fed6456c34dbe2b57784325611687565b8215610863576007805460ff19168315151790555b50505050565b6000807f0000000000000000000000002dcaff0f75765d7867887fc402b71c841b3a4bfb6001600160a01b031663c4a7761e6040518163ffffffff1660e01b81526004016040805180830381865afa1580156108c9573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906108ed9190611c58565b50905061091761090f600554836012600a6109089190611bb7565b9190610ae2565b6006546117df565b91505090565b6060610927610a88565b6000807f0000000000000000000000002dcaff0f75765d7867887fc402b71c841b3a4bfb6001600160a01b03168585604051610964929190611c7c565b6000604051808303816000865af19150503d80600081146109a1576040519150601f19603f3d011682016040523d82523d6000602084013e6109a6565b606091505b5091509150816109f85760405162461bcd60e51b815260206004820152601460248201527f5661756c74457865637574696f6e4661696c6564000000000000000000000000604482015260640161042a565b949350505050565b610a0c6012600a611bb7565b81565b610a17610a88565b6001600160a01b038116610a7c5760405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b606482015260840161042a565b610a8581610cd7565b50565b6000546001600160a01b031633146105db5760405162461bcd60e51b815260206004820181905260248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e6572604482015260640161042a565b6000808060001985870985870292508281108382030391505080600003610b1c57838281610b1257610b12611bc3565b0492505050610495565b808411610b6b5760405162461bcd60e51b815260206004820152601560248201527f4d6174683a206d756c446976206f766572666c6f770000000000000000000000604482015260640161042a565b600084868809600260036001881981018916988990049182028318808302840302808302840302808302840302808302840302808302840302918202909203026000889003889004909101858311909403939093029303949094049190911702949350505050565b60007f7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff821115610c6b5760405162461bcd60e51b815260206004820152602860248201527f53616665436173743a2076616c756520646f65736e27742066697420696e206160448201527f6e20696e74323536000000000000000000000000000000000000000000000000606482015260840161042a565b5090565b80600281900b8114610cd25760405162461bcd60e51b815260206004820152602660248201527f53616665436173743a2076616c756520646f65736e27742066697420696e203260448201526534206269747360d01b606482015260840161042a565b919050565b600080546001600160a01b0383811673ffffffffffffffffffffffffffffffffffffffff19831681178455604051919092169283917f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e09190a35050565b600080826001600160a01b0316635d752a9a6040518163ffffffff1660e01b8152600401602060405180830381865afa158015610d75573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610d999190611c8c565b90506000836001600160a01b03166351e87af76040518163ffffffff1660e01b8152600401602060405180830381865afa158015610ddb573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610dff9190611c8c565b90506000846001600160a01b0316630f35bcac6040518163ffffffff1660e01b8152600401602060405180830381865afa158015610e41573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610e659190611c8c565b905060006002610e758385611caf565b610e7f9190611cd4565b90508060020b8460020b1215945050505050919050565b6040516304237efb60e51b8152620f424060048201527f0000000000000000000000002dcaff0f75765d7867887fc402b71c841b3a4bfb6001600160a01b03169063846fdf6090602401600060405180830381600087803b158015610efa57600080fd5b505af1158015610f0e573d6000803e3d6000fd5b505060405163a632935560e01b815261bb8060048201527f0000000000000000000000002dcaff0f75765d7867887fc402b71c841b3a4bfb6001600160a01b0316925063a63293559150602401600060405180830381600087803b158015610f7557600080fd5b505af1158015610f89573d6000803e3d6000fd5b505060405163015ef28960e71b815261bb8060048201527f0000000000000000000000002dcaff0f75765d7867887fc402b71c841b3a4bfb6001600160a01b0316925063af7944809150602401600060405180830381600087803b158015610ff057600080fd5b505af1158015610863573d6000803e3d6000fd5b7f0000000000000000000000002dcaff0f75765d7867887fc402b71c841b3a4bfb6001600160a01b031663846fdf6061105d611058611041610869565b61104d6012600a611bb7565b620f42409190610ae2565b6117f5565b6040516001600160e01b031960e084901b16815262ffffff9091166004820152602401600060405180830381600087803b15801561109a57600080fd5b505af11580156110ae573d6000803e3d6000fd5b505050507f0000000000000000000000002dcaff0f75765d7867887fc402b71c841b3a4bfb6001600160a01b031663a63293556110ec60045461043e565b6040516001600160e01b031960e084901b16815260029190910b6004820152602401600060405180830381600087803b15801561112857600080fd5b505af115801561113c573d6000803e3d6000fd5b505050507f0000000000000000000000002dcaff0f75765d7867887fc402b71c841b3a4bfb6001600160a01b031663af7944806111b561117a6105dd565b6111a3576002546101f29061118f9086611bed565b600260045461119e9190611d0e565b611859565b6003546101f29061118f908690611bed565b6040516001600160e01b031960e084901b16815260029190910b6004820152602401600060405180830381600087803b1580156111f157600080fd5b505af1158015611205573d6000803e3d6000fd5b5050505050565b6000816001600160a01b031663ef78d4fd6040518163ffffffff1660e01b8152600401602060405180830381865afa15801561124c573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906112709190611d22565b60405163171d85a760e31b8152600060048201529091506001600160a01b0383169063b8ec2d3890602401600060405180830381600087803b1580156112b557600080fd5b505af11580156112c9573d6000803e3d6000fd5b50505050816001600160a01b0316637d7c2a1c6040518163ffffffff1660e01b8152600401600060405180830381600087803b15801561130857600080fd5b505af115801561131c573d6000803e3d6000fd5b505060405163171d85a760e31b815263ffffffff841660048201526001600160a01b038516925063b8ec2d389150602401600060405180830381600087803b15801561136757600080fd5b505af115801561137b573d6000803e3d6000fd5b505050505050565b8115610863576000846001600160a01b031663ebaf2d3b6040518163ffffffff1660e01b8152600401602060405180830381865afa1580156113c9573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906113ed9190611c8c565b90506000856001600160a01b0316636c3958216040518163ffffffff1660e01b8152600401602060405180830381865afa15801561142f573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906114539190611c8c565b90506000866001600160a01b031663fa0827436040518163ffffffff1660e01b8152600401602060405180830381865afa158015611495573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906114b99190611c8c565b90506000876001600160a01b031663888a91346040518163ffffffff1660e01b8152600401602060405180830381865afa1580156114fb573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061151f9190611c8c565b9050600061152f89898787611868565b9050600061153f8a8a8686611868565b90506000611557836001600160801b03168a8a610ae2565b9050600061156f836001600160801b03168b8b610ae2565b90506001600160801b038216156115f55760405163abbffcb960e01b8152600289810b600483015288900b60248201526001600160801b03831660448201526001600160a01b038d169063abbffcb990606401600060405180830381600087803b1580156115dc57600080fd5b505af11580156115f0573d6000803e3d6000fd5b505050505b6001600160801b038116156116795760405163abbffcb960e01b8152600287810b600483015286900b60248201526001600160801b03821660448201526001600160a01b038d169063abbffcb990606401600060405180830381600087803b15801561166057600080fd5b505af1158015611674573d6000803e3d6000fd5b505050505b505050505050505050505050565b6000826001600160a01b03166351e87af76040518163ffffffff1660e01b8152600401602060405180830381865afa1580156116c7573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906116eb9190611c8c565b90506000836001600160a01b0316630f35bcac6040518163ffffffff1660e01b8152600401602060405180830381865afa15801561172d573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906117519190611c8c565b9050600061176185858585611868565b60405163abbffcb960e01b8152600285810b600483015284900b60248201526001600160801b03821660448201529091506001600160a01b0386169063abbffcb990606401600060405180830381600087803b1580156117c057600080fd5b505af11580156117d4573d6000803e3d6000fd5b505050505050505050565b60008183106117ee5781610495565b5090919050565b600062ffffff821115610c6b5760405162461bcd60e51b815260206004820152602660248201527f53616665436173743a2076616c756520646f65736e27742066697420696e203260448201526534206269747360d01b606482015260840161042a565b60008183116117ee5781610495565b6040516bffffffffffffffffffffffff19606086901b16602082015260e883811b603483015282901b60378201526000908190603a0160408051601f1981840301815290829052805160209091012063514ea4bf60e01b82526004820181905291506000906001600160a01b0387169063514ea4bf9060240160a060405180830381865afa1580156118fe573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906119229190611d5f565b50929a9950505050505050505050565b6000806040838503121561194557600080fd5b50508035926020909101359150565b60006020828403121561196657600080fd5b5035919050565b6001600160a01b0381168114610a8557600080fd5b60006020828403121561199457600080fd5b81356104958161196d565b6000604082840312156119b157600080fd5b6040516040810181811067ffffffffffffffff821117156119e257634e487b7160e01b600052604160045260246000fd5b604052823581526020928301359281019290925250919050565b60008060208385031215611a0f57600080fd5b823567ffffffffffffffff80821115611a2757600080fd5b818501915085601f830112611a3b57600080fd5b813581811115611a4a57600080fd5b866020828501011115611a5c57600080fd5b60209290920196919550909350505050565b60006020808352835180602085015260005b81811015611a9c57858101830151858201604001528201611a80565b506000604082860101526040601f19601f8301168501019250505092915050565b634e487b7160e01b600052601160045260246000fd5b600181815b80851115611b0e578160001904821115611af457611af4611abd565b80851615611b0157918102915b93841c9390800290611ad8565b509250929050565b600082611b25575060016105c3565b81611b32575060006105c3565b8160018114611b485760028114611b5257611b6e565b60019150506105c3565b60ff841115611b6357611b63611abd565b50506001821b6105c3565b5060208310610133831016604e8410600b8410161715611b91575081810a6105c3565b611b9b8383611ad3565b8060001904821115611baf57611baf611abd565b029392505050565b60006104958383611b16565b634e487b7160e01b600052601260045260246000fd5b600082611be857611be8611bc3565b500690565b818103818111156105c3576105c3611abd565b600060208284031215611c1257600080fd5b815160ff8116811461049557600080fd5b60008060408385031215611c3657600080fd5b8251915060208301518015158114611c4d57600080fd5b809150509250929050565b60008060408385031215611c6b57600080fd5b505080516020909101519092909150565b8183823760009101908152919050565b600060208284031215611c9e57600080fd5b81518060020b811461049557600080fd5b600281810b9083900b01627fffff8113627fffff19821217156105c3576105c3611abd565b60008160020b8360020b80611ceb57611ceb611bc3565b627fffff19821460001982141615611d0557611d05611abd565b90059392505050565b600082611d1d57611d1d611bc3565b500490565b600060208284031215611d3457600080fd5b815163ffffffff8116811461049557600080fd5b80516001600160801b0381168114610cd257600080fd5b600080600080600060a08688031215611d7757600080fd5b611d8086611d48565b94506020860151935060408601519250611d9c60608701611d48565b9150611daa60808701611d48565b9050929550929590935056fea2646970667358221220df2b56aca8b3fe777bd28bcba3b45c81b81a287fcd6769fcfda25888e77deca364736f6c63430008180033
Verified Source Code Full Match
Compiler: v0.8.24+commit.e11b9ed9
EVM: paris
Optimization: Yes (750 runs)
Ownable.sol 83 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (access/Ownable.sol)
pragma solidity ^0.8.0;
import "../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor() {
_transferOwnership(_msgSender());
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
Context.sol 28 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.4) (utils/Context.sol)
pragma solidity ^0.8.0;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
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;
}
}
Math.sol 339 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.9.0) (utils/math/Math.sol)
pragma solidity ^0.8.0;
/**
* @dev Standard math utilities missing in the Solidity language.
*/
library Math {
enum Rounding {
Down, // Toward negative infinity
Up, // Toward infinity
Zero // Toward zero
}
/**
* @dev Returns the largest of two numbers.
*/
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a > b ? a : b;
}
/**
* @dev Returns the smallest of two numbers.
*/
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two numbers. The result is rounded towards
* zero.
*/
function average(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b) / 2 can overflow.
return (a & b) + (a ^ b) / 2;
}
/**
* @dev Returns the ceiling of the division of two numbers.
*
* This differs from standard division with `/` in that it rounds up instead
* of rounding down.
*/
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
// (a + b - 1) / b can overflow on addition, so we distribute.
return a == 0 ? 0 : (a - 1) / b + 1;
}
/**
* @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
* @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
* with further edits by Uniswap Labs also under MIT license.
*/
function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
unchecked {
// 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
// use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
// variables such that product = prod1 * 2^256 + prod0.
uint256 prod0; // Least significant 256 bits of the product
uint256 prod1; // Most significant 256 bits of the product
assembly {
let mm := mulmod(x, y, not(0))
prod0 := mul(x, y)
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
}
// Handle non-overflow cases, 256 by 256 division.
if (prod1 == 0) {
// Solidity will revert if denominator == 0, unlike the div opcode on its own.
// The surrounding unchecked block does not change this fact.
// See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
return prod0 / denominator;
}
// Make sure the result is less than 2^256. Also prevents denominator == 0.
require(denominator > prod1, "Math: mulDiv overflow");
///////////////////////////////////////////////
// 512 by 256 division.
///////////////////////////////////////////////
// Make division exact by subtracting the remainder from [prod1 prod0].
uint256 remainder;
assembly {
// Compute remainder using mulmod.
remainder := mulmod(x, y, denominator)
// Subtract 256 bit number from 512 bit number.
prod1 := sub(prod1, gt(remainder, prod0))
prod0 := sub(prod0, remainder)
}
// Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
// See https://cs.stackexchange.com/q/138556/92363.
// Does not overflow because the denominator cannot be zero at this stage in the function.
uint256 twos = denominator & (~denominator + 1);
assembly {
// Divide denominator by twos.
denominator := div(denominator, twos)
// Divide [prod1 prod0] by twos.
prod0 := div(prod0, twos)
// Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
twos := add(div(sub(0, twos), twos), 1)
}
// Shift in bits from prod1 into prod0.
prod0 |= prod1 * twos;
// Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
// that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
// four bits. That is, denominator * inv = 1 mod 2^4.
uint256 inverse = (3 * denominator) ^ 2;
// Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works
// in modular arithmetic, doubling the correct bits in each step.
inverse *= 2 - denominator * inverse; // inverse mod 2^8
inverse *= 2 - denominator * inverse; // inverse mod 2^16
inverse *= 2 - denominator * inverse; // inverse mod 2^32
inverse *= 2 - denominator * inverse; // inverse mod 2^64
inverse *= 2 - denominator * inverse; // inverse mod 2^128
inverse *= 2 - denominator * inverse; // inverse mod 2^256
// Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
// This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
// less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
// is no longer required.
result = prod0 * inverse;
return result;
}
}
/**
* @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
*/
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;
}
/**
* @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded down.
*
* Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
*/
function sqrt(uint256 a) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
// For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.
//
// We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have
// `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.
//
// This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`
// → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`
// → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`
//
// Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.
uint256 result = 1 << (log2(a) >> 1);
// At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,
// since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at
// every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision
// into the expected uint128 result.
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);
}
}
/**
* @notice Calculates sqrt(a), following the selected rounding direction.
*/
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);
}
}
/**
* @dev Return the log in base 2, rounded down, of a positive value.
* Returns 0 if given 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;
}
/**
* @dev Return the log in base 2, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
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);
}
}
/**
* @dev Return the log in base 10, rounded down, of a positive value.
* Returns 0 if given 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;
}
/**
* @dev Return the log in base 10, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
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);
}
}
/**
* @dev Return the log in base 256, rounded down, of a positive value.
* Returns 0 if given 0.
*
* Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
*/
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;
}
/**
* @dev Return the log in base 256, following the selected rounding direction, of a positive value.
* Returns 0 if given 0.
*/
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);
}
}
}
SafeCast.sol 1136 lines
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.8.0) (utils/math/SafeCast.sol)
// This file was procedurally generated from scripts/generate/templates/SafeCast.js.
pragma solidity ^0.8.0;
/**
* @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow
* checks.
*
* Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
* easily result in undesired exploitation or bugs, since developers usually
* assume that overflows raise errors. `SafeCast` restores this intuition by
* reverting the transaction when such an operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*
* Can be combined with {SafeMath} and {SignedSafeMath} to extend it to smaller types, by performing
* all math on `uint256` and `int256` and then downcasting.
*/
library SafeCast {
/**
* @dev Returns the downcasted uint248 from uint256, reverting on
* overflow (when the input is greater than largest uint248).
*
* Counterpart to Solidity's `uint248` operator.
*
* Requirements:
*
* - input must fit into 248 bits
*
* _Available since v4.7._
*/
function toUint248(uint256 value) internal pure returns (uint248) {
require(value <= type(uint248).max, "SafeCast: value doesn't fit in 248 bits");
return uint248(value);
}
/**
* @dev Returns the downcasted uint240 from uint256, reverting on
* overflow (when the input is greater than largest uint240).
*
* Counterpart to Solidity's `uint240` operator.
*
* Requirements:
*
* - input must fit into 240 bits
*
* _Available since v4.7._
*/
function toUint240(uint256 value) internal pure returns (uint240) {
require(value <= type(uint240).max, "SafeCast: value doesn't fit in 240 bits");
return uint240(value);
}
/**
* @dev Returns the downcasted uint232 from uint256, reverting on
* overflow (when the input is greater than largest uint232).
*
* Counterpart to Solidity's `uint232` operator.
*
* Requirements:
*
* - input must fit into 232 bits
*
* _Available since v4.7._
*/
function toUint232(uint256 value) internal pure returns (uint232) {
require(value <= type(uint232).max, "SafeCast: value doesn't fit in 232 bits");
return uint232(value);
}
/**
* @dev Returns the downcasted uint224 from uint256, reverting on
* overflow (when the input is greater than largest uint224).
*
* Counterpart to Solidity's `uint224` operator.
*
* Requirements:
*
* - input must fit into 224 bits
*
* _Available since v4.2._
*/
function toUint224(uint256 value) internal pure returns (uint224) {
require(value <= type(uint224).max, "SafeCast: value doesn't fit in 224 bits");
return uint224(value);
}
/**
* @dev Returns the downcasted uint216 from uint256, reverting on
* overflow (when the input is greater than largest uint216).
*
* Counterpart to Solidity's `uint216` operator.
*
* Requirements:
*
* - input must fit into 216 bits
*
* _Available since v4.7._
*/
function toUint216(uint256 value) internal pure returns (uint216) {
require(value <= type(uint216).max, "SafeCast: value doesn't fit in 216 bits");
return uint216(value);
}
/**
* @dev Returns the downcasted uint208 from uint256, reverting on
* overflow (when the input is greater than largest uint208).
*
* Counterpart to Solidity's `uint208` operator.
*
* Requirements:
*
* - input must fit into 208 bits
*
* _Available since v4.7._
*/
function toUint208(uint256 value) internal pure returns (uint208) {
require(value <= type(uint208).max, "SafeCast: value doesn't fit in 208 bits");
return uint208(value);
}
/**
* @dev Returns the downcasted uint200 from uint256, reverting on
* overflow (when the input is greater than largest uint200).
*
* Counterpart to Solidity's `uint200` operator.
*
* Requirements:
*
* - input must fit into 200 bits
*
* _Available since v4.7._
*/
function toUint200(uint256 value) internal pure returns (uint200) {
require(value <= type(uint200).max, "SafeCast: value doesn't fit in 200 bits");
return uint200(value);
}
/**
* @dev Returns the downcasted uint192 from uint256, reverting on
* overflow (when the input is greater than largest uint192).
*
* Counterpart to Solidity's `uint192` operator.
*
* Requirements:
*
* - input must fit into 192 bits
*
* _Available since v4.7._
*/
function toUint192(uint256 value) internal pure returns (uint192) {
require(value <= type(uint192).max, "SafeCast: value doesn't fit in 192 bits");
return uint192(value);
}
/**
* @dev Returns the downcasted uint184 from uint256, reverting on
* overflow (when the input is greater than largest uint184).
*
* Counterpart to Solidity's `uint184` operator.
*
* Requirements:
*
* - input must fit into 184 bits
*
* _Available since v4.7._
*/
function toUint184(uint256 value) internal pure returns (uint184) {
require(value <= type(uint184).max, "SafeCast: value doesn't fit in 184 bits");
return uint184(value);
}
/**
* @dev Returns the downcasted uint176 from uint256, reverting on
* overflow (when the input is greater than largest uint176).
*
* Counterpart to Solidity's `uint176` operator.
*
* Requirements:
*
* - input must fit into 176 bits
*
* _Available since v4.7._
*/
function toUint176(uint256 value) internal pure returns (uint176) {
require(value <= type(uint176).max, "SafeCast: value doesn't fit in 176 bits");
return uint176(value);
}
/**
* @dev Returns the downcasted uint168 from uint256, reverting on
* overflow (when the input is greater than largest uint168).
*
* Counterpart to Solidity's `uint168` operator.
*
* Requirements:
*
* - input must fit into 168 bits
*
* _Available since v4.7._
*/
function toUint168(uint256 value) internal pure returns (uint168) {
require(value <= type(uint168).max, "SafeCast: value doesn't fit in 168 bits");
return uint168(value);
}
/**
* @dev Returns the downcasted uint160 from uint256, reverting on
* overflow (when the input is greater than largest uint160).
*
* Counterpart to Solidity's `uint160` operator.
*
* Requirements:
*
* - input must fit into 160 bits
*
* _Available since v4.7._
*/
function toUint160(uint256 value) internal pure returns (uint160) {
require(value <= type(uint160).max, "SafeCast: value doesn't fit in 160 bits");
return uint160(value);
}
/**
* @dev Returns the downcasted uint152 from uint256, reverting on
* overflow (when the input is greater than largest uint152).
*
* Counterpart to Solidity's `uint152` operator.
*
* Requirements:
*
* - input must fit into 152 bits
*
* _Available since v4.7._
*/
function toUint152(uint256 value) internal pure returns (uint152) {
require(value <= type(uint152).max, "SafeCast: value doesn't fit in 152 bits");
return uint152(value);
}
/**
* @dev Returns the downcasted uint144 from uint256, reverting on
* overflow (when the input is greater than largest uint144).
*
* Counterpart to Solidity's `uint144` operator.
*
* Requirements:
*
* - input must fit into 144 bits
*
* _Available since v4.7._
*/
function toUint144(uint256 value) internal pure returns (uint144) {
require(value <= type(uint144).max, "SafeCast: value doesn't fit in 144 bits");
return uint144(value);
}
/**
* @dev Returns the downcasted uint136 from uint256, reverting on
* overflow (when the input is greater than largest uint136).
*
* Counterpart to Solidity's `uint136` operator.
*
* Requirements:
*
* - input must fit into 136 bits
*
* _Available since v4.7._
*/
function toUint136(uint256 value) internal pure returns (uint136) {
require(value <= type(uint136).max, "SafeCast: value doesn't fit in 136 bits");
return uint136(value);
}
/**
* @dev Returns the downcasted uint128 from uint256, reverting on
* overflow (when the input is greater than largest uint128).
*
* Counterpart to Solidity's `uint128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*
* _Available since v2.5._
*/
function toUint128(uint256 value) internal pure returns (uint128) {
require(value <= type(uint128).max, "SafeCast: value doesn't fit in 128 bits");
return uint128(value);
}
/**
* @dev Returns the downcasted uint120 from uint256, reverting on
* overflow (when the input is greater than largest uint120).
*
* Counterpart to Solidity's `uint120` operator.
*
* Requirements:
*
* - input must fit into 120 bits
*
* _Available since v4.7._
*/
function toUint120(uint256 value) internal pure returns (uint120) {
require(value <= type(uint120).max, "SafeCast: value doesn't fit in 120 bits");
return uint120(value);
}
/**
* @dev Returns the downcasted uint112 from uint256, reverting on
* overflow (when the input is greater than largest uint112).
*
* Counterpart to Solidity's `uint112` operator.
*
* Requirements:
*
* - input must fit into 112 bits
*
* _Available since v4.7._
*/
function toUint112(uint256 value) internal pure returns (uint112) {
require(value <= type(uint112).max, "SafeCast: value doesn't fit in 112 bits");
return uint112(value);
}
/**
* @dev Returns the downcasted uint104 from uint256, reverting on
* overflow (when the input is greater than largest uint104).
*
* Counterpart to Solidity's `uint104` operator.
*
* Requirements:
*
* - input must fit into 104 bits
*
* _Available since v4.7._
*/
function toUint104(uint256 value) internal pure returns (uint104) {
require(value <= type(uint104).max, "SafeCast: value doesn't fit in 104 bits");
return uint104(value);
}
/**
* @dev Returns the downcasted uint96 from uint256, reverting on
* overflow (when the input is greater than largest uint96).
*
* Counterpart to Solidity's `uint96` operator.
*
* Requirements:
*
* - input must fit into 96 bits
*
* _Available since v4.2._
*/
function toUint96(uint256 value) internal pure returns (uint96) {
require(value <= type(uint96).max, "SafeCast: value doesn't fit in 96 bits");
return uint96(value);
}
/**
* @dev Returns the downcasted uint88 from uint256, reverting on
* overflow (when the input is greater than largest uint88).
*
* Counterpart to Solidity's `uint88` operator.
*
* Requirements:
*
* - input must fit into 88 bits
*
* _Available since v4.7._
*/
function toUint88(uint256 value) internal pure returns (uint88) {
require(value <= type(uint88).max, "SafeCast: value doesn't fit in 88 bits");
return uint88(value);
}
/**
* @dev Returns the downcasted uint80 from uint256, reverting on
* overflow (when the input is greater than largest uint80).
*
* Counterpart to Solidity's `uint80` operator.
*
* Requirements:
*
* - input must fit into 80 bits
*
* _Available since v4.7._
*/
function toUint80(uint256 value) internal pure returns (uint80) {
require(value <= type(uint80).max, "SafeCast: value doesn't fit in 80 bits");
return uint80(value);
}
/**
* @dev Returns the downcasted uint72 from uint256, reverting on
* overflow (when the input is greater than largest uint72).
*
* Counterpart to Solidity's `uint72` operator.
*
* Requirements:
*
* - input must fit into 72 bits
*
* _Available since v4.7._
*/
function toUint72(uint256 value) internal pure returns (uint72) {
require(value <= type(uint72).max, "SafeCast: value doesn't fit in 72 bits");
return uint72(value);
}
/**
* @dev Returns the downcasted uint64 from uint256, reverting on
* overflow (when the input is greater than largest uint64).
*
* Counterpart to Solidity's `uint64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*
* _Available since v2.5._
*/
function toUint64(uint256 value) internal pure returns (uint64) {
require(value <= type(uint64).max, "SafeCast: value doesn't fit in 64 bits");
return uint64(value);
}
/**
* @dev Returns the downcasted uint56 from uint256, reverting on
* overflow (when the input is greater than largest uint56).
*
* Counterpart to Solidity's `uint56` operator.
*
* Requirements:
*
* - input must fit into 56 bits
*
* _Available since v4.7._
*/
function toUint56(uint256 value) internal pure returns (uint56) {
require(value <= type(uint56).max, "SafeCast: value doesn't fit in 56 bits");
return uint56(value);
}
/**
* @dev Returns the downcasted uint48 from uint256, reverting on
* overflow (when the input is greater than largest uint48).
*
* Counterpart to Solidity's `uint48` operator.
*
* Requirements:
*
* - input must fit into 48 bits
*
* _Available since v4.7._
*/
function toUint48(uint256 value) internal pure returns (uint48) {
require(value <= type(uint48).max, "SafeCast: value doesn't fit in 48 bits");
return uint48(value);
}
/**
* @dev Returns the downcasted uint40 from uint256, reverting on
* overflow (when the input is greater than largest uint40).
*
* Counterpart to Solidity's `uint40` operator.
*
* Requirements:
*
* - input must fit into 40 bits
*
* _Available since v4.7._
*/
function toUint40(uint256 value) internal pure returns (uint40) {
require(value <= type(uint40).max, "SafeCast: value doesn't fit in 40 bits");
return uint40(value);
}
/**
* @dev Returns the downcasted uint32 from uint256, reverting on
* overflow (when the input is greater than largest uint32).
*
* Counterpart to Solidity's `uint32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*
* _Available since v2.5._
*/
function toUint32(uint256 value) internal pure returns (uint32) {
require(value <= type(uint32).max, "SafeCast: value doesn't fit in 32 bits");
return uint32(value);
}
/**
* @dev Returns the downcasted uint24 from uint256, reverting on
* overflow (when the input is greater than largest uint24).
*
* Counterpart to Solidity's `uint24` operator.
*
* Requirements:
*
* - input must fit into 24 bits
*
* _Available since v4.7._
*/
function toUint24(uint256 value) internal pure returns (uint24) {
require(value <= type(uint24).max, "SafeCast: value doesn't fit in 24 bits");
return uint24(value);
}
/**
* @dev Returns the downcasted uint16 from uint256, reverting on
* overflow (when the input is greater than largest uint16).
*
* Counterpart to Solidity's `uint16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*
* _Available since v2.5._
*/
function toUint16(uint256 value) internal pure returns (uint16) {
require(value <= type(uint16).max, "SafeCast: value doesn't fit in 16 bits");
return uint16(value);
}
/**
* @dev Returns the downcasted uint8 from uint256, reverting on
* overflow (when the input is greater than largest uint8).
*
* Counterpart to Solidity's `uint8` operator.
*
* Requirements:
*
* - input must fit into 8 bits
*
* _Available since v2.5._
*/
function toUint8(uint256 value) internal pure returns (uint8) {
require(value <= type(uint8).max, "SafeCast: value doesn't fit in 8 bits");
return uint8(value);
}
/**
* @dev Converts a signed int256 into an unsigned uint256.
*
* Requirements:
*
* - input must be greater than or equal to 0.
*
* _Available since v3.0._
*/
function toUint256(int256 value) internal pure returns (uint256) {
require(value >= 0, "SafeCast: value must be positive");
return uint256(value);
}
/**
* @dev Returns the downcasted int248 from int256, reverting on
* overflow (when the input is less than smallest int248 or
* greater than largest int248).
*
* Counterpart to Solidity's `int248` operator.
*
* Requirements:
*
* - input must fit into 248 bits
*
* _Available since v4.7._
*/
function toInt248(int256 value) internal pure returns (int248 downcasted) {
downcasted = int248(value);
require(downcasted == value, "SafeCast: value doesn't fit in 248 bits");
}
/**
* @dev Returns the downcasted int240 from int256, reverting on
* overflow (when the input is less than smallest int240 or
* greater than largest int240).
*
* Counterpart to Solidity's `int240` operator.
*
* Requirements:
*
* - input must fit into 240 bits
*
* _Available since v4.7._
*/
function toInt240(int256 value) internal pure returns (int240 downcasted) {
downcasted = int240(value);
require(downcasted == value, "SafeCast: value doesn't fit in 240 bits");
}
/**
* @dev Returns the downcasted int232 from int256, reverting on
* overflow (when the input is less than smallest int232 or
* greater than largest int232).
*
* Counterpart to Solidity's `int232` operator.
*
* Requirements:
*
* - input must fit into 232 bits
*
* _Available since v4.7._
*/
function toInt232(int256 value) internal pure returns (int232 downcasted) {
downcasted = int232(value);
require(downcasted == value, "SafeCast: value doesn't fit in 232 bits");
}
/**
* @dev Returns the downcasted int224 from int256, reverting on
* overflow (when the input is less than smallest int224 or
* greater than largest int224).
*
* Counterpart to Solidity's `int224` operator.
*
* Requirements:
*
* - input must fit into 224 bits
*
* _Available since v4.7._
*/
function toInt224(int256 value) internal pure returns (int224 downcasted) {
downcasted = int224(value);
require(downcasted == value, "SafeCast: value doesn't fit in 224 bits");
}
/**
* @dev Returns the downcasted int216 from int256, reverting on
* overflow (when the input is less than smallest int216 or
* greater than largest int216).
*
* Counterpart to Solidity's `int216` operator.
*
* Requirements:
*
* - input must fit into 216 bits
*
* _Available since v4.7._
*/
function toInt216(int256 value) internal pure returns (int216 downcasted) {
downcasted = int216(value);
require(downcasted == value, "SafeCast: value doesn't fit in 216 bits");
}
/**
* @dev Returns the downcasted int208 from int256, reverting on
* overflow (when the input is less than smallest int208 or
* greater than largest int208).
*
* Counterpart to Solidity's `int208` operator.
*
* Requirements:
*
* - input must fit into 208 bits
*
* _Available since v4.7._
*/
function toInt208(int256 value) internal pure returns (int208 downcasted) {
downcasted = int208(value);
require(downcasted == value, "SafeCast: value doesn't fit in 208 bits");
}
/**
* @dev Returns the downcasted int200 from int256, reverting on
* overflow (when the input is less than smallest int200 or
* greater than largest int200).
*
* Counterpart to Solidity's `int200` operator.
*
* Requirements:
*
* - input must fit into 200 bits
*
* _Available since v4.7._
*/
function toInt200(int256 value) internal pure returns (int200 downcasted) {
downcasted = int200(value);
require(downcasted == value, "SafeCast: value doesn't fit in 200 bits");
}
/**
* @dev Returns the downcasted int192 from int256, reverting on
* overflow (when the input is less than smallest int192 or
* greater than largest int192).
*
* Counterpart to Solidity's `int192` operator.
*
* Requirements:
*
* - input must fit into 192 bits
*
* _Available since v4.7._
*/
function toInt192(int256 value) internal pure returns (int192 downcasted) {
downcasted = int192(value);
require(downcasted == value, "SafeCast: value doesn't fit in 192 bits");
}
/**
* @dev Returns the downcasted int184 from int256, reverting on
* overflow (when the input is less than smallest int184 or
* greater than largest int184).
*
* Counterpart to Solidity's `int184` operator.
*
* Requirements:
*
* - input must fit into 184 bits
*
* _Available since v4.7._
*/
function toInt184(int256 value) internal pure returns (int184 downcasted) {
downcasted = int184(value);
require(downcasted == value, "SafeCast: value doesn't fit in 184 bits");
}
/**
* @dev Returns the downcasted int176 from int256, reverting on
* overflow (when the input is less than smallest int176 or
* greater than largest int176).
*
* Counterpart to Solidity's `int176` operator.
*
* Requirements:
*
* - input must fit into 176 bits
*
* _Available since v4.7._
*/
function toInt176(int256 value) internal pure returns (int176 downcasted) {
downcasted = int176(value);
require(downcasted == value, "SafeCast: value doesn't fit in 176 bits");
}
/**
* @dev Returns the downcasted int168 from int256, reverting on
* overflow (when the input is less than smallest int168 or
* greater than largest int168).
*
* Counterpart to Solidity's `int168` operator.
*
* Requirements:
*
* - input must fit into 168 bits
*
* _Available since v4.7._
*/
function toInt168(int256 value) internal pure returns (int168 downcasted) {
downcasted = int168(value);
require(downcasted == value, "SafeCast: value doesn't fit in 168 bits");
}
/**
* @dev Returns the downcasted int160 from int256, reverting on
* overflow (when the input is less than smallest int160 or
* greater than largest int160).
*
* Counterpart to Solidity's `int160` operator.
*
* Requirements:
*
* - input must fit into 160 bits
*
* _Available since v4.7._
*/
function toInt160(int256 value) internal pure returns (int160 downcasted) {
downcasted = int160(value);
require(downcasted == value, "SafeCast: value doesn't fit in 160 bits");
}
/**
* @dev Returns the downcasted int152 from int256, reverting on
* overflow (when the input is less than smallest int152 or
* greater than largest int152).
*
* Counterpart to Solidity's `int152` operator.
*
* Requirements:
*
* - input must fit into 152 bits
*
* _Available since v4.7._
*/
function toInt152(int256 value) internal pure returns (int152 downcasted) {
downcasted = int152(value);
require(downcasted == value, "SafeCast: value doesn't fit in 152 bits");
}
/**
* @dev Returns the downcasted int144 from int256, reverting on
* overflow (when the input is less than smallest int144 or
* greater than largest int144).
*
* Counterpart to Solidity's `int144` operator.
*
* Requirements:
*
* - input must fit into 144 bits
*
* _Available since v4.7._
*/
function toInt144(int256 value) internal pure returns (int144 downcasted) {
downcasted = int144(value);
require(downcasted == value, "SafeCast: value doesn't fit in 144 bits");
}
/**
* @dev Returns the downcasted int136 from int256, reverting on
* overflow (when the input is less than smallest int136 or
* greater than largest int136).
*
* Counterpart to Solidity's `int136` operator.
*
* Requirements:
*
* - input must fit into 136 bits
*
* _Available since v4.7._
*/
function toInt136(int256 value) internal pure returns (int136 downcasted) {
downcasted = int136(value);
require(downcasted == value, "SafeCast: value doesn't fit in 136 bits");
}
/**
* @dev Returns the downcasted int128 from int256, reverting on
* overflow (when the input is less than smallest int128 or
* greater than largest int128).
*
* Counterpart to Solidity's `int128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*
* _Available since v3.1._
*/
function toInt128(int256 value) internal pure returns (int128 downcasted) {
downcasted = int128(value);
require(downcasted == value, "SafeCast: value doesn't fit in 128 bits");
}
/**
* @dev Returns the downcasted int120 from int256, reverting on
* overflow (when the input is less than smallest int120 or
* greater than largest int120).
*
* Counterpart to Solidity's `int120` operator.
*
* Requirements:
*
* - input must fit into 120 bits
*
* _Available since v4.7._
*/
function toInt120(int256 value) internal pure returns (int120 downcasted) {
downcasted = int120(value);
require(downcasted == value, "SafeCast: value doesn't fit in 120 bits");
}
/**
* @dev Returns the downcasted int112 from int256, reverting on
* overflow (when the input is less than smallest int112 or
* greater than largest int112).
*
* Counterpart to Solidity's `int112` operator.
*
* Requirements:
*
* - input must fit into 112 bits
*
* _Available since v4.7._
*/
function toInt112(int256 value) internal pure returns (int112 downcasted) {
downcasted = int112(value);
require(downcasted == value, "SafeCast: value doesn't fit in 112 bits");
}
/**
* @dev Returns the downcasted int104 from int256, reverting on
* overflow (when the input is less than smallest int104 or
* greater than largest int104).
*
* Counterpart to Solidity's `int104` operator.
*
* Requirements:
*
* - input must fit into 104 bits
*
* _Available since v4.7._
*/
function toInt104(int256 value) internal pure returns (int104 downcasted) {
downcasted = int104(value);
require(downcasted == value, "SafeCast: value doesn't fit in 104 bits");
}
/**
* @dev Returns the downcasted int96 from int256, reverting on
* overflow (when the input is less than smallest int96 or
* greater than largest int96).
*
* Counterpart to Solidity's `int96` operator.
*
* Requirements:
*
* - input must fit into 96 bits
*
* _Available since v4.7._
*/
function toInt96(int256 value) internal pure returns (int96 downcasted) {
downcasted = int96(value);
require(downcasted == value, "SafeCast: value doesn't fit in 96 bits");
}
/**
* @dev Returns the downcasted int88 from int256, reverting on
* overflow (when the input is less than smallest int88 or
* greater than largest int88).
*
* Counterpart to Solidity's `int88` operator.
*
* Requirements:
*
* - input must fit into 88 bits
*
* _Available since v4.7._
*/
function toInt88(int256 value) internal pure returns (int88 downcasted) {
downcasted = int88(value);
require(downcasted == value, "SafeCast: value doesn't fit in 88 bits");
}
/**
* @dev Returns the downcasted int80 from int256, reverting on
* overflow (when the input is less than smallest int80 or
* greater than largest int80).
*
* Counterpart to Solidity's `int80` operator.
*
* Requirements:
*
* - input must fit into 80 bits
*
* _Available since v4.7._
*/
function toInt80(int256 value) internal pure returns (int80 downcasted) {
downcasted = int80(value);
require(downcasted == value, "SafeCast: value doesn't fit in 80 bits");
}
/**
* @dev Returns the downcasted int72 from int256, reverting on
* overflow (when the input is less than smallest int72 or
* greater than largest int72).
*
* Counterpart to Solidity's `int72` operator.
*
* Requirements:
*
* - input must fit into 72 bits
*
* _Available since v4.7._
*/
function toInt72(int256 value) internal pure returns (int72 downcasted) {
downcasted = int72(value);
require(downcasted == value, "SafeCast: value doesn't fit in 72 bits");
}
/**
* @dev Returns the downcasted int64 from int256, reverting on
* overflow (when the input is less than smallest int64 or
* greater than largest int64).
*
* Counterpart to Solidity's `int64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*
* _Available since v3.1._
*/
function toInt64(int256 value) internal pure returns (int64 downcasted) {
downcasted = int64(value);
require(downcasted == value, "SafeCast: value doesn't fit in 64 bits");
}
/**
* @dev Returns the downcasted int56 from int256, reverting on
* overflow (when the input is less than smallest int56 or
* greater than largest int56).
*
* Counterpart to Solidity's `int56` operator.
*
* Requirements:
*
* - input must fit into 56 bits
*
* _Available since v4.7._
*/
function toInt56(int256 value) internal pure returns (int56 downcasted) {
downcasted = int56(value);
require(downcasted == value, "SafeCast: value doesn't fit in 56 bits");
}
/**
* @dev Returns the downcasted int48 from int256, reverting on
* overflow (when the input is less than smallest int48 or
* greater than largest int48).
*
* Counterpart to Solidity's `int48` operator.
*
* Requirements:
*
* - input must fit into 48 bits
*
* _Available since v4.7._
*/
function toInt48(int256 value) internal pure returns (int48 downcasted) {
downcasted = int48(value);
require(downcasted == value, "SafeCast: value doesn't fit in 48 bits");
}
/**
* @dev Returns the downcasted int40 from int256, reverting on
* overflow (when the input is less than smallest int40 or
* greater than largest int40).
*
* Counterpart to Solidity's `int40` operator.
*
* Requirements:
*
* - input must fit into 40 bits
*
* _Available since v4.7._
*/
function toInt40(int256 value) internal pure returns (int40 downcasted) {
downcasted = int40(value);
require(downcasted == value, "SafeCast: value doesn't fit in 40 bits");
}
/**
* @dev Returns the downcasted int32 from int256, reverting on
* overflow (when the input is less than smallest int32 or
* greater than largest int32).
*
* Counterpart to Solidity's `int32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*
* _Available since v3.1._
*/
function toInt32(int256 value) internal pure returns (int32 downcasted) {
downcasted = int32(value);
require(downcasted == value, "SafeCast: value doesn't fit in 32 bits");
}
/**
* @dev Returns the downcasted int24 from int256, reverting on
* overflow (when the input is less than smallest int24 or
* greater than largest int24).
*
* Counterpart to Solidity's `int24` operator.
*
* Requirements:
*
* - input must fit into 24 bits
*
* _Available since v4.7._
*/
function toInt24(int256 value) internal pure returns (int24 downcasted) {
downcasted = int24(value);
require(downcasted == value, "SafeCast: value doesn't fit in 24 bits");
}
/**
* @dev Returns the downcasted int16 from int256, reverting on
* overflow (when the input is less than smallest int16 or
* greater than largest int16).
*
* Counterpart to Solidity's `int16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*
* _Available since v3.1._
*/
function toInt16(int256 value) internal pure returns (int16 downcasted) {
downcasted = int16(value);
require(downcasted == value, "SafeCast: value doesn't fit in 16 bits");
}
/**
* @dev Returns the downcasted int8 from int256, reverting on
* overflow (when the input is less than smallest int8 or
* greater than largest int8).
*
* Counterpart to Solidity's `int8` operator.
*
* Requirements:
*
* - input must fit into 8 bits
*
* _Available since v3.1._
*/
function toInt8(int256 value) internal pure returns (int8 downcasted) {
downcasted = int8(value);
require(downcasted == value, "SafeCast: value doesn't fit in 8 bits");
}
/**
* @dev Converts an unsigned uint256 into a signed int256.
*
* Requirements:
*
* - input must be less than or equal to maxInt256.
*
* _Available since v3.0._
*/
function toInt256(uint256 value) internal pure returns (int256) {
// Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive
require(value <= uint256(type(int256).max), "SafeCast: value doesn't fit in an int256");
return int256(value);
}
}
IUniswapV3Pool.sol 24 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
import './pool/IUniswapV3PoolImmutables.sol';
import './pool/IUniswapV3PoolState.sol';
import './pool/IUniswapV3PoolDerivedState.sol';
import './pool/IUniswapV3PoolActions.sol';
import './pool/IUniswapV3PoolOwnerActions.sol';
import './pool/IUniswapV3PoolEvents.sol';
/// @title The interface for a Uniswap V3 Pool
/// @notice A Uniswap pool facilitates swapping and automated market making between any two assets that strictly conform
/// to the ERC20 specification
/// @dev The pool interface is broken up into many smaller pieces
interface IUniswapV3Pool is
IUniswapV3PoolImmutables,
IUniswapV3PoolState,
IUniswapV3PoolDerivedState,
IUniswapV3PoolActions,
IUniswapV3PoolOwnerActions,
IUniswapV3PoolEvents
{
}
IUniswapV3PoolActions.sol 103 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Permissionless pool actions
/// @notice Contains pool methods that can be called by anyone
interface IUniswapV3PoolActions {
/// @notice Sets the initial price for the pool
/// @dev Price is represented as a sqrt(amountToken1/amountToken0) Q64.96 value
/// @param sqrtPriceX96 the initial sqrt price of the pool as a Q64.96
function initialize(uint160 sqrtPriceX96) external;
/// @notice Adds liquidity for the given recipient/tickLower/tickUpper position
/// @dev The caller of this method receives a callback in the form of IUniswapV3MintCallback#uniswapV3MintCallback
/// in which they must pay any token0 or token1 owed for the liquidity. The amount of token0/token1 due depends
/// on tickLower, tickUpper, the amount of liquidity, and the current price.
/// @param recipient The address for which the liquidity will be created
/// @param tickLower The lower tick of the position in which to add liquidity
/// @param tickUpper The upper tick of the position in which to add liquidity
/// @param amount The amount of liquidity to mint
/// @param data Any data that should be passed through to the callback
/// @return amount0 The amount of token0 that was paid to mint the given amount of liquidity. Matches the value in the callback
/// @return amount1 The amount of token1 that was paid to mint the given amount of liquidity. Matches the value in the callback
function mint(
address recipient,
int24 tickLower,
int24 tickUpper,
uint128 amount,
bytes calldata data
) external returns (uint256 amount0, uint256 amount1);
/// @notice Collects tokens owed to a position
/// @dev Does not recompute fees earned, which must be done either via mint or burn of any amount of liquidity.
/// Collect must be called by the position owner. To withdraw only token0 or only token1, amount0Requested or
/// amount1Requested may be set to zero. To withdraw all tokens owed, caller may pass any value greater than the
/// actual tokens owed, e.g. type(uint128).max. Tokens owed may be from accumulated swap fees or burned liquidity.
/// @param recipient The address which should receive the fees collected
/// @param tickLower The lower tick of the position for which to collect fees
/// @param tickUpper The upper tick of the position for which to collect fees
/// @param amount0Requested How much token0 should be withdrawn from the fees owed
/// @param amount1Requested How much token1 should be withdrawn from the fees owed
/// @return amount0 The amount of fees collected in token0
/// @return amount1 The amount of fees collected in token1
function collect(
address recipient,
int24 tickLower,
int24 tickUpper,
uint128 amount0Requested,
uint128 amount1Requested
) external returns (uint128 amount0, uint128 amount1);
/// @notice Burn liquidity from the sender and account tokens owed for the liquidity to the position
/// @dev Can be used to trigger a recalculation of fees owed to a position by calling with an amount of 0
/// @dev Fees must be collected separately via a call to #collect
/// @param tickLower The lower tick of the position for which to burn liquidity
/// @param tickUpper The upper tick of the position for which to burn liquidity
/// @param amount How much liquidity to burn
/// @return amount0 The amount of token0 sent to the recipient
/// @return amount1 The amount of token1 sent to the recipient
function burn(
int24 tickLower,
int24 tickUpper,
uint128 amount
) external returns (uint256 amount0, uint256 amount1);
/// @notice Swap token0 for token1, or token1 for token0
/// @dev The caller of this method receives a callback in the form of IUniswapV3SwapCallback#uniswapV3SwapCallback
/// @param recipient The address to receive the output of the swap
/// @param zeroForOne The direction of the swap, true for token0 to token1, false for token1 to token0
/// @param amountSpecified The amount of the swap, which implicitly configures the swap as exact input (positive), or exact output (negative)
/// @param sqrtPriceLimitX96 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this
/// value after the swap. If one for zero, the price cannot be greater than this value after the swap
/// @param data Any data to be passed through to the callback
/// @return amount0 The delta of the balance of token0 of the pool, exact when negative, minimum when positive
/// @return amount1 The delta of the balance of token1 of the pool, exact when negative, minimum when positive
function swap(
address recipient,
bool zeroForOne,
int256 amountSpecified,
uint160 sqrtPriceLimitX96,
bytes calldata data
) external returns (int256 amount0, int256 amount1);
/// @notice Receive token0 and/or token1 and pay it back, plus a fee, in the callback
/// @dev The caller of this method receives a callback in the form of IUniswapV3FlashCallback#uniswapV3FlashCallback
/// @dev Can be used to donate underlying tokens pro-rata to currently in-range liquidity providers by calling
/// with 0 amount{0,1} and sending the donation amount(s) from the callback
/// @param recipient The address which will receive the token0 and token1 amounts
/// @param amount0 The amount of token0 to send
/// @param amount1 The amount of token1 to send
/// @param data Any data to be passed through to the callback
function flash(
address recipient,
uint256 amount0,
uint256 amount1,
bytes calldata data
) external;
/// @notice Increase the maximum number of price and liquidity observations that this pool will store
/// @dev This method is no-op if the pool already has an observationCardinalityNext greater than or equal to
/// the input observationCardinalityNext.
/// @param observationCardinalityNext The desired minimum number of observations for the pool to store
function increaseObservationCardinalityNext(uint16 observationCardinalityNext) external;
}
IUniswapV3PoolDerivedState.sol 40 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Pool state that is not stored
/// @notice Contains view functions to provide information about the pool that is computed rather than stored on the
/// blockchain. The functions here may have variable gas costs.
interface IUniswapV3PoolDerivedState {
/// @notice Returns the cumulative tick and liquidity as of each timestamp `secondsAgo` from the current block timestamp
/// @dev To get a time weighted average tick or liquidity-in-range, you must call this with two values, one representing
/// the beginning of the period and another for the end of the period. E.g., to get the last hour time-weighted average tick,
/// you must call it with secondsAgos = [3600, 0].
/// @dev The time weighted average tick represents the geometric time weighted average price of the pool, in
/// log base sqrt(1.0001) of token1 / token0. The TickMath library can be used to go from a tick value to a ratio.
/// @param secondsAgos From how long ago each cumulative tick and liquidity value should be returned
/// @return tickCumulatives Cumulative tick values as of each `secondsAgos` from the current block timestamp
/// @return secondsPerLiquidityCumulativeX128s Cumulative seconds per liquidity-in-range value as of each `secondsAgos` from the current block
/// timestamp
function observe(uint32[] calldata secondsAgos)
external
view
returns (int56[] memory tickCumulatives, uint160[] memory secondsPerLiquidityCumulativeX128s);
/// @notice Returns a snapshot of the tick cumulative, seconds per liquidity and seconds inside a tick range
/// @dev Snapshots must only be compared to other snapshots, taken over a period for which a position existed.
/// I.e., snapshots cannot be compared if a position is not held for the entire period between when the first
/// snapshot is taken and the second snapshot is taken.
/// @param tickLower The lower tick of the range
/// @param tickUpper The upper tick of the range
/// @return tickCumulativeInside The snapshot of the tick accumulator for the range
/// @return secondsPerLiquidityInsideX128 The snapshot of seconds per liquidity for the range
/// @return secondsInside The snapshot of seconds per liquidity for the range
function snapshotCumulativesInside(int24 tickLower, int24 tickUpper)
external
view
returns (
int56 tickCumulativeInside,
uint160 secondsPerLiquidityInsideX128,
uint32 secondsInside
);
}
IUniswapV3PoolEvents.sol 121 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Events emitted by a pool
/// @notice Contains all events emitted by the pool
interface IUniswapV3PoolEvents {
/// @notice Emitted exactly once by a pool when #initialize is first called on the pool
/// @dev Mint/Burn/Swap cannot be emitted by the pool before Initialize
/// @param sqrtPriceX96 The initial sqrt price of the pool, as a Q64.96
/// @param tick The initial tick of the pool, i.e. log base 1.0001 of the starting price of the pool
event Initialize(uint160 sqrtPriceX96, int24 tick);
/// @notice Emitted when liquidity is minted for a given position
/// @param sender The address that minted the liquidity
/// @param owner The owner of the position and recipient of any minted liquidity
/// @param tickLower The lower tick of the position
/// @param tickUpper The upper tick of the position
/// @param amount The amount of liquidity minted to the position range
/// @param amount0 How much token0 was required for the minted liquidity
/// @param amount1 How much token1 was required for the minted liquidity
event Mint(
address sender,
address indexed owner,
int24 indexed tickLower,
int24 indexed tickUpper,
uint128 amount,
uint256 amount0,
uint256 amount1
);
/// @notice Emitted when fees are collected by the owner of a position
/// @dev Collect events may be emitted with zero amount0 and amount1 when the caller chooses not to collect fees
/// @param owner The owner of the position for which fees are collected
/// @param tickLower The lower tick of the position
/// @param tickUpper The upper tick of the position
/// @param amount0 The amount of token0 fees collected
/// @param amount1 The amount of token1 fees collected
event Collect(
address indexed owner,
address recipient,
int24 indexed tickLower,
int24 indexed tickUpper,
uint128 amount0,
uint128 amount1
);
/// @notice Emitted when a position's liquidity is removed
/// @dev Does not withdraw any fees earned by the liquidity position, which must be withdrawn via #collect
/// @param owner The owner of the position for which liquidity is removed
/// @param tickLower The lower tick of the position
/// @param tickUpper The upper tick of the position
/// @param amount The amount of liquidity to remove
/// @param amount0 The amount of token0 withdrawn
/// @param amount1 The amount of token1 withdrawn
event Burn(
address indexed owner,
int24 indexed tickLower,
int24 indexed tickUpper,
uint128 amount,
uint256 amount0,
uint256 amount1
);
/// @notice Emitted by the pool for any swaps between token0 and token1
/// @param sender The address that initiated the swap call, and that received the callback
/// @param recipient The address that received the output of the swap
/// @param amount0 The delta of the token0 balance of the pool
/// @param amount1 The delta of the token1 balance of the pool
/// @param sqrtPriceX96 The sqrt(price) of the pool after the swap, as a Q64.96
/// @param liquidity The liquidity of the pool after the swap
/// @param tick The log base 1.0001 of price of the pool after the swap
event Swap(
address indexed sender,
address indexed recipient,
int256 amount0,
int256 amount1,
uint160 sqrtPriceX96,
uint128 liquidity,
int24 tick
);
/// @notice Emitted by the pool for any flashes of token0/token1
/// @param sender The address that initiated the swap call, and that received the callback
/// @param recipient The address that received the tokens from flash
/// @param amount0 The amount of token0 that was flashed
/// @param amount1 The amount of token1 that was flashed
/// @param paid0 The amount of token0 paid for the flash, which can exceed the amount0 plus the fee
/// @param paid1 The amount of token1 paid for the flash, which can exceed the amount1 plus the fee
event Flash(
address indexed sender,
address indexed recipient,
uint256 amount0,
uint256 amount1,
uint256 paid0,
uint256 paid1
);
/// @notice Emitted by the pool for increases to the number of observations that can be stored
/// @dev observationCardinalityNext is not the observation cardinality until an observation is written at the index
/// just before a mint/swap/burn.
/// @param observationCardinalityNextOld The previous value of the next observation cardinality
/// @param observationCardinalityNextNew The updated value of the next observation cardinality
event IncreaseObservationCardinalityNext(
uint16 observationCardinalityNextOld,
uint16 observationCardinalityNextNew
);
/// @notice Emitted when the protocol fee is changed by the pool
/// @param feeProtocol0Old The previous value of the token0 protocol fee
/// @param feeProtocol1Old The previous value of the token1 protocol fee
/// @param feeProtocol0New The updated value of the token0 protocol fee
/// @param feeProtocol1New The updated value of the token1 protocol fee
event SetFeeProtocol(uint8 feeProtocol0Old, uint8 feeProtocol1Old, uint8 feeProtocol0New, uint8 feeProtocol1New);
/// @notice Emitted when the collected protocol fees are withdrawn by the factory owner
/// @param sender The address that collects the protocol fees
/// @param recipient The address that receives the collected protocol fees
/// @param amount0 The amount of token0 protocol fees that is withdrawn
/// @param amount0 The amount of token1 protocol fees that is withdrawn
event CollectProtocol(address indexed sender, address indexed recipient, uint128 amount0, uint128 amount1);
}
IUniswapV3PoolImmutables.sol 35 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Pool state that never changes
/// @notice These parameters are fixed for a pool forever, i.e., the methods will always return the same values
interface IUniswapV3PoolImmutables {
/// @notice The contract that deployed the pool, which must adhere to the IUniswapV3Factory interface
/// @return The contract address
function factory() external view returns (address);
/// @notice The first of the two tokens of the pool, sorted by address
/// @return The token contract address
function token0() external view returns (address);
/// @notice The second of the two tokens of the pool, sorted by address
/// @return The token contract address
function token1() external view returns (address);
/// @notice The pool's fee in hundredths of a bip, i.e. 1e-6
/// @return The fee
function fee() external view returns (uint24);
/// @notice The pool tick spacing
/// @dev Ticks can only be used at multiples of this value, minimum of 1 and always positive
/// e.g.: a tickSpacing of 3 means ticks can be initialized every 3rd tick, i.e., ..., -6, -3, 0, 3, 6, ...
/// This value is an int24 to avoid casting even though it is always positive.
/// @return The tick spacing
function tickSpacing() external view returns (int24);
/// @notice The maximum amount of position liquidity that can use any tick in the range
/// @dev This parameter is enforced per tick to prevent liquidity from overflowing a uint128 at any point, and
/// also prevents out-of-range liquidity from being used to prevent adding in-range liquidity to a pool
/// @return The max amount of liquidity per tick
function maxLiquidityPerTick() external view returns (uint128);
}
IUniswapV3PoolOwnerActions.sol 23 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Permissioned pool actions
/// @notice Contains pool methods that may only be called by the factory owner
interface IUniswapV3PoolOwnerActions {
/// @notice Set the denominator of the protocol's % share of the fees
/// @param feeProtocol0 new protocol fee for token0 of the pool
/// @param feeProtocol1 new protocol fee for token1 of the pool
function setFeeProtocol(uint8 feeProtocol0, uint8 feeProtocol1) external;
/// @notice Collect the protocol fee accrued to the pool
/// @param recipient The address to which collected protocol fees should be sent
/// @param amount0Requested The maximum amount of token0 to send, can be 0 to collect fees in only token1
/// @param amount1Requested The maximum amount of token1 to send, can be 0 to collect fees in only token0
/// @return amount0 The protocol fee collected in token0
/// @return amount1 The protocol fee collected in token1
function collectProtocol(
address recipient,
uint128 amount0Requested,
uint128 amount1Requested
) external returns (uint128 amount0, uint128 amount1);
}
IUniswapV3PoolState.sol 116 lines
// SPDX-License-Identifier: GPL-2.0-or-later
pragma solidity >=0.5.0;
/// @title Pool state that can change
/// @notice These methods compose the pool's state, and can change with any frequency including multiple times
/// per transaction
interface IUniswapV3PoolState {
/// @notice The 0th storage slot in the pool stores many values, and is exposed as a single method to save gas
/// when accessed externally.
/// @return sqrtPriceX96 The current price of the pool as a sqrt(token1/token0) Q64.96 value
/// tick The current tick of the pool, i.e. according to the last tick transition that was run.
/// This value may not always be equal to SqrtTickMath.getTickAtSqrtRatio(sqrtPriceX96) if the price is on a tick
/// boundary.
/// observationIndex The index of the last oracle observation that was written,
/// observationCardinality The current maximum number of observations stored in the pool,
/// observationCardinalityNext The next maximum number of observations, to be updated when the observation.
/// feeProtocol The protocol fee for both tokens of the pool.
/// Encoded as two 4 bit values, where the protocol fee of token1 is shifted 4 bits and the protocol fee of token0
/// is the lower 4 bits. Used as the denominator of a fraction of the swap fee, e.g. 4 means 1/4th of the swap fee.
/// unlocked Whether the pool is currently locked to reentrancy
function slot0()
external
view
returns (
uint160 sqrtPriceX96,
int24 tick,
uint16 observationIndex,
uint16 observationCardinality,
uint16 observationCardinalityNext,
uint8 feeProtocol,
bool unlocked
);
/// @notice The fee growth as a Q128.128 fees of token0 collected per unit of liquidity for the entire life of the pool
/// @dev This value can overflow the uint256
function feeGrowthGlobal0X128() external view returns (uint256);
/// @notice The fee growth as a Q128.128 fees of token1 collected per unit of liquidity for the entire life of the pool
/// @dev This value can overflow the uint256
function feeGrowthGlobal1X128() external view returns (uint256);
/// @notice The amounts of token0 and token1 that are owed to the protocol
/// @dev Protocol fees will never exceed uint128 max in either token
function protocolFees() external view returns (uint128 token0, uint128 token1);
/// @notice The currently in range liquidity available to the pool
/// @dev This value has no relationship to the total liquidity across all ticks
function liquidity() external view returns (uint128);
/// @notice Look up information about a specific tick in the pool
/// @param tick The tick to look up
/// @return liquidityGross the total amount of position liquidity that uses the pool either as tick lower or
/// tick upper,
/// liquidityNet how much liquidity changes when the pool price crosses the tick,
/// feeGrowthOutside0X128 the fee growth on the other side of the tick from the current tick in token0,
/// feeGrowthOutside1X128 the fee growth on the other side of the tick from the current tick in token1,
/// tickCumulativeOutside the cumulative tick value on the other side of the tick from the current tick
/// secondsPerLiquidityOutsideX128 the seconds spent per liquidity on the other side of the tick from the current tick,
/// secondsOutside the seconds spent on the other side of the tick from the current tick,
/// initialized Set to true if the tick is initialized, i.e. liquidityGross is greater than 0, otherwise equal to false.
/// Outside values can only be used if the tick is initialized, i.e. if liquidityGross is greater than 0.
/// In addition, these values are only relative and must be used only in comparison to previous snapshots for
/// a specific position.
function ticks(int24 tick)
external
view
returns (
uint128 liquidityGross,
int128 liquidityNet,
uint256 feeGrowthOutside0X128,
uint256 feeGrowthOutside1X128,
int56 tickCumulativeOutside,
uint160 secondsPerLiquidityOutsideX128,
uint32 secondsOutside,
bool initialized
);
/// @notice Returns 256 packed tick initialized boolean values. See TickBitmap for more information
function tickBitmap(int16 wordPosition) external view returns (uint256);
/// @notice Returns the information about a position by the position's key
/// @param key The position's key is a hash of a preimage composed by the owner, tickLower and tickUpper
/// @return _liquidity The amount of liquidity in the position,
/// Returns feeGrowthInside0LastX128 fee growth of token0 inside the tick range as of the last mint/burn/poke,
/// Returns feeGrowthInside1LastX128 fee growth of token1 inside the tick range as of the last mint/burn/poke,
/// Returns tokensOwed0 the computed amount of token0 owed to the position as of the last mint/burn/poke,
/// Returns tokensOwed1 the computed amount of token1 owed to the position as of the last mint/burn/poke
function positions(bytes32 key)
external
view
returns (
uint128 _liquidity,
uint256 feeGrowthInside0LastX128,
uint256 feeGrowthInside1LastX128,
uint128 tokensOwed0,
uint128 tokensOwed1
);
/// @notice Returns data about a specific observation index
/// @param index The element of the observations array to fetch
/// @dev You most likely want to use #observe() instead of this method to get an observation as of some amount of time
/// ago, rather than at a specific index in the array.
/// @return blockTimestamp The timestamp of the observation,
/// Returns tickCumulative the tick multiplied by seconds elapsed for the life of the pool as of the observation timestamp,
/// Returns secondsPerLiquidityCumulativeX128 the seconds per in range liquidity for the life of the pool as of the observation timestamp,
/// Returns initialized whether the observation has been initialized and the values are safe to use
function observations(uint256 index)
external
view
returns (
uint32 blockTimestamp,
int56 tickCumulative,
uint160 secondsPerLiquidityCumulativeX128,
bool initialized
);
}
IAlphaProVault.sol 124 lines
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.24;
import { IUniswapV3Pool } from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
/// @notice Interface for the Charm's AlphaProVault
interface IAlphaProVault {
function deposit(
uint256,
uint256,
uint256,
uint256,
address
) external returns (uint256, uint256, uint256);
function withdraw(
uint256,
uint256,
uint256,
address
) external returns (uint256, uint256);
function getTotalAmounts() external view returns (uint256, uint256);
function getBalance0() external view returns (uint256);
function getBalance1() external view returns (uint256);
function rebalance() external;
function checkCanRebalance() external view;
// manager functions
function sweep(address token, uint256 amount, address to) external;
function setBaseThreshold(int24 _baseThreshold) external;
function setLimitThreshold(int24 _limitThreshold) external;
function setFullRangeWeight(uint24 _fullRangeWeight) external;
function setPeriod(uint32 _period) external;
function setMinTickMove(int24 _minTickMove) external;
function setMaxTwapDeviation(int24 _maxTwapDeviation) external;
function setTwapDuration(uint32 _twapDuration) external;
function setMaxTotalSupply(uint256 _maxTotalSupply) external;
function emergencyBurn(int24 tickLower, int24 tickUpper, uint128 liquidity) external;
function setManager(address _manager) external;
function setRebalanceDelegate(address _rebalanceDelegate) external;
function setManagerFee(uint24 _pendingManagerFee) external;
function acceptManager() external;
// state variables
function token0() external view returns (address);
function token1() external view returns (address);
function getTwap() external view returns (int24);
function fullLower() external view returns (int24);
function fullUpper() external view returns (int24);
function baseLower() external view returns (int24);
function baseUpper() external view returns (int24);
function limitLower() external view returns (int24);
function limitUpper() external view returns (int24);
function pool() external view returns (IUniswapV3Pool);
function protocolFee() external view returns (uint24);
function managerFee() external view returns (uint24);
function manager() external view returns (address);
function pendingManager() external view returns (address);
function rebalanceDelegate() external view returns (address);
function maxTotalSupply() external view returns (uint256);
function fullRangeWeight() external view returns (uint24);
function period() external view returns (uint32);
function minTickMove() external view returns (int24);
function maxTwapDeviation() external view returns (int24);
function twapDuration() external view returns (uint32);
function tickSpacing() external view returns (int24);
function accruedProtocolFees0() external view returns (uint256);
function accruedProtocolFees1() external view returns (uint256);
function accruedManagerFees0() external view returns (uint256);
function accruedManagerFees1() external view returns (uint256);
function lastTimestamp() external view returns (uint256);
function lastTick() external view returns (int24);
function baseThreshold() external view returns (int24);
function limitThreshold() external view returns (int24);
function pendingManagerFee() external view returns (uint24);
}
IMetaOracle.sol 46 lines
// SPDX-License-Identifier: BUSL-1.1
/// @notice Oracle adapter for AMPL and its family of assets.
// solhint-disable-next-line compiler-version
interface IMetaOracle {
/// @return Number of decimals representing the prices returned.
function decimals() external pure returns (uint8);
/// @return price The price of USDC tokens in dollars.
/// @return isValid True if the returned price is valid.
function usdcPrice() external returns (uint256 price, bool isValid);
/// @notice Computes SPOT's market price over FMV price.
/// @return deviation The computed deviation factor.
/// @return isValid True if the returned deviation is valid.
function spotPriceDeviation() external returns (uint256 deviation, bool isValid);
/// @notice Computes AMPL's market price over price target.
/// @return deviation The computed deviation factor.
/// @return isValid True if the returned deviation is valid.
function amplPriceDeviation() external returns (uint256 deviation, bool isValid);
/// @return price The price of SPOT in dollars.
/// @return isValid True if the returned price is valid.
function spotUsdPrice() external returns (uint256 price, bool isValid);
/// @return price The price of AMPL in dollars.
/// @return isValid True if the returned price is valid.
function amplUsdPrice() external returns (uint256 price, bool isValid);
/// @return price The SPOT FMV price in dollars.
/// @return isValid True if the returned price is valid.
function spotFmvUsdPrice() external returns (uint256 price, bool isValid);
/// @return price The AMPL target price in dollars.
/// @return isValid True if the returned price is valid.
function amplTargetUsdPrice() external returns (uint256 price, bool isValid);
/// @return price The WAMPL price in dollars.
/// @return isValid True if the returned price is valid.
function wamplUsdPrice() external returns (uint256 price, bool isValid);
/// @return price The ETH price in dollars.
/// @return isValid True if the returned price is valid.
function ethUsdPrice() external returns (uint256 price, bool isValid);
}
CommonTypes.sol 22 lines
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.24;
/// @notice A data structure to define a geometric Line with two points.
struct Line {
// @dev x-coordinate of the first point.
uint256 x1;
// @dev y-coordinate of the first point.
uint256 y1;
// @dev x-coordinate of the second point.
uint256 x2;
// @dev y-coordinate of the second point.
uint256 y2;
}
/// @notice A data structure to define a numeric Range.
struct Range {
// @dev Lower bound of the range.
uint256 lower;
// @dev Upper bound of the range.
uint256 upper;
}
AlphaVaultHelpers.sol 93 lines
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity ^0.8.24;
import { Math } from "@openzeppelin/contracts/utils/math/Math.sol";
import { IAlphaProVault } from "../_interfaces/external/IAlphaProVault.sol";
import { IUniswapV3Pool } from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
/**
* @title AlphaVaultHelpers
*
* @notice Library with helper functions for Charm's Alpha Vaults.
*
*/
library AlphaVaultHelpers {
/// @dev Checks if the vault is underweight token0 (ie overweight token1).
function isUnderweightToken0(IAlphaProVault vault) internal view returns (bool) {
// `vault.getTwap()` returns the twap tick from the underlying univ3 pool.
// https://learn.charm.fi/charm/technical-references/core/alphaprovault#gettwap
int24 _priceTick = vault.getTwap();
int24 _limitLower = vault.limitLower();
int24 _limitUpper = vault.limitUpper();
int24 _limitPriceTick = (_limitLower + _limitUpper) / 2;
// The limit range has more token1 than token0 if `_priceTick >= _limitPriceTick`,
// so the vault looks to sell token1.
return (_priceTick >= _limitPriceTick);
}
/// @dev Removes the vault's limit range liquidity completely.
function removeLimitLiquidity(IAlphaProVault vault, IUniswapV3Pool pool) internal {
int24 _limitLower = vault.limitLower();
int24 _limitUpper = vault.limitUpper();
uint128 limitLiquidity = getLiquidity(vault, pool, _limitLower, _limitUpper);
// docs: https://learn.charm.fi/charm/technical-references/core/alphaprovault#emergencyburn
vault.emergencyBurn(_limitLower, _limitUpper, limitLiquidity);
}
/// @dev Removes a percentage of the base and full range liquidity.
function trimLiquidity(
IAlphaProVault vault,
IUniswapV3Pool pool,
uint256 percToRemove,
uint256 one
) internal {
if (percToRemove <= 0) {
return;
}
int24 _fullLower = vault.fullLower();
int24 _fullUpper = vault.fullUpper();
int24 _baseLower = vault.baseLower();
int24 _baseUpper = vault.baseUpper();
uint128 fullLiquidity = getLiquidity(vault, pool, _fullLower, _fullUpper);
uint128 baseLiquidity = getLiquidity(vault, pool, _baseLower, _baseUpper);
// Calculated baseLiquidityToBurn, baseLiquidityToBurn will be lesser than fullLiquidity, baseLiquidity
// Thus, there's no risk of overflow.
uint128 fullLiquidityToBurn = uint128(
Math.mulDiv(uint256(fullLiquidity), percToRemove, one)
);
uint128 baseLiquidityToBurn = uint128(
Math.mulDiv(uint256(baseLiquidity), percToRemove, one)
);
// docs: https://learn.charm.fi/charm/technical-references/core/alphaprovault#emergencyburn
// We remove the calculated percentage of base and full range liquidity.
if (fullLiquidityToBurn > 0) {
vault.emergencyBurn(_fullLower, _fullUpper, fullLiquidityToBurn);
}
if (baseLiquidityToBurn > 0) {
vault.emergencyBurn(_baseLower, _baseUpper, baseLiquidityToBurn);
}
}
/// @dev A low-level method, which interacts directly with the vault and executes
/// a rebalance even when enough time hasn't elapsed since the last rebalance.
function forceRebalance(IAlphaProVault vault) internal {
uint32 _period = vault.period();
vault.setPeriod(0);
vault.rebalance();
vault.setPeriod(_period);
}
/// @dev Wrapper around `IUniswapV3Pool.positions()`.
function getLiquidity(
IAlphaProVault vault,
IUniswapV3Pool pool,
int24 tickLower,
int24 tickUpper
) internal view returns (uint128) {
bytes32 positionKey = keccak256(
abi.encodePacked(address(vault), tickLower, tickUpper)
);
(uint128 liquidity, , , , ) = pool.positions(positionKey);
return liquidity;
}
}
UsdcSpotManager.sol 243 lines
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.24;
import { Math } from "@openzeppelin/contracts/utils/math/Math.sol";
import { SafeCast } from "@openzeppelin/contracts/utils/math/SafeCast.sol";
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { AlphaVaultHelpers } from "../_utils/AlphaVaultHelpers.sol";
import { Range } from "../_interfaces/types/CommonTypes.sol";
import { IMetaOracle } from "../_interfaces/IMetaOracle.sol";
import { IAlphaProVault } from "../_interfaces/external/IAlphaProVault.sol";
import { IUniswapV3Pool } from "@uniswap/v3-core/contracts/interfaces/IUniswapV3Pool.sol";
/// @title UsdcSpotManager
/// @notice This contract is a programmatic manager for the USDC-SPOT Charm AlphaProVault.
///
/// @dev The vault's active zone is defined as lower and upper percentages of FMV.
/// For example, if the active zone is [0.95, 1.05]x and SPOT's FMV price is $1.35.
/// When the market price of SPOT is between [$1.28, $1.41] we consider price to be in the active zone.
///
/// When in the active zone, the vault provides concentrated liquidity around the market price.
/// When price is outside the active zone, the vault reverts to a full range position.
///
///
contract UsdcSpotManager is Ownable {
//-------------------------------------------------------------------------
// Libraries
using AlphaVaultHelpers for IAlphaProVault;
using Math for uint256;
//-------------------------------------------------------------------------
// Constants & Immutables
/// @dev Decimals.
uint256 public constant DECIMALS = 18;
uint256 public constant ONE = (10 ** DECIMALS);
/// @dev Vault parameter to set max full range weight (100%).
uint24 public constant VAULT_MAX_FRW = (10 ** 6);
int24 public constant POOL_MAX_TICK = 48000; // (-99.2/+12048.1%)
/// @notice The USDC-SPOT charm alpha vault.
IAlphaProVault public immutable VAULT;
/// @notice The underlying USDC-SPOT univ3 pool.
IUniswapV3Pool public immutable POOL;
//-------------------------------------------------------------------------
// Storage
/// @notice The meta oracle which returns prices of AMPL asset family.
IMetaOracle public oracle;
/// @notice The lower and upper deviation factor within which
/// SPOT's price is considered to be in the active zone.
Range public activeZoneDeviation;
/// @notice The width of concentrated liquidity band,
/// SPOT's price is in the active zone.
uint256 public concBandDeviationWidth;
/// @notice The maximum USDC balance of the vault's full range position.
uint256 public fullRangeMaxUsdcBal;
/// @notice The maximum percentage of vault's balanced assets in the full range position.
uint256 public fullRangeMaxPerc;
/// @notice If price was within the active zone at the time of the last successful rebalance operation.
bool public prevWithinActiveZone;
//-----------------------------------------------------------------------------
// Constructor and Initializer
/// @notice Constructor initializes the contract with provided addresses.
/// @param vault_ Address of the AlphaProVault contract.
/// @param oracle_ Address of the MetaOracle contract.
constructor(IAlphaProVault vault_, IMetaOracle oracle_) Ownable() {
VAULT = vault_;
POOL = vault_.pool();
updateOracle(oracle_);
prevWithinActiveZone = false;
activeZoneDeviation = Range({
lower: ((ONE * 95) / 100), // 0.95 or 95%
upper: ((ONE * 105) / 100) // 1.05 or 105%
});
concBandDeviationWidth = (ONE / 20); // 0.05 or 5%
fullRangeMaxUsdcBal = 250000 * (10 ** 6); // 250k USDC
fullRangeMaxPerc = (ONE / 2); // 0.5 or 50%
}
//--------------------------------------------------------------------------
// Owner only methods
/// @notice Updates the MetaOracle.
function updateOracle(IMetaOracle oracle_) public onlyOwner {
// solhint-disable-next-line custom-errors
require(DECIMALS == oracle_.decimals(), "UnexpectedDecimals");
oracle = oracle_;
}
/// @notice Forwards the given calldata to the vault.
/// @param callData The calldata to pass to the vault.
/// @return The data returned by the vault method call.
function execOnVault(
bytes calldata callData
) external onlyOwner returns (bytes memory) {
// solhint-disable-next-line avoid-low-level-calls
(bool success, bytes memory r) = address(VAULT).call(callData);
// solhint-disable-next-line custom-errors
require(success, "VaultExecutionFailed");
return r;
}
/// @notice Updates the active zone definition.
function updateActiveZone(Range memory activeZoneDeviation_) external onlyOwner {
activeZoneDeviation = activeZoneDeviation_;
}
/// @notice Updates the width of the concentrated liquidity band.
function updateConcentratedBand(uint256 concBandDeviationWidth_) external onlyOwner {
concBandDeviationWidth = concBandDeviationWidth_;
}
/// @notice Updates the absolute and percentage maximum amount of liquidity
/// in the full range liquidity band.
function updateFullRangeLiquidity(
uint256 fullRangeMaxUsdcBal_,
uint256 fullRangeMaxPerc_
) external onlyOwner {
// solhint-disable-next-line custom-errors
require(fullRangeMaxPerc_ <= ONE, "InvalidPerc");
fullRangeMaxUsdcBal = fullRangeMaxUsdcBal_;
fullRangeMaxPerc = fullRangeMaxPerc_;
}
//--------------------------------------------------------------------------
// External write methods
/// @notice Executes vault rebalance.
function rebalance() public {
(uint256 deviation, bool deviationValid) = oracle.spotPriceDeviation();
bool withinActiveZone = (deviationValid && activeZone(deviation));
bool shouldForceRebalance = (withinActiveZone != prevWithinActiveZone);
// Set liquidity parameters.
withinActiveZone ? _setupActiveZoneLiq(deviation) : _resetLiq();
// Execute rebalance.
// NOTE: the vault.rebalance() will revert if enough time has not elapsed.
// We thus override with a force rebalance.
// https://learn.charm.fi/charm/technical-references/core/alphaprovault#rebalance
shouldForceRebalance ? VAULT.forceRebalance() : VAULT.rebalance();
// Trim positions after rebalance.
if (!withinActiveZone) {
VAULT.trimLiquidity(POOL, ONE - activeFullRangePerc(), ONE);
VAULT.removeLimitLiquidity(POOL);
}
// Update valid rebalance state.
if (deviationValid) {
prevWithinActiveZone = withinActiveZone;
}
}
//-----------------------------------------------------------------------------
// External/Public read methods
/// @notice Based on the given deviation factor,
/// calculates if the pool needs to be in the active zone.
function activeZone(uint256 deviation) public view returns (bool) {
return (activeZoneDeviation.lower <= deviation &&
deviation <= activeZoneDeviation.upper);
}
/// @notice Computes the percentage of liquidity to be deployed into the full range,
/// based on owner defined maximums.
function activeFullRangePerc() public view returns (uint256) {
(uint256 usdcBal, ) = VAULT.getTotalAmounts();
return Math.min(ONE.mulDiv(fullRangeMaxUsdcBal, usdcBal), fullRangeMaxPerc);
}
/// @notice Checks the vault is overweight SPOT and looking to sell the extra SPOT for USDC.
function isOverweightSpot() public view returns (bool) {
// NOTE: In the underlying univ3 pool and token0 is USDC and token1 is SPOT.
// Underweight Token0 implies that the limit range has less USDC and more SPOT.
return VAULT.isUnderweightToken0();
}
/// @notice Calculates the Univ3 tick equivalent of the given deviation factor.
function deviationToTicks(uint256 deviation) public pure returns (int24) {
// 2% ~ 200 ticks -> (POOL.tickSpacing())
// NOTE: width can't be zero, we set the minimum possible to 200.
uint256 t = deviation.mulDiv(10000, ONE);
t -= (t % 200);
return (t >= 200 ? SafeCast.toInt24(SafeCast.toInt256(t)) : int24(200));
}
/// @return Number of decimals representing 1.0.
function decimals() external pure returns (uint8) {
return uint8(DECIMALS);
}
//-----------------------------------------------------------------------------
// Private methods
/// @dev Configures the vault to provide concentrated liquidity in the active zone.
function _setupActiveZoneLiq(uint256 deviation) private {
VAULT.setFullRangeWeight(
SafeCast.toUint24(uint256(VAULT_MAX_FRW).mulDiv(activeFullRangePerc(), ONE))
);
// IMPORTANT:
//
// If price is exactly at the bounds of `activeZoneDeviation`,
// the concentrated liquidity will be *at most*
// `deviationToTicks(concBandDeviationWidth/2)` outside the bounds.
//
VAULT.setBaseThreshold(deviationToTicks(concBandDeviationWidth));
VAULT.setLimitThreshold(
deviationToTicks(
isOverweightSpot()
? Math.max(
activeZoneDeviation.upper - deviation,
concBandDeviationWidth / 2
)
: Math.max(
deviation - activeZoneDeviation.lower,
concBandDeviationWidth / 2
)
)
);
}
/// @dev Resets the vault to provide full range liquidity.
function _resetLiq() private {
VAULT.setFullRangeWeight(VAULT_MAX_FRW);
VAULT.setBaseThreshold(POOL_MAX_TICK);
VAULT.setLimitThreshold(POOL_MAX_TICK);
}
}
Read Contract
DECIMALS 0x2e0f2625 → uint256
ONE 0xc2ee3a08 → uint256
POOL 0x7535d246 → address
POOL_MAX_TICK 0x67175c45 → int24
VAULT 0x411557d1 → address
VAULT_MAX_FRW 0xb95ec5f2 → uint24
activeFullRangePerc 0x8486747d → uint256
activeZone 0x69e96d2d → bool
activeZoneDeviation 0x556f3c4e → uint256, uint256
concBandDeviationWidth 0xe33fd580 → uint256
decimals 0x313ce567 → uint8
deviationToTicks 0x16332843 → int24
fullRangeMaxPerc 0x6c6d201a → uint256
fullRangeMaxUsdcBal 0x02f6be6d → uint256
isOverweightSpot 0x7bdab7fa → bool
oracle 0x7dc0d1d0 → address
owner 0x8da5cb5b → address
prevWithinActiveZone 0x8596f712 → bool
Write Contract 8 functions
These functions modify contract state and require a wallet transaction to execute.
execOnVault 0x8d8c3de7
bytes callData
returns: bytes
rebalance 0x7d7c2a1c
No parameters
renounceOwnership 0x715018a6
No parameters
transferOwnership 0xf2fde38b
address newOwner
updateActiveZone 0xc12e67a4
tuple activeZoneDeviation_
updateConcentratedBand 0x263d48ba
uint256 concBandDeviationWidth_
updateFullRangeLiquidity 0x02c333b3
uint256 fullRangeMaxUsdcBal_
uint256 fullRangeMaxPerc_
updateOracle 0x1cb44dfc
address oracle_
Recent Transactions
No transactions found for this address