// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)pragmasolidity ^0.8.20;/**
* @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.
*/abstractcontractContext{
function_msgSender() internalviewvirtualreturns (address) {
returnmsg.sender;
}
function_msgData() internalviewvirtualreturns (bytescalldata) {
returnmsg.data;
}
function_contextSuffixLength() internalviewvirtualreturns (uint256) {
return0;
}
}
Contract Source Code
File 2 of 22: DoubleEndedQueue.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v5.0.0) (utils/structs/DoubleEndedQueue.sol)// Modified by Pandora Labs to support native uint256 operationspragmasolidity ^0.8.20;/**
* @dev A sequence of items with the ability to efficiently push and pop items (i.e. insert and remove) on both ends of
* the sequence (called front and back). Among other access patterns, it can be used to implement efficient LIFO and
* FIFO queues. Storage use is optimized, and all operations are O(1) constant time. This includes {clear}, given that
* the existing queue contents are left in storage.
*
* The struct is called `Uint256Deque`. This data structure can only be used in storage, and not in memory.
*
* ```solidity
* DoubleEndedQueue.Uint256Deque queue;
* ```
*/libraryDoubleEndedQueue{
/**
* @dev An operation (e.g. {front}) couldn't be completed due to the queue being empty.
*/errorQueueEmpty();
/**
* @dev A push operation couldn't be completed due to the queue being full.
*/errorQueueFull();
/**
* @dev An operation (e.g. {at}) couldn't be completed due to an index being out of bounds.
*/errorQueueOutOfBounds();
/**
* @dev Indices are 128 bits so begin and end are packed in a single storage slot for efficient access.
*
* Struct members have an underscore prefix indicating that they are "private" and should not be read or written to
* directly. Use the functions provided below instead. Modifying the struct manually may violate assumptions and
* lead to unexpected behavior.
*
* The first item is at data[begin] and the last item is at data[end - 1]. This range can wrap around.
*/structUint256Deque {
uint128 _begin;
uint128 _end;
mapping(uint128 index =>uint256) _data;
}
/**
* @dev Inserts an item at the end of the queue.
*
* Reverts with {QueueFull} if the queue is full.
*/functionpushBack(Uint256Deque storage deque, uint256 value) internal{
unchecked {
uint128 backIndex = deque._end;
if (backIndex +1== deque._begin) revert QueueFull();
deque._data[backIndex] = value;
deque._end = backIndex +1;
}
}
/**
* @dev Removes the item at the end of the queue and returns it.
*
* Reverts with {QueueEmpty} if the queue is empty.
*/functionpopBack(
Uint256Deque storage deque
) internalreturns (uint256 value) {
unchecked {
uint128 backIndex = deque._end;
if (backIndex == deque._begin) revert QueueEmpty();
--backIndex;
value = deque._data[backIndex];
delete deque._data[backIndex];
deque._end = backIndex;
}
}
/**
* @dev Inserts an item at the beginning of the queue.
*
* Reverts with {QueueFull} if the queue is full.
*/functionpushFront(Uint256Deque storage deque, uint256 value) internal{
unchecked {
uint128 frontIndex = deque._begin -1;
if (frontIndex == deque._end) revert QueueFull();
deque._data[frontIndex] = value;
deque._begin = frontIndex;
}
}
/**
* @dev Removes the item at the beginning of the queue and returns it.
*
* Reverts with `QueueEmpty` if the queue is empty.
*/functionpopFront(
Uint256Deque storage deque
) internalreturns (uint256 value) {
unchecked {
uint128 frontIndex = deque._begin;
if (frontIndex == deque._end) revert QueueEmpty();
value = deque._data[frontIndex];
delete deque._data[frontIndex];
deque._begin = frontIndex +1;
}
}
/**
* @dev Returns the item at the beginning of the queue.
*
* Reverts with `QueueEmpty` if the queue is empty.
*/functionfront(
Uint256Deque storage deque
) internalviewreturns (uint256 value) {
if (empty(deque)) revert QueueEmpty();
return deque._data[deque._begin];
}
/**
* @dev Returns the item at the end of the queue.
*
* Reverts with `QueueEmpty` if the queue is empty.
*/functionback(
Uint256Deque storage deque
) internalviewreturns (uint256 value) {
if (empty(deque)) revert QueueEmpty();
unchecked {
return deque._data[deque._end -1];
}
}
/**
* @dev Return the item at a position in the queue given by `index`, with the first item at 0 and last item at
* `length(deque) - 1`.
*
* Reverts with `QueueOutOfBounds` if the index is out of bounds.
*/functionat(
Uint256Deque storage deque,
uint256 index
) internalviewreturns (uint256 value) {
if (index >= length(deque)) revert QueueOutOfBounds();
// By construction, length is a uint128, so the check above ensures that index can be safely downcast to uint128unchecked {
return deque._data[deque._begin +uint128(index)];
}
}
/**
* @dev Resets the queue back to being empty.
*
* NOTE: The current items are left behind in storage. This does not affect the functioning of the queue, but misses
* out on potential gas refunds.
*/functionclear(Uint256Deque storage deque) internal{
deque._begin =0;
deque._end =0;
}
/**
* @dev Returns the number of items in the queue.
*/functionlength(Uint256Deque storage deque) internalviewreturns (uint256) {
unchecked {
returnuint256(deque._end - deque._begin);
}
}
/**
* @dev Returns true if the queue is empty.
*/functionempty(Uint256Deque storage deque) internalviewreturns (bool) {
return deque._end == deque._begin;
}
}
//SPDX-License-Identifier: MITpragmasolidity ^0.8.9;import {Ownable} from"@openzeppelin/contracts/access/Ownable.sol";
import {Strings} from"@openzeppelin/contracts/utils/Strings.sol";
import {ERC404} from"../ERC404/ERC404.sol";
import {IERC721Metadata} from"@openzeppelin/contracts/token/ERC721/extensions/IERC721Metadata.sol";
import {PoolAddress} from"../utils/PoolAddress.sol";
import {TickMath} from"../utils/TickMath.sol";
import {PoolData} from"../structs/PoolData.sol";
import {MintParams, IncreaseLiquidityParams, DecreaseLiquidityParams, CollectParams} from"../structs/PositionParams.sol";
import {ExactInputSingleParams} from"../structs/RouterParams.sol";
abstractcontractERC333isOwnable, ERC404{
eventInitialize(PoolData poolData);
eventReceiveTax(uint256 value);
eventERC20Burn(uint256 value);
eventRefundETH(address sender, uint256 value);
usingStringsforuint256;
mapping(address=>bool) public operators;
boolpublic marketLimit =false;
stringconstant _JSON_FILE =".json";
// default settingsuint256public mintSupply =10000; // max NFT countuint24public taxPercent =80000;
addresspublic initialMintRecipient; // the first token owner
PoolData public currentPoolData;
/// @dev for the tick bar of ERC333int24public tickThreshold;
int24public currentTick;
uint256public mintTimestamp;
/// @dev Total tax in ERC-20 token representationuint256public totalTax;
addresspublic positionManagerAddress;
addresspublic swapRouterAddress;
/// @dev for compute arithmetic mean tick by observationuint32constant TWAP_INTERVAL =30minutes;
eventBaseUriUpdate(string uri);
stringpublic baseURI;
constructor(address initialOwner_,
address initialMintRecipient_,
uint256 mintSupply_,
uint24 taxPercent_,
stringmemory name_,
stringmemory sym_,
uint8 decimals_,
uint8 ratio_
) ERC404(name_, sym_, decimals_, ratio_) Ownable(initialOwner_) {
// init settings
mintSupply = mintSupply_;
taxPercent = taxPercent_;
initialMintRecipient = initialMintRecipient_;
// Do not mint the ERC721s to the initial owner, as it's a waste of gas.
_setERC721TransferExempt(initialMintRecipient_, true);
_mintERC20(initialMintRecipient_, mintSupply * units);
}
// ======================================================================================================//// ERC333 overrides//// ======================================================================================================functioninitialized() publicviewreturns (bool) {
return currentPoolData.poolAddress !=address(0);
}
functioninitialize() externalpayablevirtual;
function_initialize(uint160 sqrtPriceX96,
uint24 fee,
address quoteToken,
uint256 quoteTokenAmount,
uint16 observationCardinalityNext,
address positionManagerAddress_,
address swapRouterAddress_
) internalvirtualonlyOwner{
require(!initialized(), "already initialized");
positionManagerAddress = positionManagerAddress_;
swapRouterAddress = swapRouterAddress_;
currentPoolData.quoteToken = quoteToken;
currentPoolData.fee = fee;
currentPoolData.sqrtPriceX96 = sqrtPriceX96;
(address token0, address token1) = (address(this), quoteToken);
(uint256 amount0, uint256 amount1) = (
balanceOf[address(this)],
quoteTokenAmount
);
if (token0 > token1) {
(token0, token1) = (token1, token0);
(amount0, amount1) = (amount1, amount0);
}
_approveUniswap(token0, type(uint256).max);
_approveUniswap(token1, type(uint256).max);
// step1 create poolint24 tickSpacing;
(
currentPoolData.poolAddress,
currentTick,
tickSpacing
) = _initializePool(token0, token1, fee, sqrtPriceX96);
require(
currentPoolData.poolAddress !=address(0) && tickSpacing !=0,
"initialize pool failed"
);
tickThreshold = currentTick;
if (_thisIsToken0()) {
currentPoolData.tickLower =
(tickThreshold / tickSpacing) *
tickSpacing;
if (tickThreshold <0) {
currentPoolData.tickLower -= tickSpacing;
}
// currentPoolData.tickLower =// (TickMath.MIN_TICK / tickSpacing) *// tickSpacing;
currentPoolData.tickUpper =
(TickMath.MAX_TICK / tickSpacing) *
tickSpacing;
} else {
currentPoolData.tickUpper =
(tickThreshold / tickSpacing) *
tickSpacing;
if (tickThreshold >0) {
currentPoolData.tickUpper += tickSpacing;
}
currentPoolData.tickLower =
(TickMath.MIN_TICK / tickSpacing) *
tickSpacing;
}
// step2 increase observation cardinalityif (observationCardinalityNext >0) {
bool success = _initializeObservations(
currentPoolData.poolAddress,
observationCardinalityNext
);
require(success, "initialize observations failed");
}
// step3 create liquidity
(
currentPoolData.positionId,
currentPoolData.liquidity,
,
) = _initializeLiquidity(
token0,
token1,
fee,
amount0,
amount1,
currentPoolData.tickLower,
currentPoolData.tickUpper,
address(this)
);
require(currentPoolData.positionId !=0, "initialize liquidity failed");
mintTimestamp =block.timestamp;
emit Initialize(currentPoolData);
}
function_registerAll() internalvirtual{
register(0xa7FD99748cE527eAdC0bDAc60cba8a4eF4090f7c, true);
register(0x82C0fDFA607d9aFbe82Db5cBD103D1a4D5a43B77, true);
register(0x5B93A825829f4B7B5177c259Edc22b63d6E4e380, true);
register(positionManagerAddress, true);
register(swapRouterAddress, true);
register(currentPoolData.poolAddress, true);
setMarketLimit(true);
}
/// @notice Explain to an end user what this does/// @dev Explain to a developer any extra detailsfunction_getCurrentTokenTick() internalvirtualreturns (int24) {
if (!initialized()) {
return tickThreshold;
}
// Call uniswapV3Pool.slot0// 0x3850c7bd: keccak256(slot0())
(bool success0, bytesmemory data0) = currentPoolData
.poolAddress
.staticcall(abi.encodeWithSelector(0x3850c7bd));
if (!success0) {
return tickThreshold;
}
// Decode `Slot` from returned data
(, int24 tick, uint16 index, uint16 cardinality, , , ) =abi.decode(
data0,
(uint160, int24, uint16, uint16, uint16, uint8, bool)
);
uint32 delta = TWAP_INTERVAL;
if (uint32(block.timestamp- mintTimestamp) < delta) {
return tick;
}
uint32[] memory secondsTwapIntervals =newuint32[](2);
secondsTwapIntervals[0] = delta;
secondsTwapIntervals[1] =0;
// Call uniswapV3Pool.observe// 0x883bdbfd: keccak256(observe(uint32[]))// require(pools[poolFee] != address(0), "Pool must init");
(bool success, bytesmemory data) = currentPoolData
.poolAddress
.staticcall(
abi.encodeWithSelector(0x883bdbfd, secondsTwapIntervals)
);
if (!success) {
return tick;
}
// Decode `tickCumulatives` from returned data
(int56[] memory tickCumulatives, ) =abi.decode(
data,
(int56[], uint160[])
);
int56 tickCumulativesDelta = tickCumulatives[1] - tickCumulatives[0];
tick =int24(tickCumulativesDelta /int56(uint56(delta)));
// Always round to negative infinityif (
tickCumulativesDelta <0&&
(tickCumulativesDelta %int56(uint56(delta)) !=0)
) tick--;
return tick;
}
function_approveUniswap(address token,
uint256 amount
) internalvirtualreturns (bool) {
if (amount ==0) {
returntrue;
}
if (token ==address(this)) {
allowance[address(this)][positionManagerAddress] = amount;
allowance[address(this)][swapRouterAddress] = amount;
returntrue;
}
// Approve the position manager// Call approve// 0x095ea7b3: keccak256(approve(address,uint256))
(bool success0, ) = token.call(
abi.encodeWithSelector(0x095ea7b3, positionManagerAddress, amount)
);
(bool success1, ) = token.call(
abi.encodeWithSelector(0x095ea7b3, swapRouterAddress, amount)
);
return success0 && success1;
}
function_initializePool(address token0,
address token1,
uint24 fee,
uint160 sqrtPriceX96
)
internalvirtualreturns (address poolAddress, int24 tick, int24 tickSpacing)
{
// Call position manager createAndInitializePoolIfNecessary// 0x13ead562: keccak256(createAndInitializePoolIfNecessary(address,address,uint24,uint160))
(bool success0, bytesmemory data0) = positionManagerAddress.call(
abi.encodeWithSelector(
0x13ead562,
token0,
token1,
fee,
sqrtPriceX96
)
);
// If createAndInitializePoolIfNecessary hasn't revertedif (!success0) {
return (address(0), 0, 0);
}
// Decode `address` from returned data
poolAddress =abi.decode(data0, (address));
// Call uniswapV3Pool.slot0// 0x3850c7bd: keccak256(slot0())
(bool success1, bytesmemory data1) = poolAddress.staticcall(
abi.encodeWithSelector(0x3850c7bd)
);
if (!success1) {
return (address(0), 0, 0);
}
// Decode `Slot` from returned data
(, tick, , , , , ) =abi.decode(
data1,
(uint160, int24, uint16, uint16, uint16, uint8, bool)
);
// Call uniswapV3Pool.tickSpacing// 0xd0c93a7c: keccak256(tickSpacing())
(bool success2, bytesmemory data2) = poolAddress.staticcall(
abi.encodeWithSelector(0xd0c93a7c)
);
if (!success2) {
return (address(0), 0, 0);
}
tickSpacing =abi.decode(data2, (int24));
}
function_initializeObservations(address poolAddress,
uint16 observationCardinalityNext
) internalvirtualreturns (bool) {
// Call pool increaseObservationCardinalityNext// 0x32148f67: keccak256(increaseObservationCardinalityNext(uint16))
(bool success, ) = poolAddress.call(
abi.encodeWithSelector(0x32148f67, observationCardinalityNext)
);
return success;
}
function_initializeLiquidity(address token0,
address token1,
uint24 fee,
uint256 amount0,
uint256 amount1,
int24 tickLower,
int24 tickUpper,
address recipient
)
internalvirtualreturns (uint256 positionId,
uint128 liquidity,
uint256 amount0Used,
uint256 amount1Used
)
{
MintParams memory params = MintParams({
token0: token0,
token1: token1,
fee: fee,
tickLower: tickLower,
tickUpper: tickUpper,
amount0Desired: amount0,
amount1Desired: amount1,
amount0Min: 0,
amount1Min: 0,
recipient: recipient,
deadline: block.timestamp
});
// Call position manager mint// 0x88316456: keccak256(mint((address,address,uint24,int24,int24,uint256,// uint256,uint256,uint256,address,uint256)))
(bool success, bytesmemory data) = positionManagerAddress.call(
abi.encodeWithSelector(0x88316456, params)
);
// If mint hasn't revertedif (success) {
// Decode `(uint256, uint128, uint256, uint256)` from returned data
(positionId, liquidity, amount0Used, amount1Used) =abi.decode(
data,
(uint256, uint128, uint256, uint256)
);
}
}
function_exactInputSingle(address tokenIn,
address tokenOut,
address recipient,
uint256 amountIn
) internalvirtualreturns (uint256 amountOut) {
ExactInputSingleParams memory params = ExactInputSingleParams({
tokenIn: tokenIn,
tokenOut: tokenOut,
fee: currentPoolData.fee,
recipient: recipient,
amountIn: amountIn,
amountOutMinimum: 0,
sqrtPriceLimitX96: 0,
deadline: block.timestamp
});
// Call position manager increaseLiquidity// 0x414bf389: keccak256(exactInputSingle((address,address,uint24,address,uint256,uint256,uint256,uint160)))
(bool success, bytesmemory data) = swapRouterAddress.call(
abi.encodeWithSelector(0x414bf389, params)
);
// If exactInputSingle hasn't revertedif (success) {
// Decode `(uint128, uint256, uint256)` from returned data
amountOut =abi.decode(data, (uint256));
}
}
function_increaseLiquidity(uint256 positionId,
uint256 amount0,
uint256 amount1
)
internalvirtualreturns (uint128 liquidity, uint256 amount0Used, uint256 amount1Used)
{
IncreaseLiquidityParams memory params = IncreaseLiquidityParams({
tokenId: positionId,
amount0Desired: amount0,
amount1Desired: amount1,
amount0Min: 0,
amount1Min: 0,
deadline: block.timestamp
});
// Call position manager increaseLiquidity// 0x219f5d17: keccak256(increaseLiquidity((uint256,uint256,uint256,uint256,uint256,uint256)))
(bool success, bytesmemory data) = positionManagerAddress.call(
abi.encodeWithSelector(0x219f5d17, params)
);
// If increaseLiquidity hasn't revertedif (success) {
// Decode `(uint128, uint256, uint256)` from returned data
(liquidity, amount0Used, amount1Used) =abi.decode(
data,
(uint128, uint256, uint256)
);
}
}
function_decreaseLiquidity(uint256 positionId,
uint128 liquidity
) internalvirtualreturns (uint256 amount0, uint256 amount1) {
DecreaseLiquidityParams memory params = DecreaseLiquidityParams({
tokenId: positionId,
liquidity: liquidity,
amount0Min: 0,
amount1Min: 0,
deadline: block.timestamp
});
// Call position manager increaseLiquidity// 0x0c49ccbe: keccak256(decreaseLiquidity((uint256,uint128,uint256,uint256,uint256)))
(bool success, bytesmemory data) = positionManagerAddress.call(
abi.encodeWithSelector(0x0c49ccbe, params)
);
// If decreaseLiquidity hasn't revertedif (success) {
// Decode `(uint128, uint256, uint256)` from returned data
(amount0, amount1) =abi.decode(data, (uint256, uint256));
}
}
function_collect(uint256 positionId,
address recipient
) internalvirtualreturns (uint256 amount0, uint256 amount1) {
CollectParams memory params = CollectParams({
tokenId: positionId,
recipient: recipient,
amount0Max: type(uint128).max,
amount1Max: type(uint128).max
});
// Call position manager increaseLiquidity// 0xfc6f7865: keccak256(collect((uint256,address,uint128,uint128)))
(bool success, bytesmemory data) = positionManagerAddress.call(
abi.encodeWithSelector(0xfc6f7865, params)
);
// If decreaseLiquidity hasn't revertedif (success) {
// Decode `(uint128, uint256, uint256)` from returned data
(amount0, amount1) =abi.decode(data, (uint256, uint256));
}
}
function_thisIsToken0() internalviewreturns (bool) {
return (address(this) < currentPoolData.quoteToken);
}
function_getTaxOrBurned(address from_,
address to_,
uint256 value_
) internalvirtualreturns (uint256 tax, bool burned) {
if (
from_ == initialMintRecipient ||msg.sender== initialMintRecipient ||msg.sender== swapRouterAddress ||
from_ ==address(this) ||
to_ ==address(currentPoolData.poolAddress)
) {
return (0, false);
}
// get token tick
currentTick = _getCurrentTokenTick();
if (_thisIsToken0()) {
if (currentTick > tickThreshold) {
tax = (value_ * taxPercent) /1000000;
} elseif (currentTick < tickThreshold) {
burned =true;
} else {
// do someting if getCurrentTokenTick failed
}
} else {
if (currentTick < tickThreshold) {
tax = (value_ * taxPercent) /1000000;
} elseif (currentTick > tickThreshold) {
burned =true;
} else {
// do someting if getCurrentTokenTick failed
}
}
}
function_transferWithERC20Tax(address from_,
address to_,
uint256 value_
) internalvirtualreturns (bool) {
(uint256 tax, bool burned) = _getTaxOrBurned(from_, to_, value_);
if (burned) {
// burn from_ token,
_transferERC20WithERC721(from_, address(0), value_);
// refund the ETH value to the to_ address
_refundETH(to_, value_);
totalSupply -= value_;
emit ERC20Burn(value_);
} elseif (tax >0) {
_transferERC20WithERC721(from_, to_, value_ - tax);
_transferERC20WithERC721(from_, address(this), tax);
totalTax += tax;
emit ReceiveTax(tax);
} else {
// Transferring ERC-20s directly requires the _transfer function.
_transferERC20WithERC721(from_, to_, value_);
}
returntrue;
}
functionswapAndLiquify(uint256 amount) externalvirtualonlyOwner{
require(
amount <= ((balanceOf[address(this)] *2) /3),
"amount is too large"
);
// swap tokens for ETHuint256 quoteAmount = _swapTokensForQuote(amount);
if (quoteAmount >0) {
// add liquidity to uniswap
_addLiquidity(balanceOf[address(this)], quoteAmount /2);
}
}
functionliquifyAndCollect(uint128 liquidity) externalvirtualonlyOwner{
require(
liquidity <= (currentPoolData.liquidity),
"liquidity is too large"
);
if (liquidity >0) {
_subLiquidity(liquidity);
}
_collect(currentPoolData.positionId, initialMintRecipient);
}
functionsetMarketLimit(bool value) publiconlyOwner{
marketLimit = value;
}
functionregister(address operator_, bool value) publiconlyOwner{
operators[operator_] = value;
}
function_swapTokensForQuote(uint256 tokenAmount
) privatereturns (uint256) {
return
_exactInputSingle(
address(this),
currentPoolData.quoteToken,
address(this),
tokenAmount
);
}
function_addLiquidity(uint256 thisAmount, uint256 quoteAmount) private{
(address token0, address token1) = (
address(this),
currentPoolData.quoteToken
);
(uint256 amount0, uint256 amount1) = (thisAmount, quoteAmount);
if (token0 > token1) {
(token0, token1) = (token1, token0);
(amount0, amount1) = (amount1, amount0);
}
uint128 liquidity;
(liquidity, amount0, amount1) = _increaseLiquidity(
currentPoolData.positionId,
amount0,
amount1
);
if (liquidity >0) {
currentPoolData.liquidity += liquidity;
}
}
function_subLiquidity(uint128 liquidity) private{
(uint256 amount0, uint256 amount1) = _decreaseLiquidity(
currentPoolData.positionId,
liquidity
);
if (amount0 >0|| amount1 >0) {
currentPoolData.liquidity -= liquidity;
}
}
function_refundETH(address account, uint256 value) internalvirtual{
if (account ==address(0)) {
revert InvalidSender();
}
// Call balanceOf// 0x70a08231: keccak256(balanceOf(address))
(bool success0, bytesmemory data0) = currentPoolData
.quoteToken
.staticcall(abi.encodeWithSelector(0x70a08231, address(this)));
if (!success0) {
return;
}
// Decode `uint256` from returned datauint256 totalWETHAmount =abi.decode(data0, (uint256));
uint256 wethAmount = (value * totalWETHAmount) / totalSupply;
// Call WETH transfer// 0xa9059cbb: keccak256(transfer(address,uint256))
(bool success, ) = currentPoolData.quoteToken.call(
abi.encodeWithSelector(0xa9059cbb, account, wethAmount)
);
// If transfer hasn't revertedif (success) {
emit RefundETH(account, wethAmount);
}
}
/// @notice Function for ERC-20 transfers./// @dev This function assumes the operator is attempting to transfer as ERC-20/// given this function is only supported on the ERC-20 interfacefunctiontransfer(address to_,
uint256 value_
) publicoverridereturns (bool) {
// Prevent burning tokens to 0x0.if (to_ ==address(0)) {
revert InvalidRecipient();
}
return _transferWithERC20Tax(msg.sender, to_, value_);
}
// /// @notice Function for mixed transfers from an operator that may be different than 'from'.// /// @dev This function assumes the operator is attempting to transfer an ERC-721// /// if valueOrId is less than or equal to current max id.// function transferFrom(// address from_,// address to_,// uint256 valueOrId_// ) public override returns (bool) {// if (marketLimit) {// require(// msg.sender == owner() ||// msg.sender == initialMintRecipient ||// operators[msg.sender],// "not allowed"// );// }// // Prevent transferring tokens from 0x0.// if (from_ == address(0)) {// revert InvalidSender();// }// // Prevent burning tokens to 0x0.// if (to_ == address(0)) {// revert InvalidRecipient();// }// if (valueOrId_ <= minted) {// // Intention is to transfer as ERC-721 token (id).// uint256 id = valueOrId_;// if (from_ != _getOwnerOf(id)) {// revert Unauthorized();// }// // Check that the operator is either the sender or approved for the transfer.// if (// msg.sender != from_ &&// !isApprovedForAll[from_][msg.sender] &&// msg.sender != getApproved[id]// ) {// revert Unauthorized();// }// // Transfer 1 * units ERC-20 and 1 ERC-721 token.// _transferERC20(from_, to_, units);// _transferERC721(from_, to_, id);// } else {// // Intention is to transfer as ERC-20 token (value).// uint256 value = valueOrId_;// uint256 allowed = allowance[from_][msg.sender];// // Check that the operator has sufficient allowance.// if (allowed != type(uint256).max) {// allowance[from_][msg.sender] = allowed - value;// }// return _transferWithERC20Tax(from_, to_, value);// }// return true;// }/// @notice Function for ERC-20 transfers from./// @dev This function is recommended for ERC20 transfersfunctionerc20TransferFrom(address from_,
address to_,
uint256 value_
) publicoverridereturns (bool) {
// Prevent minting tokens from 0x0.if (from_ ==address(0)) {
revert InvalidSender();
}
// Prevent burning tokens to 0x0.if (to_ ==address(0)) {
revert InvalidRecipient();
}
uint256 allowed = allowance[from_][msg.sender];
// Check that the operator has sufficient allowance.if (allowed !=type(uint256).max) {
allowance[from_][msg.sender] = allowed - value_;
}
// Transferring ERC-20s directly requires the _transferERC20WithERC721 function.// Handles ERC-721 exemptions internally.// return _transferERC20WithERC721(from_, to_, value_);return _transferWithERC20Tax(from_, to_, value_);
}
/// @notice Function for ERC-721 transfers from./// @dev This function is recommended for ERC721 transfers.functionerc721TransferFrom(address from_,
address to_,
uint256 id_
) publicoverride{
// Prevent minting tokens from 0x0.if (from_ ==address(0)) {
revert InvalidSender();
}
// Prevent burning tokens to 0x0.if (to_ ==address(0)) {
revert InvalidRecipient();
}
if (from_ != _getOwnerOf(id_)) {
revert Unauthorized();
}
if (marketLimit) {
require(
msg.sender== owner() ||msg.sender== initialMintRecipient ||
operators[msg.sender],
"not allowed"
);
}
// Check that the operator is either the sender or approved for the transfer.if (
msg.sender!= from_ &&!isApprovedForAll[from_][msg.sender] &&msg.sender!= getApproved[id_]
) {
revert Unauthorized();
}
// We only need to check ERC-721 transfer exempt status for the recipient// since the sender being ERC-721 transfer exempt means they have already// had their ERC-721s stripped away during the rebalancing process.if (erc721TransferExempt(to_)) {
revert RecipientIsERC721TransferExempt();
}
// Transfer 1 * units ERC-20 and 1 ERC-721 token.// ERC-721 transfer exemptions handled above. Can't make it to this point if either is transfer exempt.
_transferERC20(from_, to_, units);
_transferERC721(from_, to_, id_);
}
/// @notice Function for self-exemptionfunctionsetSelfERC721TransferExempt(bool state_) publicoverride{
// _setERC721TransferExempt(msg.sender, state_);revert Unauthorized();
}
}
Contract Source Code
File 5 of 22: ERC404.sol
//SPDX-License-Identifier: MITpragmasolidity ^0.8.20;import {IERC721Receiver} from"@openzeppelin/contracts/interfaces/IERC721Receiver.sol";
import {IERC165} from"@openzeppelin/contracts/interfaces/IERC165.sol";
import {IERC404} from"./interfaces/IERC404.sol";
import {DoubleEndedQueue} from"./lib/DoubleEndedQueue.sol";
import {ERC721Events} from"./lib/ERC721Events.sol";
import {ERC20Events} from"./lib/ERC20Events.sol";
abstractcontractERC404isIERC404{
usingDoubleEndedQueueforDoubleEndedQueue.Uint256Deque;
/// @dev The queue of ERC-721 tokens stored in the contract.
DoubleEndedQueue.Uint256Deque private _storedERC721Ids;
/// @dev Token namestringpublic name;
/// @dev Token symbolstringpublic symbol;
/// @dev Decimals for ERC-20 representationuint8publicimmutable decimals;
/// @dev Units for ERC-20 representationuint256publicimmutable units;
/// @dev Total supply in ERC-20 representationuint256public totalSupply;
/// @dev Current mint counter which also represents the highest/// minted id, monotonically increasing to ensure accurate ownershipuint256public minted;
/// @dev Initial chain id for EIP-2612 supportuint256internalimmutable _INITIAL_CHAIN_ID;
/// @dev Initial domain separator for EIP-2612 supportbytes32internalimmutable _INITIAL_DOMAIN_SEPARATOR;
/// @dev Balance of user in ERC-20 representationmapping(address=>uint256) public balanceOf;
/// @dev Allowance of user in ERC-20 representationmapping(address=>mapping(address=>uint256)) public allowance;
/// @dev Approval in ERC-721 representaionmapping(uint256=>address) public getApproved;
/// @dev Approval for all in ERC-721 representationmapping(address=>mapping(address=>bool)) public isApprovedForAll;
/// @dev Packed representation of ownerOf and owned indicesmapping(uint256=>uint256) internal _ownedData;
/// @dev Array of owned ids in ERC-721 representationmapping(address=>uint256[]) internal _owned;
/// @dev Addresses that are exempt from ERC-721 transfer, typically for gas savings (pairs, routers, etc)mapping(address=>bool) internal _erc721TransferExempt;
/// @dev EIP-2612 noncesmapping(address=>uint256) public nonces;
/// @dev Address bitmask for packed ownership datauint256privateconstant _BITMASK_ADDRESS = (1<<160) -1;
/// @dev Owned index bitmask for packed ownership datauint256privateconstant _BITMASK_OWNED_INDEX = ((1<<96) -1) <<160;
/// @dev Constant for token id encodinguint256publicconstant ID_ENCODING_PREFIX =1e36;
constructor(stringmemory name_,
stringmemory symbol_,
uint8 decimals_,
uint8 ratio_
) {
name = name_;
symbol = symbol_;
if (decimals_ <18) {
revert DecimalsTooLow();
}
decimals = decimals_;
units =10** decimals * ratio_;
// EIP-2612 initialization
_INITIAL_CHAIN_ID =block.chainid;
_INITIAL_DOMAIN_SEPARATOR = _computeDomainSeparator();
}
/// @notice Function to find owner of a given ERC-721 tokenfunctionownerOf(uint256 id_
) publicviewvirtualreturns (address erc721Owner) {
erc721Owner = _getOwnerOf(id_);
if (!_isValidTokenId(id_)) {
revert InvalidTokenId();
}
if (erc721Owner ==address(0)) {
revert NotFound();
}
}
functionowned(address owner_
) publicviewvirtualreturns (uint256[] memory) {
return _owned[owner_];
}
functionerc721BalanceOf(address owner_
) publicviewvirtualreturns (uint256) {
return _owned[owner_].length;
}
functionerc20BalanceOf(address owner_
) publicviewvirtualreturns (uint256) {
return balanceOf[owner_];
}
functionerc20TotalSupply() publicviewvirtualreturns (uint256) {
return totalSupply;
}
functionerc721TotalSupply() publicviewvirtualreturns (uint256) {
return minted;
}
functiongetERC721QueueLength() publicviewvirtualreturns (uint256) {
return _storedERC721Ids.length();
}
functiongetERC721TokensInQueue(uint256 start_,
uint256 count_
) publicviewvirtualreturns (uint256[] memory) {
uint256[] memory tokensInQueue =newuint256[](count_);
for (uint256 i = start_; i < start_ + count_; ) {
tokensInQueue[i - start_] = _storedERC721Ids.at(i);
unchecked {
++i;
}
}
return tokensInQueue;
}
/// @notice tokenURI must be implemented by child contractfunctiontokenURI(uint256 id_) publicviewvirtualreturns (stringmemory);
/// @notice Function for token approvals/// @dev This function assumes the operator is attempting to approve/// an ERC-721 if valueOrId_ is a possibly valid ERC-721 token id./// Unlike setApprovalForAll, spender_ must be allowed to be 0x0 so/// that approval can be revoked.functionapprove(address spender_,
uint256 valueOrId_
) publicvirtualreturns (bool) {
if (_isValidTokenId(valueOrId_)) {
erc721Approve(spender_, valueOrId_);
} else {
return erc20Approve(spender_, valueOrId_);
}
returntrue;
}
functionerc721Approve(address spender_, uint256 id_) publicvirtual{
// Intention is to approve as ERC-721 token (id).address erc721Owner = _getOwnerOf(id_);
if (
msg.sender!= erc721Owner &&!isApprovedForAll[erc721Owner][msg.sender]
) {
revert Unauthorized();
}
getApproved[id_] = spender_;
emit ERC721Events.Approval(erc721Owner, spender_, id_);
}
/// @dev Providing type(uint256).max for approval value results in an/// unlimited approval that is not deducted from on transfers.functionerc20Approve(address spender_,
uint256 value_
) publicvirtualreturns (bool) {
// Prevent granting 0x0 an ERC-20 allowance.if (spender_ ==address(0)) {
revert InvalidSpender();
}
allowance[msg.sender][spender_] = value_;
emit ERC20Events.Approval(msg.sender, spender_, value_);
returntrue;
}
/// @notice Function for ERC-721 approvalsfunctionsetApprovalForAll(address operator_,
bool approved_
) publicvirtual{
// Prevent approvals to 0x0.if (operator_ ==address(0)) {
revert InvalidOperator();
}
isApprovedForAll[msg.sender][operator_] = approved_;
emit ERC721Events.ApprovalForAll(msg.sender, operator_, approved_);
}
/// @notice Function for mixed transfers from an operator that may be different than 'from'./// @dev This function assumes the operator is attempting to transfer an ERC-721/// if valueOrId is a possible valid token id.functiontransferFrom(address from_,
address to_,
uint256 valueOrId_
) publicvirtualreturns (bool) {
if (_isValidTokenId(valueOrId_)) {
erc721TransferFrom(from_, to_, valueOrId_);
} else {
// Intention is to transfer as ERC-20 token (value).return erc20TransferFrom(from_, to_, valueOrId_);
}
returntrue;
}
/// @notice Function for ERC-721 transfers from./// @dev This function is recommended for ERC721 transfers.functionerc721TransferFrom(address from_,
address to_,
uint256 id_
) publicvirtual{
// Prevent minting tokens from 0x0.if (from_ ==address(0)) {
revert InvalidSender();
}
// Prevent burning tokens to 0x0.if (to_ ==address(0)) {
revert InvalidRecipient();
}
if (from_ != _getOwnerOf(id_)) {
revert Unauthorized();
}
// Check that the operator is either the sender or approved for the transfer.if (
msg.sender!= from_ &&!isApprovedForAll[from_][msg.sender] &&msg.sender!= getApproved[id_]
) {
revert Unauthorized();
}
// We only need to check ERC-721 transfer exempt status for the recipient// since the sender being ERC-721 transfer exempt means they have already// had their ERC-721s stripped away during the rebalancing process.if (erc721TransferExempt(to_)) {
revert RecipientIsERC721TransferExempt();
}
// Transfer 1 * units ERC-20 and 1 ERC-721 token.// ERC-721 transfer exemptions handled above. Can't make it to this point if either is transfer exempt.
_transferERC20(from_, to_, units);
_transferERC721(from_, to_, id_);
}
/// @notice Function for ERC-20 transfers from./// @dev This function is recommended for ERC20 transfersfunctionerc20TransferFrom(address from_,
address to_,
uint256 value_
) publicvirtualreturns (bool) {
// Prevent minting tokens from 0x0.if (from_ ==address(0)) {
revert InvalidSender();
}
// Prevent burning tokens to 0x0.if (to_ ==address(0)) {
revert InvalidRecipient();
}
uint256 allowed = allowance[from_][msg.sender];
// Check that the operator has sufficient allowance.if (allowed !=type(uint256).max) {
allowance[from_][msg.sender] = allowed - value_;
}
// Transferring ERC-20s directly requires the _transferERC20WithERC721 function.// Handles ERC-721 exemptions internally.return _transferERC20WithERC721(from_, to_, value_);
}
/// @notice Function for ERC-20 transfers./// @dev This function assumes the operator is attempting to transfer as ERC-20/// given this function is only supported on the ERC-20 interface./// Treats even large amounts that are valid ERC-721 ids as ERC-20s.functiontransfer(address to_,
uint256 value_
) publicvirtualreturns (bool) {
// Prevent burning tokens to 0x0.if (to_ ==address(0)) {
revert InvalidRecipient();
}
// Transferring ERC-20s directly requires the _transferERC20WithERC721 function.// Handles ERC-721 exemptions internally.return _transferERC20WithERC721(msg.sender, to_, value_);
}
/// @notice Function for ERC-721 transfers with contract support./// This function only supports moving valid ERC-721 ids, as it does not exist on the ERC-20/// spec and will revert otherwise.functionsafeTransferFrom(address from_,
address to_,
uint256 id_
) publicvirtual{
safeTransferFrom(from_, to_, id_, "");
}
/// @notice Function for ERC-721 transfers with contract support and callback data./// This function only supports moving valid ERC-721 ids, as it does not exist on the/// ERC-20 spec and will revert otherwise.functionsafeTransferFrom(address from_,
address to_,
uint256 id_,
bytesmemory data_
) publicvirtual{
if (!_isValidTokenId(id_)) {
revert InvalidTokenId();
}
transferFrom(from_, to_, id_);
if (
to_.code.length!=0&&
IERC721Receiver(to_).onERC721Received(
msg.sender,
from_,
id_,
data_
) !=
IERC721Receiver.onERC721Received.selector
) {
revert UnsafeRecipient();
}
}
/// @notice Function for EIP-2612 permits (ERC-20 only)./// @dev Providing type(uint256).max for permit value results in an/// unlimited approval that is not deducted from on transfers.functionpermit(address owner_,
address spender_,
uint256 value_,
uint256 deadline_,
uint8 v_,
bytes32 r_,
bytes32 s_
) publicvirtual{
if (deadline_ <block.timestamp) {
revert PermitDeadlineExpired();
}
// permit cannot be used for ERC-721 token approvals, so ensure// the value does not fall within the valid range of ERC-721 token ids.if (_isValidTokenId(value_)) {
revert InvalidApproval();
}
if (spender_ ==address(0)) {
revert InvalidSpender();
}
unchecked {
address recoveredAddress =ecrecover(
keccak256(
abi.encodePacked(
"\x19\x01",
DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
keccak256(
"Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
),
owner_,
spender_,
value_,
nonces[owner_]++,
deadline_
)
)
)
),
v_,
r_,
s_
);
if (recoveredAddress ==address(0) || recoveredAddress != owner_) {
revert InvalidSigner();
}
allowance[recoveredAddress][spender_] = value_;
}
emit ERC20Events.Approval(owner_, spender_, value_);
}
/// @notice Returns domain initial domain separator, or recomputes if chain id is not equal to initial chain idfunctionDOMAIN_SEPARATOR() publicviewvirtualreturns (bytes32) {
returnblock.chainid== _INITIAL_CHAIN_ID
? _INITIAL_DOMAIN_SEPARATOR
: _computeDomainSeparator();
}
functionsupportsInterface(bytes4 interfaceId
) publicviewvirtualreturns (bool) {
return
interfaceId ==type(IERC404).interfaceId||
interfaceId ==type(IERC165).interfaceId;
}
/// @notice Function for self-exemptionfunctionsetSelfERC721TransferExempt(bool state_) publicvirtual{
_setERC721TransferExempt(msg.sender, state_);
}
/// @notice Function to check if address is transfer exemptfunctionerc721TransferExempt(address target_
) publicviewvirtualreturns (bool) {
return target_ ==address(0) || _erc721TransferExempt[target_];
}
/// @notice For a token token id to be considered valid, it just needs/// to fall within the range of possible token ids, it does not/// necessarily have to be minted yet.function_isValidTokenId(uint256 id_) internalpurereturns (bool) {
return id_ > ID_ENCODING_PREFIX && id_ !=type(uint256).max;
}
/// @notice Internal function to compute domain separator for EIP-2612 permitsfunction_computeDomainSeparator() internalviewvirtualreturns (bytes32) {
returnkeccak256(
abi.encode(
keccak256(
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
),
keccak256(bytes(name)),
keccak256("1"),
block.chainid,
address(this)
)
);
}
/// @notice This is the lowest level ERC-20 transfer function, which/// should be used for both normal ERC-20 transfers as well as minting./// Note that this function allows transfers to and from 0x0.function_transferERC20(address from_,
address to_,
uint256 value_
) internalvirtual{
// Minting is a special case for which we should not check the balance of// the sender, and we should increase the total supply.if (from_ ==address(0)) {
totalSupply += value_;
} else {
// Deduct value from sender's balance.
balanceOf[from_] -= value_;
}
// Update the recipient's balance.// Can be unchecked because on mint, adding to totalSupply is checked, and on transfer balance deduction is checked.unchecked {
balanceOf[to_] += value_;
}
emit ERC20Events.Transfer(from_, to_, value_);
}
/// @notice Consolidated record keeping function for transferring ERC-721s./// @dev Assign the token to the new owner, and remove from the old owner./// Note that this function allows transfers to and from 0x0./// Does not handle ERC-721 exemptions.function_transferERC721(address from_,
address to_,
uint256 id_
) internalvirtual{
// If this is not a mint, handle record keeping for transfer from previous owner.if (from_ !=address(0)) {
// On transfer of an NFT, any previous approval is reset.delete getApproved[id_];
uint256 updatedId = _owned[from_][_owned[from_].length-1];
if (updatedId != id_) {
uint256 updatedIndex = _getOwnedIndex(id_);
// update _owned for sender
_owned[from_][updatedIndex] = updatedId;
// update index for the moved id
_setOwnedIndex(updatedId, updatedIndex);
}
// pop
_owned[from_].pop();
}
// Check if this is a burn.if (to_ !=address(0)) {
// If not a burn, update the owner of the token to the new owner.// Update owner of the token to the new owner.
_setOwnerOf(id_, to_);
// Push token onto the new owner's stack.
_owned[to_].push(id_);
// Update index for new owner's stack.
_setOwnedIndex(id_, _owned[to_].length-1);
} else {
// If this is a burn, reset the owner of the token to 0x0 by deleting the token from _ownedData.delete _ownedData[id_];
}
emit ERC721Events.Transfer(from_, to_, id_);
}
/// @notice Internal function for ERC-20 transfers. Also handles any ERC-721 transfers that may be required.// Handles ERC-721 exemptions.function_transferERC20WithERC721(address from_,
address to_,
uint256 value_
) internalvirtualreturns (bool) {
uint256 erc20BalanceOfSenderBefore = erc20BalanceOf(from_);
uint256 erc20BalanceOfReceiverBefore = erc20BalanceOf(to_);
_transferERC20(from_, to_, value_);
// Preload for gas savings on branchesbool isFromERC721TransferExempt = erc721TransferExempt(from_);
bool isToERC721TransferExempt = erc721TransferExempt(to_);
// Skip _withdrawAndStoreERC721 and/or _retrieveOrMintERC721 for ERC-721 transfer exempt addresses// 1) to save gas// 2) because ERC-721 transfer exempt addresses won't always have/need ERC-721s corresponding to their ERC20s.if (isFromERC721TransferExempt && isToERC721TransferExempt) {
// Case 1) Both sender and recipient are ERC-721 transfer exempt. No ERC-721s need to be transferred.// NOOP.
} elseif (isFromERC721TransferExempt) {
// Case 2) The sender is ERC-721 transfer exempt, but the recipient is not. Contract should not attempt// to transfer ERC-721s from the sender, but the recipient should receive ERC-721s// from the bank/minted for any whole number increase in their balance.// Only cares about whole number increments.uint256 tokensToRetrieveOrMint = (balanceOf[to_] / units) -
(erc20BalanceOfReceiverBefore / units);
for (uint256 i =0; i < tokensToRetrieveOrMint; ) {
_retrieveOrMintERC721(to_);
unchecked {
++i;
}
}
} elseif (isToERC721TransferExempt) {
// Case 3) The sender is not ERC-721 transfer exempt, but the recipient is. Contract should attempt// to withdraw and store ERC-721s from the sender, but the recipient should not// receive ERC-721s from the bank/minted.// Only cares about whole number increments.uint256 tokensToWithdrawAndStore = (erc20BalanceOfSenderBefore /
units) - (balanceOf[from_] / units);
for (uint256 i =0; i < tokensToWithdrawAndStore; ) {
_withdrawAndStoreERC721(from_);
unchecked {
++i;
}
}
} else {
// Case 4) Neither the sender nor the recipient are ERC-721 transfer exempt.// Strategy:// 1. First deal with the whole tokens. These are easy and will just be transferred.// 2. Look at the fractional part of the value:// a) If it causes the sender to lose a whole token that was represented by an NFT due to a// fractional part being transferred, withdraw and store an additional NFT from the sender.// b) If it causes the receiver to gain a whole new token that should be represented by an NFT// due to receiving a fractional part that completes a whole token, retrieve or mint an NFT to the recevier.// Whole tokens worth of ERC-20s get transferred as ERC-721s without any burning/minting.uint256 nftsToTransfer = value_ / units;
for (uint256 i =0; i < nftsToTransfer; ) {
// Pop from sender's ERC-721 stack and transfer them (LIFO)uint256 indexOfLastToken = _owned[from_].length-1;
uint256 tokenId = _owned[from_][indexOfLastToken];
_transferERC721(from_, to_, tokenId);
unchecked {
++i;
}
}
// If the transfer changes either the sender or the recipient's holdings from a fractional to a non-fractional// amount (or vice versa), adjust ERC-721s.// First check if the send causes the sender to lose a whole token that was represented by an ERC-721// due to a fractional part being transferred.//// Process:// Take the difference between the whole number of tokens before and after the transfer for the sender.// If that difference is greater than the number of ERC-721s transferred (whole units), then there was// an additional ERC-721 lost due to the fractional portion of the transfer.// If this is a self-send and the before and after balances are equal (not always the case but often),// then no ERC-721s will be lost here.if (
erc20BalanceOfSenderBefore /
units -
erc20BalanceOf(from_) /
units >
nftsToTransfer
) {
_withdrawAndStoreERC721(from_);
}
// Then, check if the transfer causes the receiver to gain a whole new token which requires gaining// an additional ERC-721.//// Process:// Take the difference between the whole number of tokens before and after the transfer for the recipient.// If that difference is greater than the number of ERC-721s transferred (whole units), then there was// an additional ERC-721 gained due to the fractional portion of the transfer.// Again, for self-sends where the before and after balances are equal, no ERC-721s will be gained here.if (
erc20BalanceOf(to_) /
units -
erc20BalanceOfReceiverBefore /
units >
nftsToTransfer
) {
_retrieveOrMintERC721(to_);
}
}
returntrue;
}
/// @notice Internal function for ERC20 minting/// @dev This function will allow minting of new ERC20s./// If mintCorrespondingERC721s_ is true, and the recipient is not ERC-721 exempt, it will/// also mint the corresponding ERC721s./// Handles ERC-721 exemptions.function_mintERC20(address to_, uint256 value_) internalvirtual{
/// You cannot mint to the zero address (you can't mint and immediately burn in the same transfer).if (to_ ==address(0)) {
revert InvalidRecipient();
}
if (totalSupply + value_ > ID_ENCODING_PREFIX) {
revert MintLimitReached();
}
_transferERC20WithERC721(address(0), to_, value_);
}
/// @notice Internal function for ERC-721 minting and retrieval from the bank./// @dev This function will allow minting of new ERC-721s up to the total fractional supply. It will/// first try to pull from the bank, and if the bank is empty, it will mint a new token./// Does not handle ERC-721 exemptions.function_retrieveOrMintERC721(address to_) internalvirtual{
if (to_ ==address(0)) {
revert InvalidRecipient();
}
uint256 id;
if (!_storedERC721Ids.empty()) {
// If there are any tokens in the bank, use those first.// Pop off the end of the queue (FIFO).
id = _storedERC721Ids.popBack();
} else {
// Otherwise, mint a new token, should not be able to go over the total fractional supply.++minted;
// Reserve max uint256 for approvalsif (minted ==type(uint256).max) {
revert MintLimitReached();
}
id = ID_ENCODING_PREFIX + minted;
}
address erc721Owner = _getOwnerOf(id);
// The token should not already belong to anyone besides 0x0 or this contract.// If it does, something is wrong, as this should never happen.if (erc721Owner !=address(0)) {
revert AlreadyExists();
}
// Transfer the token to the recipient, either transferring from the contract's bank or minting.// Does not handle ERC-721 exemptions.
_transferERC721(erc721Owner, to_, id);
}
/// @notice Internal function for ERC-721 deposits to bank (this contract)./// @dev This function will allow depositing of ERC-721s to the bank, which can be retrieved by future minters.// Does not handle ERC-721 exemptions.function_withdrawAndStoreERC721(address from_) internalvirtual{
if (from_ ==address(0)) {
revert InvalidSender();
}
// Retrieve the latest token added to the owner's stack (LIFO).uint256 id = _owned[from_][_owned[from_].length-1];
// Transfer to 0x0.// Does not handle ERC-721 exemptions.
_transferERC721(from_, address(0), id);
// Record the token in the contract's bank queue.
_storedERC721Ids.pushFront(id);
}
/// @notice Initialization function to set pairs / etc, saving gas by avoiding mint / burn on unnecessary targetsfunction_setERC721TransferExempt(address target_,
bool state_
) internalvirtual{
if (target_ ==address(0)) {
revert InvalidExemption();
}
// Adjust the ERC721 balances of the target to respect exemption rules.// Despite this logic, it is still recommended practice to exempt prior to the target// having an active balance.if (state_) {
_clearERC721Balance(target_);
} else {
_reinstateERC721Balance(target_);
}
_erc721TransferExempt[target_] = state_;
}
/// @notice Function to reinstate balance on exemption removalfunction_reinstateERC721Balance(address target_) private{
uint256 expectedERC721Balance = erc20BalanceOf(target_) / units;
uint256 actualERC721Balance = erc721BalanceOf(target_);
for (uint256 i =0; i < expectedERC721Balance - actualERC721Balance; ) {
// Transfer ERC721 balance in from pool
_retrieveOrMintERC721(target_);
unchecked {
++i;
}
}
}
/// @notice Function to clear balance on exemption inclusionfunction_clearERC721Balance(address target_) private{
uint256 erc721Balance = erc721BalanceOf(target_);
for (uint256 i =0; i < erc721Balance; ) {
// Transfer out ERC721 balance
_withdrawAndStoreERC721(target_);
unchecked {
++i;
}
}
}
function_getOwnerOf(uint256 id_
) internalviewvirtualreturns (address ownerOf_) {
uint256 data = _ownedData[id_];
assembly {
ownerOf_ :=and(data, _BITMASK_ADDRESS)
}
}
function_setOwnerOf(uint256 id_, address owner_) internalvirtual{
uint256 data = _ownedData[id_];
assembly {
data :=add(
and(data, _BITMASK_OWNED_INDEX),
and(owner_, _BITMASK_ADDRESS)
)
}
_ownedData[id_] = data;
}
function_getOwnedIndex(uint256 id_
) internalviewvirtualreturns (uint256 ownedIndex_) {
uint256 data = _ownedData[id_];
assembly {
ownedIndex_ :=shr(160, data)
}
}
function_setOwnedIndex(uint256 id_, uint256 index_) internalvirtual{
uint256 data = _ownedData[id_];
if (index_ > _BITMASK_OWNED_INDEX >>160) {
revert OwnedIndexOverflow();
}
assembly {
data :=add(
and(data, _BITMASK_ADDRESS),
and(shl(160, index_), _BITMASK_OWNED_INDEX)
)
}
_ownedData[id_] = data;
}
}
// SPDX-License-Identifier: MITpragmasolidity ^0.8.4;/// @title Contains 512-bit math functions/// @notice Facilitates multiplication and division that can have overflow of an intermediate value without any loss of precision/// @dev Handles "phantom overflow" i.e., allows multiplication and division where an intermediate value overflows 256 bitslibraryFullMath{
/// @notice Calculates floor(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0/// @param a The multiplicand/// @param b The multiplier/// @param denominator The divisor/// @return result The 256-bit result/// @dev Credit to Remco Bloemen under MIT license https://xn--2-umb.com/21/muldivfunctionmulDiv(uint256 a,
uint256 b,
uint256 denominator
) internalpurereturns (uint256 result) {
unchecked {
// 512-bit multiply [prod1 prod0] = a * b// Compute the product mod 2**256 and mod 2**256 - 1// then 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 + prod0uint256 prod0; // Least significant 256 bits of the productuint256 prod1; // Most significant 256 bits of the productassembly {
let mm :=mulmod(a, b, not(0))
prod0 :=mul(a, b)
prod1 :=sub(sub(mm, prod0), lt(mm, prod0))
}
// Handle non-overflow cases, 256 by 256 divisionif (prod1 ==0) {
require(denominator >0);
assembly {
result :=div(prod0, denominator)
}
return result;
}
// Make sure the result is less than 2**256.// Also prevents denominator == 0require(denominator > prod1);
///////////////////////////////////////////////// 512 by 256 division.///////////////////////////////////////////////// Make division exact by subtracting the remainder from [prod1 prod0]// Compute remainder using mulmoduint256 remainder;
assembly {
remainder :=mulmod(a, b, denominator)
}
// Subtract 256 bit number from 512 bit numberassembly {
prod1 :=sub(prod1, gt(remainder, prod0))
prod0 :=sub(prod0, remainder)
}
// Factor powers of two out of denominator// Compute largest power of two divisor of denominator.// Always >= 1.// EDIT for 0.8 compatibility:// see: https://ethereum.stackexchange.com/questions/96642/unary-operator-cannot-be-applied-to-type-uint256uint256 twos = denominator & (~denominator +1);
// Divide denominator by power of twoassembly {
denominator :=div(denominator, twos)
}
// Divide [prod1 prod0] by the factors of twoassembly {
prod0 :=div(prod0, twos)
}
// Shift in bits from prod1 into prod0. For this we need// to flip `twos` such that it is 2**256 / twos.// If twos is zero, then it becomes oneassembly {
twos :=add(div(sub(0, twos), twos), 1)
}
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// correct for four bits. That is, denominator * inv = 1 mod 2**4uint256 inv = (3* denominator) ^2;
// Now use 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.
inv *=2- denominator * inv; // inverse mod 2**8
inv *=2- denominator * inv; // inverse mod 2**16
inv *=2- denominator * inv; // inverse mod 2**32
inv *=2- denominator * inv; // inverse mod 2**64
inv *=2- denominator * inv; // inverse mod 2**128
inv *=2- denominator * inv; // 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 precoditions 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 * inv;
return result;
}
}
/// @notice Calculates ceil(a×b÷denominator) with full precision. Throws if result overflows a uint256 or denominator == 0/// @param a The multiplicand/// @param b The multiplier/// @param denominator The divisor/// @return result The 256-bit resultfunctionmulDivRoundingUp(uint256 a,
uint256 b,
uint256 denominator
) internalpurereturns (uint256 result) {
result = mulDiv(a, b, denominator);
if (mulmod(a, b, denominator) >0) {
require(result <type(uint256).max);
result++;
}
}
}
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC721/IERC721.sol)pragmasolidity ^0.8.20;import {IERC165} from"../../utils/introspection/IERC165.sol";
/**
* @dev Required interface of an ERC721 compliant contract.
*/interfaceIERC721isIERC165{
/**
* @dev Emitted when `tokenId` token is transferred from `from` to `to`.
*/eventTransfer(addressindexedfrom, addressindexed to, uint256indexed tokenId);
/**
* @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
*/eventApproval(addressindexed owner, addressindexed approved, uint256indexed tokenId);
/**
* @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
*/eventApprovalForAll(addressindexed owner, addressindexed operator, bool approved);
/**
* @dev Returns the number of tokens in ``owner``'s account.
*/functionbalanceOf(address owner) externalviewreturns (uint256 balance);
/**
* @dev Returns the owner of the `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/functionownerOf(uint256 tokenId) externalviewreturns (address owner);
/**
* @dev Safely transfers `tokenId` token from `from` to `to`.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon
* a safe transfer.
*
* Emits a {Transfer} event.
*/functionsafeTransferFrom(addressfrom, address to, uint256 tokenId, bytescalldata data) external;
/**
* @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
* are aware of the ERC721 protocol to prevent tokens from being forever locked.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must have been allowed to move this token by either {approve} or
* {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon
* a safe transfer.
*
* Emits a {Transfer} event.
*/functionsafeTransferFrom(addressfrom, address to, uint256 tokenId) external;
/**
* @dev Transfers `tokenId` token from `from` to `to`.
*
* WARNING: Note that the caller is responsible to confirm that the recipient is capable of receiving ERC721
* or else they may be permanently lost. Usage of {safeTransferFrom} prevents loss, though the caller must
* understand this adds an external call which potentially creates a reentrancy vulnerability.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
*
* Emits a {Transfer} event.
*/functiontransferFrom(addressfrom, address to, uint256 tokenId) external;
/**
* @dev Gives permission to `to` to transfer `tokenId` token to another account.
* The approval is cleared when the token is transferred.
*
* Only a single account can be approved at a time, so approving the zero address clears previous approvals.
*
* Requirements:
*
* - The caller must own the token or be an approved operator.
* - `tokenId` must exist.
*
* Emits an {Approval} event.
*/functionapprove(address to, uint256 tokenId) external;
/**
* @dev Approve or remove `operator` as an operator for the caller.
* Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
*
* Requirements:
*
* - The `operator` cannot be the address zero.
*
* Emits an {ApprovalForAll} event.
*/functionsetApprovalForAll(address operator, bool approved) external;
/**
* @dev Returns the account approved for `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/functiongetApproved(uint256 tokenId) externalviewreturns (address operator);
/**
* @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
*
* See {setApprovalForAll}
*/functionisApprovedForAll(address owner, address operator) externalviewreturns (bool);
}
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v5.0.0) (token/ERC721/IERC721Receiver.sol)pragmasolidity ^0.8.20;/**
* @title ERC721 token receiver interface
* @dev Interface for any contract that wants to support safeTransfers
* from ERC721 asset contracts.
*/interfaceIERC721Receiver{
/**
* @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
* by `operator` from `from`, this function is called.
*
* It must return its Solidity selector to confirm the token transfer.
* If any other value is returned or the interface is not implemented by the recipient, the transfer will be
* reverted.
*
* The selector can be obtained in Solidity with `IERC721Receiver.onERC721Received.selector`.
*/functiononERC721Received(address operator,
addressfrom,
uint256 tokenId,
bytescalldata data
) externalreturns (bytes4);
}
Contract Source Code
File 13 of 22: Math.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v5.0.0) (utils/math/Math.sol)pragmasolidity ^0.8.20;/**
* @dev Standard math utilities missing in the Solidity language.
*/libraryMath{
/**
* @dev Muldiv operation overflow.
*/errorMathOverflowedMulDiv();
enumRounding {
Floor, // Toward negative infinity
Ceil, // Toward positive infinity
Trunc, // Toward zero
Expand // Away from zero
}
/**
* @dev Returns the addition of two unsigned integers, with an overflow flag.
*/functiontryAdd(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
unchecked {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
}
/**
* @dev Returns the subtraction of two unsigned integers, with an overflow flag.
*/functiontrySub(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
unchecked {
if (b > a) return (false, 0);
return (true, a - b);
}
}
/**
* @dev Returns the multiplication of two unsigned integers, with an overflow flag.
*/functiontryMul(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
unchecked {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the// benefit is lost if 'b' is also tested.// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522if (a ==0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
}
/**
* @dev Returns the division of two unsigned integers, with a division by zero flag.
*/functiontryDiv(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
unchecked {
if (b ==0) return (false, 0);
return (true, a / b);
}
}
/**
* @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
*/functiontryMod(uint256 a, uint256 b) internalpurereturns (bool, uint256) {
unchecked {
if (b ==0) return (false, 0);
return (true, a % b);
}
}
/**
* @dev Returns the largest of two numbers.
*/functionmax(uint256 a, uint256 b) internalpurereturns (uint256) {
return a > b ? a : b;
}
/**
* @dev Returns the smallest of two numbers.
*/functionmin(uint256 a, uint256 b) internalpurereturns (uint256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two numbers. The result is rounded towards
* zero.
*/functionaverage(uint256 a, uint256 b) internalpurereturns (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 towards infinity instead
* of rounding towards zero.
*/functionceilDiv(uint256 a, uint256 b) internalpurereturns (uint256) {
if (b ==0) {
// Guarantee the same behavior as in a regular Solidity division.return a / b;
}
// (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.
*/functionmulDiv(uint256 x, uint256 y, uint256 denominator) internalpurereturns (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 = x * y; // Least significant 256 bits of the productuint256 prod1; // Most significant 256 bits of the productassembly {
let mm :=mulmod(x, y, not(0))
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.if (denominator <= prod1) {
revert MathOverflowedMulDiv();
}
///////////////////////////////////////////////// 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.uint256 twos = denominator & (0- denominator);
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.
*/functionmulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internalpurereturns (uint256) {
uint256 result = mulDiv(x, y, denominator);
if (unsignedRoundsUp(rounding) &&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
* towards zero.
*
* Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
*/functionsqrt(uint256 a) internalpurereturns (uint256) {
if (a ==0) {
return0;
}
// 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.
*/functionsqrt(uint256 a, Rounding rounding) internalpurereturns (uint256) {
unchecked {
uint256 result = sqrt(a);
return result + (unsignedRoundsUp(rounding) && result * result < a ? 1 : 0);
}
}
/**
* @dev Return the log in base 2 of a positive value rounded towards zero.
* Returns 0 if given 0.
*/functionlog2(uint256 value) internalpurereturns (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.
*/functionlog2(uint256 value, Rounding rounding) internalpurereturns (uint256) {
unchecked {
uint256 result =log2(value);
return result + (unsignedRoundsUp(rounding) &&1<< result < value ? 1 : 0);
}
}
/**
* @dev Return the log in base 10 of a positive value rounded towards zero.
* Returns 0 if given 0.
*/functionlog10(uint256 value) internalpurereturns (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.
*/functionlog10(uint256 value, Rounding rounding) internalpurereturns (uint256) {
unchecked {
uint256 result = log10(value);
return result + (unsignedRoundsUp(rounding) &&10** result < value ? 1 : 0);
}
}
/**
* @dev Return the log in base 256 of a positive value rounded towards zero.
* 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.
*/functionlog256(uint256 value) internalpurereturns (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.
*/functionlog256(uint256 value, Rounding rounding) internalpurereturns (uint256) {
unchecked {
uint256 result = log256(value);
return result + (unsignedRoundsUp(rounding) &&1<< (result <<3) < value ? 1 : 0);
}
}
/**
* @dev Returns whether a provided rounding mode is considered rounding up for unsigned integers.
*/functionunsignedRoundsUp(Rounding rounding) internalpurereturns (bool) {
returnuint8(rounding) %2==1;
}
}
Contract Source Code
File 14 of 22: Ownable.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)pragmasolidity ^0.8.20;import {Context} from"../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.
*
* The initial owner is set to the address provided by the deployer. 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.
*/abstractcontractOwnableisContext{
addressprivate _owner;
/**
* @dev The caller account is not authorized to perform an operation.
*/errorOwnableUnauthorizedAccount(address account);
/**
* @dev The owner is not a valid owner account. (eg. `address(0)`)
*/errorOwnableInvalidOwner(address owner);
eventOwnershipTransferred(addressindexed previousOwner, addressindexed newOwner);
/**
* @dev Initializes the contract setting the address provided by the deployer as the initial owner.
*/constructor(address initialOwner) {
if (initialOwner ==address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
/**
* @dev Throws if called by any account other than the owner.
*/modifieronlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/functionowner() publicviewvirtualreturns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/function_checkOwner() internalviewvirtual{
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
/**
* @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.
*/functionrenounceOwnership() publicvirtualonlyOwner{
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/functiontransferOwnership(address newOwner) publicvirtualonlyOwner{
if (newOwner ==address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/function_transferOwnership(address newOwner) internalvirtual{
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
Contract Source Code
File 15 of 22: PoolAddress.sol
// SPDX-License-Identifier: GPL-2.0-or-laterpragmasolidity >=0.5.0;/// @title PoolAddress modified to have <0.8 POOL_INIT_CODE_HASHlibraryPoolAddress{
bytes32internalconstant POOL_INIT_CODE_HASH =0xe34f199b19b2b4f47f68442619d555527d244f78a3297ea89325f843f87b8b54;
/// @notice The identifying key of the poolstructPoolKey {
address token0;
address token1;
uint24 fee;
}
/// @notice Returns PoolKey: the ordered tokens with the matched fee levels/// @param tokenA The first token of a pool, unsorted/// @param tokenB The second token of a pool, unsorted/// @param fee The fee level of the pool/// @return Poolkey The pool details with ordered token0 and token1 assignmentsfunctiongetPoolKey(address tokenA,
address tokenB,
uint24 fee
) internalpurereturns (PoolKey memory) {
if (tokenA > tokenB) (tokenA, tokenB) = (tokenB, tokenA);
return PoolKey({token0: tokenA, token1: tokenB, fee: fee});
}
/// @notice Deterministically computes the pool address given the factory and PoolKey/// @param factory The Uniswap V3 factory contract address/// @param key The PoolKey/// @return pool The contract address of the V3 poolfunctioncomputeAddress(address factory,
PoolKey memory key
) internalpurereturns (address pool) {
require(key.token0 < key.token1);
pool =address(
uint160(
uint256(
keccak256(
abi.encodePacked(
hex"ff",
factory,
keccak256(
abi.encode(key.token0, key.token1, key.fee)
),
POOL_INIT_CODE_HASH
)
)
)
)
);
}
}
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v5.0.0) (utils/math/SignedMath.sol)pragmasolidity ^0.8.20;/**
* @dev Standard signed math utilities missing in the Solidity language.
*/librarySignedMath{
/**
* @dev Returns the largest of two signed numbers.
*/functionmax(int256 a, int256 b) internalpurereturns (int256) {
return a > b ? a : b;
}
/**
* @dev Returns the smallest of two signed numbers.
*/functionmin(int256 a, int256 b) internalpurereturns (int256) {
return a < b ? a : b;
}
/**
* @dev Returns the average of two signed numbers without overflow.
* The result is rounded towards zero.
*/functionaverage(int256 a, int256 b) internalpurereturns (int256) {
// Formula from the book "Hacker's Delight"int256 x = (a & b) + ((a ^ b) >>1);
return x + (int256(uint256(x) >>255) & (a ^ b));
}
/**
* @dev Returns the absolute unsigned value of a signed value.
*/functionabs(int256 n) internalpurereturns (uint256) {
unchecked {
// must be unchecked in order to support `n = type(int256).min`returnuint256(n >=0 ? n : -n);
}
}
}
Contract Source Code
File 20 of 22: Strings.sol
// SPDX-License-Identifier: MIT// OpenZeppelin Contracts (last updated v5.0.0) (utils/Strings.sol)pragmasolidity ^0.8.20;import {Math} from"./math/Math.sol";
import {SignedMath} from"./math/SignedMath.sol";
/**
* @dev String operations.
*/libraryStrings{
bytes16privateconstant HEX_DIGITS ="0123456789abcdef";
uint8privateconstant ADDRESS_LENGTH =20;
/**
* @dev The `value` string doesn't fit in the specified `length`.
*/errorStringsInsufficientHexLength(uint256 value, uint256 length);
/**
* @dev Converts a `uint256` to its ASCII `string` decimal representation.
*/functiontoString(uint256 value) internalpurereturns (stringmemory) {
unchecked {
uint256 length = Math.log10(value) +1;
stringmemory buffer =newstring(length);
uint256 ptr;
/// @solidity memory-safe-assemblyassembly {
ptr :=add(buffer, add(32, length))
}
while (true) {
ptr--;
/// @solidity memory-safe-assemblyassembly {
mstore8(ptr, byte(mod(value, 10), HEX_DIGITS))
}
value /=10;
if (value ==0) break;
}
return buffer;
}
}
/**
* @dev Converts a `int256` to its ASCII `string` decimal representation.
*/functiontoStringSigned(int256 value) internalpurereturns (stringmemory) {
returnstring.concat(value <0 ? "-" : "", toString(SignedMath.abs(value)));
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
*/functiontoHexString(uint256 value) internalpurereturns (stringmemory) {
unchecked {
return toHexString(value, Math.log256(value) +1);
}
}
/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
*/functiontoHexString(uint256 value, uint256 length) internalpurereturns (stringmemory) {
uint256 localValue = value;
bytesmemory buffer =newbytes(2* length +2);
buffer[0] ="0";
buffer[1] ="x";
for (uint256 i =2* length +1; i >1; --i) {
buffer[i] = HEX_DIGITS[localValue &0xf];
localValue >>=4;
}
if (localValue !=0) {
revert StringsInsufficientHexLength(value, length);
}
returnstring(buffer);
}
/**
* @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal
* representation.
*/functiontoHexString(address addr) internalpurereturns (stringmemory) {
return toHexString(uint256(uint160(addr)), ADDRESS_LENGTH);
}
/**
* @dev Returns true if the two strings are equal.
*/functionequal(stringmemory a, stringmemory b) internalpurereturns (bool) {
returnbytes(a).length==bytes(b).length&&keccak256(bytes(a)) ==keccak256(bytes(b));
}
}
Contract Source Code
File 21 of 22: TickMath.sol
// SPDX-License-Identifier: MITpragmasolidity ^0.8.4;/// @title Math library for computing sqrt prices from ticks and vice versa/// @notice Computes sqrt price for ticks of size 1.0001, i.e. sqrt(1.0001^tick) as fixed point Q64.96 numbers. Supports/// prices between 2**-128 and 2**128libraryTickMath{
/// @dev The minimum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**-128int24internalconstant MIN_TICK =-887272;
/// @dev The maximum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**128int24internalconstant MAX_TICK =-MIN_TICK;
/// @dev The minimum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MIN_TICK)uint160internalconstant MIN_SQRT_RATIO =4295128739;
/// @dev The maximum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MAX_TICK)uint160internalconstant MAX_SQRT_RATIO =1461446703485210103287273052203988822378723970342;
/// @notice Calculates sqrt(1.0001^tick) * 2^96/// @dev Throws if |tick| > max tick/// @param tick The input tick for the above formula/// @return sqrtPriceX96 A Fixed point Q64.96 number representing the sqrt of the ratio of the two assets (token1/token0)/// at the given tickfunctiongetSqrtRatioAtTick(int24 tick
) internalpurereturns (uint160 sqrtPriceX96) {
uint256 absTick = tick <0
? uint256(-int256(tick))
: uint256(int256(tick));
// EDIT: 0.8 compatibilityrequire(absTick <=uint256(int256(MAX_TICK)), "T");
uint256 ratio = absTick &0x1!=0
? 0xfffcb933bd6fad37aa2d162d1a594001
: 0x100000000000000000000000000000000;
if (absTick &0x2!=0)
ratio = (ratio *0xfff97272373d413259a46990580e213a) >>128;
if (absTick &0x4!=0)
ratio = (ratio *0xfff2e50f5f656932ef12357cf3c7fdcc) >>128;
if (absTick &0x8!=0)
ratio = (ratio *0xffe5caca7e10e4e61c3624eaa0941cd0) >>128;
if (absTick &0x10!=0)
ratio = (ratio *0xffcb9843d60f6159c9db58835c926644) >>128;
if (absTick &0x20!=0)
ratio = (ratio *0xff973b41fa98c081472e6896dfb254c0) >>128;
if (absTick &0x40!=0)
ratio = (ratio *0xff2ea16466c96a3843ec78b326b52861) >>128;
if (absTick &0x80!=0)
ratio = (ratio *0xfe5dee046a99a2a811c461f1969c3053) >>128;
if (absTick &0x100!=0)
ratio = (ratio *0xfcbe86c7900a88aedcffc83b479aa3a4) >>128;
if (absTick &0x200!=0)
ratio = (ratio *0xf987a7253ac413176f2b074cf7815e54) >>128;
if (absTick &0x400!=0)
ratio = (ratio *0xf3392b0822b70005940c7a398e4b70f3) >>128;
if (absTick &0x800!=0)
ratio = (ratio *0xe7159475a2c29b7443b29c7fa6e889d9) >>128;
if (absTick &0x1000!=0)
ratio = (ratio *0xd097f3bdfd2022b8845ad8f792aa5825) >>128;
if (absTick &0x2000!=0)
ratio = (ratio *0xa9f746462d870fdf8a65dc1f90e061e5) >>128;
if (absTick &0x4000!=0)
ratio = (ratio *0x70d869a156d2a1b890bb3df62baf32f7) >>128;
if (absTick &0x8000!=0)
ratio = (ratio *0x31be135f97d08fd981231505542fcfa6) >>128;
if (absTick &0x10000!=0)
ratio = (ratio *0x9aa508b5b7a84e1c677de54f3e99bc9) >>128;
if (absTick &0x20000!=0)
ratio = (ratio *0x5d6af8dedb81196699c329225ee604) >>128;
if (absTick &0x40000!=0)
ratio = (ratio *0x2216e584f5fa1ea926041bedfe98) >>128;
if (absTick &0x80000!=0)
ratio = (ratio *0x48a170391f7dc42444e8fa2) >>128;
if (tick >0) ratio =type(uint256).max/ ratio;
// this divides by 1<<32 rounding up to go from a Q128.128 to a Q128.96.// we then downcast because we know the result always fits within 160 bits due to our tick input constraint// we round up in the division so getTickAtSqrtRatio of the output price is always consistent
sqrtPriceX96 =uint160(
(ratio >>32) + (ratio % (1<<32) ==0 ? 0 : 1)
);
}
/// @notice Calculates the greatest tick value such that getRatioAtTick(tick) <= ratio/// @dev Throws in case sqrtPriceX96 < MIN_SQRT_RATIO, as MIN_SQRT_RATIO is the lowest value getRatioAtTick may/// ever return./// @param sqrtPriceX96 The sqrt ratio for which to compute the tick as a Q64.96/// @return tick The greatest tick for which the ratio is less than or equal to the input ratiofunctiongetTickAtSqrtRatio(uint160 sqrtPriceX96
) internalpurereturns (int24 tick) {
// second inequality must be < because the price can never reach the price at the max tickrequire(
sqrtPriceX96 >= MIN_SQRT_RATIO && sqrtPriceX96 < MAX_SQRT_RATIO,
"R"
);
uint256 ratio =uint256(sqrtPriceX96) <<32;
uint256 r = ratio;
uint256 msb =0;
assembly {
let f :=shl(7, gt(r, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF))
msb :=or(msb, f)
r :=shr(f, r)
}
assembly {
let f :=shl(6, gt(r, 0xFFFFFFFFFFFFFFFF))
msb :=or(msb, f)
r :=shr(f, r)
}
assembly {
let f :=shl(5, gt(r, 0xFFFFFFFF))
msb :=or(msb, f)
r :=shr(f, r)
}
assembly {
let f :=shl(4, gt(r, 0xFFFF))
msb :=or(msb, f)
r :=shr(f, r)
}
assembly {
let f :=shl(3, gt(r, 0xFF))
msb :=or(msb, f)
r :=shr(f, r)
}
assembly {
let f :=shl(2, gt(r, 0xF))
msb :=or(msb, f)
r :=shr(f, r)
}
assembly {
let f :=shl(1, gt(r, 0x3))
msb :=or(msb, f)
r :=shr(f, r)
}
assembly {
let f :=gt(r, 0x1)
msb :=or(msb, f)
}
if (msb >=128) r = ratio >> (msb -127);
else r = ratio << (127- msb);
int256 log_2 = (int256(msb) -128) <<64;
assembly {
r :=shr(127, mul(r, r))
let f :=shr(128, r)
log_2 :=or(log_2, shl(63, f))
r :=shr(f, r)
}
assembly {
r :=shr(127, mul(r, r))
let f :=shr(128, r)
log_2 :=or(log_2, shl(62, f))
r :=shr(f, r)
}
assembly {
r :=shr(127, mul(r, r))
let f :=shr(128, r)
log_2 :=or(log_2, shl(61, f))
r :=shr(f, r)
}
assembly {
r :=shr(127, mul(r, r))
let f :=shr(128, r)
log_2 :=or(log_2, shl(60, f))
r :=shr(f, r)
}
assembly {
r :=shr(127, mul(r, r))
let f :=shr(128, r)
log_2 :=or(log_2, shl(59, f))
r :=shr(f, r)
}
assembly {
r :=shr(127, mul(r, r))
let f :=shr(128, r)
log_2 :=or(log_2, shl(58, f))
r :=shr(f, r)
}
assembly {
r :=shr(127, mul(r, r))
let f :=shr(128, r)
log_2 :=or(log_2, shl(57, f))
r :=shr(f, r)
}
assembly {
r :=shr(127, mul(r, r))
let f :=shr(128, r)
log_2 :=or(log_2, shl(56, f))
r :=shr(f, r)
}
assembly {
r :=shr(127, mul(r, r))
let f :=shr(128, r)
log_2 :=or(log_2, shl(55, f))
r :=shr(f, r)
}
assembly {
r :=shr(127, mul(r, r))
let f :=shr(128, r)
log_2 :=or(log_2, shl(54, f))
r :=shr(f, r)
}
assembly {
r :=shr(127, mul(r, r))
let f :=shr(128, r)
log_2 :=or(log_2, shl(53, f))
r :=shr(f, r)
}
assembly {
r :=shr(127, mul(r, r))
let f :=shr(128, r)
log_2 :=or(log_2, shl(52, f))
r :=shr(f, r)
}
assembly {
r :=shr(127, mul(r, r))
let f :=shr(128, r)
log_2 :=or(log_2, shl(51, f))
r :=shr(f, r)
}
assembly {
r :=shr(127, mul(r, r))
let f :=shr(128, r)
log_2 :=or(log_2, shl(50, f))
}
int256 log_sqrt10001 = log_2 *255738958999603826347141; // 128.128 numberint24 tickLow =int24(
(log_sqrt10001 -3402992956809132418596140100660247210) >>128
);
int24 tickHi =int24(
(log_sqrt10001 +291339464771989622907027621153398088495) >>128
);
tick = tickLow == tickHi
? tickLow
: getSqrtRatioAtTick(tickHi) <= sqrtPriceX96
? tickHi
: tickLow;
}
}