diff --git a/.prettierrc b/.prettierrc index 4ed64425..cb6ea45f 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,6 +1,16 @@ { - "semi": false, - "singleQuote": true, - "printWidth": 110, - "tabWidth": 4 -} + "plugins": ["prettier-plugin-solidity"], + "overrides": [ + { + "files": "*.sol", + "options": { + "parser": "solidity-parse", + "printWidth": 80, + "tabWidth": 4, + "useTabs": false, + "singleQuote": true, + "bracketSpacing": false + } + } + ] +} \ No newline at end of file diff --git a/.solcover.js b/.solcover.js index 2b7d939b..69fa9111 100644 --- a/.solcover.js +++ b/.solcover.js @@ -4,7 +4,7 @@ module.exports = { 'utils', 'libraries/TickMap.sol', 'libraries/EpochMap.sol', - 'libraries/utils/String.sol', + 'libraries/utils', 'external', 'staking/FinStaker.sol', ], diff --git a/LICENSE b/LICENSE new file mode 100644 index 00000000..5653a4da --- /dev/null +++ b/LICENSE @@ -0,0 +1,51 @@ +Business Source License 1.1 + +License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved. "Business Source License" is a trademark of MariaDB Corporation Ab. + +Parameters + +Licensor: Poolshark Labs Ltd + +Licensed Work: Poolshark Limit The Licensed Work is (C) 2023 Poolshark Labs Ltd + +Additional Use Grant: Any uses listed and defined at limit-license-grants.pshark.eth + +Change Date: The earlier of 2025-02-09 or a date specified at limit-license-date.pshark.eth + +Change License: Server Side Public License, v 1 + +Terms + +The Licensor hereby grants you the right to copy, modify, create derivative works, redistribute, and make non-production use of the Licensed Work. The Licensor may make an Additional Use Grant, above, permitting limited production use. + +Effective on the Change Date, or the fourth anniversary of the first publicly available distribution of a specific version of the Licensed Work under this License, whichever comes first, the Licensor hereby grants you rights under the terms of the Change License, and the rights granted in the paragraph above terminate. + +If your use of the Licensed Work does not comply with the requirements currently in effect as described in this License, you must purchase a commercial license from the Licensor, its affiliated entities, or authorized resellers, or you must refrain from using the Licensed Work. + +All copies of the original and modified Licensed Work, and derivative works of the Licensed Work, are subject to this License. This License applies separately for each version of the Licensed Work and the Change Date may vary for each version of the Licensed Work released by Licensor. + +You must conspicuously display this License on each original or modified copy of the Licensed Work. If you receive the Licensed Work in original or modified form from a third party, the terms and conditions set forth in this License apply to your use of that work. + +Any use of the Licensed Work in violation of this License will automatically terminate your rights under this License for the current and all other versions of the Licensed Work. + +This License does not grant you any right in any trademark or logo of Licensor or its affiliates (provided that you may use a trademark or logo of Licensor as expressly required by this License). + +TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND TITLE. + +MariaDB hereby grants you permission to use this License’s text to license your works, and to refer to it using the trademark "Business Source License", as long as you comply with the Covenants of Licensor below. + +Covenants of Licensor + +In consideration of the right to use this License’s text and the "Business Source License" name and trademark, Licensor covenants to MariaDB, and to all other recipients of the licensed work to be provided by Licensor: + +To specify as the Change License the GPL Version 3.0 or any later version, or a license that is compatible with GPL Version 3.0 or a later version, where "compatible" means that software provided under the Change License can be included in a program with software provided under GPL Version 3.0 or a later version. Licensor may specify additional Change Licenses without limitation. + +To either: (a) specify an additional grant of rights to use that does not impose any additional restriction on the right granted in this License, as the Additional Use Grant; or (b) insert the text "None". + +To specify a Change Date. + +Not to modify this License in any other way. + +Notice + +The Business Source License (this document, or the "License") is not an Open Source license. However, the Licensed Work will eventually be made available under an Open Source License, as stated in this License. \ No newline at end of file diff --git a/contracts/LimitPool.sol b/contracts/LimitPool.sol index 2e905937..8fa7e631 100644 --- a/contracts/LimitPool.sol +++ b/contracts/LimitPool.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import './interfaces/IPool.sol'; import './interfaces/range/IRangePool.sol'; @@ -22,8 +22,49 @@ import './libraries/math/ConstantProduct.sol'; import './external/solady/LibClone.sol'; import './external/openzeppelin/security/LimitReentrancyGuard.sol'; +/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#%@@@@%@@@@%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@%%%%%%@@%==========+++**#@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@@@@@@@@%%##%%%@@%%%@@%%*-=====+++**#**+*#*#*#@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@=+===========+#@@@@@%#@%%%%%%*#%%%%%##%@@@@@@@@#****++#%#**#%=%@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@+-==+++***++++=++@@%%%%%%%%*****#%#*#@%@@@@@@@@@@@@@%#+#%#*##*=#@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@+=+*******+++%%##%%####++++++**@@%%@@@@@@@@@@@@@@@###*#%#+==@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@%=**##**+#%###########+====*@%@@%%@@@@@@@@@@@@@%###%@+%=+=*@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@#%%####*##*##*#**#**+=+=-=*%%%###%@@@@@@@@@@@####@#=====+*@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@#%#***#***#*****=*+=+==--=@@%#**###@@@@@@@@@@@###@@%=+++=++#@@@@*@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@##*********+***##++*+#%@*%@@@@@####@@@@#=%@@@@@@@@@@@+==++=+=%%#+*@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@##*****+**+*********++=%%%@@@@@@@@@*@@@%===@@@@@@@@@*===++=+++**#%#@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@#**++++*+++++===+***+==#%%#@@@@@@@@*##@*===+%@@@@%+==+=+++++++*%@%%@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@**++++++++====----+++====+###@@@@@@@%#%%*==++*+#+===+++++++++++*%%@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%###*++++++====---=*@@@@+===-%@**%@@@@@@@#%%%===*+****+++*++++*+*+*#@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@*+++*+++++===--=-*@@@@@@@@=--=@@@#%@@@@@@@@#%%*++++**#+++*++**++***#*@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@%===+++++=+++===*@@@@@@@@@@=-=%@@@@@%#@@@@@@@*===*++=++*++++****+**#%+@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@*=+@@#==+==+=-=@@@%%@@@@@@@@@@@@#***%%@@@@%+===+++*+*******+**++*#%*#@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@======-==#***%#@@@@@@@@@@@##%%%%%%@#====****+*****#**###*+++#*@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%-===*=++**###@@@@@@@@@@@@@+#%%%%@*======+*********###+++++++++%@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%-=+#%%+*###%@@@@@@@@@@@@@@@#%#@%++=+++===**#*#****#%=====+++++++%@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@-=#****####@@@@@@@@@@@@@@@@#%@*++*#++++=+#*#####*#%%==============*@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@*=#**+*###@@@@@@@@@@@@@@@@@#%+**###**+++######%%#+-==--=-=------======#@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@++#++++*##%@@@@@@@@@@@@@@@%+*#%%######*##%#+=-=#@@@@@*----------------*@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@%+#*+++==+***#@@@@@@@@@@@%#%%####%%#*+#*++%@@@@@@@@@@@@@%+=======*@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@*+**+========------=-=%@#%%%%%%%%%%%%#%#%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@#=====-==-------=#@@@@########%%%%%%##++#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%##%%@@@@@@@@@@@@%#######%%#*+=++@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#*####++++=@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%##**+=#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@***%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/ -/// @notice Poolshark Limit Pool Implementation +/** + * @title Limit Pool - Constant Product + * @author Poolshark + * @author @alphak3y + */ contract LimitPool is ILimitPool, ILimitPoolView, @@ -31,35 +72,38 @@ contract LimitPool is LimitPoolImmutables, LimitReentrancyGuard { - + /// @notice This modifier only allows `owner` to call a function modifier ownerOnly() { _onlyOwner(); _; } + /// @notice This modifier only allows `factory` to call a function modifier factoryOnly() { _onlyFactory(); _; } + /// @notice This modifier checks for canoncial limit pools modifier canonicalOnly() { _onlyCanoncialClones(); _; } + /// @notice The original address of the deployed contract address public immutable original; + /// @notice The factory address for the `initialize()` call address public immutable factory; - constructor( - address factory_ - ) { + constructor(address factory_) { original = address(this); factory = factory_; } - function initialize( - uint160 startPrice - ) external + /// @notice Initializes the LimitPool contract storage + /// @param startPrice the Q64.96 sqrt price to start the pool from + function initialize(uint160 startPrice) + external nonReentrant(globalState) factoryOnly canonicalOnly @@ -75,227 +119,251 @@ contract LimitPool is ); } - function mintRange( - MintRangeParams memory params - ) external + /// @notice Adds bidirectional liquidity (RangePosition) + /// @param params the params for minting the position + /// @dev See PoolsharkStructs.sol for struct data + function mintRange(MintRangeParams memory params) + external nonReentrant(globalState) canonicalOnly + returns (int256, int256) { MintRangeCache memory cache; cache.constants = immutables(); - MintRangeCall.perform( - positions, - ticks, - rangeTickMap, - samples, - globalState, - cache, - params - ); + return + MintRangeCall.perform( + positions, + ticks, + rangeTickMap, + samples, + globalState, + cache, + params + ); } - function burnRange( - BurnRangeParams memory params - ) external + /// @notice Removes bidirectional liquidity (RangePosition) + /// @param params the params for burning the position + /// @dev See PoolsharkStructs.sol for struct data + function burnRange(BurnRangeParams memory params) + external nonReentrant(globalState) canonicalOnly + returns (int256, int256) { BurnRangeCache memory cache; cache.constants = immutables(); - BurnRangeCall.perform( - positions, - ticks, - rangeTickMap, - samples, - globalState, - cache, - params - ); + return + BurnRangeCall.perform( + positions, + ticks, + rangeTickMap, + samples, + globalState, + cache, + params + ); } - //limitSwap - function mintLimit( - MintLimitParams memory params - ) external + /// @notice Adds directional liquidity (LimitPosition) + /// @param params the params for minting the position + /// @dev See PoolsharkStructs.sol for struct data + function mintLimit(MintLimitParams memory params) + external nonReentrant(globalState) canonicalOnly + returns (int256, int256) { MintLimitCache memory cache; cache.constants = immutables(); - MintLimitCall.perform( - params.zeroForOne ? positions0 : positions1, - ticks, - samples, - rangeTickMap, - limitTickMap, - globalState, - params, - cache - ); + return + MintLimitCall.perform( + params.zeroForOne ? positions0 : positions1, + ticks, + samples, + rangeTickMap, + limitTickMap, + globalState, + params, + cache + ); } - function burnLimit( - BurnLimitParams memory params - ) external + /// @notice Removes directional liquidity (LimitPosition) + /// @param params the params for burning the position + /// @dev See PoolsharkStructs.sol for struct data + function burnLimit(BurnLimitParams memory params) + external nonReentrant(globalState) canonicalOnly + returns (int256, int256) { BurnLimitCache memory cache; cache.constants = immutables(); - BurnLimitCall.perform( - params.zeroForOne ? positions0 : positions1, - ticks, - limitTickMap, - globalState, - params, - cache - ); + return + BurnLimitCall.perform( + params.zeroForOne ? positions0 : positions1, + ticks, + limitTickMap, + globalState, + params, + cache + ); } - function swap( - SwapParams memory params - ) external + /// @notice Swaps tokens with the liquidity pool + /// @param params the params for executing the swap + /// @dev See PoolsharkStructs.sol for struct data + function swap(SwapParams memory params) + external nonReentrant(globalState) canonicalOnly - returns ( - int256, - int256 - ) + returns (int256, int256) { SwapCache memory cache; cache.constants = immutables(); - return SwapCall.perform( - ticks, - samples, - rangeTickMap, - limitTickMap, - globalState, - params, - cache - ); + return + SwapCall.perform( + ticks, + samples, + rangeTickMap, + limitTickMap, + globalState, + params, + cache + ); } - function increaseSampleCount( - uint16 newSampleCountMax - ) external + /// @notice Increase the max sample count for oracle data + /// @param newSampleCountMax the new max sample count + function increaseSampleCount(uint16 newSampleCountMax) + external nonReentrant(globalState) - canonicalOnly + canonicalOnly { - Samples.expand( - samples, - globalState.pool, - newSampleCountMax - ); + Samples.expand(samples, globalState.pool, newSampleCountMax); } - function fees( - FeesParams memory params - ) external + /// @notice Modify or collect protocol fees + /// @param params the params for modifying fees + /// @dev See PoolsharkStructs.sol for struct data + function fees(FeesParams memory params) + external ownerOnly nonReentrant(globalState) - canonicalOnly - returns ( - uint128 token0Fees, - uint128 token1Fees - ) { - return FeesCall.perform( - globalState, - params, - immutables() - ); + canonicalOnly + returns (uint128 token0Fees, uint128 token1Fees) + { + return FeesCall.perform(globalState, params, immutables()); } - function quote( - QuoteParams memory params - ) external view - returns ( - uint256, - uint256, - uint160 - ) { + /// @notice Receive a swap quote for tokenIn and tokenOut amounts + /// @param params the params for executing the quote + /// @dev See PoolsharkStructs.sol for struct data + function quote(QuoteParams memory params) + external + view + returns ( + uint256, + uint256, + uint160 + ) + { SwapCache memory cache; cache.constants = immutables(); - return QuoteCall.perform( - ticks, - rangeTickMap, - limitTickMap, - globalState, - params, - cache - ); + return + QuoteCall.perform( + ticks, + rangeTickMap, + limitTickMap, + globalState, + params, + cache + ); } - function sample( - uint32[] memory secondsAgo - ) external view override - returns( - int56[] memory tickSecondsAccum, - uint160[] memory secondsPerLiquidityAccum, - uint160 averagePrice, - uint128 averageLiquidity, - int24 averageTick - ) + /// @notice Receive oracle samples + /// @param secondsAgo an array of seconds in the past to receive samples for + function sample(uint32[] memory secondsAgo) + external + view + override + returns ( + int56[] memory tickSecondsAccum, + uint160[] memory secondsPerLiquidityAccum, + uint160 averagePrice, + uint128 averageLiquidity, + int24 averageTick + ) { - return SampleCall.perform( - globalState, - immutables(), - secondsAgo - ); + return SampleCall.perform(globalState, immutables(), secondsAgo); } - function snapshotRange( - uint32 positionId - ) external view returns ( - int56 tickSecondsAccum, - uint160 secondsPerLiquidityAccum, - uint128 feesOwed0, - uint128 feesOwed1 - ) { - return SnapshotRangeCall.perform( - positions, - ticks, - globalState, - immutables(), - positionId - ); + /// @notice Snapshot token amounts for a RangePosition + /// @param positionId the position id to snapshot values for + function snapshotRange(uint32 positionId) + external + view + returns ( + int56 tickSecondsAccum, + uint160 secondsPerLiquidityAccum, + uint128 feesOwed0, + uint128 feesOwed1 + ) + { + return + SnapshotRangeCall.perform( + positions, + ticks, + globalState, + immutables(), + positionId + ); } - function snapshotLimit( - SnapshotLimitParams memory params - ) external view returns( - uint128, - uint128 - ) { - return SnapshotLimitCall.perform( - params.zeroForOne ? positions0 : positions1, - ticks, - limitTickMap, - globalState, - immutables(), - params - ); + /// @notice Snapshot token amounts for a LimitPosition + /// @param params the params to snapshot values for + /// @dev See PoolsharkStructs.sol for struct data + function snapshotLimit(SnapshotLimitParams memory params) + external + view + returns (uint128, uint128) + { + return + SnapshotLimitCall.perform( + params.zeroForOne ? positions0 : positions1, + ticks, + limitTickMap, + globalState, + immutables(), + params + ); } - function immutables() public view returns ( - LimitImmutables memory - ) { - return LimitImmutables( - owner(), - original, - factory, - PriceBounds(minPrice(), maxPrice()), - token0(), - token1(), - poolToken(), - genesisTime(), - tickSpacing(), - swapFee() - ); + /// @notice Immutable values embedded directly in the contract bytecode + /// @dev See PoolsharkStructs.sol for struct data + function immutables() public view returns (LimitImmutables memory) { + return + LimitImmutables( + owner(), + original, + factory, + PriceBounds(minPrice(), maxPrice()), + token0(), + token1(), + poolToken(), + genesisTime(), + tickSpacing(), + swapFee() + ); } - function priceBounds( - int16 tickSpacing - ) external pure returns (uint160, uint160) { + /// @notice The price bounds for the given curve math + function priceBounds(int16 tickSpacing) + external + pure + returns (uint160, uint160) + { return ConstantProduct.priceBounds(tickSpacing); } @@ -305,8 +373,10 @@ contract LimitPool is function _onlyCanoncialClones() private view { // compute pool key - bytes32 key = keccak256(abi.encode(original, token0(), token1(), swapFee())); - + bytes32 key = keccak256( + abi.encode(original, token0(), token1(), swapFee()) + ); + // compute canonical pool address address predictedAddress = LibClone.predictDeterministicAddress( original, @@ -325,7 +395,8 @@ contract LimitPool is factory ); // only allow delegateCall from canonical clones - if (address(this) != predictedAddress) require(false, 'NoDelegateCall()'); + if (address(this) != predictedAddress) + require(false, 'NoDelegateCall()'); } function _onlyFactory() private view { diff --git a/contracts/LimitPoolFactory.sol b/contracts/LimitPoolFactory.sol index 15e707b3..dfee96fa 100644 --- a/contracts/LimitPoolFactory.sol +++ b/contracts/LimitPoolFactory.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import './interfaces/range/IRangePool.sol'; import './interfaces/limit/ILimitPool.sol'; @@ -14,7 +14,50 @@ import './libraries/utils/SafeCast.sol'; import './libraries/utils/PositionTokens.sol'; import './libraries/math/ConstantProduct.sol'; -contract LimitPoolFactory is +/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#%@@@@%@@@@%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@%%%%%%@@%==========+++**#@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@@@@@@@@%%##%%%@@%%%@@%%*-=====+++**#**+*#*#*#@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@=+===========+#@@@@@%#@%%%%%%*#%%%%%##%@@@@@@@@#****++#%#**#%=%@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@+-==+++***++++=++@@%%%%%%%%*****#%#*#@%@@@@@@@@@@@@@%#+#%#*##*=#@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@+=+*******+++%%##%%####++++++**@@%%@@@@@@@@@@@@@@@###*#%#+==@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@%=**##**+#%###########+====*@%@@%%@@@@@@@@@@@@@%###%@+%=+=*@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@#%%####*##*##*#**#**+=+=-=*%%%###%@@@@@@@@@@@####@#=====+*@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@#%#***#***#*****=*+=+==--=@@%#**###@@@@@@@@@@@###@@%=+++=++#@@@@*@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@##*********+***##++*+#%@*%@@@@@####@@@@#=%@@@@@@@@@@@+==++=+=%%#+*@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@##*****+**+*********++=%%%@@@@@@@@@*@@@%===@@@@@@@@@*===++=+++**#%#@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@#**++++*+++++===+***+==#%%#@@@@@@@@*##@*===+%@@@@%+==+=+++++++*%@%%@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@**++++++++====----+++====+###@@@@@@@%#%%*==++*+#+===+++++++++++*%%@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%###*++++++====---=*@@@@+===-%@**%@@@@@@@#%%%===*+****+++*++++*+*+*#@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@*+++*+++++===--=-*@@@@@@@@=--=@@@#%@@@@@@@@#%%*++++**#+++*++**++***#*@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@%===+++++=+++===*@@@@@@@@@@=-=%@@@@@%#@@@@@@@*===*++=++*++++****+**#%+@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@*=+@@#==+==+=-=@@@%%@@@@@@@@@@@@#***%%@@@@%+===+++*+*******+**++*#%*#@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@======-==#***%#@@@@@@@@@@@##%%%%%%@#====****+*****#**###*+++#*@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%-===*=++**###@@@@@@@@@@@@@+#%%%%@*======+*********###+++++++++%@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%-=+#%%+*###%@@@@@@@@@@@@@@@#%#@%++=+++===**#*#****#%=====+++++++%@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@-=#****####@@@@@@@@@@@@@@@@#%@*++*#++++=+#*#####*#%%==============*@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@*=#**+*###@@@@@@@@@@@@@@@@@#%+**###**+++######%%#+-==--=-=------======#@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@++#++++*##%@@@@@@@@@@@@@@@%+*#%%######*##%#+=-=#@@@@@*----------------*@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@%+#*+++==+***#@@@@@@@@@@@%#%%####%%#*+#*++%@@@@@@@@@@@@@%+=======*@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@*+**+========------=-=%@#%%%%%%%%%%%%#%#%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@#=====-==-------=#@@@@########%%%%%%##++#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%##%%@@@@@@@@@@@@%#######%%#*+=++@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#*####++++=@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%##**+=#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@***%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/ + +/** + * @notice Limit Pool Factory + * @author Poolshark + * @author @alphak3y + */ +contract LimitPoolFactory is ILimitPoolFactory, LimitPoolStructs, RangePoolStructs, @@ -23,51 +66,68 @@ contract LimitPoolFactory is using LibClone for address; using SafeCast for uint256; - address immutable public owner; - address immutable public original; + /// @notice The original address of the deployed contract + address public immutable original; + /// @notice The owner address for all LimitPool contracts + address public immutable owner; - constructor( - address owner_ - ) { + constructor(address owner_) { owner = owner_; original = address(this); } - function createLimitPool( - LimitPoolParams memory params - ) public override returns ( - address pool, - address poolToken - ) { + /// @notice This modifier ensures against delegate calls + modifier originalOnly() { + _onlyOriginal(); + _; + } + + /// @notice Creates a new LimitPool for the selected poolTypeId + /// @param params the params for which to create the pool with + /// @dev See PoolsharkStructs.sol for struct data + function createLimitPool(LimitPoolParams memory params) + public + override + originalOnly + returns (address pool, address poolToken) + { // validate token pair - if (params.tokenIn == params.tokenOut || params.tokenIn == address(0) || params.tokenOut == address(0)) { + if ( + params.tokenIn == params.tokenOut || + params.tokenIn == address(0) || + params.tokenOut == address(0) + ) { require(false, 'InvalidTokenAddress()'); } // sort tokens by address LimitImmutables memory constants; - (constants.token0, constants.token1) = params.tokenIn < params.tokenOut ? (params.tokenIn, params.tokenOut) - : (params.tokenOut, params.tokenIn); + (constants.token0, constants.token1) = params.tokenIn < params.tokenOut + ? (params.tokenIn, params.tokenOut) + : (params.tokenOut, params.tokenIn); // check if tick spacing supported constants.swapFee = params.swapFee; - constants.tickSpacing = ILimitPoolManager(owner).feeTiers(params.swapFee); + constants.tickSpacing = ILimitPoolManager(owner).feeTiers( + params.swapFee + ); if (constants.tickSpacing == 0) require(false, 'FeeTierNotSupported()'); // check if pool type supported - ( - address poolImpl, - address tokenImpl - ) = ILimitPoolManager(owner).poolTypes(params.poolTypeId); - if (poolImpl == address(0) || tokenImpl == address(0)) require(false, 'PoolTypeNotSupported()'); + (address poolImpl, address tokenImpl) = ILimitPoolManager(owner) + .poolTypes(params.poolTypeId); + if (poolImpl == address(0) || tokenImpl == address(0)) + require(false, 'PoolTypeNotSupported()'); // generate key for pool - bytes32 key = keccak256(abi.encode( - poolImpl, - constants.token0, - constants.token1, - constants.swapFee - )); + bytes32 key = keccak256( + abi.encode( + poolImpl, + constants.token0, + constants.token1, + constants.swapFee + ) + ); // check if pool already exists if (pools[key] != address(0)) require(false, 'PoolAlreadyExists()'); @@ -76,10 +136,8 @@ contract LimitPoolFactory is constants.owner = owner; constants.factory = original; constants.genesisTime = block.timestamp.toUint32(); - ( - constants.bounds.min, - constants.bounds.max - ) = ILimitPoolView(poolImpl).priceBounds(constants.tickSpacing); + (constants.bounds.min, constants.bounds.max) = ILimitPoolView(poolImpl) + .priceBounds(constants.tickSpacing); // take that ERC1155 contract address and pass that into pool // launch pool token @@ -113,7 +171,17 @@ contract LimitPoolFactory is // save pool in mapping pools[key] = pool; + // emit standard event emit PoolCreated( + constants.token0, + constants.token1, + constants.swapFee, + constants.tickSpacing, + pool + ); + + // emit custom event + emit LimitPoolCreated( pool, constants.poolToken, constants.token0, @@ -126,15 +194,17 @@ contract LimitPoolFactory is return (pool, constants.poolToken); } + /// @notice Gets a new LimitPool for the selected poolTypeId + /// @param tokenIn the first token in the pair + /// @param tokenOut the second token in the pair + /// @param swapFee the swap fee for the pool + /// @param poolTypeId the poolTypeId for the pool function getLimitPool( address tokenIn, address tokenOut, uint16 swapFee, uint16 poolTypeId - ) public view override returns ( - address pool, - address poolToken - ) { + ) public view override returns (address pool, address poolToken) { // set lexographical token address ordering address token0 = tokenIn < tokenOut ? tokenIn : tokenOut; address token1 = tokenIn < tokenOut ? tokenOut : tokenIn; @@ -144,19 +214,13 @@ contract LimitPoolFactory is if (tickSpacing == 0) require(false, 'FeeTierNotSupported()'); // check if pool type supported - ( - address poolImpl, - address tokenImpl - ) = ILimitPoolManager(owner).poolTypes(poolTypeId); - if (poolImpl == address(0) || tokenImpl == address(0)) require(false, 'PoolTypeNotSupported()'); + (address poolImpl, address tokenImpl) = ILimitPoolManager(owner) + .poolTypes(poolTypeId); + if (poolImpl == address(0) || tokenImpl == address(0)) + require(false, 'PoolTypeNotSupported()'); // generate key for pool - bytes32 key = keccak256(abi.encode( - poolImpl, - token0, - token1, - swapFee - )); + bytes32 key = keccak256(abi.encode(poolImpl, token0, token1, swapFee)); pool = pools[key]; @@ -172,4 +236,8 @@ contract LimitPoolFactory is return (pool, poolToken); } + + function _onlyOriginal() private view { + if (address(this) != original) require(false, 'OriginalOnly()'); + } } diff --git a/contracts/base/events/FinStakerEvents.sol b/contracts/base/events/FinStakerEvents.sol deleted file mode 100644 index 64878c85..00000000 --- a/contracts/base/events/FinStakerEvents.sol +++ /dev/null @@ -1,29 +0,0 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; - -abstract contract FinStakerEvents { - event StakeFin( - address owner, - uint256 amount - ); - - event UnstakeFin( - address pool, - uint256 amount - ); - - event StakeFinAccrued( - address owner, - uint128 stakingPointsAccrued - ); - - event FeeToTransfer( - address indexed previousFeeTo, - address indexed newFeeTo - ); - - event OwnerTransfer( - address indexed previousOwner, - address indexed newOwner - ); -} diff --git a/contracts/base/events/LimitPoolEvents.sol b/contracts/base/events/LimitPoolEvents.sol index 188d49b9..d14435e5 100644 --- a/contracts/base/events/LimitPoolEvents.sol +++ b/contracts/base/events/LimitPoolEvents.sol @@ -1,15 +1,23 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; -abstract contract LimitPoolEvents { - event Initialize( +/// @notice Events emitted by the LimitPool contract(s) +interface LimitPoolEvents { + + ///////////////////////////////////////////////////////////// + /////////////////////// Custom Events /////////////////////// + ///////////////////////////////////////////////////////////// + + /// @notice Custom event emitted when pool is initialized by the factory + event InitializeLimit( int24 minTick, int24 maxTick, uint160 startPrice, int24 startTick ); - event Swap( + /// @notice Custom event emitted when a swap is successful + event SwapLimit( address indexed recipient, uint256 amountIn, uint256 amountOut, @@ -23,15 +31,7 @@ abstract contract LimitPoolEvents { bool indexed exactIn ); - event SampleRecorded( - int56 tickSecondsAccum, - uint160 secondsPerLiquidityAccum - ); - - event SampleCountIncreased( - uint16 newSampleCountMax - ); - + /// @notice Event emitted when liquidity added to RangePosition event MintRange( address indexed recipient, int24 lower, @@ -42,6 +42,7 @@ abstract contract LimitPoolEvents { int128 amount1Delta ); + /// @notice Event emitted when liquidity removed from RangePosition event BurnRange( address indexed recipient, uint256 indexed positionId, @@ -50,25 +51,17 @@ abstract contract LimitPoolEvents { int128 amount1 ); - event CompoundRange( - uint32 indexed positionId, - uint128 liquidityCompounded - ); - - event CollectRange0( - uint128 amount0 - ); - - event CollectRange1( - uint128 amount1 - ); + /// @notice Event emitted when liquidity is added as a result of calling `burnRange` + event CompoundRange(uint32 indexed positionId, uint128 liquidityCompounded); + /// @notice Event emitted when a RangeTick is updated event SyncRangeTick( uint200 feeGrowthOutside0, uint200 feeGrowthOutside1, int24 tick ); + /// @notice Event emitted when liquidity is added to a LimitPosition event MintLimit( address indexed to, int24 lower, @@ -80,6 +73,7 @@ abstract contract LimitPoolEvents { uint128 liquidityMinted ); + /// @notice Event emitted when liquidity is removed from a LimitPosition event BurnLimit( address indexed to, uint32 positionId, @@ -93,6 +87,7 @@ abstract contract LimitPoolEvents { uint128 tokenOutBurned ); + /// @notice Event emitted when a LimitPosition undercuts pool0 or pool1 event SyncLimitPool( uint160 price, uint128 liquidity, @@ -101,15 +96,71 @@ abstract contract LimitPoolEvents { bool isPool0 ); + /// @notice Event emitted when a LimitTick is created via undercutting event SyncLimitLiquidity( uint128 liquidityAdded, int24 tick, bool zeroForOne ); - event SyncLimitTick( - uint32 epoch, - int24 tick, - bool zeroForOne + /// @notice Event emitted when a LimitTick is crossed or initialized + event SyncLimitTick(uint32 epoch, int24 tick, bool zeroForOne); + + /// @notice Event emitted when an oracle sample is recorded + event SampleRecorded( + int56 tickSecondsAccum, + uint160 secondsPerLiquidityAccum + ); + + /// @notice Event emitted when max sample count is increased + event SampleCountIncreased(uint16 newSampleCountMax); + + ///////////////////////////////////////////////////////////// + ////////////////////// Standard Events ////////////////////// + ///////////////////////////////////////////////////////////// + + /// @notice Emitted when pool is initialized by the factory + event Initialize(uint160 price, int24 tick); + + /// @notice Emitted when liquidity is added + event Mint( + address sender, + address indexed owner, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount, + uint256 amount0, + uint256 amount1 + ); + + /// @notice Emitted when liquidity is removed + event Burn( + address indexed owner, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount, + uint256 amount0, + uint256 amount1 + ); + + /// @notice Emitted when liquidity is swapped + event Swap( + address indexed sender, + address indexed recipient, + int256 amount0, + int256 amount1, + uint160 price, + uint128 liquidity, + int24 tickAtPrice + ); + + /// @notice Emitted when fees are collected by the owner of a position + event Collect( + address indexed owner, + address recipient, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount0, + uint128 amount1 ); } diff --git a/contracts/base/events/LimitPoolFactoryEvents.sol b/contracts/base/events/LimitPoolFactoryEvents.sol index 08389743..8b4561d2 100644 --- a/contracts/base/events/LimitPoolFactoryEvents.sol +++ b/contracts/base/events/LimitPoolFactoryEvents.sol @@ -1,8 +1,14 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; +/// @notice Events emitted by the LimitPoolFactory contract abstract contract LimitPoolFactoryEvents { - event PoolCreated( + + ///////////////////////////////////////////////////////////// + /////////////////////// Custom Events /////////////////////// + ///////////////////////////////////////////////////////////// + + event LimitPoolCreated( address pool, address token, address indexed token0, @@ -11,4 +17,16 @@ abstract contract LimitPoolFactoryEvents { int16 tickSpacing, uint16 poolTypeId ); + + ///////////////////////////////////////////////////////////// + ////////////////////// Standard Events ////////////////////// + ///////////////////////////////////////////////////////////// + + event PoolCreated( + address indexed token0, + address indexed token1, + uint24 indexed fee, + int24 tickSpacing, + address pool + ); } diff --git a/contracts/base/events/LimitPoolManagerEvents.sol b/contracts/base/events/LimitPoolManagerEvents.sol index 56571f91..b773daf9 100644 --- a/contracts/base/events/LimitPoolManagerEvents.sol +++ b/contracts/base/events/LimitPoolManagerEvents.sol @@ -1,35 +1,75 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import '../../interfaces/structs/PoolsharkStructs.sol'; +/// @notice Events emitted by the LimitPoolManager contract abstract contract LimitPoolManagerEvents is PoolsharkStructs { - event FactoryChanged(address indexed previousFactory, address indexed newFactory); + + ///////////////////////////////////////////////////////////// + /////////////////////// Custom Events /////////////////////// + ///////////////////////////////////////////////////////////// + + /// @notice Event emitted when pool is initialized by the factory + event FactoryChanged( + address indexed previousFactory, + address indexed newFactory + ); + + /// @notice Event emitted the fee delta constant is modified + event FeeDeltaConstChanged( + uint16 oldFeeDeltaConst, + uint16 newFeeDeltaConst + ); + + /// @notice Event emitted the fee delta constant is modified + event PoolFeeDeltaConstChanged( + address pool, + uint16 oldFeeDeltaConst, + uint16 newFeeDeltaConst + ); + + /// @notice Event emitted when a new pool type is enabled event PoolTypeEnabled( bytes32 poolTypeName, address poolImpl, address tokenImpl, - uint16 poolTypeId + uint16 poolTypeId ); - event FeeTierEnabled( - uint16 swapFee, - int16 tickSpacing + + /// @notice Event emitted when a new fee tier is enabled + event FeeTierEnabled(uint16 swapFee, int16 tickSpacing); + + /// @notice Event emitted when the feeTo address is modified + event FeeToTransfer( + address indexed previousFeeTo, + address indexed newFeeTo + ); + + /// @notice Event emitted when the owner address is modified + event OwnerTransfer( + address indexed previousOwner, + address indexed newOwner ); - event FeeToTransfer(address indexed previousFeeTo, address indexed newFeeTo); - event OwnerTransfer(address indexed previousOwner, address indexed newOwner); + + /// @notice Event emitted when protocolSwapFees0 or protocolSwapFees1 is modified event ProtocolSwapFeesModified( address[] pools, int16[] protocolSwapFees0, int16[] protocolSwapFees1 ); + + /// @notice Event emitted when protocolSwapFees0 or protocolSwapFees1 is modified event ProtocolFillFeesModified( address[] pools, int16[] protocolFillFees0, int16[] protocolFillFees1 ); + + /// @notice Event emitted when protocol fees are collected event ProtocolFeesCollected( address[] pools, uint128[] token0FeesCollected, uint128[] token1FeesCollected ); -} \ No newline at end of file +} diff --git a/contracts/base/events/PoolsharkRouterEvents.sol b/contracts/base/events/PoolsharkRouterEvents.sol index b20b90f6..78a1472b 100644 --- a/contracts/base/events/PoolsharkRouterEvents.sol +++ b/contracts/base/events/PoolsharkRouterEvents.sol @@ -1,7 +1,9 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; +/// @notice Events emitted by the PoolsharkRouter contract abstract contract PoolsharkRouterEvents { + /// @notice Event emitted when the router is initially deployed event RouterDeployed( address router, address limitPoolFactory, diff --git a/contracts/base/events/PositionERC1155Events.sol b/contracts/base/events/PositionERC1155Events.sol index e940d4c7..f838238e 100644 --- a/contracts/base/events/PositionERC1155Events.sol +++ b/contracts/base/events/PositionERC1155Events.sol @@ -1,7 +1,9 @@ -// SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; +/// @notice Events emitted by the PositionERC1155 contract(s) abstract contract PositionERC1155Events { + /// @notice Event emitted when single token is transferred event TransferSingle( address indexed sender, address indexed from, @@ -10,6 +12,7 @@ abstract contract PositionERC1155Events { uint256 amount ); + /// @notice Event emitted when multiple tokens are transferred event TransferBatch( address indexed sender, address indexed from, @@ -18,6 +21,7 @@ abstract contract PositionERC1155Events { uint256[] amounts ); + /// @notice Event emitted when spender is approved for all token ids event ApprovalForAll( address indexed account, address indexed sender, diff --git a/contracts/base/events/RangeStakerEvents.sol b/contracts/base/events/RangeStakerEvents.sol index 263a74da..927f0b99 100644 --- a/contracts/base/events/RangeStakerEvents.sol +++ b/contracts/base/events/RangeStakerEvents.sol @@ -1,7 +1,9 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; +/// @notice Events emitted by the RangeStaker contract abstract contract RangeStakerEvents { + /// @notice Event emitted when a RangePosition is staked event StakeRange( address pool, uint32 positionId, @@ -11,12 +13,10 @@ abstract contract RangeStakerEvents { uint128 liquidity ); - event UnstakeRange( - address pool, - uint32 positionId, - address recipient - ); + /// @notice Event emitted when a Range Position is unstaked + event UnstakeRange(address pool, uint32 positionId, address recipient); + /// @notice Event emitted when a staked RangePosition accrues fees event StakeRangeAccrued( address pool, uint32 positionId, @@ -24,11 +24,13 @@ abstract contract RangeStakerEvents { uint256 feeGrowth1Accrued ); + /// @notice Event emitted when the feeTo address is modified event FeeToTransfer( address indexed previousFeeTo, address indexed newFeeTo ); + /// @notice Event emitted when the owner address is modified event OwnerTransfer( address indexed previousOwner, address indexed newOwner diff --git a/contracts/base/storage/LimitPoolFactoryStorage.sol b/contracts/base/storage/LimitPoolFactoryStorage.sol index 67529676..365a2840 100644 --- a/contracts/base/storage/LimitPoolFactoryStorage.sol +++ b/contracts/base/storage/LimitPoolFactoryStorage.sol @@ -1,10 +1,6 @@ -// SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; abstract contract LimitPoolFactoryStorage { - mapping(bytes32 => address) public pools; + mapping(bytes32 => address) public pools; ///@dev - map for limit pool lookup by key } - - - - diff --git a/contracts/base/storage/LimitPoolImmutables.sol b/contracts/base/storage/LimitPoolImmutables.sol index c03f9df4..4197188e 100644 --- a/contracts/base/storage/LimitPoolImmutables.sol +++ b/contracts/base/storage/LimitPoolImmutables.sol @@ -1,7 +1,7 @@ -// SPDX-License-Identifier: BSD -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; -import { Clone } from "../../external/solady/Clone.sol"; +import {Clone} from '../../external/solady/Clone.sol'; contract LimitPoolImmutables is Clone { function owner() public pure returns (address) { @@ -39,4 +39,4 @@ contract LimitPoolImmutables is Clone { function swapFee() public pure returns (uint16) { return _getArgUint16(126); } -} \ No newline at end of file +} diff --git a/contracts/base/storage/LimitPoolStorage.sol b/contracts/base/storage/LimitPoolStorage.sol index f484d3e6..29490120 100644 --- a/contracts/base/storage/LimitPoolStorage.sol +++ b/contracts/base/storage/LimitPoolStorage.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../interfaces/structs/RangePoolStructs.sol'; import '../../interfaces/structs/LimitPoolStructs.sol'; @@ -12,7 +12,7 @@ abstract contract LimitPoolStorage is ILimitPoolStorageView, RangePoolStructs { TickMap public limitTickMap; ///@dev - tick bitmap for limit ticks Sample[65535] public samples; ///@dev - oracle TWAP samples mapping(int24 => Tick) public ticks; ///@dev - range and limit tick data - mapping(uint256 => RangePosition) public positions; ///@dev - range positions token0 <> token1 + mapping(uint256 => RangePosition) public positions; ///@dev - range positions token0 <> token1 mapping(uint256 => LimitPosition) public positions0; ///@dev - limit positions token0 -> token1 mapping(uint256 => LimitPosition) public positions1; ///@dev - limit positions token0 <- token1 } diff --git a/contracts/base/storage/PositionERC1155Immutables.sol b/contracts/base/storage/PositionERC1155Immutables.sol index 6a96ed5d..9ed5b716 100644 --- a/contracts/base/storage/PositionERC1155Immutables.sol +++ b/contracts/base/storage/PositionERC1155Immutables.sol @@ -1,7 +1,7 @@ -// SPDX-License-Identifier: BSD -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; -import { Clone } from "../../external/solady/Clone.sol"; +import {Clone} from '../../external/solady/Clone.sol'; contract PositionERC1155Immutables is Clone { function tokenName() public pure returns (bytes32) { @@ -11,4 +11,4 @@ contract PositionERC1155Immutables is Clone { function tokenSymbol() public pure returns (bytes32) { return _getArgBytes32(32); } -} \ No newline at end of file +} diff --git a/contracts/external/openzeppelin/security/LimitReentrancyGuard.sol b/contracts/external/openzeppelin/security/LimitReentrancyGuard.sol index 76e0bc8b..1b43efff 100644 --- a/contracts/external/openzeppelin/security/LimitReentrancyGuard.sol +++ b/contracts/external/openzeppelin/security/LimitReentrancyGuard.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v4.9.0) (security/ReentrancyGuard.sol) -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../../interfaces/structs/PoolsharkStructs.sol'; @@ -85,7 +85,11 @@ abstract contract LimitReentrancyGuard is PoolsharkStructs { * @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a * `nonReentrant` function in the call stack. */ - function _reentrancyGuardEntered(GlobalState storage state) internal view returns (bool) { + function _reentrancyGuardEntered(GlobalState storage state) + internal + view + returns (bool) + { return state.unlocked == _ENTERED; } -} \ No newline at end of file +} diff --git a/contracts/external/openzeppelin/security/ReentrancyGuard.sol b/contracts/external/openzeppelin/security/ReentrancyGuard.sol index 6a182c87..43eb14dd 100644 --- a/contracts/external/openzeppelin/security/ReentrancyGuard.sol +++ b/contracts/external/openzeppelin/security/ReentrancyGuard.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: MIT // OpenZeppelin Contracts (last updated v5.0.0) (utils/ReentrancyGuard.sol) -pragma solidity 0.8.13; +pragma solidity 0.8.18; /** * @dev Contract module that helps prevent reentrant calls to a function. @@ -81,4 +81,4 @@ abstract contract ReentrancyGuard { function _reentrancyGuardEntered() internal view returns (bool) { return _status == ENTERED; } -} \ No newline at end of file +} diff --git a/contracts/external/solady/Clone.sol b/contracts/external/solady/Clone.sol index 13fdd6ef..715afba5 100644 --- a/contracts/external/solady/Clone.sol +++ b/contracts/external/solady/Clone.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; /// @notice Class with helper read functions for clone with immutable args. /// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/Clone.sol) @@ -25,7 +25,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type address. - function _getArgAddress(uint256 argOffset) internal pure returns (address arg) { + function _getArgAddress(uint256 argOffset) + internal + pure + returns (address arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -66,7 +70,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type bytes32. - function _getArgBytes32(uint256 argOffset) internal pure returns (bytes32 arg) { + function _getArgBytes32(uint256 argOffset) + internal + pure + returns (bytes32 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -75,7 +83,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint256. - function _getArgUint256(uint256 argOffset) internal pure returns (uint256 arg) { + function _getArgUint256(uint256 argOffset) + internal + pure + returns (uint256 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -84,7 +96,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint248. - function _getArgUint248(uint256 argOffset) internal pure returns (uint248 arg) { + function _getArgUint248(uint256 argOffset) + internal + pure + returns (uint248 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -93,7 +109,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint240. - function _getArgUint240(uint256 argOffset) internal pure returns (uint240 arg) { + function _getArgUint240(uint256 argOffset) + internal + pure + returns (uint240 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -102,7 +122,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint232. - function _getArgUint232(uint256 argOffset) internal pure returns (uint232 arg) { + function _getArgUint232(uint256 argOffset) + internal + pure + returns (uint232 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -111,7 +135,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint224. - function _getArgUint224(uint256 argOffset) internal pure returns (uint224 arg) { + function _getArgUint224(uint256 argOffset) + internal + pure + returns (uint224 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -120,7 +148,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint216. - function _getArgUint216(uint256 argOffset) internal pure returns (uint216 arg) { + function _getArgUint216(uint256 argOffset) + internal + pure + returns (uint216 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -129,7 +161,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint208. - function _getArgUint208(uint256 argOffset) internal pure returns (uint208 arg) { + function _getArgUint208(uint256 argOffset) + internal + pure + returns (uint208 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -138,7 +174,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint200. - function _getArgUint200(uint256 argOffset) internal pure returns (uint200 arg) { + function _getArgUint200(uint256 argOffset) + internal + pure + returns (uint200 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -147,7 +187,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint192. - function _getArgUint192(uint256 argOffset) internal pure returns (uint192 arg) { + function _getArgUint192(uint256 argOffset) + internal + pure + returns (uint192 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -156,7 +200,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint184. - function _getArgUint184(uint256 argOffset) internal pure returns (uint184 arg) { + function _getArgUint184(uint256 argOffset) + internal + pure + returns (uint184 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -165,7 +213,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint176. - function _getArgUint176(uint256 argOffset) internal pure returns (uint176 arg) { + function _getArgUint176(uint256 argOffset) + internal + pure + returns (uint176 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -174,7 +226,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint168. - function _getArgUint168(uint256 argOffset) internal pure returns (uint168 arg) { + function _getArgUint168(uint256 argOffset) + internal + pure + returns (uint168 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -183,7 +239,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint160. - function _getArgUint160(uint256 argOffset) internal pure returns (uint160 arg) { + function _getArgUint160(uint256 argOffset) + internal + pure + returns (uint160 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -192,7 +252,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint152. - function _getArgUint152(uint256 argOffset) internal pure returns (uint152 arg) { + function _getArgUint152(uint256 argOffset) + internal + pure + returns (uint152 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -201,7 +265,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint144. - function _getArgUint144(uint256 argOffset) internal pure returns (uint144 arg) { + function _getArgUint144(uint256 argOffset) + internal + pure + returns (uint144 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -210,7 +278,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint136. - function _getArgUint136(uint256 argOffset) internal pure returns (uint136 arg) { + function _getArgUint136(uint256 argOffset) + internal + pure + returns (uint136 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -219,7 +291,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint128. - function _getArgUint128(uint256 argOffset) internal pure returns (uint128 arg) { + function _getArgUint128(uint256 argOffset) + internal + pure + returns (uint128 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -228,7 +304,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint120. - function _getArgUint120(uint256 argOffset) internal pure returns (uint120 arg) { + function _getArgUint120(uint256 argOffset) + internal + pure + returns (uint120 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -237,7 +317,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint112. - function _getArgUint112(uint256 argOffset) internal pure returns (uint112 arg) { + function _getArgUint112(uint256 argOffset) + internal + pure + returns (uint112 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -246,7 +330,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint104. - function _getArgUint104(uint256 argOffset) internal pure returns (uint104 arg) { + function _getArgUint104(uint256 argOffset) + internal + pure + returns (uint104 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -255,7 +343,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint96. - function _getArgUint96(uint256 argOffset) internal pure returns (uint96 arg) { + function _getArgUint96(uint256 argOffset) + internal + pure + returns (uint96 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -264,7 +356,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint88. - function _getArgUint88(uint256 argOffset) internal pure returns (uint88 arg) { + function _getArgUint88(uint256 argOffset) + internal + pure + returns (uint88 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -273,7 +369,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint80. - function _getArgUint80(uint256 argOffset) internal pure returns (uint80 arg) { + function _getArgUint80(uint256 argOffset) + internal + pure + returns (uint80 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -282,7 +382,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint72. - function _getArgUint72(uint256 argOffset) internal pure returns (uint72 arg) { + function _getArgUint72(uint256 argOffset) + internal + pure + returns (uint72 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -291,7 +395,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint64. - function _getArgUint64(uint256 argOffset) internal pure returns (uint64 arg) { + function _getArgUint64(uint256 argOffset) + internal + pure + returns (uint64 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -300,7 +408,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint56. - function _getArgUint56(uint256 argOffset) internal pure returns (uint56 arg) { + function _getArgUint56(uint256 argOffset) + internal + pure + returns (uint56 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -309,7 +421,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint48. - function _getArgUint48(uint256 argOffset) internal pure returns (uint48 arg) { + function _getArgUint48(uint256 argOffset) + internal + pure + returns (uint48 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -318,7 +434,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint40. - function _getArgUint40(uint256 argOffset) internal pure returns (uint40 arg) { + function _getArgUint40(uint256 argOffset) + internal + pure + returns (uint40 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -327,7 +447,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint32. - function _getArgUint32(uint256 argOffset) internal pure returns (uint32 arg) { + function _getArgUint32(uint256 argOffset) + internal + pure + returns (uint32 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -336,7 +460,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint24. - function _getArgUint24(uint256 argOffset) internal pure returns (uint24 arg) { + function _getArgUint24(uint256 argOffset) + internal + pure + returns (uint24 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -345,7 +473,11 @@ abstract contract Clone { } /// @dev Reads an immutable arg with type uint16. - function _getArgUint16(uint256 argOffset) internal pure returns (uint16 arg) { + function _getArgUint16(uint256 argOffset) + internal + pure + returns (uint16 arg) + { uint256 offset = _getImmutableArgsOffset(); /// @solidity memory-safe-assembly assembly { @@ -366,7 +498,10 @@ abstract contract Clone { function _getImmutableArgsOffset() internal pure returns (uint256 offset) { /// @solidity memory-safe-assembly assembly { - offset := sub(calldatasize(), shr(240, calldataload(sub(calldatasize(), 2)))) + offset := sub( + calldatasize(), + shr(240, calldataload(sub(calldatasize(), 2))) + ) } } -} \ No newline at end of file +} diff --git a/contracts/external/solady/LibClone.sol b/contracts/external/solady/LibClone.sol index d86a6db7..cc94bdc5 100644 --- a/contracts/external/solady/LibClone.sol +++ b/contracts/external/solady/LibClone.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +pragma solidity 0.8.18; /// @notice Minimal proxy library. /// @author Solady (https://github.com/vectorized/solady/blob/main/src/utils/LibClone.sol) @@ -34,10 +34,11 @@ library LibClone { /// @dev Deploys a deterministic clone of `implementation`, /// using immutable arguments encoded in `data`, with `salt`. - function cloneDeterministic(address implementation, bytes memory data, bytes32 salt) - internal - returns (address instance) - { + function cloneDeterministic( + address implementation, + bytes memory data, + bytes32 salt + ) internal returns (address instance) { assembly { // Compute the boundaries of the data and cache the memory slots around it. let mBefore3 := mload(sub(data, 0x60)) @@ -57,20 +58,32 @@ library LibClone { // Write the rest of the bytecode. mstore( sub(data, 0x21), - or(shl(0x48, extraLength), 0x593da1005b363d3d373d3d3d3d610000806062363936013d73) + or( + shl(0x48, extraLength), + 0x593da1005b363d3d373d3d3d3d610000806062363936013d73 + ) ) // `keccak256("ReceiveETH(uint256)")` mstore( - sub(data, 0x3a), 0x9e4ac34f21c619cefc926c8bd93b54bf5a39c7ab2127a895af1cc0691d7e3dff + sub(data, 0x3a), + 0x9e4ac34f21c619cefc926c8bd93b54bf5a39c7ab2127a895af1cc0691d7e3dff ) mstore( sub(data, 0x5a), - or(shl(0x78, add(extraLength, 0x62)), 0x6100003d81600a3d39f336602c57343d527f) + or( + shl(0x78, add(extraLength, 0x62)), + 0x6100003d81600a3d39f336602c57343d527f + ) ) mstore(dataEnd, shl(0xf0, extraLength)) // Create the instance. - instance := create2(0, sub(data, 0x4c), add(extraLength, 0x6c), salt) + instance := create2( + 0, + sub(data, 0x4c), + add(extraLength, 0x6c), + salt + ) // If `instance` is zero, revert. if iszero(instance) { @@ -116,15 +129,22 @@ library LibClone { // Write the rest of the bytecode. mstore( sub(data, 0x21), - or(shl(0x48, extraLength), 0x593da1005b363d3d373d3d3d3d610000806062363936013d73) + or( + shl(0x48, extraLength), + 0x593da1005b363d3d373d3d3d3d610000806062363936013d73 + ) ) // `keccak256("ReceiveETH(uint256)")` mstore( - sub(data, 0x3a), 0x9e4ac34f21c619cefc926c8bd93b54bf5a39c7ab2127a895af1cc0691d7e3dff + sub(data, 0x3a), + 0x9e4ac34f21c619cefc926c8bd93b54bf5a39c7ab2127a895af1cc0691d7e3dff ) mstore( sub(data, 0x5a), - or(shl(0x78, add(extraLength, 0x62)), 0x6100003d81600a3d39f336602c57343d527f) + or( + shl(0x78, add(extraLength, 0x62)), + 0x6100003d81600a3d39f336602c57343d527f + ) ) mstore(dataEnd, shl(0xf0, extraLength)) @@ -158,11 +178,11 @@ library LibClone { /// @dev Returns the address when a contract with initialization code hash, /// `hash`, is deployed with `salt`, by `deployer`. - function predictDeterministicAddress(bytes32 hash, bytes32 salt, address deployer) - internal - pure - returns (address predicted) - { + function predictDeterministicAddress( + bytes32 hash, + bytes32 salt, + address deployer + ) internal pure returns (address predicted) { /// @solidity memory-safe-assembly assembly { // Compute and store the bytecode hash. @@ -189,4 +209,4 @@ library LibClone { } } } -} \ No newline at end of file +} diff --git a/contracts/interfaces/IERC20Minimal.sol b/contracts/interfaces/IERC20Minimal.sol index d75e9a1b..142bccd4 100644 --- a/contracts/interfaces/IERC20Minimal.sol +++ b/contracts/interfaces/IERC20Minimal.sol @@ -1,9 +1,9 @@ -// SPDX-License-Identifier: GPL-2.0-or-later -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; interface IERC20Minimal { /// @notice Returns the balance of a token /// @param account The address for which to look up the balance for /// @return amount of tokens held by the account function balanceOf(address account) external view returns (uint256); -} \ No newline at end of file +} diff --git a/contracts/interfaces/IPool.sol b/contracts/interfaces/IPool.sol index d5c70c15..2f407ebe 100644 --- a/contracts/interfaces/IPool.sol +++ b/contracts/interfaces/IPool.sol @@ -1,64 +1,67 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import '../interfaces/structs/PoolsharkStructs.sol'; interface IPool is PoolsharkStructs { function immutables() external view returns (LimitImmutables memory); - - function swap( - SwapParams memory params - ) external returns ( - int256 amount0, - int256 amount1 - ); - function quote( - QuoteParams memory params - ) external view returns ( - int256 inAmount, - int256 outAmount, - uint160 priceAfter - ); + function swap(SwapParams memory params) + external + returns (int256 amount0, int256 amount1); - function fees( - FeesParams memory params - ) external returns ( - uint128 token0Fees, - uint128 token1Fees - ); + function quote(QuoteParams memory params) + external + view + returns ( + int256 inAmount, + int256 outAmount, + uint160 priceAfter + ); - function sample( - uint32[] memory secondsAgo - ) external view returns ( - int56[] memory tickSecondsAccum, - uint160[] memory secondsPerLiquidityAccum, - uint160 averagePrice, - uint128 averageLiquidity, - int24 averageTick - ); + function fees(FeesParams memory params) + external + returns (uint128 token0Fees, uint128 token1Fees); - function snapshotRange( - uint32 positionId - ) external view returns( - int56 tickSecondsAccum, - uint160 secondsPerLiquidityAccum, - uint128 feesOwed0, - uint128 feesOwed1 - ); + function sample(uint32[] memory secondsAgo) + external + view + returns ( + int56[] memory tickSecondsAccum, + uint160[] memory secondsPerLiquidityAccum, + uint160 averagePrice, + uint128 averageLiquidity, + int24 averageTick + ); - function snapshotLimit( - SnapshotLimitParams memory params - ) external view returns( - uint128 amountIn, - uint128 amountOut - ); + function snapshotRange(uint32 positionId) + external + view + returns ( + int56 tickSecondsAccum, + uint160 secondsPerLiquidityAccum, + uint128 feesOwed0, + uint128 feesOwed1 + ); - function poolToken() external view returns( - address poolToken - ); + function snapshotLimit(SnapshotLimitParams memory params) + external + view + returns (uint128 amountIn, uint128 amountOut); + + function poolToken() external view returns (address poolToken); function token0() external view returns (address token0); function token1() external view returns (address token1); + + function globalState() external view returns ( + RangePoolState memory pool, + LimitPoolState memory pool0, + LimitPoolState memory pool1, + uint128 liquidityGlobal, + uint32 positionIdNext, + uint32 epoch, + uint8 unlocked + ); } diff --git a/contracts/interfaces/IPositionERC1155.sol b/contracts/interfaces/IPositionERC1155.sol index 6651cba2..9dd3aa0b 100644 --- a/contracts/interfaces/IPositionERC1155.sol +++ b/contracts/interfaces/IPositionERC1155.sol @@ -1,9 +1,9 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: SSPL-1.0 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../interfaces/structs/PoolsharkStructs.sol'; -import "@openzeppelin/contracts/utils/introspection/IERC165.sol"; +import '@openzeppelin/contracts/utils/introspection/IERC165.sol'; interface IPositionERC1155 is IERC165, PoolsharkStructs { event TransferSingle( @@ -32,18 +32,22 @@ interface IPositionERC1155 is IERC165, PoolsharkStructs { function symbol() external view returns (string memory); - function balanceOf(address account, uint256 id) external view returns (uint256); + function balanceOf(address account, uint256 id) + external + view + returns (uint256); - function balanceOfBatch( - address[] calldata accounts, - uint256[] calldata ids - ) external view returns ( - uint256[] memory batchBalances - ); + function balanceOfBatch(address[] calldata accounts, uint256[] calldata ids) + external + view + returns (uint256[] memory batchBalances); - function totalSupply(uint256 id) external view returns (uint256); + function totalSupply() external view returns (uint256); - function isApprovedForAll(address owner, address spender) external view returns (bool); + function isApprovedForAll(address owner, address spender) + external + view + returns (bool); function setApprovalForAll(address sender, bool approved) external; @@ -79,4 +83,4 @@ interface IPositionERC1155 is IERC165, PoolsharkStructs { address recipient, PoolsharkStructs.LimitImmutables memory constants ) external; -} \ No newline at end of file +} diff --git a/contracts/interfaces/IWETH9.sol b/contracts/interfaces/IWETH9.sol index 8a9df11b..2947af41 100644 --- a/contracts/interfaces/IWETH9.sol +++ b/contracts/interfaces/IWETH9.sol @@ -1,16 +1,16 @@ -// SPDX-License-Identifier: GPL-2.0-or-later -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; interface IWETH9 { /// @notice Deposits ether in return for wrapped ether function deposit() external payable; /// @notice Withdraws ether from wrapped ether balance - function withdraw(uint wad) external; + function withdraw(uint256 wad) external; /// @notice Withdraws ether from wrapped ether balance - function transfer(address dst, uint wad) external returns (bool); + function transfer(address dst, uint256 wad) external returns (bool); /// @notice Returns balance for address function balanceOf(address account) external returns (uint256); -} \ No newline at end of file +} diff --git a/contracts/interfaces/callbacks/ICoverPoolCallback.sol b/contracts/interfaces/callbacks/ICoverPoolCallback.sol index bd33aa50..71f7581f 100644 --- a/contracts/interfaces/callbacks/ICoverPoolCallback.sol +++ b/contracts/interfaces/callbacks/ICoverPoolCallback.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; /// @title Callback for mints /// @notice Any contract that calls the `mint` function must implement this interface. @@ -14,7 +14,6 @@ interface ICoverPoolMintCallback { ) external; } - /// @title Callback for swaps /// @notice Any contract that calls the `swap` function must implement this interface. interface ICoverPoolSwapCallback { @@ -28,4 +27,4 @@ interface ICoverPoolSwapCallback { int256 amount1Delta, bytes calldata data ) external; -} \ No newline at end of file +} diff --git a/contracts/interfaces/callbacks/ILimitPoolCallback.sol b/contracts/interfaces/callbacks/ILimitPoolCallback.sol index 9b3407d2..786b07dd 100644 --- a/contracts/interfaces/callbacks/ILimitPoolCallback.sol +++ b/contracts/interfaces/callbacks/ILimitPoolCallback.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; /// @title Callback for range mints /// @notice Any contract that calls the `mintRange` function must implement this interface. @@ -40,4 +40,4 @@ interface ILimitPoolSwapCallback { int256 amount1Delta, bytes calldata data ) external; -} \ No newline at end of file +} diff --git a/contracts/interfaces/cover/ICoverPool.sol b/contracts/interfaces/cover/ICoverPool.sol index 44c6a00d..201edd99 100644 --- a/contracts/interfaces/cover/ICoverPool.sol +++ b/contracts/interfaces/cover/ICoverPool.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity ^0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity ^0.8.18; import '../structs/PoolsharkStructs.sol'; @@ -9,11 +9,10 @@ import '../structs/PoolsharkStructs.sol'; * @notice Defines the basic interface for a Cover Pool. */ interface ICoverPool is PoolsharkStructs { - - function immutables( - ) external view returns ( - CoverImmutables memory constants - ); + function immutables() + external + view + returns (CoverImmutables memory constants); /** * @notice Deposits `amountIn` of asset to be auctioned off each time price range is crossed further into. @@ -24,9 +23,7 @@ interface ICoverPool is PoolsharkStructs { * @dev The position will be minted with the `to` address as the owner. * @param params The parameters for the function. See MintCoverParams. */ - function mint( - MintCoverParams memory params - ) external; + function mint(MintCoverParams memory params) external; /** * @notice Withdraws the input token and returns any filled and/or unfilled amounts to the 'to' address specified. @@ -38,9 +35,7 @@ interface ICoverPool is PoolsharkStructs { * @dev The `sync` flag can be set to false so users can exit safely without syncing latestTick. * @param params The parameters for the function. See BurnCoverParams. */ - function burn( - BurnCoverParams memory params - ) external; + function burn(BurnCoverParams memory params) external; /** * @notice Swaps `tokenIn` for `tokenOut`. @@ -53,12 +48,9 @@ interface ICoverPool is PoolsharkStructs { * @return amount0Delta The amount of token0 spent (negative) or received (positive) by the user * @return amount1Delta The amount of token1 spent (negative) or received (positive) by the user */ - function swap( - SwapParams memory params - ) external returns ( - int256 amount0Delta, - int256 amount1Delta - ); + function swap(SwapParams memory params) + external + returns (int256 amount0Delta, int256 amount1Delta); /** * @notice Quotes the amount of `tokenIn` for `tokenOut`. @@ -72,11 +64,12 @@ interface ICoverPool is PoolsharkStructs { * @return outAmount The amount of tokenOut to be received * @return priceAfter The Q64.96 square root price after the swap */ - function quote( - QuoteParams memory params - ) external view returns ( - int256 inAmount, - int256 outAmount, - uint256 priceAfter - ); + function quote(QuoteParams memory params) + external + view + returns ( + int256 inAmount, + int256 outAmount, + uint256 priceAfter + ); } diff --git a/contracts/interfaces/cover/ICoverPoolFactory.sol b/contracts/interfaces/cover/ICoverPoolFactory.sol index 644830f7..91ad5501 100644 --- a/contracts/interfaces/cover/ICoverPoolFactory.sol +++ b/contracts/interfaces/cover/ICoverPoolFactory.sol @@ -1,14 +1,13 @@ -// SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; abstract contract ICoverPoolFactory { - struct CoverPoolParams { bytes32 poolType; address tokenIn; address tokenOut; uint16 feeTier; - int16 tickSpread; + int16 tickSpread; uint16 twapLength; } @@ -16,21 +15,18 @@ abstract contract ICoverPoolFactory { * @notice Creates a new CoverPool. * @param params The CoverPoolParams struct referenced above. */ - function createCoverPool( - CoverPoolParams memory params - ) external virtual returns ( - address pool, - address poolToken - ); + function createCoverPool(CoverPoolParams memory params) + external + virtual + returns (address pool, address poolToken); /** * @notice Fetches an existing CoverPool. * @param params The CoverPoolParams struct referenced above. */ - function getCoverPool( - CoverPoolParams memory params - ) external view virtual returns ( - address pool, - address poolToken - ); + function getCoverPool(CoverPoolParams memory params) + external + view + virtual + returns (address pool, address poolToken); } diff --git a/contracts/interfaces/cover/ITwapSource.sol b/contracts/interfaces/cover/ITwapSource.sol index e5c02aa8..692b6b98 100644 --- a/contracts/interfaces/cover/ITwapSource.sol +++ b/contracts/interfaces/cover/ITwapSource.sol @@ -1,37 +1,28 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity ^0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity ^0.8.18; import '../structs/PoolsharkStructs.sol'; interface ITwapSource { - function initialize( - PoolsharkStructs.CoverImmutables memory constants - ) external returns ( - uint8 initializable, - int24 startingTick - ); + function initialize(PoolsharkStructs.CoverImmutables memory constants) + external + returns (uint8 initializable, int24 startingTick); function calculateAverageTick( PoolsharkStructs.CoverImmutables memory constants, int24 latestTick - ) external view returns ( - int24 averageTick - ); + ) external view returns (int24 averageTick); function getPool( address tokenA, address tokenB, uint16 feeTier - ) external view returns ( - address pool - ); + ) external view returns (address pool); - function feeTierTickSpacing( - uint16 feeTier - ) external view returns ( - int24 tickSpacing - ); + function feeTierTickSpacing(uint16 feeTier) + external + view + returns (int24 tickSpacing); - function factory() - external view returns (address); + function factory() external view returns (address); } diff --git a/contracts/interfaces/limit/ILimitPool.sol b/contracts/interfaces/limit/ILimitPool.sol index eef76c6c..b24477d7 100644 --- a/contracts/interfaces/limit/ILimitPool.sol +++ b/contracts/interfaces/limit/ILimitPool.sol @@ -1,25 +1,20 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import '../structs/LimitPoolStructs.sol'; interface ILimitPool is LimitPoolStructs { - function initialize( - uint160 startPrice - ) external; + function initialize(uint160 startPrice) external; - function mintLimit( - MintLimitParams memory params - ) external; + function mintLimit(MintLimitParams memory params) + external + returns (int256, int256); - function burnLimit( - BurnLimitParams memory params - ) external; + function burnLimit(BurnLimitParams memory params) + external + returns (int256, int256); - function fees( - FeesParams memory params - ) external returns ( - uint128 token0Fees, - uint128 token1Fees - ); + function fees(FeesParams memory params) + external + returns (uint128 token0Fees, uint128 token1Fees); } diff --git a/contracts/interfaces/limit/ILimitPoolFactory.sol b/contracts/interfaces/limit/ILimitPoolFactory.sol index 76a2dad4..13e9f00d 100644 --- a/contracts/interfaces/limit/ILimitPoolFactory.sol +++ b/contracts/interfaces/limit/ILimitPoolFactory.sol @@ -1,24 +1,22 @@ -// SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import '../structs/PoolsharkStructs.sol'; import '../../base/storage/LimitPoolFactoryStorage.sol'; -abstract contract ILimitPoolFactory is LimitPoolFactoryStorage, PoolsharkStructs { - function createLimitPool( - LimitPoolParams memory params - ) external virtual returns ( - address pool, - address poolToken - ); +abstract contract ILimitPoolFactory is + LimitPoolFactoryStorage, + PoolsharkStructs +{ + function createLimitPool(LimitPoolParams memory params) + external + virtual + returns (address pool, address poolToken); function getLimitPool( address tokenIn, address tokenOut, - uint16 swapFee, - uint16 poolTypeId - ) external view virtual returns ( - address pool, - address poolToken - ); + uint16 swapFee, + uint16 poolTypeId + ) external view virtual returns (address pool, address poolToken); } diff --git a/contracts/interfaces/limit/ILimitPoolManager.sol b/contracts/interfaces/limit/ILimitPoolManager.sol index 5df59cb7..78980b8d 100644 --- a/contracts/interfaces/limit/ILimitPoolManager.sol +++ b/contracts/interfaces/limit/ILimitPoolManager.sol @@ -1,20 +1,18 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; /// @notice LimitPoolManager interface interface ILimitPoolManager { - function owner() external view returns (address); + function feeTo() external view returns (address); - function poolTypes( - uint16 poolType - ) external view returns ( - address poolImpl, - address tokenImpl - ); - function feeTiers( - uint16 swapFee - ) external view returns ( - int16 tickSpacing - ); + + function feeDeltaConsts(address pool) external view returns (uint16); + + function poolTypes(uint16 poolType) + external + view + returns (address poolImpl, address tokenImpl); + + function feeTiers(uint16 swapFee) external view returns (int16 tickSpacing); } diff --git a/contracts/interfaces/limit/ILimitPoolStorageView.sol b/contracts/interfaces/limit/ILimitPoolStorageView.sol index bd6767b5..16d05a46 100644 --- a/contracts/interfaces/limit/ILimitPoolStorageView.sol +++ b/contracts/interfaces/limit/ILimitPoolStorageView.sol @@ -1,16 +1,27 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import '../structs/LimitPoolStructs.sol'; interface ILimitPoolStorageView is LimitPoolStructs { - function globalState() external view returns ( - RangePoolState memory pool, - LimitPoolState memory pool0, - LimitPoolState memory pool1, - uint128 liquidityGlobal, - uint32 positionIdNext, - uint32 epoch, - uint8 unlocked - ); + function globalState() + external + view + returns ( + RangePoolState memory pool, + LimitPoolState memory pool0, + LimitPoolState memory pool1, + uint128 liquidityGlobal, + uint32 positionIdNext, + uint32 epoch, + uint8 unlocked + ); + + function ticks(int24 tickIndex) + external + view + returns ( + RangeTick memory, + LimitTick memory + ); } diff --git a/contracts/interfaces/limit/ILimitPoolView.sol b/contracts/interfaces/limit/ILimitPoolView.sol index f7ad341c..9e0df209 100644 --- a/contracts/interfaces/limit/ILimitPoolView.sol +++ b/contracts/interfaces/limit/ILimitPoolView.sol @@ -1,36 +1,29 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import '../structs/LimitPoolStructs.sol'; interface ILimitPoolView is LimitPoolStructs { + function snapshotLimit(SnapshotLimitParams memory params) + external + view + returns (uint128, uint128); - function snapshotLimit( - SnapshotLimitParams memory params - ) external view returns( - uint128, - uint128 - ); + function immutables() external view returns (LimitImmutables memory); - function immutables( - ) external view returns( - LimitImmutables memory - ); + function priceBounds(int16 tickSpacing) + external + pure + returns (uint160 minPrice, uint160 maxPrice); - function priceBounds( - int16 tickSpacing - ) external pure returns ( - uint160 minPrice, - uint160 maxPrice - ); - - function sample( - uint32[] memory secondsAgo - ) external view returns( - int56[] memory tickSecondsAccum, - uint160[] memory secondsPerLiquidityAccum, - uint160 averagePrice, - uint128 averageLiquidity, - int24 averageTick - ); + function sample(uint32[] memory secondsAgo) + external + view + returns ( + int56[] memory tickSecondsAccum, + uint160[] memory secondsPerLiquidityAccum, + uint160 averagePrice, + uint128 averageLiquidity, + int24 averageTick + ); } diff --git a/contracts/interfaces/range/IRangePool.sol b/contracts/interfaces/range/IRangePool.sol index b3caf43d..447e9c0b 100644 --- a/contracts/interfaces/range/IRangePool.sol +++ b/contracts/interfaces/range/IRangePool.sol @@ -1,72 +1,76 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import '../structs/RangePoolStructs.sol'; import './IRangePoolManager.sol'; interface IRangePool is RangePoolStructs { - function mintRange( - MintRangeParams memory mintParams - ) external; + function mintRange(MintRangeParams memory mintParams) + external + returns (int256, int256); - function burnRange( - BurnRangeParams memory burnParams - ) external; + function burnRange(BurnRangeParams memory burnParams) + external + returns (int256, int256); - function swap( - SwapParams memory params - ) external returns ( - int256 amount0, - int256 amount1 - ); + function swap(SwapParams memory params) + external + returns (int256 amount0, int256 amount1); - function quote( - QuoteParams memory params - ) external view returns ( - uint256 inAmount, - uint256 outAmount, - uint160 priceAfter - ); + function quote(QuoteParams memory params) + external + view + returns ( + uint256 inAmount, + uint256 outAmount, + uint160 priceAfter + ); - function snapshotRange( - uint32 positionId - ) external view returns( - int56 tickSecondsAccum, - uint160 secondsPerLiquidityAccum, - uint128 feesOwed0, - uint128 feesOwed1 - ); + function snapshotRange(uint32 positionId) + external + view + returns ( + int56 tickSecondsAccum, + uint160 secondsPerLiquidityAccum, + uint128 feesOwed0, + uint128 feesOwed1 + ); - function sample( - uint32[] memory secondsAgo - ) external view returns( - int56[] memory tickSecondsAccum, - uint160[] memory secondsPerLiquidityAccum, - uint160 averagePrice, - uint128 averageLiquidity, - int24 averageTick - ); + function sample(uint32[] memory secondsAgo) + external + view + returns ( + int56[] memory tickSecondsAccum, + uint160[] memory secondsPerLiquidityAccum, + uint160 averagePrice, + uint128 averageLiquidity, + int24 averageTick + ); - function positions(uint256 positionId) external view returns ( - uint256 feeGrowthInside0Last, - uint256 feeGrowthInside1Last, - uint128 liquidity, - int24 lower, - int24 upper - ); + function positions(uint256 positionId) + external + view + returns ( + uint256 feeGrowthInside0Last, + uint256 feeGrowthInside1Last, + uint128 liquidity, + int24 lower, + int24 upper + ); - function increaseSampleCount( - uint16 newSampleCountMax - ) external; + function increaseSampleCount(uint16 newSampleCountMax) external; - function ticks(int24) external view returns ( - RangeTick memory, - LimitTick memory - ); + function ticks(int24) + external + view + returns (RangeTick memory, LimitTick memory); - function samples(uint256) external view returns ( - uint32, - int56, - uint160 - ); + function samples(uint256) + external + view + returns ( + uint32, + int56, + uint160 + ); } diff --git a/contracts/interfaces/range/IRangePoolFactory.sol b/contracts/interfaces/range/IRangePoolFactory.sol index 4f7e3396..e226f0b9 100644 --- a/contracts/interfaces/range/IRangePoolFactory.sol +++ b/contracts/interfaces/range/IRangePoolFactory.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; interface IRangePoolFactory { function createRangePool( @@ -15,5 +15,5 @@ interface IRangePoolFactory { uint256 fee ) external view returns (address); - function owner() external view returns(address); + function owner() external view returns (address); } diff --git a/contracts/interfaces/range/IRangePoolManager.sol b/contracts/interfaces/range/IRangePoolManager.sol index 11361579..44c9abf5 100644 --- a/contracts/interfaces/range/IRangePoolManager.sol +++ b/contracts/interfaces/range/IRangePoolManager.sol @@ -1,11 +1,14 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import '../structs/RangePoolStructs.sol'; interface IRangePoolManager { function owner() external view returns (address); + function feeTo() external view returns (address); + function protocolFees(address pool) external view returns (uint16); + function feeTiers(uint16 swapFee) external view returns (int24); -} \ No newline at end of file +} diff --git a/contracts/interfaces/staking/IRangeStaker.sol b/contracts/interfaces/staking/IRangeStaker.sol index bb068639..1812f9dc 100644 --- a/contracts/interfaces/staking/IRangeStaker.sol +++ b/contracts/interfaces/staking/IRangeStaker.sol @@ -1,9 +1,9 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: SSPL-1.0 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../structs/PoolsharkStructs.sol'; interface IRangeStaker is PoolsharkStructs { function stakeRange(StakeRangeParams memory) external; -} \ No newline at end of file +} diff --git a/contracts/interfaces/structs/LimitPoolStructs.sol b/contracts/interfaces/structs/LimitPoolStructs.sol index af1ba8a6..fff9d99d 100644 --- a/contracts/interfaces/structs/LimitPoolStructs.sol +++ b/contracts/interfaces/structs/LimitPoolStructs.sol @@ -1,16 +1,15 @@ -// SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import './PoolsharkStructs.sol'; interface LimitPoolStructs is PoolsharkStructs { - struct LimitPosition { uint128 liquidity; // expected amount to be used not actual - uint32 epochLast; // epoch when this position was created at - int24 lower; // lower price tick of position range - int24 upper; // upper price tick of position range - bool crossedInto; // whether the position was crossed into already + uint32 epochLast; // epoch when this position was created at + int24 lower; // lower price tick of position range + int24 upper; // upper price tick of position range + bool crossedInto; // whether the position was crossed into already } struct MintLimitCache { diff --git a/contracts/interfaces/structs/PoolsharkStructs.sol b/contracts/interfaces/structs/PoolsharkStructs.sol index c5926621..2c01abe2 100644 --- a/contracts/interfaces/structs/PoolsharkStructs.sol +++ b/contracts/interfaces/structs/PoolsharkStructs.sol @@ -1,15 +1,326 @@ -// SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import '../cover/ITwapSource.sol'; interface PoolsharkStructs { + /** + * @custom:struct LimitPoolParams + */ + struct LimitPoolParams { + /** + * @custom:field tokenIn + * @notice Address for the first token in the pair + */ + address tokenIn; + /** + * @custom:field tokenOut + * @notice Address for the second token in the pair + */ + address tokenOut; + /** + * @custom:field startPrice + * @notice Q64.96 formatted sqrt price to start the pool at + */ + uint160 startPrice; + /** + * @custom:field swapFee + * @notice The base swap fee for the pool; 1000 = 0.1% fee + */ + uint16 swapFee; + /** + * @custom:field poolTypeId + * @notice The pool type id for which to clone the implementation for + */ + uint16 poolTypeId; + } + + /** + * @custom:struct MintRangeParams + */ + struct MintRangeParams { + /** + * @custom:field to + * @notice Address for the receiver of the minted position + */ + address to; + /** + * @custom:field lower + * @notice The lower price tick for the position range + */ + int24 lower; + /** + * @custom:field upper + * @notice The upper price tick for the position range + */ + int24 upper; + /** + * @custom:field positionId + * @notice 0 if creating a new position; id of previous if adding liquidity + */ + uint32 positionId; + /** + * @custom:field amount0 + * @notice token0 amount to be deposited into the minted position + */ + uint128 amount0; + /** + * @custom:field amount1 + * @notice token1 amount to be deposited into the minted position + */ + uint128 amount1; + /** + * @custom:field callbackData + * @notice callback data which gets passed back to msg.sender at the end of a `mint` call + */ + bytes callbackData; + } + + struct BurnRangeParams { + /** + * @custom:field to + * @notice Address for the receiver of the burned liquidity + */ + address to; + /** + * @custom:field positionId + * @notice id of previous position minted + */ + uint32 positionId; + /** + * @custom:field burnPercent + * @notice Percent of the remaining liquidity to be removed + * @notice 1e38 represents 100% + * @notice 5e37 represents 50% + * @notice 1e37 represents 10% + */ + uint128 burnPercent; + } + + /** + * @custom:struct MintLimitParams + */ + struct MintLimitParams { + /** + * @custom:field to + * @notice Address for the receiver of the minted position + */ + address to; + /** + * @custom:field amount + * @notice Token amount to be deposited into the minted position + */ + uint128 amount; + /** + * @custom:field mintPercent + * @notice The percent of `amount` below which a LimitPosition will not be minted + * @notice 1e26 = 1% + * @notice 5e25 = 0.5% + */ + uint96 mintPercent; + /** + * @custom:field positionId + * @notice 0 if creating a new position; id of previous if adding liquidity + */ + uint32 positionId; + /** + * @custom:field lower + * @notice The lower price tick for the position range + */ + int24 lower; + /** + * @custom:field upper + * @notice The upper price tick for the position range + */ + int24 upper; + /** + * @custom:field zeroForOne + * @notice True if depositing token0, the first token address in lexographical order + * @notice False if depositing token1, the second token address in lexographical order + */ + bool zeroForOne; + /** + * @custom:field callbackData + * @notice callback data which gets passed back to msg.sender at the end of a `mint` call + */ + bytes callbackData; + } + + /** + * @custom:struct BurnLimitParams + */ + struct BurnLimitParams { + /** + * @custom:field to + * @notice Address for the receiver of the collected position amounts + */ + address to; + /** + * @custom:field burnPercent + * @notice Percent of the remaining liquidity to be removed + * @notice 1e38 represents 100% + * @notice 5e37 represents 50% + * @notice 1e37 represents 10% + */ + uint128 burnPercent; + /** + * @custom:field positionId + * @notice 0 if creating a new position; id of previous if adding liquidity + */ + uint32 positionId; + /** + * @custom:field claim + * @notice The most recent tick crossed in this range + * @notice if `zeroForOne` is true, claim tick progresses from lower => upper + * @notice if `zeroForOne` is false, claim tick progresses from upper => lower + */ + int24 claim; + /** + * @custom:field zeroForOne + * @notice True if deposited token0, the first token address in lexographical order + * @notice False if deposited token1, the second token address in lexographical order + */ + bool zeroForOne; + } + + struct SwapParams { + /** + * @custom:field to + * @notice Address for the receiver of the swap token output + */ + address to; + /** + * @custom:field priceLimit + * @notice The Q64.96 formatted sqrt price to stop swapping at + * @notice zeroForOne (i.e. token0 => token1 swap) moves price lower + * @notice !zeroForOne (i.e. token1 => token0 swap) moves price higher + */ + uint160 priceLimit; + /** + * @custom:field amount + * @notice The maximum tokenIn to be spent (exactIn) + * @notice OR tokenOut amount to be received (!exactIn) + */ + uint128 amount; + /** + * @custom:field exactIn + * @notice True if `amount` is in tokenIn; False if `amount` is in tokenOut + */ + bool exactIn; + /** + * @custom:field zeroForOne + * @notice True if swapping token0 => token1 + * @notice False if swapping token1 => token0 + */ + bool zeroForOne; + /** + * @custom:field callbackData + * @notice callback data which gets passed back to msg.sender at the end of a `mint` call + */ + bytes callbackData; + } + + struct QuoteParams { + /** + * @custom:field priceLimit + * @notice The Q64.96 formatted sqrt price to stop swapping at + * @notice zeroForOne (i.e. token0 => token1 swap) moves price lower + * @notice !zeroForOne (i.e. token1 => token0 swap) moves price higher + */ + uint160 priceLimit; + /** + * @custom:field amount + * @notice The maximum tokenIn to be spent (exactIn) + * @notice OR tokenOut amount to be received (!exactIn) + */ + uint128 amount; + /** + * @custom:field exactIn + * @notice True if `amount` is in tokenIn; False if `amount` is in tokenOut + */ + bool exactIn; + /** + * @custom:field zeroForOne + * @notice True if swapping token0 => token1 + * @notice False if swapping token1 => token0 + */ + bool zeroForOne; + } + + struct SnapshotLimitParams { + /** + * @custom:field owner + * @notice The owner address of the Limit Position + */ + address owner; + /** + * @custom:field burnPercent + * @notice The % of liquidity to burn + * @notice 1e38 = 100% + */ + uint128 burnPercent; + /** + * @custom:field positionId + * @notice The position id for the LimitPosition + */ + uint32 positionId; + /** + * @custom:field claim + * @notice The most recent tick crossed in this range + * @notice if `zeroForOne` is true, claim tick progresses from lower => upper + * @notice if `zeroForOne` is false, claim tick progresses from upper => lower + */ + int24 claim; + /** + * @custom:field zeroForOne + * @notice True if swapping token0 => token1 + * @notice False if swapping token1 => token0 + */ + bool zeroForOne; + } + + struct FeesParams { + /** + * @custom:field protocolSwapFee0 + * @notice The protocol fee taken on all token0 fees + * @notice 1e4 = 100% + */ + uint16 protocolSwapFee0; + /** + * @custom:field protocolSwapFee1 + * @notice The protocol fee taken on all token1 fees + * @notice 1e4 = 100% + */ + uint16 protocolSwapFee1; + /** + * @custom:field protocolFillFee0 + * @notice The protocol fee taken on all token0 LimitPosition fills + * @notice 1e2 = 1% + */ + uint16 protocolFillFee0; + /** + * @custom:field protocolFillFee1 + * @notice The protocol fee taken on all token1 LimitPosition fills + * @notice 1e2 = 1% + */ + uint16 protocolFillFee1; + /** + * @custom:field setFeesFlags + * @notice The flags for which protocol fees will be set + * @notice - PROTOCOL_SWAP_FEE_0 = 2**0; + * @notice - PROTOCOL_SWAP_FEE_1 = 2**1; + * @notice - PROTOCOL_FILL_FEE_0 = 2**2; + * @notice - PROTOCOL_FILL_FEE_1 = 2**3; + */ + uint8 setFeesFlags; + } + struct GlobalState { RangePoolState pool; LimitPoolState pool0; LimitPoolState pool1; uint128 liquidityGlobal; - uint32 positionIdNext; + uint32 positionIdNext; uint32 epoch; uint8 unlocked; } @@ -23,14 +334,14 @@ interface PoolsharkStructs { } struct RangePoolState { - SampleState samples; + SampleState samples; uint200 feeGrowthGlobal0; uint200 feeGrowthGlobal1; uint160 secondsPerLiquidityAccum; - uint160 price; /// @dev Starting price current - uint128 liquidity; /// @dev Liquidity currently active - int56 tickSecondsAccum; - int24 tickAtPrice; + uint160 price; /// @dev Starting price current + uint128 liquidity; /// @dev Liquidity currently active + int56 tickSecondsAccum; + int24 tickAtPrice; uint16 protocolSwapFee0; uint16 protocolSwapFee1; } @@ -56,107 +367,113 @@ interface PoolsharkStructs { } struct Sample { - uint32 blockTimestamp; - int56 tickSecondsAccum; + uint32 blockTimestamp; + int56 tickSecondsAccum; uint160 secondsPerLiquidityAccum; } struct SampleState { - uint16 index; - uint16 count; - uint16 countMax; - } - - struct LimitPoolParams { - address tokenIn; - address tokenOut; - uint160 startPrice; - uint16 swapFee; - uint16 poolTypeId; - } - - struct SwapParams { - address to; - uint160 priceLimit; - uint128 amount; - bool exactIn; - bool zeroForOne; - bytes callbackData; + uint16 index; + uint16 count; + uint16 countMax; } - struct MintLimitParams { + struct StakeRangeParams { address to; - uint128 amount; - uint96 mintPercent; + address pool; uint32 positionId; - int24 lower; - int24 upper; - bool zeroForOne; - bytes callbackData; } - struct BurnLimitParams { + struct UnstakeRangeParams { address to; - uint128 burnPercent; + address pool; uint32 positionId; - int24 claim; - bool zeroForOne; } - struct MintRangeParams { + struct StakeFinParams { address to; - int24 lower; - int24 upper; - uint32 positionId; - uint128 amount0; - uint128 amount1; - bytes callbackData; + uint128 amount; } - struct BurnRangeParams { - address to; - uint32 positionId; - uint128 burnPercent; + struct QuoteResults { + address pool; + int256 amountIn; + int256 amountOut; + uint160 priceAfter; } - struct QuoteParams { - uint160 priceLimit; - uint128 amount; - bool exactIn; - bool zeroForOne; + struct LimitImmutables { + address owner; + address poolImpl; + address factory; + PriceBounds bounds; + address token0; + address token1; + address poolToken; + uint32 genesisTime; + int16 tickSpacing; + uint16 swapFee; } - struct FeesParams { - uint16 protocolSwapFee0; - uint16 protocolSwapFee1; - uint16 protocolFillFee0; - uint16 protocolFillFee1; - uint8 setFeesFlags; + struct CoverImmutables { + ITwapSource source; + PriceBounds bounds; + address owner; + address token0; + address token1; + address poolImpl; + address poolToken; + address inputPool; + uint128 minAmountPerAuction; + uint32 genesisTime; + int16 minPositionWidth; + int16 tickSpread; + uint16 twapLength; + uint16 auctionLength; + uint16 sampleInterval; + uint8 token0Decimals; + uint8 token1Decimals; + bool minAmountLowerPriced; } - struct SnapshotLimitParams { - address owner; - uint128 burnPercent; - uint32 positionId; - int24 claim; - bool zeroForOne; + struct PriceBounds { + uint160 min; + uint160 max; } - struct StakeRangeParams { - address to; - address pool; - uint32 positionId; + struct TickMap { + uint256 blocks; /// @dev - sets of words + mapping(uint256 => uint256) words; /// @dev - blocks to words + mapping(uint256 => uint256) ticks; /// @dev - words to ticks + mapping(uint256 => mapping(uint256 => mapping(uint256 => uint256))) epochs0; /// @dev - ticks to epochs + mapping(uint256 => mapping(uint256 => mapping(uint256 => uint256))) epochs1; /// @dev - ticks to epochs } - struct UnstakeRangeParams { - address to; - address pool; - uint32 positionId; + struct SwapCache { + GlobalState state; + LimitImmutables constants; + uint256 price; + uint256 liquidity; + uint256 amountLeft; + uint256 input; + uint256 output; + uint160 crossPrice; + uint160 averagePrice; + uint160 secondsPerLiquidityAccum; + uint128 feeAmount; + int56 tickSecondsAccum; + int56 tickSecondsAccumBase; + int24 crossTick; + uint8 crossStatus; + bool limitActive; + bool exactIn; + bool cross; } - struct StakeFinParams { - address to; - uint128 amount; + enum CrossStatus { + RANGE, + LIMIT, + BOTH } /** @@ -168,38 +485,32 @@ interface PoolsharkStructs { * @notice Address for the receiver of the minted position */ address to; - /** * @custom:field amount * @notice Token amount to be deposited into the minted position */ uint128 amount; - /** * @custom:field positionId * @notice 0 if creating a new position; id of previous if adding liquidity */ uint32 positionId; - /** * @custom:field lower * @notice The lower price tick for the position range */ int24 lower; - /** * @custom:field upper * @notice The upper price tick for the position range */ int24 upper; - /** * @custom:field zeroForOne * @notice True if depositing token0, the first token address in lexographical order - * @notice False if depositing token1, the second token address in lexographical order + * @notice False if depositing token1, the second token address in lexographical order */ bool zeroForOne; - /** * @custom:field callbackData * @notice callback data which gets passed back to msg.sender at the end of a `mint` call @@ -216,7 +527,6 @@ interface PoolsharkStructs { * @notice Address for the receiver of the collected position amounts */ address to; - /** * @custom:field burnPercent * @notice Percent of the remaining liquidity to be removed @@ -225,13 +535,11 @@ interface PoolsharkStructs { * @notice 1e37 represents 10% */ uint128 burnPercent; - /** * @custom:field positionId * @notice 0 if creating a new position; id of previous if adding liquidity */ uint32 positionId; - /** * @custom:field claim * @notice The most recent tick crossed in this range @@ -239,18 +547,16 @@ interface PoolsharkStructs { * @notice if `zeroForOne` is false, claim tick progresses from lower => upper */ int24 claim; - /** * @custom:field zeroForOne * @notice True if deposited token0, the first token address in lexographical order - * @notice False if deposited token1, the second token address in lexographical order + * @notice False if deposited token1, the second token address in lexographical order */ bool zeroForOne; - /** * @custom:field sync * @notice True will sync the pool latestTick - * @notice False will skip syncing latestTick + * @notice False will skip syncing latestTick */ bool sync; } @@ -264,13 +570,11 @@ interface PoolsharkStructs { * @notice Address of the position owner */ address owner; - /** * @custom:field positionId * @notice id of position */ uint32 positionId; - /** * @custom:field burnPercent * @notice Percent of the remaining liquidity to be removed @@ -279,7 +583,6 @@ interface PoolsharkStructs { * @notice 1e37 represents 10% */ uint128 burnPercent; - /** * @custom:field claim * @notice The most recent tick crossed in this range @@ -287,93 +590,11 @@ interface PoolsharkStructs { * @notice if `zeroForOne` is false, claim tick progresses from lower => upper */ int24 claim; - /** * @custom:field zeroForOne * @notice True if deposited token0, the first token address in lexographical order - * @notice False if deposited token1, the second token address in lexographical order + * @notice False if deposited token1, the second token address in lexographical order */ bool zeroForOne; } - - struct QuoteResults { - address pool; - int256 amountIn; - int256 amountOut; - uint160 priceAfter; - } - - struct LimitImmutables { - address owner; - address poolImpl; - address factory; - PriceBounds bounds; - address token0; - address token1; - address poolToken; - uint32 genesisTime; - int16 tickSpacing; - uint16 swapFee; - } - - struct CoverImmutables { - ITwapSource source; - PriceBounds bounds; - address owner; - address token0; - address token1; - address poolImpl; - address poolToken; - address inputPool; - uint128 minAmountPerAuction; - uint32 genesisTime; - int16 minPositionWidth; - int16 tickSpread; - uint16 twapLength; - uint16 auctionLength; - uint16 sampleInterval; - uint8 token0Decimals; - uint8 token1Decimals; - bool minAmountLowerPriced; - } - - struct PriceBounds { - uint160 min; - uint160 max; - } - - struct TickMap { - uint256 blocks; /// @dev - sets of words - mapping(uint256 => uint256) words; /// @dev - sets to words - mapping(uint256 => uint256) ticks; /// @dev - words to ticks - mapping(uint256 => mapping(uint256 => mapping(uint256 => uint256))) epochs0; /// @dev - ticks to epochs - mapping(uint256 => mapping(uint256 => mapping(uint256 => uint256))) epochs1; /// @dev - ticks to epochs - } - - struct SwapCache { - GlobalState state; - LimitImmutables constants; - uint256 price; - uint256 liquidity; - uint256 amountLeft; - uint256 input; - uint256 output; - uint160 crossPrice; - uint160 averagePrice; - uint160 secondsPerLiquidityAccum; - uint128 feeAmount; - int56 tickSecondsAccum; - int56 tickSecondsAccumBase; - int24 crossTick; - uint8 crossStatus; - bool limitActive; - bool exactIn; - bool cross; - } - - enum CrossStatus { - RANGE, - LIMIT, - BOTH - } -} \ No newline at end of file +} diff --git a/contracts/interfaces/structs/RangePoolStructs.sol b/contracts/interfaces/structs/RangePoolStructs.sol index b68287c4..87d2e490 100644 --- a/contracts/interfaces/structs/RangePoolStructs.sol +++ b/contracts/interfaces/structs/RangePoolStructs.sol @@ -1,10 +1,9 @@ -// SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import './PoolsharkStructs.sol'; interface RangePoolStructs is PoolsharkStructs { - struct RangePosition { uint256 feeGrowthInside0Last; uint256 feeGrowthInside1Last; @@ -84,12 +83,12 @@ interface RangePoolStructs is PoolsharkStructs { uint128 liquidity; uint128 amount0; uint128 amount1; - int56 tickSecondsAccum; - int56 tickSecondsAccumLower; - int56 tickSecondsAccumUpper; - uint32 secondsOutsideLower; - uint32 secondsOutsideUpper; - uint32 blockTimestamp; - int24 tick; + int56 tickSecondsAccum; + int56 tickSecondsAccumLower; + int56 tickSecondsAccumUpper; + uint32 secondsOutsideLower; + uint32 secondsOutsideUpper; + uint32 blockTimestamp; + int24 tick; } } diff --git a/contracts/libraries/Samples.sol b/contracts/libraries/Samples.sol index f2b3474a..218724b8 100644 --- a/contracts/libraries/Samples.sol +++ b/contracts/libraries/Samples.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import './math/ConstantProduct.sol'; import './utils/SafeCast.sol'; @@ -16,17 +16,12 @@ library Samples { uint160 secondsPerLiquidityAccum ); - event SampleCountIncreased( - uint16 newSampleCountMax - ); + event SampleCountIncreased(uint16 newSampleCountMax); function initialize( RangePoolStructs.Sample[65535] storage samples, PoolsharkStructs.RangePoolState memory state - ) internal returns ( - PoolsharkStructs.RangePoolState memory - ) - { + ) internal returns (PoolsharkStructs.RangePoolState memory) { samples[0] = PoolsharkStructs.Sample({ blockTimestamp: uint32(block.timestamp), tickSecondsAccum: 0, @@ -44,11 +39,8 @@ library Samples { RangePoolStructs.Sample[65535] storage samples, PoolsharkStructs.SampleState memory sampleState, uint128 startLiquidity, /// @dev - liquidity from start of block - int24 tick - ) internal returns ( - uint16 sampleIndexNew, - uint16 sampleLengthNew - ) { + int24 tick + ) internal returns (uint16 sampleIndexNew, uint16 sampleLengthNew) { // grab the latest sample RangePoolStructs.Sample memory newSample = samples[sampleState.index]; @@ -56,15 +48,22 @@ library Samples { if (newSample.blockTimestamp + 2 > uint32(block.timestamp)) return (sampleState.index, sampleState.count); - if (sampleState.countMax > sampleState.count - && sampleState.index == (sampleState.count - 1)) { + if ( + sampleState.countMax > sampleState.count && + sampleState.index == (sampleState.count - 1) + ) { // increase sampleLengthNew if old size exceeded sampleLengthNew = sampleState.count + 1; } else { sampleLengthNew = sampleState.count; } sampleIndexNew = (sampleState.index + 1) % sampleLengthNew; - samples[sampleIndexNew] = _build(newSample, uint32(block.timestamp), tick, startLiquidity); + samples[sampleIndexNew] = _build( + newSample, + uint32(block.timestamp), + tick, + startLiquidity + ); emit SampleRecorded( samples[sampleIndexNew].tickSecondsAccum, @@ -77,7 +76,7 @@ library Samples { PoolsharkStructs.RangePoolState storage pool, uint16 newSampleCountMax ) internal { - if (newSampleCountMax <= pool.samples.countMax) return ; + if (newSampleCountMax <= pool.samples.countMax) return; for (uint16 i = pool.samples.countMax; i < newSampleCountMax; i++) { samples[i].blockTimestamp = 1; } @@ -85,85 +84,108 @@ library Samples { emit SampleCountIncreased(newSampleCountMax); } - function get( - address pool, - RangePoolStructs.SampleParams memory params - ) internal view returns ( - int56[] memory tickSecondsAccum, - uint160[] memory secondsPerLiquidityAccum, - uint160 averagePrice, - uint128 averageLiquidity, - int24 averageTick - ) { + function get(address pool, RangePoolStructs.SampleParams memory params) + internal + view + returns ( + int56[] memory tickSecondsAccum, + uint160[] memory secondsPerLiquidityAccum, + uint160 averagePrice, + uint128 averageLiquidity, + int24 averageTick + ) + { if (params.sampleLength == 0) require(false, 'InvalidSampleLength()'); - if (params.secondsAgo.length == 0) require(false, 'SecondsAgoArrayEmpty()'); - uint256 size = params.secondsAgo.length > 1 ? params.secondsAgo.length : 2; + if (params.secondsAgo.length == 0) + require(false, 'SecondsAgoArrayEmpty()'); + uint256 size = params.secondsAgo.length > 1 + ? params.secondsAgo.length + : 2; uint32[] memory secondsAgo = new uint32[](size); if (params.secondsAgo.length == 1) { secondsAgo = new uint32[](2); secondsAgo[0] = params.secondsAgo[0]; secondsAgo[1] = params.secondsAgo[0] + 2; - } - else secondsAgo = params.secondsAgo; + } else secondsAgo = params.secondsAgo; - if (secondsAgo[0] == secondsAgo[secondsAgo.length - 1]) require(false, 'SecondsAgoArrayValuesEqual()'); + if (secondsAgo[0] == secondsAgo[secondsAgo.length - 1]) + require(false, 'SecondsAgoArrayValuesEqual()'); tickSecondsAccum = new int56[](secondsAgo.length); secondsPerLiquidityAccum = new uint160[](secondsAgo.length); for (uint256 i = 0; i < secondsAgo.length; i++) { - ( - tickSecondsAccum[i], - secondsPerLiquidityAccum[i] - ) = getSingle( + (tickSecondsAccum[i], secondsPerLiquidityAccum[i]) = getSingle( IRangePool(pool), params, secondsAgo[i] ); } if (secondsAgo[secondsAgo.length - 1] > secondsAgo[0]) { - averageTick = int24((tickSecondsAccum[0] - tickSecondsAccum[secondsAgo.length - 1]) - / int32(secondsAgo[secondsAgo.length - 1] - secondsAgo[0])); - averagePrice = ConstantProduct.getPriceAtTick(averageTick, params.constants); - averageLiquidity = uint128((secondsPerLiquidityAccum[0] - secondsPerLiquidityAccum[secondsAgo.length - 1]) - * (secondsAgo[secondsAgo.length - 1] - secondsAgo[0])); + averageTick = int24( + (tickSecondsAccum[0] - + tickSecondsAccum[secondsAgo.length - 1]) / + int32(secondsAgo[secondsAgo.length - 1] - secondsAgo[0]) + ); + averagePrice = ConstantProduct.getPriceAtTick( + averageTick, + params.constants + ); + averageLiquidity = uint128( + (secondsPerLiquidityAccum[0] - + secondsPerLiquidityAccum[secondsAgo.length - 1]) * + (secondsAgo[secondsAgo.length - 1] - secondsAgo[0]) + ); } else { - averageTick = int24((tickSecondsAccum[secondsAgo.length - 1] - tickSecondsAccum[0]) - / int32(secondsAgo[0] - secondsAgo[secondsAgo.length - 1])); - averagePrice = ConstantProduct.getPriceAtTick(averageTick, params.constants); - averageLiquidity = uint128((secondsPerLiquidityAccum[secondsAgo.length - 1] - secondsPerLiquidityAccum[0]) - * (secondsAgo[0] - secondsAgo[secondsAgo.length - 1])); + averageTick = int24( + (tickSecondsAccum[secondsAgo.length - 1] - + tickSecondsAccum[0]) / + int32(secondsAgo[0] - secondsAgo[secondsAgo.length - 1]) + ); + averagePrice = ConstantProduct.getPriceAtTick( + averageTick, + params.constants + ); + averageLiquidity = uint128( + (secondsPerLiquidityAccum[secondsAgo.length - 1] - + secondsPerLiquidityAccum[0]) * + (secondsAgo[0] - secondsAgo[secondsAgo.length - 1]) + ); } } - function _poolSample( - IRangePool pool, - uint256 sampleIndex - ) internal view returns ( - RangePoolStructs.Sample memory - ) { + function _poolSample(IRangePool pool, uint256 sampleIndex) + internal + view + returns (RangePoolStructs.Sample memory) + { ( uint32 blockTimestamp, int56 tickSecondsAccum, uint160 liquidityPerSecondsAccum ) = IRangePool(pool).samples(sampleIndex); - return PoolsharkStructs.Sample( - blockTimestamp, - tickSecondsAccum, - liquidityPerSecondsAccum - ); + return + PoolsharkStructs.Sample( + blockTimestamp, + tickSecondsAccum, + liquidityPerSecondsAccum + ); } function getSingle( IRangePool pool, RangePoolStructs.SampleParams memory params, uint32 secondsAgo - ) internal view returns ( - int56 tickSecondsAccum, - uint160 secondsPerLiquidityAccum - ) { - RangePoolStructs.Sample memory latest = _poolSample(pool, params.sampleIndex); + ) + internal + view + returns (int56 tickSecondsAccum, uint160 secondsPerLiquidityAccum) + { + RangePoolStructs.Sample memory latest = _poolSample( + pool, + params.sampleIndex + ); if (secondsAgo == 0) { // if 2 seconds have elapsed build new sample @@ -174,11 +196,8 @@ library Samples { params.tick, params.liquidity ); - } - return ( - latest.tickSecondsAccum, - latest.secondsPerLiquidityAccum - ); + } + return (latest.tickSecondsAccum, latest.secondsPerLiquidityAccum); } uint32 targetTime = uint32(block.timestamp) - secondsAgo; @@ -187,12 +206,7 @@ library Samples { ( RangePoolStructs.Sample memory firstSample, RangePoolStructs.Sample memory secondSample - ) = _getAdjacentSamples( - pool, - latest, - params, - targetTime - ); + ) = _getAdjacentSamples(pool, latest, params, targetTime); if (targetTime == firstSample.blockTimestamp) { // first sample @@ -208,20 +222,24 @@ library Samples { ); } else { // average two samples - int32 sampleTimeDelta = int32(secondSample.blockTimestamp - firstSample.blockTimestamp); - int56 targetDelta = int56(int32(targetTime - firstSample.blockTimestamp)); + int32 sampleTimeDelta = int32( + secondSample.blockTimestamp - firstSample.blockTimestamp + ); + int56 targetDelta = int56( + int32(targetTime - firstSample.blockTimestamp) + ); return ( firstSample.tickSecondsAccum + - ((secondSample.tickSecondsAccum - firstSample.tickSecondsAccum) - / sampleTimeDelta) - * targetDelta, + ((secondSample.tickSecondsAccum - + firstSample.tickSecondsAccum) / sampleTimeDelta) * + targetDelta, firstSample.secondsPerLiquidityAccum + uint160( (uint256( - secondSample.secondsPerLiquidityAccum - firstSample.secondsPerLiquidityAccum - ) - * uint256(uint56(targetDelta))) - / uint32(sampleTimeDelta) + secondSample.secondsPerLiquidityAccum - + firstSample.secondsPerLiquidityAccum + ) * uint256(uint56(targetDelta))) / + uint32(sampleTimeDelta) ) ); } @@ -231,43 +249,42 @@ library Samples { PoolsharkStructs.GlobalState memory state, PoolsharkStructs.LimitImmutables memory constants, uint256 liquidity - ) internal view returns ( - uint160 latestPrice, - uint160 secondsPerLiquidityAccum, - int56 tickSecondsAccum - ) { + ) + internal + view + returns ( + uint160 latestPrice, + uint160 secondsPerLiquidityAccum, + int56 tickSecondsAccum + ) + { uint32 timeDelta = timeElapsed(constants); - ( - tickSecondsAccum, - secondsPerLiquidityAccum - ) = getSingle( - IRangePool(address(this)), - RangePoolStructs.SampleParams( - state.pool.samples.index, - state.pool.samples.count, - uint32(block.timestamp), - new uint32[](2), - state.pool.tickAtPrice, - liquidity.toUint128(), - constants - ), - 0 + (tickSecondsAccum, secondsPerLiquidityAccum) = getSingle( + IRangePool(address(this)), + RangePoolStructs.SampleParams( + state.pool.samples.index, + state.pool.samples.count, + uint32(block.timestamp), + new uint32[](2), + state.pool.tickAtPrice, + liquidity.toUint128(), + constants + ), + 0 ); // grab older sample for dynamic fee calculation - ( - int56 tickSecondsAccumBase, - ) = Samples.getSingle( - IRangePool(address(this)), - RangePoolStructs.SampleParams( - state.pool.samples.index, - state.pool.samples.count, - uint32(block.timestamp), - new uint32[](2), - state.pool.tickAtPrice, - liquidity.toUint128(), - constants - ), - timeDelta + (int56 tickSecondsAccumBase, ) = Samples.getSingle( + IRangePool(address(this)), + RangePoolStructs.SampleParams( + state.pool.samples.index, + state.pool.samples.count, + uint32(block.timestamp), + new uint32[](2), + state.pool.tickAtPrice, + liquidity.toUint128(), + constants + ), + timeDelta ); latestPrice = calculateLatestPrice( @@ -285,9 +302,7 @@ library Samples { uint32 timeDelta, uint32 timeDeltaMax, PoolsharkStructs.LimitImmutables memory constants - ) private pure returns ( - uint160 averagePrice - ) { + ) private pure returns (uint160 averagePrice) { int56 tickSecondsAccumDiff = tickSecondsAccum - tickSecondsAccumBase; int24 averageTick; if (timeDelta == timeDeltaMax) { @@ -298,22 +313,18 @@ library Samples { averagePrice = ConstantProduct.getPriceAtTick(averageTick, constants); } - - function timeElapsed( - PoolsharkStructs.LimitImmutables memory constants - ) private view returns ( - uint32 - ) + function timeElapsed(PoolsharkStructs.LimitImmutables memory constants) + private + view + returns (uint32) { - return uint32(block.timestamp) - constants.genesisTime >= TIME_DELTA_MAX - ? TIME_DELTA_MAX - : uint32(block.timestamp - constants.genesisTime); + return + uint32(block.timestamp) - constants.genesisTime >= TIME_DELTA_MAX + ? TIME_DELTA_MAX + : uint32(block.timestamp - constants.genesisTime); } - function _lte( - uint32 timeA, - uint32 timeB - ) private view returns (bool) { + function _lte(uint32 timeA, uint32 timeB) private view returns (bool) { uint32 currentTime = uint32(block.timestamp); if (timeA <= currentTime && timeB <= currentTime) return timeA <= timeB; @@ -332,20 +343,23 @@ library Samples { function _build( RangePoolStructs.Sample memory newSample, - uint32 blockTimestamp, - int24 tick, + uint32 blockTimestamp, + int24 tick, uint128 liquidity - ) internal pure returns ( - RangePoolStructs.Sample memory - ) { - int56 timeDelta = int56(uint56(blockTimestamp - newSample.blockTimestamp)); + ) internal pure returns (RangePoolStructs.Sample memory) { + int56 timeDelta = int56( + uint56(blockTimestamp - newSample.blockTimestamp) + ); return PoolsharkStructs.Sample({ blockTimestamp: blockTimestamp, - tickSecondsAccum: newSample.tickSecondsAccum + int56(tick) * int32(timeDelta), + tickSecondsAccum: newSample.tickSecondsAccum + + int56(tick) * + int32(timeDelta), secondsPerLiquidityAccum: newSample.secondsPerLiquidityAccum + - ((uint160(uint56(timeDelta)) << 128) / (liquidity > 0 ? liquidity : 1)) + ((uint160(uint56(timeDelta)) << 128) / + (liquidity > 0 ? liquidity : 1)) }); } @@ -354,12 +368,16 @@ library Samples { uint32 targetTime, uint16 sampleIndex, uint16 sampleLength - ) private view returns ( - RangePoolStructs.Sample memory firstSample, - RangePoolStructs.Sample memory secondSample - ) { + ) + private + view + returns ( + RangePoolStructs.Sample memory firstSample, + RangePoolStructs.Sample memory secondSample + ) + { uint256 oldIndex = (sampleIndex + 1) % sampleLength; - uint256 newIndex = oldIndex + sampleLength - 1; + uint256 newIndex = oldIndex + sampleLength - 1; uint256 index; while (true) { // start in the middle @@ -378,8 +396,14 @@ library Samples { secondSample = _poolSample(pool, (index + 1) % sampleLength); // check if target time within first and second sample - bool targetAfterFirst = _lte(firstSample.blockTimestamp, targetTime); - bool targetBeforeSecond = _lte(targetTime, secondSample.blockTimestamp); + bool targetAfterFirst = _lte( + firstSample.blockTimestamp, + targetTime + ); + bool targetBeforeSecond = _lte( + targetTime, + secondSample.blockTimestamp + ); if (targetAfterFirst && targetBeforeSecond) break; if (!targetAfterFirst) newIndex = index - 1; else oldIndex = index + 1; @@ -391,28 +415,42 @@ library Samples { RangePoolStructs.Sample memory firstSample, RangePoolStructs.SampleParams memory params, uint32 targetTime - ) private view returns ( - RangePoolStructs.Sample memory, - RangePoolStructs.Sample memory - ) { + ) + private + view + returns (RangePoolStructs.Sample memory, RangePoolStructs.Sample memory) + { if (_lte(firstSample.blockTimestamp, targetTime)) { if (firstSample.blockTimestamp == targetTime) { - return (firstSample, PoolsharkStructs.Sample(0,0,0)); + return (firstSample, PoolsharkStructs.Sample(0, 0, 0)); } else { - return (firstSample, _build(firstSample, targetTime, params.tick, params.liquidity)); + return ( + firstSample, + _build( + firstSample, + targetTime, + params.tick, + params.liquidity + ) + ); } } - firstSample = _poolSample(pool, (params.sampleIndex + 1) % params.sampleLength); + firstSample = _poolSample( + pool, + (params.sampleIndex + 1) % params.sampleLength + ); if (firstSample.blockTimestamp == 0) { firstSample = _poolSample(pool, 0); } - if(!_lte(firstSample.blockTimestamp, targetTime)) require(false, 'SampleLengthNotAvailable()'); + if (!_lte(firstSample.blockTimestamp, targetTime)) + require(false, 'SampleLengthNotAvailable()'); - return _binarySearch( - pool, - targetTime, - params.sampleIndex, - params.sampleLength - ); + return + _binarySearch( + pool, + targetTime, + params.sampleIndex, + params.sampleLength + ); } -} \ No newline at end of file +} diff --git a/contracts/libraries/TickMap.sol b/contracts/libraries/TickMap.sol index da97a597..7ff890bd 100644 --- a/contracts/libraries/TickMap.sol +++ b/contracts/libraries/TickMap.sol @@ -1,25 +1,22 @@ -// SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity 0.8.18; import './math/ConstantProduct.sol'; import '../interfaces/structs/PoolsharkStructs.sol'; library TickMap { - function get( PoolsharkStructs.TickMap storage tickMap, int24 tick, int24 tickSpacing - ) internal view returns ( - bool exists - ) { - ( - uint256 tickIndex, - uint256 wordIndex, - ) = getIndices(tick, tickSpacing); + ) internal view returns (bool exists) { + (uint256 tickIndex, uint256 wordIndex, ) = getIndices( + tick, + tickSpacing + ); // check if bit is already set - uint256 word = tickMap.ticks[wordIndex] | 1 << (tickIndex & 0xFF); + uint256 word = tickMap.ticks[wordIndex] | (1 << (tickIndex & 0xFF)); if (word == tickMap.ticks[wordIndex]) { return true; } @@ -30,24 +27,21 @@ library TickMap { PoolsharkStructs.TickMap storage tickMap, int24 tick, int24 tickSpacing - ) internal returns ( - bool exists - ) { - ( - uint256 tickIndex, - uint256 wordIndex, - uint256 blockIndex - ) = getIndices(tick, tickSpacing); + ) internal returns (bool exists) { + (uint256 tickIndex, uint256 wordIndex, uint256 blockIndex) = getIndices( + tick, + tickSpacing + ); // check if bit is already set - uint256 word = tickMap.ticks[wordIndex] | 1 << (tickIndex & 0xFF); + uint256 word = tickMap.ticks[wordIndex] | (1 << (tickIndex & 0xFF)); if (word == tickMap.ticks[wordIndex]) { return true; } - tickMap.ticks[wordIndex] = word; - tickMap.words[blockIndex] |= 1 << (wordIndex & 0xFF); // same as modulus 255 - tickMap.blocks |= 1 << blockIndex; + tickMap.ticks[wordIndex] = word; + tickMap.words[blockIndex] |= 1 << (wordIndex & 0xFF); // same as modulus 255 + tickMap.blocks |= 1 << blockIndex; return false; } @@ -56,11 +50,10 @@ library TickMap { int24 tick, int16 tickSpacing ) internal { - ( - uint256 tickIndex, - uint256 wordIndex, - uint256 blockIndex - ) = getIndices(tick, tickSpacing); + (uint256 tickIndex, uint256 wordIndex, uint256 blockIndex) = getIndices( + tick, + tickSpacing + ); tickMap.ticks[wordIndex] &= ~(1 << (tickIndex & 0xFF)); if (tickMap.ticks[wordIndex] == 0) { @@ -76,31 +69,34 @@ library TickMap { int24 tick, int16 tickSpacing, bool inclusive - ) internal view returns ( - int24 previousTick - ) { + ) internal view returns (int24 previousTick) { unchecked { // rounds up to ensure relative position if (tick % (tickSpacing / 2) != 0 || inclusive) { - if (tick < (ConstantProduct.maxTick(tickSpacing) - tickSpacing / 2)) { + if ( + tick < + (ConstantProduct.maxTick(tickSpacing) - tickSpacing / 2) + ) { /// @dev - ensures we cross when tick >= 0 if (tick >= 0) { tick += tickSpacing / 2; } else if (inclusive && tick % (tickSpacing / 2) == 0) { - /// @dev - ensures we cross when tick == tickAtPrice + /// @dev - ensures we cross when tick == tickAtPrice tick += tickSpacing / 2; } } } ( - uint256 tickIndex, - uint256 wordIndex, - uint256 blockIndex + uint256 tickIndex, + uint256 wordIndex, + uint256 blockIndex ) = getIndices(tick, tickSpacing); - uint256 word = tickMap.ticks[wordIndex] & ((1 << (tickIndex & 0xFF)) - 1); + uint256 word = tickMap.ticks[wordIndex] & + ((1 << (tickIndex & 0xFF)) - 1); if (word == 0) { - uint256 block_ = tickMap.words[blockIndex] & ((1 << (wordIndex & 0xFF)) - 1); + uint256 block_ = tickMap.words[blockIndex] & + ((1 << (wordIndex & 0xFF)) - 1); if (block_ == 0) { uint256 blockMap = tickMap.blocks & ((1 << blockIndex) - 1); if (blockMap == 0) return tick; @@ -120,45 +116,49 @@ library TickMap { int24 tick, int16 tickSpacing, bool inclusive - ) internal view returns ( - int24 nextTick - ) { + ) internal view returns (int24 nextTick) { unchecked { /// @dev - handles tickAtPrice being past tickSpacing / 2 if (inclusive && tick % tickSpacing != 0) { // e.g. tick is 5 we subtract 1 to look ahead at 5 if (tick > 0 && (tick % tickSpacing <= (tickSpacing / 2))) tick -= 1; - // e.g. tick is -5 we subtract 1 to look ahead at -5 + // e.g. tick is -5 we subtract 1 to look ahead at -5 else if (tick < 0 && (tick % tickSpacing <= -(tickSpacing / 2))) tick -= 1; - // e.g. tick = 7 and tickSpacing = 10 we sub 5 to look ahead at 5 - // e.g. tick = -2 and tickSpacing = 10 we sub 5 to look ahead at -5 - else - tick -= tickSpacing / 2; + // e.g. tick = 7 and tickSpacing = 10 we sub 5 to look ahead at 5 + // e.g. tick = -2 and tickSpacing = 10 we sub 5 to look ahead at -5 + else tick -= tickSpacing / 2; } /// @dev - handles negative ticks rounding up if (tick % (tickSpacing / 2) != 0) { if (tick < 0) - if (tick > (ConstantProduct.minTick(tickSpacing) + tickSpacing / 2)) - tick -= tickSpacing / 2; + if ( + tick > + (ConstantProduct.minTick(tickSpacing) + tickSpacing / 2) + ) tick -= tickSpacing / 2; } ( - uint256 tickIndex, - uint256 wordIndex, - uint256 blockIndex + uint256 tickIndex, + uint256 wordIndex, + uint256 blockIndex ) = getIndices(tick, tickSpacing); uint256 word; if ((tickIndex & 0xFF) != 255) { - word = tickMap.ticks[wordIndex] & ~((1 << ((tickIndex & 0xFF) + 1)) - 1); + word = + tickMap.ticks[wordIndex] & + ~((1 << ((tickIndex & 0xFF) + 1)) - 1); } if (word == 0) { uint256 block_; if ((blockIndex & 0xFF) != 255) { - block_ = tickMap.words[blockIndex] & ~((1 << ((wordIndex & 0xFF) + 1)) - 1); + block_ = + tickMap.words[blockIndex] & + ~((1 << ((wordIndex & 0xFF) + 1)) - 1); } if (block_ == 0) { - uint256 blockMap = tickMap.blocks & ~((1 << blockIndex + 1) - 1); + uint256 blockMap = tickMap.blocks & + ~((1 << (blockIndex + 1)) - 1); if (blockMap == 0) return tick; blockIndex = _lsb(blockMap); block_ = tickMap.words[blockIndex]; @@ -177,43 +177,59 @@ library TickMap { int24 stopTick, int24[] memory previousTicks, uint16 ticksIncluded - ) internal view returns ( - int24[] memory, - uint16, - int24 - ) { + ) + internal + view + returns ( + int24[] memory, + uint16, + int24 + ) + { // rounds up to ensure relative position if (tick % (tickSpacing / 2) != 0) { - if (tick < (ConstantProduct.maxTick(tickSpacing) - tickSpacing / 2)) { + if ( + tick < (ConstantProduct.maxTick(tickSpacing) - tickSpacing / 2) + ) { /// @dev - ensures we cross when tick >= 0 if (tick >= 0) { tick += tickSpacing / 2; } } } - LimitPoolStructs.TickMapLocals memory locals; - ( - locals.tickIndex, - locals.wordIndex, - locals.blockIndex - ) = getIndices(tick, tickSpacing); - locals.word = tickMap.ticks[locals.wordIndex] & ((1 << (locals.tickIndex & 0xFF)) - 1); + LimitPoolStructs.TickMapLocals memory locals; + (locals.tickIndex, locals.wordIndex, locals.blockIndex) = getIndices( + tick, + tickSpacing + ); + locals.word = + tickMap.ticks[locals.wordIndex] & + ((1 << (locals.tickIndex & 0xFF)) - 1); while (locals.word != 0 && tick > stopTick) { // ticks left within word - tick = _tick((locals.wordIndex << 8) | _msb(locals.word), tickSpacing); + tick = _tick( + (locals.wordIndex << 8) | _msb(locals.word), + tickSpacing + ); previousTicks[ticksIncluded] = tick; unchecked { ++ticksIncluded; } - ( - locals.tickIndex,, - ) = getIndices(tick, tickSpacing); + (locals.tickIndex, , ) = getIndices(tick, tickSpacing); locals.word = locals.word & ((1 << (locals.tickIndex & 0xFF)) - 1); } // no ticks left within word - // int24 firstTickNextWord = - return (previousTicks, ticksIncluded, locals.wordIndex > 0 ? _tick((locals.wordIndex - 1) << 8 | _msb(1 << 255), tickSpacing) - : ConstantProduct.minTick(tickSpacing)); + // int24 firstTickNextWord = + return ( + previousTicks, + ticksIncluded, + locals.wordIndex > 0 + ? _tick( + ((locals.wordIndex - 1) << 8) | _msb(1 << 255), + tickSpacing + ) + : ConstantProduct.minTick(tickSpacing) + ); } function nextTicksWithinWord( @@ -223,86 +239,106 @@ library TickMap { int24 stopTick, int24[] memory nextTicks, uint16 ticksIncluded - ) internal view returns ( - int24[] memory, - uint16, - int24 - ) { + ) + internal + view + returns ( + int24[] memory, + uint16, + int24 + ) + { /// @dev - handles negative ticks rounding up if (tick % (tickSpacing / 2) != 0) { if (tick < 0) - if (tick > (ConstantProduct.minTick(tickSpacing) + tickSpacing / 2)) - tick -= tickSpacing / 2; + if ( + tick > + (ConstantProduct.minTick(tickSpacing) + tickSpacing / 2) + ) tick -= tickSpacing / 2; } - LimitPoolStructs.TickMapLocals memory locals; - ( - locals.tickIndex, - locals.wordIndex, - locals.blockIndex - ) = getIndices(tick, tickSpacing); + LimitPoolStructs.TickMapLocals memory locals; + (locals.tickIndex, locals.wordIndex, locals.blockIndex) = getIndices( + tick, + tickSpacing + ); if ((locals.tickIndex & 0xFF) != 255) { - locals.word = tickMap.ticks[locals.wordIndex] & ~((1 << ((locals.tickIndex & 0xFF) + 1)) - 1); + locals.word = + tickMap.ticks[locals.wordIndex] & + ~((1 << ((locals.tickIndex & 0xFF) + 1)) - 1); } while (locals.word != 0 && tick < stopTick) { // ticks left within word - tick = _tick((locals.wordIndex << 8) | _lsb(locals.word), tickSpacing); + tick = _tick( + (locals.wordIndex << 8) | _lsb(locals.word), + tickSpacing + ); nextTicks[ticksIncluded] = tick; unchecked { ++ticksIncluded; } - ( - locals.tickIndex,, - ) = getIndices(tick, tickSpacing); + (locals.tickIndex, , ) = getIndices(tick, tickSpacing); if ((locals.tickIndex & 0xFF) != 255) { - locals.word = locals.word & ~((1 << ((locals.tickIndex & 0xFF) + 1)) - 1); + locals.word = + locals.word & + ~((1 << ((locals.tickIndex & 0xFF) + 1)) - 1); } } // no ticks left within word - return (nextTicks, ticksIncluded, _tick((locals.wordIndex + 1) << 8 | _lsb(1), tickSpacing)); + return ( + nextTicks, + ticksIncluded, + _tick(((locals.wordIndex + 1) << 8) | _lsb(1), tickSpacing) + ); } - function getIndices( - int24 tick, - int24 tickSpacing - ) public pure returns ( + function getIndices(int24 tick, int24 tickSpacing) + public + pure + returns ( uint256 tickIndex, uint256 wordIndex, uint256 blockIndex ) { unchecked { - if (tick > ConstantProduct.MAX_TICK) require(false, 'TickIndexOverflow()'); - if (tick < ConstantProduct.MIN_TICK) require(false, 'TickIndexUnderflow()'); - if (tick % (tickSpacing / 2) != 0) tick = round(tick, tickSpacing / 2); - tickIndex = uint256(int256((round(tick, tickSpacing / 2) - - round(ConstantProduct.MIN_TICK, tickSpacing / 2)) - / (tickSpacing / 2))); - wordIndex = tickIndex >> 8; // 2^8 ticks per word + if (tick > ConstantProduct.MAX_TICK) + require(false, 'TickIndexOverflow()'); + if (tick < ConstantProduct.MIN_TICK) + require(false, 'TickIndexUnderflow()'); + if (tick % (tickSpacing / 2) != 0) + tick = round(tick, tickSpacing / 2); + tickIndex = uint256( + int256( + (round(tick, tickSpacing / 2) - + round(ConstantProduct.MIN_TICK, tickSpacing / 2)) / + (tickSpacing / 2) + ) + ); + wordIndex = tickIndex >> 8; // 2^8 ticks per word blockIndex = tickIndex >> 16; // 2^8 words per block if (blockIndex > 255) require(false, 'BlockIndexOverflow()'); } } - - - function _tick ( - uint256 tickIndex, - int24 tickSpacing - ) internal pure returns ( - int24 tick - ) { + function _tick(uint256 tickIndex, int24 tickSpacing) + internal + pure + returns (int24 tick) + { unchecked { - if (tickIndex > uint24(round(ConstantProduct.MAX_TICK, tickSpacing) * 2) * 2) - require(false, 'TickIndexOverflow()'); - tick = int24(int256(tickIndex) * (tickSpacing / 2) + round(ConstantProduct.MIN_TICK, tickSpacing / 2)); + if ( + tickIndex > + uint24(round(ConstantProduct.MAX_TICK, tickSpacing) * 2) * 2 + ) require(false, 'TickIndexOverflow()'); + tick = int24( + int256(tickIndex) * + (tickSpacing / 2) + + round(ConstantProduct.MIN_TICK, tickSpacing / 2) + ); } } - function _msb( - uint256 x - ) internal pure returns ( - uint8 r - ) { + function _msb(uint256 x) internal pure returns (uint8 r) { unchecked { assert(x > 0); if (x >= 0x100000000000000000000000000000000) { @@ -337,11 +373,7 @@ library TickMap { } } - function _lsb( - uint256 x - ) internal pure returns ( - uint8 r - ) { + function _lsb(uint256 x) internal pure returns (uint8 r) { unchecked { assert(x > 0); // if x is 0 return 0 r = 255; @@ -384,38 +416,35 @@ library TickMap { } } - function round( - int24 tick, - int24 tickSpacing - ) internal pure returns ( - int24 roundedTick - ) { - return tick / tickSpacing * tickSpacing; + function round(int24 tick, int24 tickSpacing) + internal + pure + returns (int24 roundedTick) + { + return (tick / tickSpacing) * tickSpacing; } function roundHalf( int24 tick, PoolsharkStructs.LimitImmutables memory constants, uint256 price - ) internal pure returns ( - int24 roundedTick, - uint160 roundedTickPrice - ) { + ) internal pure returns (int24 roundedTick, uint160 roundedTickPrice) { //pool.tickAtPrice -99.5 //pool.tickAtPrice -100 //-105 //-95 - roundedTick = tick / constants.tickSpacing * constants.tickSpacing; - roundedTickPrice = ConstantProduct.getPriceAtTick(roundedTick, constants); - if (price == roundedTickPrice) - return (roundedTick, roundedTickPrice); + roundedTick = (tick / constants.tickSpacing) * constants.tickSpacing; + roundedTickPrice = ConstantProduct.getPriceAtTick( + roundedTick, + constants + ); + if (price == roundedTickPrice) return (roundedTick, roundedTickPrice); if (roundedTick > 0) { roundedTick += constants.tickSpacing / 2; } else if (roundedTick < 0) { if (roundedTickPrice < price) roundedTick += constants.tickSpacing / 2; - else - roundedTick -= constants.tickSpacing / 2; + else roundedTick -= constants.tickSpacing / 2; } else { if (price > roundedTickPrice) { roundedTick += constants.tickSpacing / 2; @@ -430,13 +459,13 @@ library TickMap { PoolsharkStructs.LimitImmutables memory constants, bool zeroForOne, uint256 price - ) internal pure returns ( - int24 roundedTick - ) { - roundedTick = tick / constants.tickSpacing * constants.tickSpacing; - uint160 roundedTickPrice = ConstantProduct.getPriceAtTick(roundedTick, constants); - if (price == roundedTickPrice) - return roundedTick; + ) internal pure returns (int24 roundedTick) { + roundedTick = (tick / constants.tickSpacing) * constants.tickSpacing; + uint160 roundedTickPrice = ConstantProduct.getPriceAtTick( + roundedTick, + constants + ); + if (price == roundedTickPrice) return roundedTick; if (zeroForOne) { // round up if positive if (roundedTick > 0 || (roundedTick == 0 && tick >= 0)) @@ -450,7 +479,7 @@ library TickMap { } else { // round down if negative if (roundedTick < 0 || (roundedTick == 0 && tick < 0)) - /// @dev - strictly less due to TickMath always rounding to lesser values + /// @dev - strictly less due to TickMath always rounding to lesser values roundedTick -= constants.tickSpacing; } } @@ -460,13 +489,13 @@ library TickMap { PoolsharkStructs.LimitImmutables memory constants, bool zeroForOne, uint256 price - ) internal pure returns ( - int24 roundedTick - ) { - roundedTick = tick / constants.tickSpacing * constants.tickSpacing; - uint160 roundedTickPrice = ConstantProduct.getPriceAtTick(roundedTick, constants); - if (price == roundedTickPrice) - return roundedTick; + ) internal pure returns (int24 roundedTick) { + roundedTick = (tick / constants.tickSpacing) * constants.tickSpacing; + uint160 roundedTickPrice = ConstantProduct.getPriceAtTick( + roundedTick, + constants + ); + if (price == roundedTickPrice) return roundedTick; if (zeroForOne) { // round down if negative if (roundedTick < 0 || (roundedTick == 0 && tick < 0)) @@ -483,4 +512,4 @@ library TickMap { } } } -} \ No newline at end of file +} diff --git a/contracts/libraries/Ticks.sol b/contracts/libraries/Ticks.sol index 8c53c8ab..3d770059 100644 --- a/contracts/libraries/Ticks.sol +++ b/contracts/libraries/Ticks.sol @@ -1,7 +1,8 @@ -// SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity 0.8.18; import '../interfaces/structs/PoolsharkStructs.sol'; +import '../base/events/LimitPoolEvents.sol'; import './range/math/FeeMath.sol'; import './math/OverflowMath.sol'; import './math/ConstantProduct.sol'; @@ -13,7 +14,6 @@ import './limit/EpochMap.sol'; import './limit/LimitTicks.sol'; library Ticks { - using SafeCast for uint256; // cross flags @@ -24,7 +24,8 @@ library Ticks { // for Q64.96 numbers uint256 internal constant Q96 = 0x1000000000000000000000000; - event Initialize( + event Initialize(uint160 price, int24 tick); + event InitializeLimit( int24 minTick, int24 maxTick, uint160 startPrice, @@ -32,6 +33,15 @@ library Ticks { ); event Swap( + address indexed sender, + address indexed recipient, + int256 amount0, + int256 amount1, + uint160 price, + uint128 liquidity, + int24 tickAtPrice + ); + event SwapLimit( address indexed recipient, uint256 amountIn, uint256 amountOut, @@ -58,27 +68,43 @@ library Ticks { PoolsharkStructs.GlobalState memory state, PoolsharkStructs.LimitImmutables memory constants, uint160 startPrice - ) external returns ( - PoolsharkStructs.GlobalState memory - ) - { + ) external returns (PoolsharkStructs.GlobalState memory) { // state should only be initialized once - if (state.pool0.price > 0) require (false, 'PoolAlreadyInitialized()'); + if (state.pool0.price > 0) require(false, 'PoolAlreadyInitialized()'); // initialize state state.epoch = 1; state.positionIdNext = 1; // check price bounds - if (startPrice < constants.bounds.min || startPrice >= constants.bounds.max) require(false, 'StartPriceInvalid()'); + if ( + startPrice < constants.bounds.min || + startPrice >= constants.bounds.max + ) require(false, 'StartPriceInvalid()'); // initialize range ticks - TickMap.set(rangeTickMap, ConstantProduct.minTick(constants.tickSpacing), constants.tickSpacing); - TickMap.set(rangeTickMap, ConstantProduct.maxTick(constants.tickSpacing), constants.tickSpacing); - + TickMap.set( + rangeTickMap, + ConstantProduct.minTick(constants.tickSpacing), + constants.tickSpacing + ); + TickMap.set( + rangeTickMap, + ConstantProduct.maxTick(constants.tickSpacing), + constants.tickSpacing + ); + // initialize limit ticks - TickMap.set(limitTickMap, ConstantProduct.minTick(constants.tickSpacing), constants.tickSpacing); - TickMap.set(limitTickMap, ConstantProduct.maxTick(constants.tickSpacing), constants.tickSpacing); + TickMap.set( + limitTickMap, + ConstantProduct.minTick(constants.tickSpacing), + constants.tickSpacing + ); + TickMap.set( + limitTickMap, + ConstantProduct.maxTick(constants.tickSpacing), + constants.tickSpacing + ); // initialize price state.pool.price = startPrice; @@ -93,17 +119,23 @@ library Ticks { // intialize samples state.pool = Samples.initialize(samples, state.pool); - // emit event + // emit standard event emit Initialize( + startPrice, + startTick + ); + + // emit custom event + emit InitializeLimit( ConstantProduct.minTick(constants.tickSpacing), ConstantProduct.maxTick(constants.tickSpacing), - state.pool0.price, - state.pool0.tickAtPrice + startPrice, + startTick ); return state; } - + function swap( mapping(int24 => PoolsharkStructs.Tick) storage ticks, RangePoolStructs.Sample[65535] storage samples, @@ -111,15 +143,19 @@ library Ticks { PoolsharkStructs.TickMap storage limitTickMap, PoolsharkStructs.SwapParams memory params, PoolsharkStructs.SwapCache memory cache - ) external returns ( - PoolsharkStructs.SwapCache memory - ) - { + ) external returns (PoolsharkStructs.SwapCache memory) { cache.price = cache.state.pool.price; cache.crossTick = cache.state.pool.tickAtPrice; // set initial cross state - cache = _iterate(ticks, rangeTickMap, limitTickMap, cache, params.zeroForOne, true); + cache = _iterate( + ticks, + rangeTickMap, + limitTickMap, + cache, + params.zeroForOne, + true + ); uint128 startLiquidity = cache.state.pool.liquidity; @@ -130,7 +166,7 @@ library Ticks { price: cache.price, liquidity: cache.liquidity, amountLeft: params.amount, - input: 0, + input: 0, output: 0, crossPrice: cache.crossPrice, secondsPerLiquidityAccum: 0, @@ -149,7 +185,11 @@ library Ticks { cache.averagePrice, cache.secondsPerLiquidityAccum, cache.tickSecondsAccum - ) = Samples.getLatest(cache.state, cache.constants, cache.state.pool.liquidity); + ) = Samples.getLatest( + cache.state, + cache.constants, + cache.state.pool.liquidity + ); // grab latest sample and store in cache for _cross while (cache.cross) { @@ -165,6 +205,7 @@ library Ticks { ); } } + /// @dev - write oracle entry after start of block ( cache.state.pool.samples.index, @@ -175,11 +216,15 @@ library Ticks { startLiquidity, cache.state.pool.tickAtPrice ); + // pool liquidity should be updated along the way cache.state.pool.price = cache.price.toUint160(); if (cache.price != cache.crossPrice) { - cache.state.pool.tickAtPrice = ConstantProduct.getTickAtPrice(cache.price.toUint160(), cache.constants); + cache.state.pool.tickAtPrice = ConstantProduct.getTickAtPrice( + cache.price.toUint160(), + cache.constants + ); } else { cache.state.pool.tickAtPrice = cache.crossTick; } @@ -192,7 +237,20 @@ library Ticks { cache.state.pool0.tickAtPrice = cache.state.pool.tickAtPrice; } } + + // emit standard event emit Swap( + msg.sender, + params.to, + params.zeroForOne ? int256(cache.input) : -int256(cache.output), + params.zeroForOne ? -int256(cache.output) : int256(cache.input), + cache.state.pool.price, + cache.liquidity.toUint128(), + cache.state.pool.tickAtPrice + ); + + // emit custom event + emit SwapLimit( params.to, cache.input, cache.output, @@ -205,6 +263,7 @@ library Ticks { params.zeroForOne, params.exactIn ); + return cache; } @@ -214,17 +273,28 @@ library Ticks { PoolsharkStructs.TickMap storage limitTickMap, PoolsharkStructs.QuoteParams memory params, PoolsharkStructs.SwapCache memory cache - ) internal view returns ( - uint256, - uint256, - uint160 - ) { + ) + internal + view + returns ( + uint256, + uint256, + uint160 + ) + { // start with range price cache.price = cache.state.pool.price; cache.crossTick = cache.state.pool.tickAtPrice; - cache = _iterate(ticks, rangeTickMap, limitTickMap, cache, params.zeroForOne, true); - + cache = _iterate( + ticks, + rangeTickMap, + limitTickMap, + cache, + params.zeroForOne, + true + ); + // set crossTick/crossPrice based on the best between limit and range // grab sample for accumulators cache = PoolsharkStructs.SwapCache({ @@ -233,7 +303,7 @@ library Ticks { price: cache.price, liquidity: cache.liquidity, amountLeft: params.amount, - input: 0, + input: 0, output: 0, crossPrice: cache.crossPrice, secondsPerLiquidityAccum: 0, @@ -247,57 +317,68 @@ library Ticks { cross: true, averagePrice: 0 }); + // grab latest price and sample ( cache.averagePrice, cache.secondsPerLiquidityAccum, cache.tickSecondsAccum - ) = Samples.getLatest(cache.state, cache.constants, cache.state.pool.liquidity); + ) = Samples.getLatest( + cache.state, + cache.constants, + cache.state.pool.liquidity + ); + while (cache.cross) { cache = _quoteSingle(cache, params.priceLimit, params.zeroForOne); if (cache.cross) { - cache = _pass( - ticks, - rangeTickMap, - limitTickMap, - cache, - params - ); + cache = _pass(ticks, rangeTickMap, limitTickMap, cache, params); } } - return ( - cache.input, - cache.output, - cache.price.toUint160() - ); + + return (cache.input, cache.output, cache.price.toUint160()); } function _quoteSingle( PoolsharkStructs.SwapCache memory cache, uint160 priceLimit, bool zeroForOne - ) internal pure returns ( - PoolsharkStructs.SwapCache memory - ) { - if ((zeroForOne ? priceLimit >= cache.price - : priceLimit <= cache.price) || + ) internal view returns (PoolsharkStructs.SwapCache memory) { + if ( + ( + zeroForOne + ? priceLimit >= cache.price + : priceLimit <= cache.price + ) || (zeroForOne && cache.price == cache.constants.bounds.min) || (!zeroForOne && cache.price == cache.constants.bounds.max) || - (cache.amountLeft == 0 && cache.liquidity > 0)) - { + (cache.amountLeft == 0 && cache.liquidity > 0) + ) { cache.cross = false; return cache; } uint256 nextPrice = cache.crossPrice; - uint256 amountIn; uint256 amountOut; + uint256 amountIn; + uint256 amountOut; if (zeroForOne) { // Trading token 0 (x) for token 1 (y). // price is decreasing. if (nextPrice < priceLimit) { nextPrice = priceLimit; } - uint256 amountMax = cache.exactIn ? ConstantProduct.getDx(cache.liquidity, nextPrice, cache.price, true) - : ConstantProduct.getDy(cache.liquidity, nextPrice, cache.price, false); + uint256 amountMax = cache.exactIn + ? ConstantProduct.getDx( + cache.liquidity, + nextPrice, + cache.price, + true + ) + : ConstantProduct.getDy( + cache.liquidity, + nextPrice, + cache.price, + false + ); if (cache.amountLeft < amountMax) { // calculate price after swap uint256 newPrice = ConstantProduct.getNewPrice( @@ -307,11 +388,23 @@ library Ticks { zeroForOne, cache.exactIn ); + if (newPrice < nextPrice) + require(false, 'NextPriceExceeded()'); if (cache.exactIn) { amountIn = cache.amountLeft; - amountOut = ConstantProduct.getDy(cache.liquidity, newPrice, cache.price, false); + amountOut = ConstantProduct.getDy( + cache.liquidity, + newPrice, + cache.price, + false + ); } else { - amountIn = ConstantProduct.getDx(cache.liquidity, newPrice, cache.price, true); + amountIn = ConstantProduct.getDx( + cache.liquidity, + newPrice, + cache.price, + true + ); amountOut = cache.amountLeft; } cache.amountLeft = 0; @@ -320,10 +413,19 @@ library Ticks { } else { if (cache.exactIn) { amountIn = amountMax; - amountOut = ConstantProduct.getDy(cache.liquidity, nextPrice, cache.price, false); - + amountOut = ConstantProduct.getDy( + cache.liquidity, + nextPrice, + cache.price, + false + ); } else { - amountIn = ConstantProduct.getDx(cache.liquidity, nextPrice, cache.price, true); + amountIn = ConstantProduct.getDx( + cache.liquidity, + nextPrice, + cache.price, + true + ); amountOut = amountMax; } cache.amountLeft -= amountMax; @@ -336,8 +438,19 @@ library Ticks { if (nextPrice > priceLimit) { nextPrice = priceLimit; } - uint256 amountMax = cache.exactIn ? ConstantProduct.getDy(cache.liquidity, cache.price, nextPrice, true) - : ConstantProduct.getDx(cache.liquidity, cache.price, nextPrice, false); + uint256 amountMax = cache.exactIn + ? ConstantProduct.getDy( + cache.liquidity, + cache.price, + nextPrice, + true + ) + : ConstantProduct.getDx( + cache.liquidity, + cache.price, + nextPrice, + false + ); if (cache.amountLeft < amountMax) { uint256 newPrice = ConstantProduct.getNewPrice( cache.price, @@ -346,11 +459,23 @@ library Ticks { zeroForOne, cache.exactIn ); + if (newPrice > nextPrice) + require(false, 'NextPriceExceeded()'); if (cache.exactIn) { amountIn = cache.amountLeft; - amountOut = ConstantProduct.getDx(cache.liquidity, cache.price, newPrice, false); + amountOut = ConstantProduct.getDx( + cache.liquidity, + cache.price, + newPrice, + false + ); } else { - amountIn = ConstantProduct.getDy(cache.liquidity, cache.price, newPrice, true); + amountIn = ConstantProduct.getDy( + cache.liquidity, + cache.price, + newPrice, + true + ); amountOut = cache.amountLeft; } cache.amountLeft = 0; @@ -359,9 +484,19 @@ library Ticks { } else { if (cache.exactIn) { amountIn = amountMax; - amountOut = ConstantProduct.getDx(cache.liquidity, cache.price, nextPrice, false); + amountOut = ConstantProduct.getDx( + cache.liquidity, + cache.price, + nextPrice, + false + ); } else { - amountIn = ConstantProduct.getDy(cache.liquidity, cache.price, nextPrice, true); + amountIn = ConstantProduct.getDy( + cache.liquidity, + cache.price, + nextPrice, + true + ); amountOut = amountMax; } cache.amountLeft -= amountMax; @@ -380,20 +515,32 @@ library Ticks { PoolsharkStructs.TickMap storage limitTickMap, PoolsharkStructs.SwapCache memory cache, PoolsharkStructs.SwapParams memory params - ) internal returns ( - PoolsharkStructs.SwapCache memory - ) { + ) internal returns (PoolsharkStructs.SwapCache memory) { // crossing range ticks if ((cache.crossStatus & RANGE_TICK) > 0) { // skip if crossing down and stopping at crossPrice - if (!params.zeroForOne || (cache.amountLeft > 0 && params.priceLimit < cache.crossPrice)) { - PoolsharkStructs.RangeTick memory crossTick = ticks[cache.crossTick].range; - crossTick.feeGrowthOutside0 = cache.state.pool.feeGrowthGlobal0 - crossTick.feeGrowthOutside0; - crossTick.feeGrowthOutside1 = cache.state.pool.feeGrowthGlobal1 - crossTick.feeGrowthOutside1; - crossTick.tickSecondsAccumOutside = cache.tickSecondsAccum - crossTick.tickSecondsAccumOutside; - crossTick.secondsPerLiquidityAccumOutside = cache.secondsPerLiquidityAccum - crossTick.secondsPerLiquidityAccumOutside; + if ( + !params.zeroForOne || + (cache.amountLeft > 0 && params.priceLimit < cache.crossPrice) + ) { + PoolsharkStructs.RangeTick memory crossTick = ticks[ + cache.crossTick + ].range; + crossTick.feeGrowthOutside0 = + cache.state.pool.feeGrowthGlobal0 - + crossTick.feeGrowthOutside0; + crossTick.feeGrowthOutside1 = + cache.state.pool.feeGrowthGlobal1 - + crossTick.feeGrowthOutside1; + crossTick.tickSecondsAccumOutside = + cache.tickSecondsAccum - + crossTick.tickSecondsAccumOutside; + crossTick.secondsPerLiquidityAccumOutside = + cache.secondsPerLiquidityAccum - + crossTick.secondsPerLiquidityAccumOutside; ticks[cache.crossTick].range = crossTick; int128 liquidityDelta = crossTick.liquidityDelta; + // emit custom event emit SyncRangeTick( crossTick.feeGrowthOutside0, crossTick.feeGrowthOutside1, @@ -401,22 +548,30 @@ library Ticks { ); if (params.zeroForOne) { unchecked { - if (liquidityDelta >= 0){ + if (liquidityDelta >= 0) { cache.liquidity -= uint128(liquidityDelta); - cache.state.pool.liquidity -= uint128(liquidityDelta); + cache.state.pool.liquidity -= uint128( + liquidityDelta + ); } else { cache.liquidity += uint128(-liquidityDelta); - cache.state.pool.liquidity += uint128(-liquidityDelta); + cache.state.pool.liquidity += uint128( + -liquidityDelta + ); } } } else { unchecked { if (liquidityDelta >= 0) { cache.liquidity += uint128(liquidityDelta); - cache.state.pool.liquidity += uint128(liquidityDelta); + cache.state.pool.liquidity += uint128( + liquidityDelta + ); } else { cache.liquidity -= uint128(-liquidityDelta); - cache.state.pool.liquidity -= uint128(-liquidityDelta); + cache.state.pool.liquidity -= uint128( + -liquidityDelta + ); } } } @@ -428,32 +583,52 @@ library Ticks { // crossing limit tick if ((cache.crossStatus & LIMIT_TICK) > 0) { // cross limit tick - EpochMap.set(cache.crossTick, !params.zeroForOne, cache.state.epoch, limitTickMap, cache.constants); + EpochMap.set( + cache.crossTick, + !params.zeroForOne, + cache.state.epoch, + limitTickMap, + cache.constants + ); int128 liquidityDelta = ticks[cache.crossTick].limit.liquidityDelta; if (liquidityDelta >= 0) { cache.liquidity += uint128(liquidityDelta); - if (params.zeroForOne) cache.state.pool1.liquidity += uint128(liquidityDelta); + if (params.zeroForOne) + cache.state.pool1.liquidity += uint128(liquidityDelta); else cache.state.pool0.liquidity += uint128(liquidityDelta); - } - else { + } else { cache.liquidity -= uint128(-liquidityDelta); - if (params.zeroForOne) cache.state.pool1.liquidity -= uint128(-liquidityDelta); + if (params.zeroForOne) + cache.state.pool1.liquidity -= uint128(-liquidityDelta); else cache.state.pool0.liquidity -= uint128(-liquidityDelta); } // zero out liquidityDelta and priceAt - ticks[cache.crossTick].limit = PoolsharkStructs.LimitTick(0,0,0); - LimitTicks.clear(ticks, cache.constants, limitTickMap, cache.crossTick); + ticks[cache.crossTick].limit = PoolsharkStructs.LimitTick(0, 0, 0); + LimitTicks.clear( + ticks, + cache.constants, + limitTickMap, + cache.crossTick + ); /// @dev - price and tickAtPrice updated at end of loop } if ((cache.crossStatus & LIMIT_POOL) > 0) { // add one-way liquidity - uint128 liquidityDelta = params.zeroForOne ? cache.state.pool1.liquidity - : cache.state.pool0.liquidity; + uint128 liquidityDelta = params.zeroForOne + ? cache.state.pool1.liquidity + : cache.state.pool0.liquidity; if (liquidityDelta > 0) cache.liquidity += liquidityDelta; } if (cache.cross) - cache = _iterate(ticks, rangeTickMap, limitTickMap, cache, params.zeroForOne, false); + cache = _iterate( + ticks, + rangeTickMap, + limitTickMap, + cache, + params.zeroForOne, + false + ); return cache; } @@ -464,26 +639,37 @@ library Ticks { PoolsharkStructs.TickMap storage limitTickMap, PoolsharkStructs.SwapCache memory cache, PoolsharkStructs.QuoteParams memory params - ) internal view returns ( - PoolsharkStructs.SwapCache memory - ) { + ) internal view returns (PoolsharkStructs.SwapCache memory) { if ((cache.crossStatus & RANGE_TICK) > 0) { - if (!params.zeroForOne || (cache.amountLeft > 0 && params.priceLimit < cache.crossPrice)) { - int128 liquidityDelta = ticks[cache.crossTick].range.liquidityDelta; + if ( + !params.zeroForOne || + (cache.amountLeft > 0 && params.priceLimit < cache.crossPrice) + ) { + int128 liquidityDelta = ticks[cache.crossTick] + .range + .liquidityDelta; if (params.zeroForOne) { unchecked { - if (liquidityDelta >= 0){ - cache.state.pool.liquidity -= uint128(liquidityDelta); + if (liquidityDelta >= 0) { + cache.state.pool.liquidity -= uint128( + liquidityDelta + ); } else { - cache.state.pool.liquidity += uint128(-liquidityDelta); + cache.state.pool.liquidity += uint128( + -liquidityDelta + ); } } } else { unchecked { if (liquidityDelta >= 0) { - cache.state.pool.liquidity += uint128(liquidityDelta); + cache.state.pool.liquidity += uint128( + liquidityDelta + ); } else { - cache.state.pool.liquidity -= uint128(-liquidityDelta); + cache.state.pool.liquidity -= uint128( + -liquidityDelta + ); } } } @@ -497,10 +683,10 @@ library Ticks { if (liquidityDelta > 0) { cache.liquidity += uint128(liquidityDelta); - if (params.zeroForOne) cache.state.pool1.liquidity += uint128(liquidityDelta); + if (params.zeroForOne) + cache.state.pool1.liquidity += uint128(liquidityDelta); else cache.state.pool0.liquidity += uint128(liquidityDelta); - } - else { + } else { cache.liquidity -= uint128(-liquidityDelta); if (params.zeroForOne) { cache.state.pool1.liquidity -= uint128(-liquidityDelta); @@ -511,8 +697,9 @@ library Ticks { } if ((cache.crossStatus & LIMIT_POOL) > 0) { // add limit pool - uint128 liquidityDelta = params.zeroForOne ? cache.state.pool1.liquidity - : cache.state.pool0.liquidity; + uint128 liquidityDelta = params.zeroForOne + ? cache.state.pool1.liquidity + : cache.state.pool0.liquidity; if (liquidityDelta > 0) { cache.liquidity += liquidityDelta; @@ -520,7 +707,14 @@ library Ticks { } if (cache.cross) - cache = _iterate(ticks, rangeTickMap, limitTickMap, cache, params.zeroForOne, false); + cache = _iterate( + ticks, + rangeTickMap, + limitTickMap, + cache, + params.zeroForOne, + false + ); return cache; } @@ -532,19 +726,28 @@ library Ticks { PoolsharkStructs.SwapCache memory cache, bool zeroForOne, bool inclusive - ) internal view returns ( - PoolsharkStructs.SwapCache memory - ) - { + ) internal view returns (PoolsharkStructs.SwapCache memory) { if (zeroForOne) { if (cache.price > cache.state.pool1.price) { // load range pool cache.limitActive = false; cache.liquidity = cache.state.pool.liquidity; - (cache.crossTick,) = TickMap.roundHalf(cache.crossTick, cache.constants, cache.price); + (cache.crossTick, ) = TickMap.roundHalf( + cache.crossTick, + cache.constants, + cache.price + ); // next range tick vs. limit pool price - cache.crossTick = TickMap.previous(rangeTickMap, cache.crossTick, cache.constants.tickSpacing, inclusive); - cache.crossPrice = ConstantProduct.getPriceAtTick(cache.crossTick, cache.constants); + cache.crossTick = TickMap.previous( + rangeTickMap, + cache.crossTick, + cache.constants.tickSpacing, + inclusive + ); + cache.crossPrice = ConstantProduct.getPriceAtTick( + cache.crossTick, + cache.constants + ); if (cache.state.pool1.price >= cache.crossPrice) { // cross into limit pool cache.crossStatus = LIMIT_POOL; @@ -555,29 +758,53 @@ library Ticks { cache.crossTick = cache.state.pool1.tickAtPrice; cache.crossPrice = cache.state.pool1.price; } - } - else { + } else { // cross only range tick cache.crossStatus = RANGE_TICK; } } else { // load range and limit pools cache.limitActive = true; - cache.liquidity = cache.state.pool.liquidity + cache.state.pool1.liquidity; - (cache.crossTick,) = TickMap.roundHalf(cache.crossTick, cache.constants, cache.price); - int24 rangeTickAhead; int24 limitTickAhead; - if (cache.crossStatus == LIMIT_POOL && - cache.crossTick % cache.constants.tickSpacing != 0 && - TickMap.get(limitTickMap, cache.crossTick, cache.constants.tickSpacing)) - { + cache.liquidity = + cache.state.pool.liquidity + + cache.state.pool1.liquidity; + (cache.crossTick, ) = TickMap.roundHalf( + cache.crossTick, + cache.constants, + cache.price + ); + int24 rangeTickAhead; + int24 limitTickAhead; + if ( + cache.crossStatus == LIMIT_POOL && + cache.crossTick % cache.constants.tickSpacing != 0 && + TickMap.get( + limitTickMap, + cache.crossTick, + cache.constants.tickSpacing + ) + ) { limitTickAhead = cache.crossTick; - rangeTickAhead = cache.crossTick - cache.constants.tickSpacing / 2; + rangeTickAhead = + cache.crossTick - + cache.constants.tickSpacing / + 2; } else { - rangeTickAhead = TickMap.previous(rangeTickMap, cache.crossTick, cache.constants.tickSpacing, inclusive); - limitTickAhead = TickMap.previous(limitTickMap, cache.crossTick, cache.constants.tickSpacing, inclusive); + rangeTickAhead = TickMap.previous( + rangeTickMap, + cache.crossTick, + cache.constants.tickSpacing, + inclusive + ); + limitTickAhead = TickMap.previous( + limitTickMap, + cache.crossTick, + cache.constants.tickSpacing, + inclusive + ); } // next range tick vs. next limit tick - + if (rangeTickAhead >= limitTickAhead) { cache.crossTick = rangeTickAhead; // cross range tick @@ -585,13 +812,20 @@ library Ticks { if (rangeTickAhead == limitTickAhead) // also cross limit tick cache.crossStatus |= LIMIT_TICK; - cache.crossPrice = ConstantProduct.getPriceAtTick(cache.crossTick, cache.constants); + cache.crossPrice = ConstantProduct.getPriceAtTick( + cache.crossTick, + cache.constants + ); } else { // only cross limit tick cache.crossTick = limitTickAhead; cache.crossStatus = LIMIT_TICK; - cache.crossPrice = ticks[cache.crossTick].limit.priceAt == 0 ? ConstantProduct.getPriceAtTick(cache.crossTick, cache.constants) - : ticks[cache.crossTick].limit.priceAt; + cache.crossPrice = ticks[cache.crossTick].limit.priceAt == 0 + ? ConstantProduct.getPriceAtTick( + cache.crossTick, + cache.constants + ) + : ticks[cache.crossTick].limit.priceAt; } } } else { @@ -599,10 +833,22 @@ library Ticks { // load range pool cache.limitActive = false; cache.liquidity = cache.state.pool.liquidity; - (cache.crossTick,) = TickMap.roundHalf(cache.crossTick, cache.constants, cache.price); + (cache.crossTick, ) = TickMap.roundHalf( + cache.crossTick, + cache.constants, + cache.price + ); // next range tick vs. limit pool price - cache.crossTick = TickMap.next(rangeTickMap, cache.crossTick, cache.constants.tickSpacing, inclusive); - cache.crossPrice = ConstantProduct.getPriceAtTick(cache.crossTick, cache.constants); + cache.crossTick = TickMap.next( + rangeTickMap, + cache.crossTick, + cache.constants.tickSpacing, + inclusive + ); + cache.crossPrice = ConstantProduct.getPriceAtTick( + cache.crossTick, + cache.constants + ); if (cache.state.pool0.price <= cache.crossPrice) { // cross into limit pool cache.crossStatus = LIMIT_POOL; @@ -613,27 +859,51 @@ library Ticks { cache.crossTick = cache.state.pool0.tickAtPrice; cache.crossPrice = cache.state.pool0.price; } - } - else { + } else { // cross only range tick cache.crossStatus = RANGE_TICK; } } else { // load range and limit pools cache.limitActive = true; - cache.liquidity = cache.state.pool.liquidity + cache.state.pool0.liquidity; - (cache.crossTick,) = TickMap.roundHalf(cache.crossTick, cache.constants, cache.price); + cache.liquidity = + cache.state.pool.liquidity + + cache.state.pool0.liquidity; + (cache.crossTick, ) = TickMap.roundHalf( + cache.crossTick, + cache.constants, + cache.price + ); // next range tick vs. next limit tick - int24 rangeTickAhead; int24 limitTickAhead; - if (cache.crossStatus == LIMIT_POOL && - cache.crossTick % cache.constants.tickSpacing != 0 && - TickMap.get(limitTickMap, cache.crossTick, cache.constants.tickSpacing)) - { + int24 rangeTickAhead; + int24 limitTickAhead; + if ( + cache.crossStatus == LIMIT_POOL && + cache.crossTick % cache.constants.tickSpacing != 0 && + TickMap.get( + limitTickMap, + cache.crossTick, + cache.constants.tickSpacing + ) + ) { limitTickAhead = cache.crossTick; - rangeTickAhead = cache.crossTick + cache.constants.tickSpacing / 2; + rangeTickAhead = + cache.crossTick + + cache.constants.tickSpacing / + 2; } else { - rangeTickAhead = TickMap.next(rangeTickMap, cache.crossTick, cache.constants.tickSpacing, inclusive); - limitTickAhead = TickMap.next(limitTickMap, cache.crossTick, cache.constants.tickSpacing, inclusive); + rangeTickAhead = TickMap.next( + rangeTickMap, + cache.crossTick, + cache.constants.tickSpacing, + inclusive + ); + limitTickAhead = TickMap.next( + limitTickMap, + cache.crossTick, + cache.constants.tickSpacing, + inclusive + ); } if (rangeTickAhead <= limitTickAhead) { cache.crossTick = rangeTickAhead; @@ -642,16 +912,23 @@ library Ticks { if (rangeTickAhead == limitTickAhead) // also cross limit tick cache.crossStatus |= LIMIT_TICK; - cache.crossPrice = ConstantProduct.getPriceAtTick(cache.crossTick, cache.constants); + cache.crossPrice = ConstantProduct.getPriceAtTick( + cache.crossTick, + cache.constants + ); } else { // only cross limit tick cache.crossTick = limitTickAhead; cache.crossStatus |= LIMIT_TICK; - cache.crossPrice = ticks[cache.crossTick].limit.priceAt == 0 ? ConstantProduct.getPriceAtTick(cache.crossTick, cache.constants) - : ticks[cache.crossTick].limit.priceAt; + cache.crossPrice = ticks[cache.crossTick].limit.priceAt == 0 + ? ConstantProduct.getPriceAtTick( + cache.crossTick, + cache.constants + ) + : ticks[cache.crossTick].limit.priceAt; } } } return cache; } -} \ No newline at end of file +} diff --git a/contracts/libraries/limit/Claims.sol b/contracts/libraries/limit/Claims.sol index b06dc62e..461376ae 100644 --- a/contracts/libraries/limit/Claims.sol +++ b/contracts/libraries/limit/Claims.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../../interfaces/structs/LimitPoolStructs.sol'; import './EpochMap.sol'; @@ -8,7 +8,6 @@ import '../utils/String.sol'; import '../utils/SafeCast.sol'; library Claims { - using SafeCast for uint256; // if claim tick searched, look max 512 spacings ahead @@ -19,24 +18,39 @@ library Claims { PoolsharkStructs.TickMap storage tickMap, PoolsharkStructs.BurnLimitParams memory params, LimitPoolStructs.BurnLimitCache memory cache - ) internal view returns ( - PoolsharkStructs.BurnLimitParams memory, - LimitPoolStructs.BurnLimitCache memory - ) { - if (params.claim < cache.position.lower || - params.claim > cache.position.upper) - require (false, 'ClaimTick::OutsidePositionBounds()'); - + ) + internal + view + returns ( + PoolsharkStructs.BurnLimitParams memory, + LimitPoolStructs.BurnLimitCache memory + ) + { + if ( + params.claim < cache.position.lower || + params.claim > cache.position.upper + ) require(false, 'ClaimTick::OutsidePositionBounds()'); + if (params.claim % (cache.constants.tickSpacing / 2) != 0) - require (false, 'ClaimTick::NotHalfTickOrFullTick()'); + require(false, 'ClaimTick::NotHalfTickOrFullTick()'); - uint32 claimTickEpoch = EpochMap.get(params.claim, params.zeroForOne, tickMap, cache.constants); + uint32 claimTickEpoch = EpochMap.get( + params.claim, + params.zeroForOne, + tickMap, + cache.constants + ); - if (params.zeroForOne){ + if (params.zeroForOne) { if (cache.pool.price >= cache.priceClaim) { if (cache.pool.price <= cache.priceUpper) { cache.priceClaim = cache.pool.price; - params.claim = TickMap.roundBack(cache.pool.tickAtPrice, cache.constants, params.zeroForOne, cache.priceClaim); + params.claim = TickMap.roundBack( + cache.pool.tickAtPrice, + cache.constants, + params.zeroForOne, + cache.priceClaim + ); } else { cache.priceClaim = cache.priceUpper; params.claim = cache.position.upper; @@ -47,10 +61,9 @@ library Claims { if (cache.claimTick.priceAt == 0) { // if tick untouched since position creation revert if (claimTickEpoch <= cache.position.epochLast) - require (false, 'ClaimTick::HalfTickClaimInvalid()'); - else + require(false, 'ClaimTick::HalfTickClaimInvalid()'); // search ahead for the correct claim tick - cache.search = true; + else cache.search = true; } cache.priceClaim = cache.claimTick.priceAt; } @@ -58,7 +71,12 @@ library Claims { if (cache.pool.price <= cache.priceClaim) { if (cache.pool.price >= cache.priceLower) { cache.priceClaim = cache.pool.price; - params.claim = TickMap.roundBack(cache.pool.tickAtPrice, cache.constants, params.zeroForOne, cache.priceClaim); + params.claim = TickMap.roundBack( + cache.pool.tickAtPrice, + cache.constants, + params.zeroForOne, + cache.priceClaim + ); } else { cache.priceClaim = cache.priceLower; params.claim = cache.position.lower; @@ -68,26 +86,33 @@ library Claims { } else if (params.claim % cache.constants.tickSpacing != 0) { if (cache.claimTick.priceAt == 0) { if (claimTickEpoch <= cache.position.epochLast) - require (false, 'ClaimTick::HalfTickClaimInvalid()'); - else + require(false, 'ClaimTick::HalfTickClaimInvalid()'); // search ahead for the correct claim tick - cache.search = true; + else cache.search = true; } cache.priceClaim = cache.claimTick.priceAt; } } - if (params.claim == (params.zeroForOne ? cache.position.upper : cache.position.lower)) { + if ( + params.claim == + (params.zeroForOne ? cache.position.upper : cache.position.lower) + ) { // check if final tick crossed cache.liquidityBurned = 0; if (claimTickEpoch <= cache.position.epochLast) // nothing to search - require (false, 'ClaimTick::FinalTickNotCrossedYet()'); + require(false, 'ClaimTick::FinalTickNotCrossedYet()'); } else if (cache.liquidityBurned > 0) { /// @dev - partway claim is valid as long as liquidity is not being removed if (params.zeroForOne) { // check final tick first - uint32 endTickEpoch = EpochMap.get(cache.position.upper, params.zeroForOne, tickMap, cache.constants); + uint32 endTickEpoch = EpochMap.get( + cache.position.upper, + params.zeroForOne, + tickMap, + cache.constants + ); if (endTickEpoch > cache.position.epochLast) { // final tick crossed params.claim = cache.position.upper; @@ -96,8 +121,18 @@ library Claims { cache.liquidityBurned = 0; } else { // check claim tick passed is valid - int24 claimTickNext = TickMap.next(tickMap, params.claim, cache.constants.tickSpacing, false); - uint32 claimTickNextEpoch = EpochMap.get(claimTickNext, params.zeroForOne, tickMap, cache.constants); + int24 claimTickNext = TickMap.next( + tickMap, + params.claim, + cache.constants.tickSpacing, + false + ); + uint32 claimTickNextEpoch = EpochMap.get( + claimTickNext, + params.zeroForOne, + tickMap, + cache.constants + ); if (claimTickNextEpoch > cache.position.epochLast) { ///@dev - next tick in range should not have been crossed // require (false, 'ClaimTick::NextTickAlreadyCrossed()'); @@ -106,7 +141,12 @@ library Claims { } } else { // check final tick first - uint32 endTickEpoch = EpochMap.get(cache.position.lower, params.zeroForOne, tickMap, cache.constants); + uint32 endTickEpoch = EpochMap.get( + cache.position.lower, + params.zeroForOne, + tickMap, + cache.constants + ); if (endTickEpoch > cache.position.epochLast) { // final tick crossed params.claim = cache.position.lower; @@ -115,8 +155,18 @@ library Claims { cache.liquidityBurned = 0; } else { // check claim tick passed is valid - int24 claimTickNext = TickMap.previous(tickMap, params.claim, cache.constants.tickSpacing, false); - uint32 claimTickNextEpoch = EpochMap.get(claimTickNext, params.zeroForOne, tickMap, cache.constants); + int24 claimTickNext = TickMap.previous( + tickMap, + params.claim, + cache.constants.tickSpacing, + false + ); + uint32 claimTickNextEpoch = EpochMap.get( + claimTickNext, + params.zeroForOne, + tickMap, + cache.constants + ); if (claimTickNextEpoch > cache.position.epochLast) { ///@dev - next tick in range should not have been crossed // require (false, 'ClaimTick::NextTickAlreadyCrossed()'); @@ -127,14 +177,22 @@ library Claims { } if (cache.search) { - (params, cache, claimTickEpoch) = search(ticks, tickMap, params, cache); + (params, cache, claimTickEpoch) = search( + ticks, + tickMap, + params, + cache + ); } /// @dev - start tick does not overwrite position and final tick clears position - if (params.claim != cache.position.upper && params.claim != cache.position.lower) { + if ( + params.claim != cache.position.upper && + params.claim != cache.position.lower + ) { // check epochLast on claim tick if (claimTickEpoch <= cache.position.epochLast) - require (false, 'ClaimTick::TickNotCrossed()'); + require(false, 'ClaimTick::TickNotCrossed()'); } return (params, cache); @@ -144,9 +202,7 @@ library Claims { PoolsharkStructs.BurnLimitParams memory params, LimitPoolStructs.BurnLimitCache memory cache, PoolsharkStructs.LimitImmutables memory constants - ) internal pure returns ( - LimitPoolStructs.BurnLimitCache memory - ) { + ) internal pure returns (LimitPoolStructs.BurnLimitCache memory) { // if half tick priceAt > 0 add amountOut to amountOutClaimed // set claimPriceLast if zero if (!cache.position.crossedInto) { @@ -156,35 +212,95 @@ library Claims { if (params.claim % constants.tickSpacing != 0) // this should pass price at the claim tick - locals.previousFullTick = TickMap.roundBack(params.claim, constants, params.zeroForOne, ConstantProduct.getPriceAtTick(params.claim, constants)); - else - locals.previousFullTick = params.claim; - locals.pricePrevious = ConstantProduct.getPriceAtTick(locals.previousFullTick, constants); - if (params.zeroForOne ? locals.previousFullTick > cache.position.lower - : locals.previousFullTick < cache.position.upper) { - + locals.previousFullTick = TickMap.roundBack( + params.claim, + constants, + params.zeroForOne, + ConstantProduct.getPriceAtTick(params.claim, constants) + ); + else locals.previousFullTick = params.claim; + locals.pricePrevious = ConstantProduct.getPriceAtTick( + locals.previousFullTick, + constants + ); + if ( + params.zeroForOne + ? locals.previousFullTick > cache.position.lower + : locals.previousFullTick < cache.position.upper + ) { // claim amounts up to latest full tick crossed - cache.amountIn += uint128(params.zeroForOne ? ConstantProduct.getDy(cache.position.liquidity, cache.priceLower, locals.pricePrevious, false) - : ConstantProduct.getDx(cache.position.liquidity, locals.pricePrevious, cache.priceUpper, false)); + cache.amountIn += uint128( + params.zeroForOne + ? ConstantProduct.getDy( + cache.position.liquidity, + cache.priceLower, + locals.pricePrevious, + false + ) + : ConstantProduct.getDx( + cache.position.liquidity, + locals.pricePrevious, + cache.priceUpper, + false + ) + ); } if (cache.liquidityBurned > 0) { - // if tick hasn't been set back calculate amountIn - if (params.zeroForOne ? cache.priceClaim > locals.pricePrevious - : cache.priceClaim < locals.pricePrevious) { + // if tick hasn't been set back calculate amountIn + if ( + params.zeroForOne + ? cache.priceClaim > locals.pricePrevious + : cache.priceClaim < locals.pricePrevious + ) { // allow partial tick claim if removing liquidity - cache.amountIn += uint128(params.zeroForOne ? ConstantProduct.getDy(cache.liquidityBurned, locals.pricePrevious, cache.priceClaim, false) - : ConstantProduct.getDx(cache.liquidityBurned, cache.priceClaim, locals.pricePrevious, false)); + cache.amountIn += uint128( + params.zeroForOne + ? ConstantProduct.getDy( + cache.liquidityBurned, + locals.pricePrevious, + cache.priceClaim, + false + ) + : ConstantProduct.getDx( + cache.liquidityBurned, + cache.priceClaim, + locals.pricePrevious, + false + ) + ); } // use priceClaim if tick hasn't been set back // else use claimPriceLast to calculate amountOut - if (params.claim != (params.zeroForOne ? cache.position.upper : cache.position.lower)) { - cache.amountOut += uint128(params.zeroForOne ? ConstantProduct.getDx(cache.liquidityBurned, cache.priceClaim, cache.priceUpper, false) - : ConstantProduct.getDy(cache.liquidityBurned, cache.priceLower, cache.priceClaim, false)); + if ( + params.claim != + ( + params.zeroForOne + ? cache.position.upper + : cache.position.lower + ) + ) { + cache.amountOut += uint128( + params.zeroForOne + ? ConstantProduct.getDx( + cache.liquidityBurned, + cache.priceClaim, + cache.priceUpper, + false + ) + : ConstantProduct.getDy( + cache.liquidityBurned, + cache.priceLower, + cache.priceClaim, + false + ) + ); } } // take protocol fee if needed if (cache.pool.protocolFillFee > 0 && cache.amountIn > 0) { - uint128 protocolFeeAmount = OverflowMath.mulDiv(cache.amountIn, cache.pool.protocolFillFee, 1e4).toUint128(); + uint128 protocolFeeAmount = OverflowMath + .mulDiv(cache.amountIn, cache.pool.protocolFillFee, 1e4) + .toUint128(); cache.amountIn -= protocolFeeAmount; cache.pool.protocolFees += protocolFeeAmount; } @@ -196,18 +312,26 @@ library Claims { PoolsharkStructs.TickMap storage tickMap, PoolsharkStructs.BurnLimitParams memory params, LimitPoolStructs.BurnLimitCache memory cache - ) internal view returns ( - PoolsharkStructs.BurnLimitParams memory, - LimitPoolStructs.BurnLimitCache memory, - uint32 claimTickEpoch - ) { + ) + internal + view + returns ( + PoolsharkStructs.BurnLimitParams memory, + LimitPoolStructs.BurnLimitCache memory, + uint32 claimTickEpoch + ) + { LimitPoolStructs.SearchLocals memory locals; locals.ticksFound = new int24[](256); locals.searchTick = params.claim; if (params.zeroForOne) { - for (uint i=0; i < maxWordsSearched;) { - (locals.ticksFound, locals.ticksIncluded, locals.searchTick) = TickMap.nextTicksWithinWord( + for (uint256 i = 0; i < maxWordsSearched; ) { + ( + locals.ticksFound, + locals.ticksIncluded, + locals.searchTick + ) = TickMap.nextTicksWithinWord( tickMap, locals.searchTick, cache.constants.tickSpacing, @@ -216,15 +340,24 @@ library Claims { locals.ticksIncluded ); // add start of next word if tick exists and is within range - if (locals.searchTick < cache.position.upper && TickMap.get(tickMap, locals.searchTick, cache.constants.tickSpacing)) { + if ( + locals.searchTick < cache.position.upper && + TickMap.get( + tickMap, + locals.searchTick, + cache.constants.tickSpacing + ) + ) { locals.ticksFound[locals.ticksIncluded] = locals.searchTick; unchecked { ++locals.ticksIncluded; } } // if we reached the final tick break the loop - if (locals.ticksIncluded > 0 && - locals.searchTick >= cache.position.upper) { + if ( + locals.ticksIncluded > 0 && + locals.searchTick >= cache.position.upper + ) { break; } unchecked { @@ -232,8 +365,12 @@ library Claims { } } } else { - for (int i=0; i<2;) { - (locals.ticksFound, locals.ticksIncluded, locals.searchTick) = TickMap.previousTicksWithinWord( + for (int256 i = 0; i < 2; ) { + ( + locals.ticksFound, + locals.ticksIncluded, + locals.searchTick + ) = TickMap.previousTicksWithinWord( tickMap, locals.searchTick, cache.constants.tickSpacing, @@ -242,49 +379,84 @@ library Claims { locals.ticksIncluded ); // add start of next word if tick exists and is within range - if (locals.searchTick > cache.position.lower && TickMap.get(tickMap, locals.searchTick, cache.constants.tickSpacing)) { + if ( + locals.searchTick > cache.position.lower && + TickMap.get( + tickMap, + locals.searchTick, + cache.constants.tickSpacing + ) + ) { locals.ticksFound[locals.ticksIncluded] = locals.searchTick; unchecked { ++locals.ticksIncluded; } } // if we reached the final tick break the loop - if (locals.ticksIncluded > 0 && - locals.searchTick <= cache.position.lower) { + if ( + locals.ticksIncluded > 0 && + locals.searchTick <= cache.position.lower + ) { break; } unchecked { ++i; } - } + } } // set initial endIdx if (locals.ticksIncluded > 0) { locals.endIdx = locals.ticksIncluded - 1; } else { - require(false, "ClaimTick::NoTicksFoundViaSearch()"); + require(false, 'ClaimTick::NoTicksFoundViaSearch()'); } while (locals.startIdx <= locals.endIdx) { // set idx at middle of start & end - locals.searchIdx = (locals.endIdx - locals.startIdx) / 2 + locals.startIdx; - + locals.searchIdx = + (locals.endIdx - locals.startIdx) / + 2 + + locals.startIdx; + // set ticks locals.searchTick = locals.ticksFound[locals.searchIdx]; if (locals.searchIdx + 1 < locals.ticksIncluded) { // tick ahead in array - locals.searchTickAhead = locals.ticksFound[locals.searchIdx + 1]; + locals.searchTickAhead = locals.ticksFound[ + locals.searchIdx + 1 + ]; } else { // tick ahead in storage - locals.searchTickAhead = params.zeroForOne ? TickMap.next(tickMap, locals.searchTick, cache.constants.tickSpacing, false) - : TickMap.previous(tickMap, locals.searchTick, cache.constants.tickSpacing, false); + locals.searchTickAhead = params.zeroForOne + ? TickMap.next( + tickMap, + locals.searchTick, + cache.constants.tickSpacing, + false + ) + : TickMap.previous( + tickMap, + locals.searchTick, + cache.constants.tickSpacing, + false + ); } // set epochs - locals.claimTickEpoch = EpochMap.get(locals.searchTick, params.zeroForOne, tickMap, cache.constants); - locals.claimTickAheadEpoch = EpochMap.get(locals.searchTickAhead, params.zeroForOne, tickMap, cache.constants); - + locals.claimTickEpoch = EpochMap.get( + locals.searchTick, + params.zeroForOne, + tickMap, + cache.constants + ); + locals.claimTickAheadEpoch = EpochMap.get( + locals.searchTickAhead, + params.zeroForOne, + tickMap, + cache.constants + ); + // check epochs if (locals.claimTickEpoch > cache.position.epochLast) { if (locals.claimTickAheadEpoch <= cache.position.epochLast) { @@ -304,22 +476,31 @@ library Claims { } // final check on valid claim tick - if (locals.claimTickEpoch <= cache.position.epochLast || - locals.claimTickAheadEpoch > cache.position.epochLast) { - require(false, "ClaimTick::NotFoundViaSearch()"); + if ( + locals.claimTickEpoch <= cache.position.epochLast || + locals.claimTickAheadEpoch > cache.position.epochLast + ) { + require(false, 'ClaimTick::NotFoundViaSearch()'); } - + cache.claimTick = ticks[locals.searchTick].limit; if ((locals.searchTick % cache.constants.tickSpacing) == 0) - cache.priceClaim = ConstantProduct.getPriceAtTick(locals.searchTick, cache.constants); + cache.priceClaim = ConstantProduct.getPriceAtTick( + locals.searchTick, + cache.constants + ); else { cache.priceClaim = cache.claimTick.priceAt; } if (cache.liquidityBurned == 0) - params.claim = TickMap.roundBack(locals.searchTick, cache.constants, params.zeroForOne, cache.priceClaim); - else - params.claim = locals.searchTick; + params.claim = TickMap.roundBack( + locals.searchTick, + cache.constants, + params.zeroForOne, + cache.priceClaim + ); + else params.claim = locals.searchTick; return (params, cache, locals.claimTickEpoch); } -} \ No newline at end of file +} diff --git a/contracts/libraries/limit/EpochMap.sol b/contracts/libraries/limit/EpochMap.sol index 0fa89d72..5c31c69b 100644 --- a/contracts/libraries/limit/EpochMap.sol +++ b/contracts/libraries/limit/EpochMap.sol @@ -1,18 +1,14 @@ // SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../math/ConstantProduct.sol'; import '../../interfaces/structs/LimitPoolStructs.sol'; library EpochMap { - event SyncLimitTick( - uint32 epoch, - int24 tick, - bool zeroForOne - ); + event SyncLimitTick(uint32 epoch, int24 tick, bool zeroForOne); function set( - int24 tick, + int24 tick, bool zeroForOne, uint256 epoch, PoolsharkStructs.TickMap storage tickMap, @@ -25,15 +21,17 @@ library EpochMap { uint256 volumeIndex ) = getIndices(tick, constants); // assert epoch isn't bigger than max uint32 - uint256 epochValue = zeroForOne ? tickMap.epochs0[volumeIndex][blockIndex][wordIndex] - : tickMap.epochs1[volumeIndex][blockIndex][wordIndex]; + uint256 epochValue = zeroForOne + ? tickMap.epochs0[volumeIndex][blockIndex][wordIndex] + : tickMap.epochs1[volumeIndex][blockIndex][wordIndex]; // clear previous value - epochValue &= ~(((1 << 9) - 1) << ((tickIndex & 0x7) * 32)); + epochValue &= ~(((1 << 9) - 1) << ((tickIndex & 0x7) * 32)); // add new value to word epochValue |= epoch << ((tickIndex & 0x7) * 32); // store word in map - zeroForOne ? tickMap.epochs0[volumeIndex][blockIndex][wordIndex] = epochValue - : tickMap.epochs1[volumeIndex][blockIndex][wordIndex] = epochValue; + zeroForOne + ? tickMap.epochs0[volumeIndex][blockIndex][wordIndex] = epochValue + : tickMap.epochs1[volumeIndex][blockIndex][wordIndex] = epochValue; emit SyncLimitTick(uint32(epoch), tick, zeroForOne); } @@ -43,9 +41,7 @@ library EpochMap { bool zeroForOne, PoolsharkStructs.TickMap storage tickMap, PoolsharkStructs.LimitImmutables memory constants - ) internal view returns ( - uint32 epoch - ) { + ) internal view returns (uint32 epoch) { ( uint256 tickIndex, uint256 wordIndex, @@ -53,8 +49,9 @@ library EpochMap { uint256 volumeIndex ) = getIndices(tick, constants); - uint256 epochValue = zeroForOne ? tickMap.epochs0[volumeIndex][blockIndex][wordIndex] - : tickMap.epochs1[volumeIndex][blockIndex][wordIndex]; + uint256 epochValue = zeroForOne + ? tickMap.epochs0[volumeIndex][blockIndex][wordIndex] + : tickMap.epochs1[volumeIndex][blockIndex][wordIndex]; // right shift so first 8 bits are epoch value epochValue >>= ((tickIndex & 0x7) * 32); // clear other bits @@ -65,7 +62,10 @@ library EpochMap { function getIndices( int24 tick, PoolsharkStructs.LimitImmutables memory constants - ) internal pure returns ( + ) + internal + pure + returns ( uint256 tickIndex, uint256 wordIndex, uint256 blockIndex, @@ -73,27 +73,34 @@ library EpochMap { ) { unchecked { - if (tick > ConstantProduct.maxTick(constants.tickSpacing)) require (false, 'TickIndexOverflow()'); - if (tick < ConstantProduct.minTick(constants.tickSpacing)) require (false, 'TickIndexUnderflow()'); + if (tick > ConstantProduct.maxTick(constants.tickSpacing)) + require(false, 'TickIndexOverflow()'); + if (tick < ConstantProduct.minTick(constants.tickSpacing)) + require(false, 'TickIndexUnderflow()'); if (tick % (constants.tickSpacing / 2) != 0) { - require (false, 'TickIndexInvalid()'); - } - tickIndex = uint256(int256((_round(tick, constants.tickSpacing / 2) - - _round(ConstantProduct.MIN_TICK, constants.tickSpacing / 2)) - / (constants.tickSpacing / 2))); - wordIndex = tickIndex >> 3; // 2^3 epochs per word - blockIndex = tickIndex >> 11; // 2^8 words per block - volumeIndex = tickIndex >> 19; // 2^8 blocks per volume - if (blockIndex > 2046) require (false, 'BlockIndexOverflow()'); + require(false, 'TickIndexInvalid()'); + } + tickIndex = uint256( + int256( + (_round(tick, constants.tickSpacing / 2) - + _round( + ConstantProduct.MIN_TICK, + constants.tickSpacing / 2 + )) / (constants.tickSpacing / 2) + ) + ); + wordIndex = tickIndex >> 3; // 2^3 epochs per word + blockIndex = tickIndex >> 11; // 2^8 words per block + volumeIndex = tickIndex >> 19; // 2^8 blocks per volume + if (blockIndex > 2046) require(false, 'BlockIndexOverflow()'); } } - function _round( - int24 tick, - int24 tickSpacing - ) internal pure returns ( - int24 roundedTick - ) { - return tick / tickSpacing * tickSpacing; + function _round(int24 tick, int24 tickSpacing) + internal + pure + returns (int24 roundedTick) + { + return (tick / tickSpacing) * tickSpacing; } } diff --git a/contracts/libraries/limit/LimitPositions.sol b/contracts/libraries/limit/LimitPositions.sol index 35d71993..18a47a69 100644 --- a/contracts/libraries/limit/LimitPositions.sol +++ b/contracts/libraries/limit/LimitPositions.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: BUSL-1.1 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import './LimitTicks.sol'; import '../../interfaces/IPositionERC1155.sol'; @@ -15,6 +15,15 @@ import '../Ticks.sol'; library LimitPositions { using SafeCast for uint256; + event Burn( + address indexed owner, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount, + uint256 amount0, + uint256 amount1 + ); + event BurnLimit( address indexed to, uint32 positionId, @@ -35,14 +44,24 @@ library LimitPositions { PoolsharkStructs.TickMap storage limitTickMap, PoolsharkStructs.MintLimitParams memory params, LimitPoolStructs.MintLimitCache memory cache - ) external returns ( - PoolsharkStructs.MintLimitParams memory, - LimitPoolStructs.MintLimitCache memory ) + external + returns ( + PoolsharkStructs.MintLimitParams memory, + LimitPoolStructs.MintLimitCache memory + ) { - cache.priceLower = ConstantProduct.getPriceAtTick(params.lower, cache.constants); - cache.priceUpper = ConstantProduct.getPriceAtTick(params.upper, cache.constants); - cache.mintSize = uint256(params.mintPercent) * uint256(params.amount) / 1e28; + cache.priceLower = ConstantProduct.getPriceAtTick( + params.lower, + cache.constants + ); + cache.priceUpper = ConstantProduct.getPriceAtTick( + params.upper, + cache.constants + ); + cache.mintSize = + (uint256(params.mintPercent) * uint256(params.amount)) / + 1e28; // calculate L constant cache.liquidityMinted = ConstantProduct.getLiquidityForAmounts( @@ -53,16 +72,36 @@ library LimitPositions { params.zeroForOne ? uint256(params.amount) : 0 ); - if (cache.liquidityMinted == 0) require (false, 'NoLiquidityBeingAdded()'); + if (cache.liquidityMinted == 0) + require(false, 'NoLiquidityBeingAdded()'); // calculate price limit by using half of input { - cache.priceLimit = params.zeroForOne ? ConstantProduct.getNewPrice(cache.priceUpper, cache.liquidityMinted, params.amount / 2, true, true) - : ConstantProduct.getNewPrice(cache.priceLower, cache.liquidityMinted, params.amount / 2, false, true); - if (cache.priceLimit == 0) require (false, 'PriceLimitZero()'); + cache.priceLimit = params.zeroForOne + ? ConstantProduct.getNewPrice( + cache.priceUpper, + cache.liquidityMinted, + params.amount / 2, + true, + true + ) + : ConstantProduct.getNewPrice( + cache.priceLower, + cache.liquidityMinted, + params.amount / 2, + false, + true + ); + if (cache.priceLimit == 0) require(false, 'PriceLimitZero()'); // get tick at price - cache.tickLimit = ConstantProduct.getTickAtPrice(cache.priceLimit.toUint160(), cache.constants); + cache.tickLimit = ConstantProduct.getTickAtPrice( + cache.priceLimit.toUint160(), + cache.constants + ); // round to nearest tick spacing - cache.priceLimit = ConstantProduct.getPriceAtTick(cache.tickLimit, cache.constants); + cache.priceLimit = ConstantProduct.getPriceAtTick( + cache.tickLimit, + cache.constants + ); } PoolsharkStructs.SwapCache memory swapCache; @@ -71,9 +110,14 @@ library LimitPositions { swapCache.price = cache.state.pool.price; // swap zero if no liquidity near market price - if (cache.state.pool.liquidity == 0 && - (params.zeroForOne ? swapCache.price > cache.priceLower - : swapCache.price < cache.priceUpper)) { + if ( + cache.state.pool.liquidity == 0 && + ( + params.zeroForOne + ? swapCache.price > cache.priceLower + : swapCache.price < cache.priceUpper + ) + ) { swapCache = Ticks.swap( ticks, samples, @@ -81,8 +125,9 @@ library LimitPositions { limitTickMap, PoolsharkStructs.SwapParams({ to: params.to, - priceLimit: (params.zeroForOne ? cache.priceLower - : cache.priceUpper).toUint160(), + priceLimit: ( + params.zeroForOne ? cache.priceLower : cache.priceUpper + ).toUint160(), amount: 0, exactIn: true, zeroForOne: params.zeroForOne, @@ -93,8 +138,11 @@ library LimitPositions { } // only swap if priceLimit is beyond current pool price - if (params.zeroForOne ? cache.priceLimit < swapCache.price - : cache.priceLimit > swapCache.price) { + if ( + params.zeroForOne + ? cache.priceLimit < swapCache.price + : cache.priceLimit > swapCache.price + ) { // swap and save the pool state swapCache = Ticks.swap( ticks, @@ -113,7 +161,6 @@ library LimitPositions { ); // subtract from remaining input amount params.amount -= uint128(swapCache.input); - } // save to cache cache.swapCache = swapCache; @@ -121,42 +168,72 @@ library LimitPositions { if (params.amount < cache.mintSize) params.amount = 0; // move start tick based on amount filled in swap - if ((params.amount > 0 && swapCache.input > 0) || - (params.zeroForOne ? cache.priceLower < swapCache.price - : cache.priceUpper > swapCache.price) + if ( + (params.amount > 0 && swapCache.input > 0) || + ( + params.zeroForOne + ? cache.priceLower < swapCache.price + : cache.priceUpper > swapCache.price + ) ) { // move the tick limit based on pool.tickAtPrice - if (params.zeroForOne ? cache.priceLower < swapCache.price - : cache.priceUpper > swapCache.price) { + if ( + params.zeroForOne + ? cache.priceLower < swapCache.price + : cache.priceUpper > swapCache.price + ) { cache.tickLimit = swapCache.state.pool.tickAtPrice; } // round ahead tickLimit to avoid crossing epochs - cache.tickLimit = TickMap.roundAhead(cache.tickLimit, cache.constants, params.zeroForOne, swapCache.price); + cache.tickLimit = TickMap.roundAhead( + cache.tickLimit, + cache.constants, + params.zeroForOne, + swapCache.price + ); if (params.zeroForOne) { if (cache.priceLower < swapCache.price) { // if rounding goes past limit trim position /// @dev - if swap didn't go to limit user would be 100% filled params.lower = cache.tickLimit; - cache.priceLower = ConstantProduct.getPriceAtTick(params.lower, cache.constants); + cache.priceLower = ConstantProduct.getPriceAtTick( + params.lower, + cache.constants + ); } - if (params.lower >= params.upper && - params.lower < ConstantProduct.maxTick(cache.constants.tickSpacing) - cache.constants.tickSpacing + if ( + params.lower >= params.upper && + params.lower < + ConstantProduct.maxTick(cache.constants.tickSpacing) - + cache.constants.tickSpacing ) { params.upper = params.lower + cache.constants.tickSpacing; } - cache.priceUpper = ConstantProduct.getPriceAtTick(params.upper, cache.constants); + cache.priceUpper = ConstantProduct.getPriceAtTick( + params.upper, + cache.constants + ); } else { if (cache.priceUpper > swapCache.price) { // if rounding goes past limit trim position params.upper = cache.tickLimit; - cache.priceUpper = ConstantProduct.getPriceAtTick(params.upper, cache.constants); + cache.priceUpper = ConstantProduct.getPriceAtTick( + params.upper, + cache.constants + ); } - if (params.upper <= params.lower && - params.lower > ConstantProduct.minTick(cache.constants.tickSpacing) + cache.constants.tickSpacing + if ( + params.upper <= params.lower && + params.lower > + ConstantProduct.minTick(cache.constants.tickSpacing) + + cache.constants.tickSpacing ) { params.lower = params.upper - cache.constants.tickSpacing; } - cache.priceLower = ConstantProduct.getPriceAtTick(params.lower, cache.constants); + cache.priceLower = ConstantProduct.getPriceAtTick( + params.lower, + cache.constants + ); } if (params.amount > 0 && params.lower < params.upper) { cache.liquidityMinted = ConstantProduct.getLiquidityForAmounts( @@ -166,10 +243,10 @@ library LimitPositions { params.zeroForOne ? 0 : uint256(params.amount), params.zeroForOne ? uint256(params.amount) : 0 ); - if (cache.liquidityMinted == 0) { + if (cache.liquidityMinted == 0) { // skip minting params.amount = 0; - } + } } else { // skip minting params.amount = 0; @@ -184,13 +261,12 @@ library LimitPositions { } // liquidity overflow check - if (cache.state.liquidityGlobal + cache.liquidityMinted > uint128(type(int128).max)) - require(false, 'LiquidityOverflow()'); + if ( + cache.state.liquidityGlobal + cache.liquidityMinted > + uint128(type(int128).max) + ) require(false, 'LiquidityOverflow()'); - return ( - params, - cache - ); + return (params, cache); } function add( @@ -198,10 +274,13 @@ library LimitPositions { mapping(int24 => LimitPoolStructs.Tick) storage ticks, PoolsharkStructs.TickMap storage tickMap, PoolsharkStructs.MintLimitParams memory params - ) internal returns ( - PoolsharkStructs.LimitPoolState memory, - LimitPoolStructs.LimitPosition memory - ) { + ) + internal + returns ( + PoolsharkStructs.LimitPoolState memory, + LimitPoolStructs.LimitPosition memory + ) + { if (cache.liquidityMinted == 0) return (cache.pool, cache.position); if (cache.position.liquidity == 0) { @@ -217,23 +296,26 @@ library LimitPositions { // safety check in case we somehow get here if ( params.zeroForOne - ? EpochMap.get(params.lower, params.zeroForOne, tickMap, cache.constants) - > cache.position.epochLast - : EpochMap.get(params.upper, params.zeroForOne, tickMap, cache.constants) - > cache.position.epochLast + ? EpochMap.get( + params.lower, + params.zeroForOne, + tickMap, + cache.constants + ) > cache.position.epochLast + : EpochMap.get( + params.upper, + params.zeroForOne, + tickMap, + cache.constants + ) > cache.position.epochLast ) { - require (false, 'PositionAlreadyEntered()'); + require(false, 'PositionAlreadyEntered()'); } /// @auditor maybe this shouldn't be a revert but rather just not mint the position? } - + // add liquidity to ticks - LimitTicks.insert( - ticks, - tickMap, - cache, - params - ); + LimitTicks.insert(ticks, tickMap, cache, params); // update liquidity global cache.state.liquidityGlobal += uint128(cache.liquidityMinted); @@ -248,68 +330,71 @@ library LimitPositions { PoolsharkStructs.TickMap storage tickMap, LimitPoolStructs.BurnLimitCache memory cache, PoolsharkStructs.BurnLimitParams memory params - ) internal returns ( - PoolsharkStructs.BurnLimitParams memory, - LimitPoolStructs.BurnLimitCache memory ) + internal + returns ( + PoolsharkStructs.BurnLimitParams memory, + LimitPoolStructs.BurnLimitCache memory + ) { - ( - params, - cache - ) = _deltas( - ticks, - tickMap, - params, - cache - ); + (params, cache) = _deltas(ticks, tickMap, params, cache); // update pool liquidity if (cache.priceClaim == cache.pool.price && cache.liquidityBurned > 0) { // handle pool.price at edge of range - if (params.zeroForOne ? cache.priceClaim < cache.priceUpper - : cache.priceClaim > cache.priceLower) - cache.pool.liquidity -= cache.liquidityBurned; + if ( + params.zeroForOne + ? cache.priceClaim < cache.priceUpper + : cache.priceClaim > cache.priceLower + ) cache.pool.liquidity -= cache.liquidityBurned; } if (cache.liquidityBurned > 0) { - if (params.claim == (params.zeroForOne ? cache.position.upper : cache.position.lower)) { + if ( + params.claim == + ( + params.zeroForOne + ? cache.position.upper + : cache.position.lower + ) + ) { // if claim is final tick no liquidity to remove cache.removeLower = false; cache.removeUpper = false; } else { // else remove liquidity from final tick - params.zeroForOne ? cache.removeUpper = true - : cache.removeLower = true; + params.zeroForOne + ? cache.removeUpper = true + : cache.removeLower = true; if (params.zeroForOne) { - - if (params.claim == cache.position.lower && + if ( + params.claim == cache.position.lower && cache.pool.price < cache.priceLower ) { // full tick price was touched cache.removeLower = true; - } else if (params.claim % cache.constants.tickSpacing != 0 && - cache.pool.price < cache.priceClaim) + } else if ( + params.claim % cache.constants.tickSpacing != 0 && + cache.pool.price < cache.priceClaim + ) // half tick was created cache.removeLower = true; } else { - if (params.claim == cache.position.upper && + if ( + params.claim == cache.position.upper && cache.pool.price > cache.priceUpper ) // full tick price was touched cache.removeUpper = true; - else if (params.claim % cache.constants.tickSpacing != 0 && - cache.pool.price > cache.priceClaim) + else if ( + params.claim % cache.constants.tickSpacing != 0 && + cache.pool.price > cache.priceClaim + ) // half tick was created cache.removeUpper = true; } } - LimitTicks.remove( - ticks, - tickMap, - params, - cache, - cache.constants - ); + LimitTicks.remove(ticks, tickMap, params, cache, cache.constants); // update position liquidity cache.position.liquidity -= uint128(cache.liquidityBurned); // update global liquidity @@ -319,12 +404,35 @@ library LimitPositions { // round back claim tick for storage if (params.claim % cache.constants.tickSpacing != 0) { cache.claim = params.claim; - params.claim = TickMap.roundBack(params.claim, cache.constants, params.zeroForOne, cache.priceClaim); + params.claim = TickMap.roundBack( + params.claim, + cache.constants, + params.zeroForOne, + cache.priceClaim + ); } + // emit custom event + emit BurnLimit( + params.to, + params.positionId, + cache.position.lower, + cache.position.upper, + cache.claim, + params.claim, + params.zeroForOne, + cache.liquidityBurned, + cache.amountIn, + cache.amountOut + ); + // clear filled position - if (params.zeroForOne ? params.claim == cache.position.upper - : params.claim == cache.position.lower) { + if ( + params.zeroForOne + ? params.claim == cache.position.upper + : params.claim == cache.position.lower + ) { + cache.liquidityBurned = cache.position.liquidity; cache.state.liquidityGlobal -= cache.position.liquidity; cache.position.liquidity = 0; } @@ -334,18 +442,17 @@ library LimitPositions { cache.position.epochLast = 0; cache.position.crossedInto = false; } - - emit BurnLimit( - params.to, - params.positionId, + + // emit standard event + emit Burn( + msg.sender, cache.position.lower, cache.position.upper, - cache.claim, - params.claim, - params.zeroForOne, - cache.liquidityBurned, - cache.amountIn, - cache.amountOut + uint128(cache.liquidityBurned), + params.zeroForOne ? cache.amountOut + : cache.amountIn, + params.zeroForOne ? cache.amountIn + : cache.amountOut ); // save pool to state in memory @@ -360,19 +467,8 @@ library LimitPositions { PoolsharkStructs.TickMap storage tickMap, LimitPoolStructs.BurnLimitCache memory cache, PoolsharkStructs.BurnLimitParams memory params - ) internal view returns ( - uint128 amountIn, - uint128 amountOut - ) { - ( - params, - cache - ) = _deltas( - ticks, - tickMap, - params, - cache - ); + ) internal view returns (uint128 amountIn, uint128 amountOut) { + (params, cache) = _deltas(ticks, tickMap, params, cache); return (cache.amountIn, cache.amountOut); } @@ -382,21 +478,35 @@ library LimitPositions { PoolsharkStructs.TickMap storage tickMap, PoolsharkStructs.BurnLimitParams memory params, LimitPoolStructs.BurnLimitCache memory cache - ) internal view returns ( - PoolsharkStructs.BurnLimitParams memory, - LimitPoolStructs.BurnLimitCache memory - ) { + ) + internal + view + returns ( + PoolsharkStructs.BurnLimitParams memory, + LimitPoolStructs.BurnLimitCache memory + ) + { cache = LimitPoolStructs.BurnLimitCache({ state: cache.state, pool: params.zeroForOne ? cache.state.pool0 : cache.state.pool1, claimTick: ticks[params.claim].limit, position: cache.position, constants: cache.constants, - priceLower: ConstantProduct.getPriceAtTick(cache.position.lower, cache.constants), - priceClaim: ticks[params.claim].limit.priceAt == 0 ? ConstantProduct.getPriceAtTick(params.claim, cache.constants) - : ticks[params.claim].limit.priceAt, - priceUpper: ConstantProduct.getPriceAtTick(cache.position.upper, cache.constants), - liquidityBurned: _convert(cache.position.liquidity, params.burnPercent), + priceLower: ConstantProduct.getPriceAtTick( + cache.position.lower, + cache.constants + ), + priceClaim: ticks[params.claim].limit.priceAt == 0 + ? ConstantProduct.getPriceAtTick(params.claim, cache.constants) + : ticks[params.claim].limit.priceAt, + priceUpper: ConstantProduct.getPriceAtTick( + cache.position.upper, + cache.constants + ), + liquidityBurned: _convert( + cache.position.liquidity, + params.burnPercent + ), amountIn: 0, amountOut: 0, claim: params.claim, @@ -406,12 +516,7 @@ library LimitPositions { }); // check claim is valid - (params, cache) = Claims.validate( - ticks, - tickMap, - params, - cache - ); + (params, cache) = Claims.validate(ticks, tickMap, params, cache); // calculate position deltas cache = Claims.getDeltas(params, cache, cache.constants); @@ -419,15 +524,14 @@ library LimitPositions { return (params, cache); } - function _convert( - uint128 liquidity, - uint128 percent - ) internal pure returns ( - uint128 - ) { + function _convert(uint128 liquidity, uint128 percent) + internal + pure + returns (uint128) + { // convert percentage to liquidity amount if (percent > 1e38) percent = 1e38; - if (liquidity == 0 && percent > 0) require (false, 'PositionNotFound()'); - return uint128(uint256(liquidity) * uint256(percent) / 1e38); + if (liquidity == 0 && percent > 0) require(false, 'PositionNotFound()'); + return uint128((uint256(liquidity) * uint256(percent)) / 1e38); } -} \ No newline at end of file +} diff --git a/contracts/libraries/limit/LimitTicks.sol b/contracts/libraries/limit/LimitTicks.sol index 75373471..1b135450 100644 --- a/contracts/libraries/limit/LimitTicks.sol +++ b/contracts/libraries/limit/LimitTicks.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity 0.8.18; import '../../interfaces/structs/LimitPoolStructs.sol'; import '../../interfaces/limit/ILimitPoolFactory.sol'; @@ -14,7 +14,6 @@ import '../utils/SafeCast.sol'; /// @notice Tick management library for limit pools library LimitTicks { - using SafeCast for uint256; uint256 internal constant Q96 = 0x1000000000000000000000000; @@ -31,9 +30,11 @@ library LimitTicks { int24 tickSpacing ) internal pure { if (lower % tickSpacing != 0) require(false, 'InvalidLowerTick()'); - if (lower <= ConstantProduct.MIN_TICK) require(false, 'InvalidLowerTick()'); + if (lower <= ConstantProduct.MIN_TICK) + require(false, 'InvalidLowerTick()'); if (upper % tickSpacing != 0) require(false, 'InvalidUpperTick()'); - if (upper >= ConstantProduct.MAX_TICK) require(false, 'InvalidUpperTick()'); + if (upper >= ConstantProduct.MAX_TICK) + require(false, 'InvalidUpperTick()'); if (lower >= upper) require(false, 'InvalidPositionBounds()'); } @@ -48,18 +49,42 @@ library LimitTicks { // check if adding liquidity necessary if (!params.zeroForOne || cache.priceLower > cache.pool.price) { // sets bit in map - if(!TickMap.set(tickMap, params.lower, cache.constants.tickSpacing)){ - // inherit epoch + if ( + !TickMap.set(tickMap, params.lower, cache.constants.tickSpacing) + ) { + // inherit epoch int24 tickAhead; if (params.zeroForOne) { - tickAhead = TickMap.next(tickMap, params.lower, cache.constants.tickSpacing, false); + tickAhead = TickMap.next( + tickMap, + params.lower, + cache.constants.tickSpacing, + false + ); } else { - tickAhead = TickMap.previous(tickMap, params.lower, cache.constants.tickSpacing, false); + tickAhead = TickMap.previous( + tickMap, + params.lower, + cache.constants.tickSpacing, + false + ); } - uint32 epochAhead = EpochMap.get(tickAhead, params.zeroForOne, tickMap, cache.constants); - EpochMap.set(params.lower, params.zeroForOne, epochAhead, tickMap, cache.constants); + uint32 epochAhead = EpochMap.get( + tickAhead, + params.zeroForOne, + tickMap, + cache.constants + ); + EpochMap.set( + params.lower, + params.zeroForOne, + epochAhead, + tickMap, + cache.constants + ); } - PoolsharkStructs.LimitTick memory tickLower = ticks[params.lower].limit; + PoolsharkStructs.LimitTick memory tickLower = ticks[params.lower] + .limit; if (params.zeroForOne) { tickLower.liquidityDelta += int128(liquidityMinted); } else { @@ -71,21 +96,51 @@ library LimitTicks { /// @dev - i.e. if zeroForOne && cache.priceLower <= cache.pool.price cache.state.epoch += 1; // mark epoch on undercut tick - EpochMap.set(params.lower, params.zeroForOne, cache.state.epoch, tickMap, cache.constants); + EpochMap.set( + params.lower, + params.zeroForOne, + cache.state.epoch, + tickMap, + cache.constants + ); } if (params.zeroForOne || cache.priceUpper < cache.pool.price) { - if(!TickMap.set(tickMap, params.upper, cache.constants.tickSpacing)) { + if ( + !TickMap.set(tickMap, params.upper, cache.constants.tickSpacing) + ) { int24 tickAhead; if (params.zeroForOne) { - tickAhead = TickMap.next(tickMap, params.upper, cache.constants.tickSpacing, false); + tickAhead = TickMap.next( + tickMap, + params.upper, + cache.constants.tickSpacing, + false + ); } else { - tickAhead = TickMap.previous(tickMap, params.upper, cache.constants.tickSpacing, false); + tickAhead = TickMap.previous( + tickMap, + params.upper, + cache.constants.tickSpacing, + false + ); } - uint32 epochAhead = EpochMap.get(tickAhead, params.zeroForOne, tickMap, cache.constants); - EpochMap.set(params.upper, params.zeroForOne, epochAhead, tickMap, cache.constants); + uint32 epochAhead = EpochMap.get( + tickAhead, + params.zeroForOne, + tickMap, + cache.constants + ); + EpochMap.set( + params.upper, + params.zeroForOne, + epochAhead, + tickMap, + cache.constants + ); } - PoolsharkStructs.LimitTick memory tickUpper = ticks[params.upper].limit; + PoolsharkStructs.LimitTick memory tickUpper = ticks[params.upper] + .limit; if (params.zeroForOne) { tickUpper.liquidityDelta -= int128(liquidityMinted); } else { @@ -97,7 +152,13 @@ library LimitTicks { /// @dev - i.e. if !zeroForOne && cache.priceUpper >= cache.pool.price cache.state.epoch += 1; // mark epoch on undercut tick - EpochMap.set(params.upper, params.zeroForOne, cache.state.epoch, tickMap, cache.constants); + EpochMap.set( + params.upper, + params.zeroForOne, + cache.state.epoch, + tickMap, + cache.constants + ); } } @@ -108,58 +169,127 @@ library LimitTicks { LimitPoolStructs.MintLimitCache memory cache, PoolsharkStructs.LimitPoolState memory pool, PoolsharkStructs.LimitImmutables memory constants - ) internal returns ( - PoolsharkStructs.LimitPoolState memory - ){ + ) internal returns (PoolsharkStructs.LimitPoolState memory) { /// @auditor - would be smart to protect against the case of epochs crossing - ( - int24 tickToSave, - uint160 roundedPrice - ) = TickMap.roundHalf(pool.tickAtPrice, constants, pool.price); + (int24 tickToSave, uint160 roundedPrice) = TickMap.roundHalf( + pool.tickAtPrice, + constants, + pool.price + ); // update tick to save LimitPoolStructs.LimitTick memory tick = ticks[tickToSave].limit; /// @auditor - tick.priceAt will be zero for tick % tickSpacing == 0 if (tick.priceAt == 0) { - if (pool.price != (params.zeroForOne ? cache.priceLower : cache.priceUpper)) { + if ( + pool.price != + (params.zeroForOne ? cache.priceLower : cache.priceUpper) + ) { TickMap.set(tickMap, tickToSave, constants.tickSpacing); } - EpochMap.set(tickToSave, params.zeroForOne, cache.state.epoch, tickMap, constants); + EpochMap.set( + tickToSave, + params.zeroForOne, + cache.state.epoch, + tickMap, + constants + ); } // skip if we are at the nearest full tick - if(pool.price != roundedPrice) { + if (pool.price != roundedPrice) { // if empty just save the pool price if (tick.priceAt == 0) { tick.priceAt = pool.price; - } - else { + } else { // we need to blend the two partial fills into a single tick LimitPoolStructs.InsertSingleLocals memory locals; if (params.zeroForOne) { // price moves up so previousFullTick is lesser - locals.previousFullTick = tickToSave - constants.tickSpacing / 2; - locals.pricePrevious = ConstantProduct.getPriceAtTick(locals.previousFullTick, constants); + locals.previousFullTick = + tickToSave - + constants.tickSpacing / + 2; + locals.pricePrevious = ConstantProduct.getPriceAtTick( + locals.previousFullTick, + constants + ); // calculate amountOut filled across both partial fills - locals.amountOutExact = ConstantProduct.getDy(pool.liquidity, locals.pricePrevious, pool.price, false); - locals.amountOutExact += ConstantProduct.getDy(uint128(tick.liquidityDelta), locals.pricePrevious, tick.priceAt, false); + locals.amountOutExact = ConstantProduct.getDy( + pool.liquidity, + locals.pricePrevious, + pool.price, + false + ); + locals.amountOutExact += ConstantProduct.getDy( + uint128(tick.liquidityDelta), + locals.pricePrevious, + tick.priceAt, + false + ); // add current pool liquidity to partial tick - uint128 combinedLiquidity = pool.liquidity + uint128(tick.liquidityDelta); + uint128 combinedLiquidity = pool.liquidity + + uint128(tick.liquidityDelta); // advance price based on combined fill - tick.priceAt = ConstantProduct.getNewPrice(uint256(locals.pricePrevious), combinedLiquidity, locals.amountOutExact, false, true).toUint160(); + tick.priceAt = ConstantProduct + .getNewPrice( + uint256(locals.pricePrevious), + combinedLiquidity, + locals.amountOutExact, + false, + true + ) + .toUint160(); // dx to the next tick is less than before the tick blend - EpochMap.set(tickToSave, params.zeroForOne, cache.state.epoch, tickMap, constants); + EpochMap.set( + tickToSave, + params.zeroForOne, + cache.state.epoch, + tickMap, + constants + ); } else { // price moves down so previousFullTick is greater - locals.previousFullTick = tickToSave + constants.tickSpacing / 2; - locals.pricePrevious = ConstantProduct.getPriceAtTick(locals.previousFullTick, constants); + locals.previousFullTick = + tickToSave + + constants.tickSpacing / + 2; + locals.pricePrevious = ConstantProduct.getPriceAtTick( + locals.previousFullTick, + constants + ); // calculate amountOut filled across both partial fills - locals.amountOutExact = ConstantProduct.getDx(pool.liquidity, pool.price, locals.pricePrevious, false); - locals.amountOutExact += ConstantProduct.getDx(uint128(tick.liquidityDelta), tick.priceAt, locals.pricePrevious, false); + locals.amountOutExact = ConstantProduct.getDx( + pool.liquidity, + pool.price, + locals.pricePrevious, + false + ); + locals.amountOutExact += ConstantProduct.getDx( + uint128(tick.liquidityDelta), + tick.priceAt, + locals.pricePrevious, + false + ); // add current pool liquidity to partial tick - uint128 combinedLiquidity = pool.liquidity + uint128(tick.liquidityDelta); + uint128 combinedLiquidity = pool.liquidity + + uint128(tick.liquidityDelta); // advance price based on combined fill - tick.priceAt = ConstantProduct.getNewPrice(uint256(locals.pricePrevious), combinedLiquidity, locals.amountOutExact, true, true).toUint160(); + tick.priceAt = ConstantProduct + .getNewPrice( + uint256(locals.pricePrevious), + combinedLiquidity, + locals.amountOutExact, + true, + true + ) + .toUint160(); // mark epoch for second partial fill positions - EpochMap.set(tickToSave, params.zeroForOne, cache.state.epoch, tickMap, constants); + EpochMap.set( + tickToSave, + params.zeroForOne, + cache.state.epoch, + tickMap, + constants + ); } } } @@ -167,7 +297,11 @@ library LimitTicks { if ((tickToSave != (params.zeroForOne ? params.lower : params.upper))) { tick.liquidityDelta += int128(pool.liquidity); tick.liquidityAbsolute += pool.liquidity; - emit SyncLimitLiquidity(pool.liquidity, tickToSave, params.zeroForOne); + emit SyncLimitLiquidity( + pool.liquidity, + tickToSave, + params.zeroForOne + ); pool.liquidity = 0; } ticks[tickToSave].limit = tick; @@ -184,9 +318,9 @@ library LimitTicks { // set ticks based on claim and zeroForOne int24 lower = params.zeroForOne ? params.claim : cache.position.lower; int24 upper = params.zeroForOne ? cache.position.upper : params.claim; - { + { PoolsharkStructs.LimitTick memory tickLower = ticks[lower].limit; - + if (cache.removeLower) { if (params.zeroForOne) { tickLower.liquidityDelta -= int128(cache.liquidityBurned); @@ -213,31 +347,65 @@ library LimitTicks { } } - function unlock( + function unlock( LimitPoolStructs.MintLimitCache memory cache, PoolsharkStructs.LimitPoolState memory pool, mapping(int24 => LimitPoolStructs.Tick) storage ticks, PoolsharkStructs.TickMap storage tickMap, bool zeroForOne - ) internal returns ( - LimitPoolStructs.MintLimitCache memory, - PoolsharkStructs.LimitPoolState memory ) + internal + returns ( + LimitPoolStructs.MintLimitCache memory, + PoolsharkStructs.LimitPoolState memory + ) { if (pool.liquidity > 0) return (cache, pool); - (int24 startTick,) = TickMap.roundHalf(pool.tickAtPrice, cache.constants, pool.price); + (int24 startTick, ) = TickMap.roundHalf( + pool.tickAtPrice, + cache.constants, + pool.price + ); if (zeroForOne) { - pool.tickAtPrice = TickMap.next(tickMap, startTick, cache.constants.tickSpacing, true); - if (pool.tickAtPrice < ConstantProduct.maxTick(cache.constants.tickSpacing)) { - EpochMap.set(pool.tickAtPrice, zeroForOne, cache.state.epoch, tickMap, cache.constants); + pool.tickAtPrice = TickMap.next( + tickMap, + startTick, + cache.constants.tickSpacing, + true + ); + if ( + pool.tickAtPrice < + ConstantProduct.maxTick(cache.constants.tickSpacing) + ) { + EpochMap.set( + pool.tickAtPrice, + zeroForOne, + cache.state.epoch, + tickMap, + cache.constants + ); } } else { /// @dev - roundedUp true since liquidity could be equal to the current pool tickAtPrice - pool.tickAtPrice = TickMap.previous(tickMap, startTick, cache.constants.tickSpacing, true); - if (pool.tickAtPrice > ConstantProduct.minTick(cache.constants.tickSpacing)) { - EpochMap.set(pool.tickAtPrice, zeroForOne, cache.state.epoch, tickMap, cache.constants); + pool.tickAtPrice = TickMap.previous( + tickMap, + startTick, + cache.constants.tickSpacing, + true + ); + if ( + pool.tickAtPrice > + ConstantProduct.minTick(cache.constants.tickSpacing) + ) { + EpochMap.set( + pool.tickAtPrice, + zeroForOne, + cache.state.epoch, + tickMap, + cache.constants + ); } } @@ -248,15 +416,21 @@ library LimitTicks { if (tickPriceAt == 0) { // if full tick crossed - pool.price = ConstantProduct.getPriceAtTick(pool.tickAtPrice, cache.constants); + pool.price = ConstantProduct.getPriceAtTick( + pool.tickAtPrice, + cache.constants + ); } else { // if half tick crossed pool.price = tickPriceAt; - pool.tickAtPrice = ConstantProduct.getTickAtPrice(tickPriceAt, cache.constants); + pool.tickAtPrice = ConstantProduct.getTickAtPrice( + tickPriceAt, + cache.constants + ); } // zero out tick - ticks[tickToClear].limit = PoolsharkStructs.LimitTick(0,0,0); + ticks[tickToClear].limit = PoolsharkStructs.LimitTick(0, 0, 0); clear(ticks, cache.constants, tickMap, tickToClear); return (cache, pool); @@ -269,19 +443,21 @@ library LimitTicks { int24 tickToClear ) internal { if (_empty(ticks[tickToClear])) { - if (tickToClear != ConstantProduct.maxTick(constants.tickSpacing) && - tickToClear != ConstantProduct.minTick(constants.tickSpacing)) { - ticks[tickToClear].limit = PoolsharkStructs.LimitTick(0,0,0); + if ( + tickToClear != ConstantProduct.maxTick(constants.tickSpacing) && + tickToClear != ConstantProduct.minTick(constants.tickSpacing) + ) { + ticks[tickToClear].limit = PoolsharkStructs.LimitTick(0, 0, 0); TickMap.unset(tickMap, tickToClear, constants.tickSpacing); } } } - function _empty( - LimitPoolStructs.Tick memory tick - ) internal pure returns ( - bool - ) { + function _empty(LimitPoolStructs.Tick memory tick) + internal + pure + returns (bool) + { return tick.limit.liquidityAbsolute == 0; } } diff --git a/contracts/libraries/limit/pool/BurnLimitCall.sol b/contracts/libraries/limit/pool/BurnLimitCall.sol index 308a1d84..03fda008 100644 --- a/contracts/libraries/limit/pool/BurnLimitCall.sol +++ b/contracts/libraries/limit/pool/BurnLimitCall.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity 0.8.18; import '../../../interfaces/structs/LimitPoolStructs.sol'; import '../../../interfaces/IPositionERC1155.sol'; @@ -8,6 +8,8 @@ import '../../utils/Collect.sol'; import '../../utils/PositionTokens.sol'; library BurnLimitCall { + using SafeCast for uint128; + event BurnLimit( address indexed to, uint32 positionId, @@ -21,42 +23,52 @@ library BurnLimitCall { uint128 tokenOutBurned ); + struct BurnLimitLocals { + int128 amount0Delta; + int128 amount1Delta; + } + function perform( - mapping(uint256 => LimitPoolStructs.LimitPosition) - storage positions, + mapping(uint256 => LimitPoolStructs.LimitPosition) storage positions, mapping(int24 => LimitPoolStructs.Tick) storage ticks, PoolsharkStructs.TickMap storage tickMap, PoolsharkStructs.GlobalState storage globalState, PoolsharkStructs.BurnLimitParams memory params, LimitPoolStructs.BurnLimitCache memory cache - ) external { + ) + external + returns ( + int256, // amount0Delta + int256 // amount1Delta + ) + { // check for invalid receiver - if (params.to == address(0)) - require(false, 'CollectToZeroAddress()'); + if (params.to == address(0)) require(false, 'CollectToZeroAddress()'); // initialize cache cache.state = globalState; cache.position = positions[params.positionId]; - if (cache.position.liquidity == 0) - require(false, 'PositionNotFound()'); - if (PositionTokens.balanceOf(cache.constants, msg.sender, params.positionId) == 0) - require(false, 'PositionOwnerMismatch()'); - + if (cache.position.liquidity == 0) require(false, 'PositionNotFound()'); + if ( + PositionTokens.balanceOf( + cache.constants, + msg.sender, + params.positionId + ) == 0 + ) require(false, 'PositionOwnerMismatch()'); + // update position - ( - params, - cache - ) = LimitPositions.update( - ticks, - tickMap, - cache, - params - ); + (params, cache) = LimitPositions.update(ticks, tickMap, cache, params); // save position before transfer - if ((params.zeroForOne ? params.claim != cache.position.upper - : params.claim != cache.position.lower)) { + if ( + ( + params.zeroForOne + ? params.claim != cache.position.upper + : params.claim != cache.position.lower + ) + ) { if (cache.position.liquidity > 0) { if (params.zeroForOne) { cache.position.lower = params.claim; @@ -65,21 +77,34 @@ library BurnLimitCall { } positions[params.positionId] = cache.position; } else { - IPositionERC1155(cache.constants.poolToken).burn(msg.sender, params.positionId, 1, cache.constants); + IPositionERC1155(cache.constants.poolToken).burn( + msg.sender, + params.positionId, + 1, + cache.constants + ); delete positions[params.positionId]; } } else { - IPositionERC1155(cache.constants.poolToken).burn(msg.sender, params.positionId, 1, cache.constants); + IPositionERC1155(cache.constants.poolToken).burn( + msg.sender, + params.positionId, + 1, + cache.constants + ); delete positions[params.positionId]; } // save state before transfer call save(cache, globalState, params.zeroForOne); - - cache = Collect.burnLimit( + + BurnLimitLocals memory locals; + (cache, locals.amount0Delta, locals.amount1Delta) = CollectLib.burnLimit( cache, params ); + + return (locals.amount0Delta, locals.amount1Delta); } function save( diff --git a/contracts/libraries/limit/pool/MintLimitCall.sol b/contracts/libraries/limit/pool/MintLimitCall.sol index 1866f7d7..fe7138b5 100644 --- a/contracts/libraries/limit/pool/MintLimitCall.sol +++ b/contracts/libraries/limit/pool/MintLimitCall.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity 0.8.18; import '../../../interfaces/structs/LimitPoolStructs.sol'; import '../../../interfaces/callbacks/ILimitPoolCallback.sol'; @@ -9,6 +9,17 @@ import '../../utils/Collect.sol'; import '../../utils/PositionTokens.sol'; library MintLimitCall { + + event Mint( + address sender, + address indexed owner, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount, + uint256 amount0, + uint256 amount1 + ); + event MintLimit( address indexed to, int24 lower, @@ -29,8 +40,7 @@ library MintLimitCall { ); function perform( - mapping(uint256 => LimitPoolStructs.LimitPosition) - storage positions, + mapping(uint256 => LimitPoolStructs.LimitPosition) storage positions, mapping(int24 => LimitPoolStructs.Tick) storage ticks, RangePoolStructs.Sample[65535] storage samples, PoolsharkStructs.TickMap storage rangeTickMap, @@ -38,15 +48,24 @@ library MintLimitCall { PoolsharkStructs.GlobalState storage globalState, PoolsharkStructs.MintLimitParams memory params, LimitPoolStructs.MintLimitCache memory cache - ) external { + ) + external + returns ( + int256, // amount0Delta + int256 // amount1Delta + ) + { // check for invalid receiver - if (params.to == address(0)) - require(false, "CollectToZeroAddress()"); + if (params.to == address(0)) require(false, 'CollectToZeroAddress()'); cache.state = globalState; // validate position ticks - ConstantProduct.checkTicks(params.lower, params.upper, cache.constants.tickSpacing); + ConstantProduct.checkTicks( + params.lower, + params.upper, + cache.constants.tickSpacing + ); if (params.positionId > 0) { cache.position = positions[params.positionId]; @@ -54,8 +73,13 @@ library MintLimitCall { // position doesn't exist require(false, 'PositionNotFound()'); } - if (PositionTokens.balanceOf(cache.constants, params.to, params.positionId) == 0) - require(false, 'PositionOwnerMismatch()'); + if ( + PositionTokens.balanceOf( + cache.constants, + params.to, + params.positionId + ) == 0 + ) require(false, 'PositionOwnerMismatch()'); } // resize position if necessary @@ -71,20 +95,24 @@ library MintLimitCall { // save state for reentrancy safety save(cache, globalState, !params.zeroForOne); - // transfer out if swap output + // transfer out if swap output if (cache.swapCache.output > 0) SafeTransfers.transferOut( params.to, - params.zeroForOne ? cache.constants.token1 - : cache.constants.token0, + params.zeroForOne + ? cache.constants.token1 + : cache.constants.token0, cache.swapCache.output ); // mint position if amount is left if (params.amount > 0 && params.lower < params.upper) { // check if new position created - if (params.positionId == 0 || // new position - params.lower != cache.position.lower || // lower mismatch - params.upper != cache.position.upper) { // upper mismatch + if ( + params.positionId == 0 || // new position + params.lower != cache.position.lower || // lower mismatch + params.upper != cache.position.upper + ) { + // upper mismatch LimitPoolStructs.LimitPosition memory newPosition; newPosition.lower = params.lower; newPosition.upper = params.upper; @@ -93,19 +121,37 @@ library MintLimitCall { params.positionId = cache.state.positionIdNext; cache.state.positionIdNext += 1; } - cache.pool = params.zeroForOne ? cache.state.pool0 : cache.state.pool1; + cache.pool = params.zeroForOne + ? cache.state.pool0 + : cache.state.pool1; // bump to the next tick if there is no liquidity if (cache.pool.liquidity == 0) { /// @dev - this makes sure to have liquidity unlocked if undercutting - (cache, cache.pool) = LimitTicks.unlock(cache, cache.pool, ticks, limitTickMap, params.zeroForOne); + (cache, cache.pool) = LimitTicks.unlock( + cache, + cache.pool, + ticks, + limitTickMap, + params.zeroForOne + ); } if (params.zeroForOne) { - uint160 priceLower = ConstantProduct.getPriceAtTick(params.lower, cache.constants); + uint160 priceLower = ConstantProduct.getPriceAtTick( + params.lower, + cache.constants + ); if (priceLower <= cache.pool.price) { // save liquidity if active if (cache.pool.liquidity > 0) { - cache.pool = LimitTicks.insertSingle(params, ticks, limitTickMap, cache, cache.pool, cache.constants); + cache.pool = LimitTicks.insertSingle( + params, + ticks, + limitTickMap, + cache, + cache.pool, + cache.constants + ); } cache.pool.price = priceLower; cache.pool.tickAtPrice = params.lower; @@ -114,13 +160,29 @@ library MintLimitCall { cache.position.crossedInto = true; // set epoch on start tick to signify position being crossed into /// @auditor - this is safe assuming we have swapped at least this far on the other side - emit SyncLimitPool(cache.pool.price, cache.pool.liquidity, cache.state.epoch, cache.pool.tickAtPrice, params.zeroForOne); + emit SyncLimitPool( + cache.pool.price, + cache.pool.liquidity, + cache.state.epoch, + cache.pool.tickAtPrice, + params.zeroForOne + ); } } else { - uint160 priceUpper = ConstantProduct.getPriceAtTick(params.upper, cache.constants); + uint160 priceUpper = ConstantProduct.getPriceAtTick( + params.upper, + cache.constants + ); if (priceUpper >= cache.pool.price) { if (cache.pool.liquidity > 0) { - cache.pool = LimitTicks.insertSingle(params, ticks, limitTickMap, cache, cache.pool, cache.constants); + cache.pool = LimitTicks.insertSingle( + params, + ticks, + limitTickMap, + cache, + cache.pool, + cache.constants + ); } cache.pool.price = priceUpper; cache.pool.tickAtPrice = params.upper; @@ -128,7 +190,13 @@ library MintLimitCall { cache.position.crossedInto = true; // set epoch on start tick to signify position being crossed into /// @auditor - this is safe assuming we have swapped at least this far on the other side - emit SyncLimitPool(cache.pool.price, cache.pool.liquidity, cache.state.epoch, cache.pool.tickAtPrice, params.zeroForOne); + emit SyncLimitPool( + cache.pool.price, + cache.pool.liquidity, + cache.state.epoch, + cache.pool.tickAtPrice, + params.zeroForOne + ); } } (cache.pool, cache.position) = LimitPositions.add( @@ -141,7 +209,19 @@ library MintLimitCall { // save position to storage positions[params.positionId] = cache.position; - params.zeroForOne ? cache.state.pool0 = cache.pool : cache.state.pool1 = cache.pool; + params.zeroForOne + ? cache.state.pool0 = cache.pool + : cache.state.pool1 = cache.pool; + + emit Mint( + msg.sender, + params.to, + cache.position.lower, + cache.position.upper, + uint128(cache.liquidityMinted), + uint128(params.zeroForOne ? params.amount : 0), + uint128(params.zeroForOne ? 0 : params.amount) + ); emit MintLimit( params.to, @@ -160,14 +240,29 @@ library MintLimitCall { // check balance and execute callback uint256 balanceStart = balance(params, cache); ILimitPoolMintLimitCallback(msg.sender).limitPoolMintLimitCallback( - params.zeroForOne ? -int256(params.amount + cache.swapCache.input) : int256(cache.swapCache.output), - params.zeroForOne ? int256(cache.swapCache.output) : -int256(params.amount + cache.swapCache.input), + params.zeroForOne + ? -int256(params.amount + cache.swapCache.input) + : int256(cache.swapCache.output), + params.zeroForOne + ? int256(cache.swapCache.output) + : -int256(params.amount + cache.swapCache.input), params.callbackData ); // check balance requirements after callback - if (balance(params, cache) < balanceStart + params.amount + cache.swapCache.input) - require(false, 'MintInputAmountTooLow()'); + if ( + balance(params, cache) < + balanceStart + params.amount + cache.swapCache.input + ) require(false, 'MintInputAmountTooLow()'); + + return ( + params.zeroForOne + ? -int256(params.amount + cache.swapCache.input) + : int256(cache.swapCache.output), + params.zeroForOne + ? int256(cache.swapCache.output) + : -int256(params.amount + cache.swapCache.input) + ); } function save( @@ -187,22 +282,18 @@ library MintLimitCall { } } - function balance( PoolsharkStructs.MintLimitParams memory params, LimitPoolStructs.MintLimitCache memory cache ) private view returns (uint256) { - ( - bool success, - bytes memory data - ) = (params.zeroForOne ? cache.constants.token0 - : cache.constants.token1) - .staticcall( - abi.encodeWithSelector( - IERC20Minimal.balanceOf.selector, - address(this) - ) - ); + (bool success, bytes memory data) = ( + params.zeroForOne ? cache.constants.token0 : cache.constants.token1 + ).staticcall( + abi.encodeWithSelector( + IERC20Minimal.balanceOf.selector, + address(this) + ) + ); require(success && data.length >= 32); return abi.decode(data, (uint256)); } diff --git a/contracts/libraries/limit/pool/SnapshotLimitCall.sol b/contracts/libraries/limit/pool/SnapshotLimitCall.sol index 4c3799cc..a012640c 100644 --- a/contracts/libraries/limit/pool/SnapshotLimitCall.sol +++ b/contracts/libraries/limit/pool/SnapshotLimitCall.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity 0.8.18; import '../../../interfaces/structs/LimitPoolStructs.sol'; import '../LimitPositions.sol'; @@ -22,37 +22,28 @@ library SnapshotLimitCall { ); function perform( - mapping(uint256 => LimitPoolStructs.LimitPosition) - storage positions, + mapping(uint256 => LimitPoolStructs.LimitPosition) storage positions, mapping(int24 => LimitPoolStructs.Tick) storage ticks, PoolsharkStructs.TickMap storage tickMap, PoolsharkStructs.GlobalState memory state, PoolsharkStructs.LimitImmutables memory constants, LimitPoolStructs.SnapshotLimitParams memory params - ) external view returns ( - uint128, - uint128 - ) - { + ) external view returns (uint128, uint128) { if (state.unlocked == _ENTERED) require(false, 'ReentrancyGuardReadOnlyReentrantCall()'); LimitPoolStructs.BurnLimitCache memory cache; cache.state = state; cache.constants = constants; cache.position = positions[params.positionId]; - PoolsharkStructs.BurnLimitParams memory burnParams = PoolsharkStructs.BurnLimitParams ({ - to: params.owner, - burnPercent: params.burnPercent, - positionId: params.positionId, - claim: params.claim, - zeroForOne: params.zeroForOne - }); + PoolsharkStructs.BurnLimitParams memory burnParams = PoolsharkStructs + .BurnLimitParams({ + to: params.owner, + burnPercent: params.burnPercent, + positionId: params.positionId, + claim: params.claim, + zeroForOne: params.zeroForOne + }); if (cache.position.epochLast == 0) require(false, 'PositionNotFound()'); - return LimitPositions.snapshot( - ticks, - tickMap, - cache, - burnParams - ); + return LimitPositions.snapshot(ticks, tickMap, cache, burnParams); } } diff --git a/contracts/libraries/math/ConstantProduct.sol b/contracts/libraries/math/ConstantProduct.sol index c40362c3..dfd6486c 100644 --- a/contracts/libraries/math/ConstantProduct.sol +++ b/contracts/libraries/math/ConstantProduct.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: GPL-3.0-or-later -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import './OverflowMath.sol'; import '../../interfaces/structs/LimitPoolStructs.sol'; @@ -27,9 +27,17 @@ library ConstantProduct { unchecked { if (liquidity == 0) return 0; if (roundUp) { - dy = OverflowMath.mulDivRoundingUp(liquidity, priceUpper - priceLower, Q96); + dy = OverflowMath.mulDivRoundingUp( + liquidity, + priceUpper - priceLower, + Q96 + ); } else { - dy = OverflowMath.mulDiv(liquidity, priceUpper - priceLower, Q96); + dy = OverflowMath.mulDiv( + liquidity, + priceUpper - priceLower, + Q96 + ); } } } @@ -44,19 +52,21 @@ library ConstantProduct { if (liquidity == 0) return 0; if (roundUp) { dx = OverflowMath.divRoundingUp( - OverflowMath.mulDivRoundingUp( - liquidity << 96, - priceUpper - priceLower, - priceUpper - ), - priceLower + OverflowMath.mulDivRoundingUp( + liquidity << 96, + priceUpper - priceLower, + priceUpper + ), + priceLower ); } else { - dx = OverflowMath.mulDiv( + dx = + OverflowMath.mulDiv( liquidity << 96, priceUpper - priceLower, priceUpper - ) / priceLower; + ) / + priceLower; } } } @@ -70,7 +80,11 @@ library ConstantProduct { ) internal pure returns (uint256 liquidity) { unchecked { if (priceUpper <= currentPrice) { - liquidity = OverflowMath.mulDiv(dy, Q96, priceUpper - priceLower); + liquidity = OverflowMath.mulDiv( + dy, + Q96, + priceUpper - priceLower + ); } else if (currentPrice <= priceLower) { liquidity = OverflowMath.mulDiv( dx, @@ -83,7 +97,11 @@ library ConstantProduct { OverflowMath.mulDiv(priceUpper, currentPrice, Q96), priceUpper - currentPrice ); - uint256 liquidity1 = OverflowMath.mulDiv(dy, Q96, currentPrice - priceLower); + uint256 liquidity1 = OverflowMath.mulDiv( + dy, + Q96, + currentPrice - priceLower + ); liquidity = liquidity0 < liquidity1 ? liquidity0 : liquidity1; } } @@ -97,15 +115,25 @@ library ConstantProduct { bool roundUp ) internal pure returns (uint128 token0amount, uint128 token1amount) { if (priceUpper <= currentPrice) { - token1amount = uint128(getDy(liquidityAmount, priceLower, priceUpper, roundUp)); + token1amount = uint128( + getDy(liquidityAmount, priceLower, priceUpper, roundUp) + ); } else if (currentPrice <= priceLower) { - token0amount = uint128(getDx(liquidityAmount, priceLower, priceUpper, roundUp)); + token0amount = uint128( + getDx(liquidityAmount, priceLower, priceUpper, roundUp) + ); } else { - token0amount = uint128(getDx(liquidityAmount, currentPrice, priceUpper, roundUp)); - token1amount = uint128(getDy(liquidityAmount, priceLower, currentPrice, roundUp)); + token0amount = uint128( + getDx(liquidityAmount, currentPrice, priceUpper, roundUp) + ); + token1amount = uint128( + getDy(liquidityAmount, priceLower, currentPrice, roundUp) + ); } - if (token0amount > uint128(type(int128).max)) require(false, 'AmountsOutOfBounds()'); - if (token1amount > uint128(type(int128).max)) require(false, 'AmountsOutOfBounds()'); + if (token0amount > uint128(type(int128).max)) + require(false, 'AmountsOutOfBounds()'); + if (token1amount > uint128(type(int128).max)) + require(false, 'AmountsOutOfBounds()'); } function getNewPrice( @@ -114,92 +142,71 @@ library ConstantProduct { uint256 amount, bool zeroForOne, bool exactIn - ) internal pure returns ( - uint256 newPrice - ) { + ) internal pure returns (uint256 newPrice) { if (exactIn) { if (zeroForOne) { uint256 liquidityPadded = liquidity << 96; newPrice = OverflowMath.mulDivRoundingUp( - liquidityPadded, - price, - liquidityPadded + price * amount - ); + liquidityPadded, + price, + liquidityPadded + price * amount + ); } else { newPrice = price + (amount << 96) / liquidity; } } else { if (zeroForOne) { - newPrice = price - - OverflowMath.divRoundingUp(amount << 96, liquidity); + newPrice = + price - + OverflowMath.divRoundingUp(amount << 96, liquidity); } else { uint256 liquidityPadded = uint256(liquidity) << 96; newPrice = OverflowMath.mulDivRoundingUp( - liquidityPadded, - price, - liquidityPadded - uint256(price) * amount + liquidityPadded, + price, + liquidityPadded - uint256(price) * amount ); } } } - function getPrice( - uint256 sqrtPrice - ) internal pure returns (uint256 price) { - if (sqrtPrice >= 2 ** 48) - price = OverflowMath.mulDiv(sqrtPrice, sqrtPrice, 2 ** 96); - else - price = sqrtPrice; + function getPrice(uint256 sqrtPrice) internal pure returns (uint256 price) { + if (sqrtPrice >= 2**48) + price = OverflowMath.mulDiv(sqrtPrice, sqrtPrice, 2**96); + else price = sqrtPrice; } ///////////////////////////////////////////////////////////// ///////////////////////// TICK MATH ///////////////////////// ///////////////////////////////////////////////////////////// - int24 internal constant MIN_TICK = -887272; /// @dev - tick for price of 2^-128 + int24 internal constant MIN_TICK = -887272; /// @dev - tick for price of 2^-128 int24 internal constant MAX_TICK = -MIN_TICK; /// @dev - tick for price of 2^128 - function minTick( - int16 tickSpacing - ) internal pure returns ( - int24 tick - ) { - return MIN_TICK / tickSpacing * tickSpacing; + function minTick(int16 tickSpacing) internal pure returns (int24 tick) { + return (MIN_TICK / tickSpacing) * tickSpacing; } - function maxTick( - int16 tickSpacing - ) internal pure returns ( - int24 tick - ) { - return MAX_TICK / tickSpacing * tickSpacing; + function maxTick(int16 tickSpacing) internal pure returns (int24 tick) { + return (MAX_TICK / tickSpacing) * tickSpacing; } - function priceBounds( - int16 tickSpacing - ) internal pure returns ( - uint160, - uint160 - ) { + function priceBounds(int16 tickSpacing) + internal + pure + returns (uint160, uint160) + { return (minPrice(tickSpacing), maxPrice(tickSpacing)); } - function minPrice( - int16 tickSpacing - ) internal pure returns ( - uint160 price - ) { - PoolsharkStructs.LimitImmutables memory constants; + function minPrice(int16 tickSpacing) internal pure returns (uint160 price) { + PoolsharkStructs.LimitImmutables memory constants; constants.tickSpacing = tickSpacing; return getPriceAtTick(minTick(tickSpacing), constants); } - function maxPrice( - int16 tickSpacing - ) internal pure returns ( - uint160 price - ) { - PoolsharkStructs.LimitImmutables memory constants; + function maxPrice(int16 tickSpacing) internal pure returns (uint160 price) { + PoolsharkStructs.LimitImmutables memory constants; constants.tickSpacing = tickSpacing; return getPriceAtTick(maxTick(tickSpacing), constants); } @@ -208,20 +215,24 @@ library ConstantProduct { int24 lower, int24 upper, int16 tickSpacing - ) internal pure - { - if (lower < minTick(tickSpacing)) require (false, 'LowerTickOutOfBounds()'); - if (upper > maxTick(tickSpacing)) require (false, 'UpperTickOutOfBounds()'); - if (lower % tickSpacing != 0) require (false, 'LowerTickOutsideTickSpacing()'); - if (upper % tickSpacing != 0) require (false, 'UpperTickOutsideTickSpacing()'); - if (lower >= upper) require (false, 'LowerUpperTickOrderInvalid()'); + ) internal pure { + if (lower < minTick(tickSpacing)) + require(false, 'LowerTickOutOfBounds()'); + if (upper > maxTick(tickSpacing)) + require(false, 'UpperTickOutOfBounds()'); + if (lower % tickSpacing != 0) + require(false, 'LowerTickOutsideTickSpacing()'); + if (upper % tickSpacing != 0) + require(false, 'UpperTickOutsideTickSpacing()'); + if (lower >= upper) require(false, 'LowerUpperTickOrderInvalid()'); } - function checkPrice( - uint160 price, - PriceBounds memory bounds - ) internal pure { - if (price < bounds.min || price >= bounds.max) require (false, 'PriceOutOfBounds()'); + function checkPrice(uint160 price, PriceBounds memory bounds) + internal + pure + { + if (price < bounds.min || price >= bounds.max) + require(false, 'PriceOutOfBounds()'); } /// @notice Calculates sqrt(1.0001^tick) * 2^96. @@ -232,34 +243,54 @@ library ConstantProduct { function getPriceAtTick( int24 tick, PoolsharkStructs.LimitImmutables memory constants - ) internal pure returns ( - uint160 price - ) { - uint256 absTick = tick < 0 ? uint256(-int256(tick)) : uint256(int256(tick)); - if (absTick > uint256(uint24(maxTick(constants.tickSpacing)))) require (false, 'TickOutOfBounds()'); + ) internal pure returns (uint160 price) { + uint256 absTick = tick < 0 + ? uint256(-int256(tick)) + : uint256(int256(tick)); + if (absTick > uint256(uint24(maxTick(constants.tickSpacing)))) + require(false, 'TickOutOfBounds()'); unchecked { 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 (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. @@ -274,11 +305,11 @@ library ConstantProduct { /// @return tick The greatest tick for which the ratio is less than or equal to the input ratio. function getTickAtPrice( uint160 price, - PoolsharkStructs.LimitImmutables memory constants + PoolsharkStructs.LimitImmutables memory constants ) internal pure returns (int24 tick) { // Second inequality must be < because the price can never reach the price at the max tick. if (price < constants.bounds.min || price > constants.bounds.max) - require (false, 'PriceOutOfBounds()'); + require(false, 'PriceOutOfBounds()'); uint256 ratio = uint256(price) << 32; uint256 r = ratio; @@ -415,11 +446,17 @@ library ConstantProduct { int256 log_sqrt10001 = log_2 * 255738958999603826347141; // 128.128 number - int24 tickLow = int24((log_sqrt10001 - 3402992956809132418596140100660247210) >> 128); - int24 tickHi = int24((log_sqrt10001 + 291339464771989622907027621153398088495) >> 128); + int24 tickLow = int24( + (log_sqrt10001 - 3402992956809132418596140100660247210) >> 128 + ); + int24 tickHi = int24( + (log_sqrt10001 + 291339464771989622907027621153398088495) >> 128 + ); - tick = tickLow == tickHi ? tickLow : getPriceAtTick(tickHi, constants) <= price + tick = tickLow == tickHi + ? tickLow + : getPriceAtTick(tickHi, constants) <= price ? tickHi : tickLow; } -} \ No newline at end of file +} diff --git a/contracts/libraries/math/OverflowMath.sol b/contracts/libraries/math/OverflowMath.sol index 1e66376f..3e3784c4 100644 --- a/contracts/libraries/math/OverflowMath.sol +++ b/contracts/libraries/math/OverflowMath.sol @@ -1,11 +1,14 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; /// @notice Math library that facilitates multiplication and division that can have overflow of an intermediate value without any loss of precision. library OverflowMath { - // @dev no underflow or overflow checks - function divRoundingUp(uint256 x, uint256 y) internal pure returns (uint256 z) { + function divRoundingUp(uint256 x, uint256 y) + internal + pure + returns (uint256 z) + { assembly { z := add(div(x, y), gt(mod(x, y), 0)) } @@ -118,7 +121,8 @@ library OverflowMath { result = mulDiv(a, b, denominator); unchecked { if (mulmod(a, b, denominator) != 0) { - if (result >= type(uint256).max) require (false, 'MaxUintExceeded()'); + if (result >= type(uint256).max) + require(false, 'MaxUintExceeded()'); result++; } } diff --git a/contracts/libraries/pool/FeesCall.sol b/contracts/libraries/pool/FeesCall.sol index 74158876..1e0039c9 100644 --- a/contracts/libraries/pool/FeesCall.sol +++ b/contracts/libraries/pool/FeesCall.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity 0.8.18; import '../../interfaces/IPositionERC1155.sol'; import '../../interfaces/structs/PoolsharkStructs.sol'; @@ -7,10 +7,9 @@ import '../../interfaces/limit/ILimitPoolManager.sol'; import '../utils/SafeTransfers.sol'; library FeesCall { - // protocol fee ceilings - uint16 public constant MAX_PROTOCOL_SWAP_FEE = 1e4; // max protocol swap fee of 100% - uint16 public constant MAX_PROTOCOL_FILL_FEE = 1e2; // max protocol fill fee of 1% + uint16 public constant MAX_PROTOCOL_SWAP_FEE = 1e4; // max protocol swap fee of 100% + uint16 public constant MAX_PROTOCOL_FILL_FEE = 1e2; // max protocol fill fee of 1% // protocol fee flags uint8 internal constant PROTOCOL_SWAP_FEE_0 = 2**0; @@ -21,16 +20,11 @@ library FeesCall { // eth address for safe withdrawal address public constant ethAddress = address(0); - /// @dev - LimitPoolManager (i.e. constants.owner) emits events in aggregate - function perform( PoolsharkStructs.GlobalState storage globalState, PoolsharkStructs.FeesParams memory params, PoolsharkStructs.LimitImmutables memory constants - ) external returns ( - uint128 token0Fees, - uint128 token1Fees - ) { + ) external returns (uint128 token0Fees, uint128 token1Fees) { // swap fee token0 if ((params.setFeesFlags & PROTOCOL_SWAP_FEE_0) > 0) { if (params.protocolSwapFee0 > MAX_PROTOCOL_SWAP_FEE) diff --git a/contracts/libraries/pool/QuoteCall.sol b/contracts/libraries/pool/QuoteCall.sol index 9f29b46c..b3137fb4 100644 --- a/contracts/libraries/pool/QuoteCall.sol +++ b/contracts/libraries/pool/QuoteCall.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity 0.8.18; import '../../interfaces/structs/LimitPoolStructs.sol'; import '../Ticks.sol'; @@ -7,15 +7,7 @@ import '../Ticks.sol'; library QuoteCall { uint8 private constant _ENTERED = 2; - event Swap( - address indexed recipient, - bool zeroForOne, - uint256 amountIn, - uint256 amountOut, - uint160 price, - uint128 liquidity, - int24 tickAtPrice - ); + event Event(); function perform( mapping(int24 => LimitPoolStructs.Tick) storage ticks, @@ -24,20 +16,18 @@ library QuoteCall { PoolsharkStructs.GlobalState storage globalState, PoolsharkStructs.QuoteParams memory params, PoolsharkStructs.SwapCache memory cache - ) external view returns ( - uint256, - uint256, - uint160 - ) { + ) + external + view + returns ( + uint256, + uint256, + uint160 + ) + { if (cache.state.unlocked == _ENTERED) require(false, 'ReentrancyGuardReadOnlyReentrantCall()'); cache.state = globalState; - return Ticks.quote( - ticks, - rangeTickMap, - limitTickMap, - params, - cache - ); + return Ticks.quote(ticks, rangeTickMap, limitTickMap, params, cache); } } diff --git a/contracts/libraries/pool/SampleCall.sol b/contracts/libraries/pool/SampleCall.sol index 9ade269e..f09144f5 100644 --- a/contracts/libraries/pool/SampleCall.sol +++ b/contracts/libraries/pool/SampleCall.sol @@ -1,45 +1,43 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity 0.8.18; import '../../interfaces/structs/RangePoolStructs.sol'; import '../Samples.sol'; library SampleCall { uint8 private constant _ENTERED = 2; - - event SampleRecorded( - int56 tickSecondsAccum, - uint160 secondsPerLiquidityAccum - ); - event SampleLengthIncreased( - uint16 sampleLengthNext - ); + event Event(); function perform( PoolsharkStructs.GlobalState memory state, PoolsharkStructs.LimitImmutables memory constants, uint32[] memory secondsAgo - ) external view returns ( - int56[] memory tickSecondsAccum, - uint160[] memory secondsPerLiquidityAccum, - uint160 averagePrice, - uint128 averageLiquidity, - int24 averageTick - ) { + ) + external + view + returns ( + int56[] memory tickSecondsAccum, + uint160[] memory secondsPerLiquidityAccum, + uint160 averagePrice, + uint128 averageLiquidity, + int24 averageTick + ) + { if (state.unlocked == _ENTERED) require(false, 'ReentrancyGuardReadOnlyReentrantCall()'); - return Samples.get( - address(this), - RangePoolStructs.SampleParams( - state.pool.samples.index, - state.pool.samples.count, - uint32(block.timestamp), - secondsAgo, - state.pool.tickAtPrice, - state.pool.liquidity, - constants - ) - ); + return + Samples.get( + address(this), + RangePoolStructs.SampleParams( + state.pool.samples.index, + state.pool.samples.count, + uint32(block.timestamp), + secondsAgo, + state.pool.tickAtPrice, + state.pool.liquidity, + constants + ) + ); } } diff --git a/contracts/libraries/pool/SwapCall.sol b/contracts/libraries/pool/SwapCall.sol index 88c5c4dd..1ce01381 100644 --- a/contracts/libraries/pool/SwapCall.sol +++ b/contracts/libraries/pool/SwapCall.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity 0.8.18; import '../../interfaces/structs/LimitPoolStructs.sol'; import '../../interfaces/callbacks/ILimitPoolCallback.sol'; @@ -9,16 +9,8 @@ import '../utils/Collect.sol'; import '../utils/SafeTransfers.sol'; library SwapCall { - event Swap( - address indexed recipient, - bool zeroForOne, - uint256 amountIn, - uint256 amountOut, - uint160 price, - uint128 liquidity, - uint128 feeAmount, - int24 tickAtPrice - ); + + event Event(); function perform( mapping(int24 => LimitPoolStructs.Tick) storage ticks, @@ -28,17 +20,13 @@ library SwapCall { PoolsharkStructs.GlobalState storage globalState, PoolsharkStructs.SwapParams memory params, PoolsharkStructs.SwapCache memory cache - ) external returns ( - int256, - int256 - ) { + ) external returns (int256, int256) { // check for invalid receiver - if (params.to == address(0)) - require(false, "CollectToZeroAddress()"); - + if (params.to == address(0)) require(false, 'CollectToZeroAddress()'); + // initialize state cache.state = globalState; - + // execute swap cache = Ticks.swap( ticks, @@ -54,9 +42,8 @@ library SwapCall { // transfer output amount SafeTransfers.transferOut( - params.to, - params.zeroForOne ? cache.constants.token1 - : cache.constants.token0, + params.to, + params.zeroForOne ? cache.constants.token1 : cache.constants.token0, cache.output ); @@ -70,19 +57,13 @@ library SwapCall { // check balance requirements after callback if (balance(params, cache) < balanceStart + cache.input) { - require(false, 'SwapInputAmountTooLow()'); + require(false, 'SwapInputAmountTooLow()'); } return ( - params.zeroForOne ? - ( - -int256(cache.input), - int256(cache.output) - ) - : ( - int256(cache.output), - -int256(cache.input) - ) + params.zeroForOne + ? (-int256(cache.input), int256(cache.output)) + : (int256(cache.output), -int256(cache.input)) ); } @@ -93,27 +74,22 @@ library SwapCall { ) internal { globalState.epoch = cache.state.epoch; globalState.pool = cache.state.pool; - if (zeroForOne) - globalState.pool1 = cache.state.pool1; - else - globalState.pool0 = cache.state.pool0; + if (zeroForOne) globalState.pool1 = cache.state.pool1; + else globalState.pool0 = cache.state.pool0; } function balance( PoolsharkStructs.SwapParams memory params, PoolsharkStructs.SwapCache memory cache ) private view returns (uint256) { - ( - bool success, - bytes memory data - ) = (params.zeroForOne ? cache.constants.token0 - : cache.constants.token1) - .staticcall( - abi.encodeWithSelector( - IERC20Minimal.balanceOf.selector, - address(this) - ) - ); + (bool success, bytes memory data) = ( + params.zeroForOne ? cache.constants.token0 : cache.constants.token1 + ).staticcall( + abi.encodeWithSelector( + IERC20Minimal.balanceOf.selector, + address(this) + ) + ); require(success && data.length >= 32); return abi.decode(data, (uint256)); } diff --git a/contracts/libraries/range/RangePositions.sol b/contracts/libraries/range/RangePositions.sol index 812f48e8..db67abec 100644 --- a/contracts/libraries/range/RangePositions.sol +++ b/contracts/libraries/range/RangePositions.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity 0.8.18; import '../../interfaces/IPool.sol'; import '../../interfaces/IPositionERC1155.sol'; @@ -21,6 +21,25 @@ library RangePositions { uint256 internal constant Q96 = 0x1000000000000000000000000; uint256 internal constant Q128 = 0x100000000000000000000000000000000; + event Mint( + address sender, + address indexed owner, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount, + uint256 amount0, + uint256 amount1 + ); + + event Burn( + address indexed owner, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount, + uint256 amount0, + uint256 amount1 + ); + event BurnRange( address indexed recipient, uint256 indexed positionId, @@ -29,18 +48,19 @@ library RangePositions { int128 amount1 ); - event CompoundRange( - uint32 indexed positionId, - uint128 liquidityCompounded - ); + event CompoundRange(uint32 indexed positionId, uint128 liquidityCompounded); function validate( RangePoolStructs.MintRangeParams memory params, RangePoolStructs.MintRangeCache memory cache - ) internal pure returns ( - RangePoolStructs.MintRangeParams memory, - RangePoolStructs.MintRangeCache memory - ) { + ) + internal + pure + returns ( + RangePoolStructs.MintRangeParams memory, + RangePoolStructs.MintRangeCache memory + ) + { cache.liquidityMinted = ConstantProduct.getLiquidityForAmounts( cache.priceLower, cache.priceUpper, @@ -48,16 +68,20 @@ library RangePositions { params.amount1, params.amount0 ); - if (cache.liquidityMinted == 0) require(false, 'NoLiquidityBeingAdded()'); - (params.amount0, params.amount1) = ConstantProduct.getAmountsForLiquidity( - cache.priceLower, - cache.priceUpper, - cache.state.pool.price, - cache.liquidityMinted, - true - ); - if (cache.state.liquidityGlobal + cache.liquidityMinted > uint128(type(int128).max)) - require(false, 'LiquidityOverflow()'); + if (cache.liquidityMinted == 0) + require(false, 'NoLiquidityBeingAdded()'); + (params.amount0, params.amount1) = ConstantProduct + .getAmountsForLiquidity( + cache.priceLower, + cache.priceUpper, + cache.state.pool.price, + cache.liquidityMinted, + true + ); + if ( + cache.state.liquidityGlobal + cache.liquidityMinted > + uint128(type(int128).max) + ) require(false, 'LiquidityOverflow()'); return (params, cache); } @@ -68,9 +92,7 @@ library RangePositions { PoolsharkStructs.TickMap storage tickMap, RangePoolStructs.MintRangeCache memory cache, RangePoolStructs.MintRangeParams memory params - ) internal returns ( - RangePoolStructs.MintRangeCache memory - ) { + ) internal returns (RangePoolStructs.MintRangeCache memory) { if (params.amount0 == 0 && params.amount1 == 0) return cache; cache.state = RangeTicks.insert( @@ -111,25 +133,35 @@ library RangePositions { PoolsharkStructs.TickMap storage tickMap, RangePoolStructs.BurnRangeParams memory params, RangePoolStructs.BurnRangeCache memory cache - ) internal returns ( - RangePoolStructs.BurnRangeCache memory - ) { - cache.priceLower = ConstantProduct.getPriceAtTick(cache.position.lower, cache.constants); - cache.priceUpper = ConstantProduct.getPriceAtTick(cache.position.upper, cache.constants); - cache.liquidityBurned = _convert(cache.position.liquidity, params.burnPercent); - if (cache.liquidityBurned == 0) { + ) internal returns (RangePoolStructs.BurnRangeCache memory) { + cache.priceLower = ConstantProduct.getPriceAtTick( + cache.position.lower, + cache.constants + ); + cache.priceUpper = ConstantProduct.getPriceAtTick( + cache.position.upper, + cache.constants + ); + cache.liquidityBurned = _convert( + cache.position.liquidity, + params.burnPercent + ); + if (cache.liquidityBurned == 0) { return cache; } - if (cache.liquidityBurned > cache.position.liquidity) require(false, 'NotEnoughPositionLiquidity()'); + if (cache.liquidityBurned > cache.position.liquidity) + require(false, 'NotEnoughPositionLiquidity()'); { - uint128 amount0Removed; uint128 amount1Removed; - (amount0Removed, amount1Removed) = ConstantProduct.getAmountsForLiquidity( - cache.priceLower, - cache.priceUpper, - cache.state.pool.price, - cache.liquidityBurned , - false - ); + uint128 amount0Removed; + uint128 amount1Removed; + (amount0Removed, amount1Removed) = ConstantProduct + .getAmountsForLiquidity( + cache.priceLower, + cache.priceUpper, + cache.state.pool.price, + cache.liquidityBurned, + false + ); cache.amount0 += amount0Removed.toInt128(); cache.amount1 += amount1Removed.toInt128(); cache.position.liquidity -= cache.liquidityBurned.toUint128(); @@ -144,6 +176,18 @@ library RangePositions { cache.position.upper, uint128(cache.liquidityBurned) ); + + // emit standard event + emit Burn( + msg.sender, + cache.position.lower, + cache.position.upper, + uint128(cache.liquidityBurned), + cache.amount0.toUint128(), + cache.amount1.toUint128() + ); + + // emit custom event emit BurnRange( params.to, params.positionId, @@ -151,10 +195,13 @@ library RangePositions { cache.amount0, cache.amount1 ); + + // clear position bounds if (cache.position.liquidity == 0) { cache.position.lower = 0; cache.position.upper = 0; } + return cache; } @@ -166,12 +213,15 @@ library RangePositions { PoolsharkStructs.LimitImmutables memory constants, RangePoolStructs.RangePosition memory position, RangePoolStructs.CompoundRangeParams memory params - ) internal returns ( - RangePoolStructs.RangePosition memory, - PoolsharkStructs.GlobalState memory, - int128, - int128 - ) { + ) + internal + returns ( + RangePoolStructs.RangePosition memory, + PoolsharkStructs.GlobalState memory, + int128, + int128 + ) + { // price tells you the ratio so you need to swap into the correct ratio and add liquidity uint256 liquidityAmount = ConstantProduct.getLiquidityForAmounts( params.priceLower, @@ -191,7 +241,8 @@ library RangePositions { position.upper, uint128(liquidityAmount) ); - uint256 amount0; uint256 amount1; + uint256 amount0; + uint256 amount1; (amount0, amount1) = ConstantProduct.getAmountsForLiquidity( params.priceLower, params.priceUpper, @@ -199,15 +250,30 @@ library RangePositions { liquidityAmount, true ); - params.amount0 -= (amount0 <= params.amount0) ? uint128(amount0) : params.amount0; - params.amount1 -= (amount1 <= params.amount1) ? uint128(amount1) : params.amount1; + params.amount0 -= (amount0 <= params.amount0) + ? uint128(amount0) + : params.amount0; + params.amount1 -= (amount1 <= params.amount1) + ? uint128(amount1) + : params.amount1; position.liquidity += uint128(liquidityAmount); } - emit CompoundRange( - params.positionId, - uint128(liquidityAmount) + emit Mint( + msg.sender, + msg.sender, + position.lower, + position.upper, + liquidityAmount.toUint128(), + 0, + 0 + ); + emit CompoundRange(params.positionId, uint128(liquidityAmount)); + return ( + position, + state, + params.amount0.toInt128(), + params.amount1.toInt128() ); - return (position, state, params.amount0.toInt128(), params.amount1.toInt128()); } function update( @@ -216,17 +282,28 @@ library RangePositions { PoolsharkStructs.GlobalState memory state, PoolsharkStructs.LimitImmutables memory constants, RangePoolStructs.UpdateParams memory params - ) internal returns ( - RangePoolStructs.RangePosition memory, - int128, - int128 - ) { + ) + internal + returns ( + RangePoolStructs.RangePosition memory, + int128, + int128 + ) + { RangePoolStructs.RangePositionCache memory cache; /// @dev - only true if burn call if (params.burnPercent > 0) { - cache.liquidityAmount = _convert(position.liquidity, params.burnPercent); + cache.liquidityAmount = _convert( + position.liquidity, + params.burnPercent + ); if (position.liquidity == cache.liquidityAmount) - IPositionERC1155(constants.poolToken).burn(msg.sender, params.positionId, 1, constants); + IPositionERC1155(constants.poolToken).burn( + msg.sender, + params.positionId, + 1, + constants + ); } (uint256 rangeFeeGrowth0, uint256 rangeFeeGrowth1) = rangeFeeGrowth( @@ -237,17 +314,23 @@ library RangePositions { position.upper ); - int128 amount0Fees = OverflowMath.mulDiv( - rangeFeeGrowth0 - position.feeGrowthInside0Last, - uint256(position.liquidity), - Q128 - ).toInt256().toInt128(); + int128 amount0Fees = OverflowMath + .mulDiv( + rangeFeeGrowth0 - position.feeGrowthInside0Last, + uint256(position.liquidity), + Q128 + ) + .toInt256() + .toInt128(); - int128 amount1Fees = OverflowMath.mulDiv( - rangeFeeGrowth1 - position.feeGrowthInside1Last, - position.liquidity, - Q128 - ).toInt256().toInt128(); + int128 amount1Fees = OverflowMath + .mulDiv( + rangeFeeGrowth1 - position.feeGrowthInside1Last, + position.liquidity, + Q128 + ) + .toInt256() + .toInt128(); position.feeGrowthInside0Last = rangeFeeGrowth0; position.feeGrowthInside1Last = rangeFeeGrowth1; @@ -261,8 +344,11 @@ library RangePositions { PoolsharkStructs.GlobalState memory state, int24 lower, int24 upper - ) internal pure returns (uint256 feeGrowthInside0, uint256 feeGrowthInside1) { - + ) + internal + pure + returns (uint256 feeGrowthInside0, uint256 feeGrowthInside1) + { uint256 feeGrowthGlobal0 = state.pool.feeGrowthGlobal0; uint256 feeGrowthGlobal1 = state.pool.feeGrowthGlobal1; @@ -290,41 +376,49 @@ library RangePositions { } function snapshot( - mapping(uint256 => RangePoolStructs.RangePosition) - storage positions, + mapping(uint256 => RangePoolStructs.RangePosition) storage positions, mapping(int24 => PoolsharkStructs.Tick) storage ticks, PoolsharkStructs.GlobalState memory state, PoolsharkStructs.LimitImmutables memory constants, uint32 positionId - ) internal view returns ( - int56 tickSecondsAccum, - uint160 secondsPerLiquidityAccum, - uint128 feesOwed0, - uint128 feesOwed1 - ) { + ) + internal + view + returns ( + int56 tickSecondsAccum, + uint160 secondsPerLiquidityAccum, + uint128 feesOwed0, + uint128 feesOwed1 + ) + { RangePoolStructs.SnapshotRangeCache memory cache; cache.position = positions[positionId]; // early return if position empty - if (cache.position.liquidity == 0) - return (0,0,0,0); + if (cache.position.liquidity == 0) return (0, 0, 0, 0); cache.price = state.pool.price; cache.liquidity = state.pool.liquidity; cache.samples = state.pool.samples; // grab lower tick - PoolsharkStructs.RangeTick memory tickLower = ticks[cache.position.lower].range; - + PoolsharkStructs.RangeTick memory tickLower = ticks[ + cache.position.lower + ].range; + // grab upper tick - PoolsharkStructs.RangeTick memory tickUpper = ticks[cache.position.upper].range; + PoolsharkStructs.RangeTick memory tickUpper = ticks[ + cache.position.upper + ].range; - cache.tickSecondsAccumLower = tickLower.tickSecondsAccumOutside; - cache.secondsPerLiquidityAccumLower = tickLower.secondsPerLiquidityAccumOutside; + cache.tickSecondsAccumLower = tickLower.tickSecondsAccumOutside; + cache.secondsPerLiquidityAccumLower = tickLower + .secondsPerLiquidityAccumOutside; // if both have never been crossed into return 0 cache.tickSecondsAccumUpper = tickUpper.tickSecondsAccumOutside; - cache.secondsPerLiquidityAccumUpper = tickUpper.secondsPerLiquidityAccumOutside; + cache.secondsPerLiquidityAccumUpper = tickUpper + .secondsPerLiquidityAccumOutside; cache.constants = constants; (uint256 rangeFeeGrowth0, uint256 rangeFeeGrowth1) = rangeFeeGrowth( @@ -357,36 +451,35 @@ library RangePositions { // lower accum values are greater return ( cache.tickSecondsAccumLower - cache.tickSecondsAccumUpper, - cache.secondsPerLiquidityAccumLower - cache.secondsPerLiquidityAccumUpper, + cache.secondsPerLiquidityAccumLower - + cache.secondsPerLiquidityAccumUpper, cache.amount0, cache.amount1 ); } else if (cache.position.upper >= cache.tick) { // grab current sample cache.blockTimestamp = uint32(block.timestamp); - ( - cache.tickSecondsAccum, - cache.secondsPerLiquidityAccum - ) = Samples.getSingle( - IRangePool(address(this)), - RangePoolStructs.SampleParams( - cache.samples.index, - cache.samples.count, - uint32(block.timestamp), - new uint32[](2), - cache.tick, - cache.liquidity, - cache.constants - ), - 0 - ); + (cache.tickSecondsAccum, cache.secondsPerLiquidityAccum) = Samples + .getSingle( + IRangePool(address(this)), + RangePoolStructs.SampleParams( + cache.samples.index, + cache.samples.count, + uint32(block.timestamp), + new uint32[](2), + cache.tick, + cache.liquidity, + cache.constants + ), + 0 + ); return ( - cache.tickSecondsAccum - - cache.tickSecondsAccumLower - - cache.tickSecondsAccumUpper, - cache.secondsPerLiquidityAccum - - cache.secondsPerLiquidityAccumLower - - cache.secondsPerLiquidityAccumUpper, + cache.tickSecondsAccum - + cache.tickSecondsAccumLower - + cache.tickSecondsAccumUpper, + cache.secondsPerLiquidityAccum - + cache.secondsPerLiquidityAccumLower - + cache.secondsPerLiquidityAccumUpper, cache.amount0, cache.amount1 ); @@ -394,22 +487,22 @@ library RangePositions { // upper accum values are greater return ( cache.tickSecondsAccumUpper - cache.tickSecondsAccumLower, - cache.secondsPerLiquidityAccumUpper - cache.secondsPerLiquidityAccumLower, + cache.secondsPerLiquidityAccumUpper - + cache.secondsPerLiquidityAccumLower, cache.amount0, cache.amount1 ); } } - function _convert( - uint128 liquidity, - uint128 percent - ) internal pure returns ( - uint128 - ) { + function _convert(uint128 liquidity, uint128 percent) + internal + pure + returns (uint128) + { // convert percentage to liquidity amount if (percent > 1e38) percent = 1e38; - if (liquidity == 0 && percent > 0) require (false, 'PositionNotFound()'); - return uint128(uint256(liquidity) * uint256(percent) / 1e38); + if (liquidity == 0 && percent > 0) require(false, 'PositionNotFound()'); + return uint128((uint256(liquidity) * uint256(percent)) / 1e38); } } diff --git a/contracts/libraries/range/RangeTicks.sol b/contracts/libraries/range/RangeTicks.sol index fb22027d..d2b676b2 100644 --- a/contracts/libraries/range/RangeTicks.sol +++ b/contracts/libraries/range/RangeTicks.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity 0.8.18; import '../../interfaces/structs/PoolsharkStructs.sol'; import '../../interfaces/structs/RangePoolStructs.sol'; @@ -14,7 +14,6 @@ import '../Samples.sol'; /// @notice Tick management library for range pools library RangeTicks { - event SyncRangeTick( uint200 feeGrowthOutside0, uint200 feeGrowthOutside1, @@ -30,9 +29,11 @@ library RangeTicks { int16 tickSpacing ) internal pure { if (lower % tickSpacing != 0) require(false, 'InvalidLowerTick()'); - if (lower < ConstantProduct.minTick(tickSpacing)) require(false, 'InvalidLowerTick()'); + if (lower < ConstantProduct.minTick(tickSpacing)) + require(false, 'InvalidLowerTick()'); if (upper % tickSpacing != 0) require(false, 'InvalidUpperTick()'); - if (upper > ConstantProduct.maxTick(tickSpacing)) require(false, 'InvalidUpperTick()'); + if (upper > ConstantProduct.maxTick(tickSpacing)) + require(false, 'InvalidUpperTick()'); if (lower >= upper) require(false, 'InvalidPositionBounds()'); } @@ -46,11 +47,10 @@ library RangeTicks { int24 upper, uint128 amount ) internal returns (PoolsharkStructs.GlobalState memory) { - // get tick at price int24 tickAtPrice = state.pool.tickAtPrice; - if(TickMap.set(tickMap, lower, constants.tickSpacing)) { + if (TickMap.set(tickMap, lower, constants.tickSpacing)) { ticks[lower].range.liquidityDelta += int128(amount); ticks[lower].range.liquidityAbsolute += amount; } else { @@ -59,7 +59,7 @@ library RangeTicks { int56 tickSecondsAccum, uint160 secondsPerLiquidityAccum ) = Samples.getSingle( - IRangePool(address(this)), + IRangePool(address(this)), RangePoolStructs.SampleParams( state.pool.samples.index, state.pool.samples.count, @@ -70,14 +70,14 @@ library RangeTicks { constants ), 0 - ); + ); ticks[lower].range = PoolsharkStructs.RangeTick( state.pool.feeGrowthGlobal0, state.pool.feeGrowthGlobal1, secondsPerLiquidityAccum, tickSecondsAccum, - int128(amount), // liquidityDelta - amount // liquidityAbsolute + int128(amount), // liquidityDelta + amount // liquidityAbsolute ); emit SyncRangeTick( state.pool.feeGrowthGlobal0, @@ -89,17 +89,16 @@ library RangeTicks { ticks[lower].range.liquidityAbsolute += amount; } } - if(TickMap.set(tickMap, upper, constants.tickSpacing)) { + if (TickMap.set(tickMap, upper, constants.tickSpacing)) { ticks[upper].range.liquidityDelta -= int128(amount); ticks[upper].range.liquidityAbsolute += amount; } else { if (upper <= tickAtPrice) { - ( int56 tickSecondsAccum, uint160 secondsPerLiquidityAccum ) = Samples.getSingle( - IRangePool(address(this)), + IRangePool(address(this)), RangePoolStructs.SampleParams( state.pool.samples.index, state.pool.samples.count, @@ -110,7 +109,7 @@ library RangeTicks { constants ), 0 - ); + ); ticks[upper].range = PoolsharkStructs.RangeTick( state.pool.feeGrowthGlobal0, state.pool.feeGrowthGlobal1, @@ -151,7 +150,7 @@ library RangeTicks { RangePoolStructs.Sample[65535] storage samples, PoolsharkStructs.TickMap storage tickMap, PoolsharkStructs.GlobalState memory state, - PoolsharkStructs.LimitImmutables memory constants, + PoolsharkStructs.LimitImmutables memory constants, int24 lower, int24 upper, uint128 amount @@ -159,8 +158,10 @@ library RangeTicks { validate(lower, upper, constants.tickSpacing); //check for amount to overflow liquidity delta & global if (amount == 0) return state; - if (amount > uint128(type(int128).max)) require(false, 'LiquidityUnderflow()'); - if (amount > state.liquidityGlobal) require(false, 'LiquidityUnderflow()'); + if (amount > uint128(type(int128).max)) + require(false, 'LiquidityUnderflow()'); + if (amount > state.liquidityGlobal) + require(false, 'LiquidityUnderflow()'); // get pool tick at price int24 tickAtPrice = state.pool.tickAtPrice; @@ -193,7 +194,7 @@ library RangeTicks { state.pool.liquidity, tickAtPrice ); - state.pool.liquidity -= amount; + state.pool.liquidity -= amount; } state.liquidityGlobal -= amount; @@ -207,19 +208,28 @@ library RangeTicks { int24 tickToClear ) internal { if (_empty(ticks[tickToClear])) { - if (tickToClear != ConstantProduct.maxTick(constants.tickSpacing) && - tickToClear != ConstantProduct.minTick(constants.tickSpacing)) { - ticks[tickToClear].range = PoolsharkStructs.RangeTick(0,0,0,0,0,0); + if ( + tickToClear != ConstantProduct.maxTick(constants.tickSpacing) && + tickToClear != ConstantProduct.minTick(constants.tickSpacing) + ) { + ticks[tickToClear].range = PoolsharkStructs.RangeTick( + 0, + 0, + 0, + 0, + 0, + 0 + ); TickMap.unset(tickMap, tickToClear, constants.tickSpacing); } } } - function _empty( - LimitPoolStructs.Tick memory tick - ) internal pure returns ( - bool - ) { + function _empty(LimitPoolStructs.Tick memory tick) + internal + pure + returns (bool) + { return tick.range.liquidityAbsolute == 0; } } diff --git a/contracts/libraries/range/math/FeeMath.sol b/contracts/libraries/range/math/FeeMath.sol index fc537489..138ec96c 100644 --- a/contracts/libraries/range/math/FeeMath.sol +++ b/contracts/libraries/range/math/FeeMath.sol @@ -1,19 +1,17 @@ -// SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity 0.8.18; import '../../Samples.sol'; import '../../utils/SafeCast.sol'; -import "../../math/OverflowMath.sol"; +import '../../math/OverflowMath.sol'; +import '../../../interfaces/limit/ILimitPoolManager.sol'; import '../../../interfaces/structs/PoolsharkStructs.sol'; -import "../../../interfaces/structs/RangePoolStructs.sol"; +import '../../../interfaces/structs/RangePoolStructs.sol'; /// @notice Math library that facilitates fee handling. library FeeMath { using SafeCast for uint256; - uint256 internal constant FEE_DELTA_CONST = 0; - //TODO: change FEE_DELTA_CONST before launch - // uint256 internal constant FEE_DELTA_CONST = 5000; uint256 internal constant Q128 = 0x100000000000000000000000000000000; struct CalculateLocals { @@ -33,15 +31,14 @@ library FeeMath { uint256 amountIn, uint256 amountOut, bool zeroForOne - ) internal pure returns ( - PoolsharkStructs.SwapCache memory - ) - { + ) internal view returns (PoolsharkStructs.SwapCache memory) { CalculateLocals memory locals; if (cache.state.pool.liquidity != 0) { // calculate dynamic fee { - locals.minPrice = ConstantProduct.getPrice(cache.constants.bounds.min); + locals.minPrice = ConstantProduct.getPrice( + cache.constants.bounds.min + ); // square prices to take delta locals.price = ConstantProduct.getPrice(cache.price); locals.lastPrice = ConstantProduct.getPrice(cache.averagePrice); @@ -51,13 +48,14 @@ library FeeMath { locals.lastPrice = locals.minPrice; // delta is % modifier on the swapFee uint256 delta = OverflowMath.mulDiv( - FEE_DELTA_CONST / uint16(cache.constants.tickSpacing), // higher FEE_DELTA_CONST means - ( // more aggressive dynamic fee - locals.price > locals.lastPrice - ? locals.price - locals.lastPrice - : locals.lastPrice - locals.price - ) * 1_000_000, - locals.lastPrice + ILimitPoolManager(cache.constants.owner).feeDeltaConsts(address(this)) / // higher feeDeltaConst means + uint16(cache.constants.tickSpacing), // more aggressive dynamic fee + ( + locals.price > locals.lastPrice + ? locals.price - locals.lastPrice + : locals.lastPrice - locals.price + ) * 1_000_000, + locals.lastPrice ); // max fee increase at 5x if (delta > 4_000_000) delta = 4_000_000; @@ -66,55 +64,100 @@ library FeeMath { // adjust fee based on direction if (zeroForOne == locals.feeDirection) { // if swapping away from twap price, increase fee - locals.swapFee = cache.constants.swapFee + OverflowMath.mulDiv(delta,cache.constants.swapFee, 1e6); + locals.swapFee = + cache.constants.swapFee + + OverflowMath.mulDiv( + delta, + cache.constants.swapFee, + 1e6 + ); } else if (delta < 1e6) { // if swapping towards twap price, decrease fee - locals.swapFee = cache.constants.swapFee - OverflowMath.mulDiv(delta,cache.constants.swapFee, 1e6); + locals.swapFee = + cache.constants.swapFee - + OverflowMath.mulDiv( + delta, + cache.constants.swapFee, + 1e6 + ); } else { // if swapping towards twap price and delta > 100%, set fee to zero locals.swapFee = 0; } - // console.log('price movement', locals.lastPrice, locals.price); - // console.log('swap fee adjustment',cache.constants.swapFee + delta * cache.constants.swapFee / 1e6); } if (cache.exactIn) { // calculate output from range liquidity - locals.amountRange = OverflowMath.mulDiv(amountOut, cache.state.pool.liquidity, cache.liquidity); + locals.amountRange = OverflowMath.mulDiv( + amountOut, + cache.state.pool.liquidity, + cache.liquidity + ); // take enough fees to cover fee growth - locals.feeAmount = OverflowMath.mulDivRoundingUp(locals.amountRange, locals.swapFee, 1e6); + locals.feeAmount = OverflowMath.mulDivRoundingUp( + locals.amountRange, + locals.swapFee, + 1e6 + ); amountOut -= locals.feeAmount; } else { // calculate input from range liquidity - locals.amountRange = OverflowMath.mulDiv(amountIn, cache.state.pool.liquidity, cache.liquidity); + locals.amountRange = OverflowMath.mulDiv( + amountIn, + cache.state.pool.liquidity, + cache.liquidity + ); // take enough fees to cover fee growth - locals.feeAmount = OverflowMath.mulDivRoundingUp(locals.amountRange, locals.swapFee, 1e6); + locals.feeAmount = OverflowMath.mulDivRoundingUp( + locals.amountRange, + locals.swapFee, + 1e6 + ); amountIn += locals.feeAmount; } // add to total fees paid for swap cache.feeAmount += locals.feeAmount.toUint128(); // load protocol fee from cache - // zeroForOne && exactIn = fee on token1 // zeroForOne && !exactIn = fee on token0 - // !zeroForOne && !exactIn = fee on token1 - // !zeroForOne && exactIn = fee on token0 - locals.protocolFee = (zeroForOne == cache.exactIn) ? cache.state.pool.protocolSwapFee1 - : cache.state.pool.protocolSwapFee0; + // zeroForOne && exactIn = fee on token1 + locals.protocolFee = (zeroForOne == cache.exactIn) + ? cache.state.pool.protocolSwapFee1 + : cache.state.pool.protocolSwapFee0; // calculate fee - locals.protocolFeesAccrued = OverflowMath.mulDiv(locals.feeAmount, locals.protocolFee, 1e4); + locals.protocolFeesAccrued = OverflowMath.mulDiv( + locals.feeAmount, + locals.protocolFee, + 1e4 + ); // fees for this swap step locals.feeAmount -= locals.protocolFeesAccrued; // save fee growth and protocol fees if (zeroForOne == cache.exactIn) { - cache.state.pool0.protocolFees += uint128(locals.protocolFeesAccrued); - cache.state.pool.feeGrowthGlobal1 += uint200(OverflowMath.mulDiv(locals.feeAmount, Q128, cache.state.pool.liquidity)); + cache.state.pool0.protocolFees += uint128( + locals.protocolFeesAccrued + ); + cache.state.pool.feeGrowthGlobal1 += uint200( + OverflowMath.mulDiv( + locals.feeAmount, + Q128, + cache.state.pool.liquidity + ) + ); } else { - cache.state.pool1.protocolFees += uint128(locals.protocolFeesAccrued); - cache.state.pool.feeGrowthGlobal0 += uint200(OverflowMath.mulDiv(locals.feeAmount, Q128, cache.state.pool.liquidity)); + cache.state.pool1.protocolFees += uint128( + locals.protocolFeesAccrued + ); + cache.state.pool.feeGrowthGlobal0 += uint200( + OverflowMath.mulDiv( + locals.feeAmount, + Q128, + cache.state.pool.liquidity + ) + ); } } - cache.input += amountIn; + cache.input += amountIn; cache.output += amountOut; return cache; } -} \ No newline at end of file +} diff --git a/contracts/libraries/range/pool/BurnRangeCall.sol b/contracts/libraries/range/pool/BurnRangeCall.sol index 499deb31..f2e4f5e4 100644 --- a/contracts/libraries/range/pool/BurnRangeCall.sol +++ b/contracts/libraries/range/pool/BurnRangeCall.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity 0.8.18; import '../../../interfaces/structs/RangePoolStructs.sol'; import '../../utils/Collect.sol'; @@ -9,6 +9,15 @@ import '../RangePositions.sol'; library BurnRangeCall { using SafeCast for int128; + event Burn( + address indexed owner, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount, + uint256 amount0, + uint256 amount1 + ); + event BurnRange( address indexed recipient, int24 lower, @@ -20,51 +29,49 @@ library BurnRangeCall { ); function perform( - mapping(uint256 => RangePoolStructs.RangePosition) - storage positions, + mapping(uint256 => RangePoolStructs.RangePosition) storage positions, mapping(int24 => PoolsharkStructs.Tick) storage ticks, PoolsharkStructs.TickMap storage tickMap, RangePoolStructs.Sample[65535] storage samples, PoolsharkStructs.GlobalState storage globalState, RangePoolStructs.BurnRangeCache memory cache, RangePoolStructs.BurnRangeParams memory params - ) external { + ) + external + returns ( + int256, // amount0Delta + int256 // amount1Delta + ) + { // check for invalid receiver - if (params.to == address(0)) - require(false, "CollectToZeroAddress()"); - + if (params.to == address(0)) require(false, 'CollectToZeroAddress()'); + // initialize cache cache.state = globalState; cache.position = positions[params.positionId]; - if (cache.position.liquidity == 0) - require(false, 'PositionNotFound()'); - if (PositionTokens.balanceOf(cache.constants, msg.sender, params.positionId) == 0) - require(false, 'PositionOwnerMismatch()'); - - ( - cache.position, - cache.amount0, - cache.amount1 - ) = RangePositions.update( - ticks, - cache.position, - cache.state, + if (cache.position.liquidity == 0) require(false, 'PositionNotFound()'); + if ( + PositionTokens.balanceOf( cache.constants, - RangePoolStructs.UpdateParams( - cache.position.lower, - cache.position.upper, - params.positionId, - params.burnPercent - ) - ); - cache = RangePositions.remove( + msg.sender, + params.positionId + ) == 0 + ) require(false, 'PositionOwnerMismatch()'); + + (cache.position, cache.amount0, cache.amount1) = RangePositions.update( ticks, - samples, - tickMap, - params, - cache + cache.position, + cache.state, + cache.constants, + RangePoolStructs.UpdateParams( + cache.position.lower, + cache.position.upper, + params.positionId, + params.burnPercent + ) ); + cache = RangePositions.remove(ticks, samples, tickMap, params, cache); // only compound if burnPercent is zero if (params.burnPercent == 0) if (cache.amount0 > 0 || cache.amount1 > 0) { @@ -94,17 +101,21 @@ library BurnRangeCall { // transfer amounts to user if (cache.amount0 > 0 || cache.amount1 > 0) - Collect.range( + CollectLib.range( + cache.position, cache.constants, + msg.sender, params.to, cache.amount0, cache.amount1 ); + + // return amount deltas + return (cache.amount0, cache.amount1); } function save( - mapping(uint256 => RangePoolStructs.RangePosition) - storage positions, + mapping(uint256 => RangePoolStructs.RangePosition) storage positions, PoolsharkStructs.GlobalState storage globalState, RangePoolStructs.BurnRangeCache memory cache, uint32 positionId diff --git a/contracts/libraries/range/pool/MintRangeCall.sol b/contracts/libraries/range/pool/MintRangeCall.sol index 4ff9af2f..50e33983 100644 --- a/contracts/libraries/range/pool/MintRangeCall.sol +++ b/contracts/libraries/range/pool/MintRangeCall.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity 0.8.18; import '../../../interfaces/structs/RangePoolStructs.sol'; import '../../../interfaces/callbacks/ILimitPoolCallback.sol'; @@ -14,6 +14,16 @@ library MintRangeCall { using SafeCast for int128; using SafeCast for uint128; + event Mint( + address sender, + address indexed owner, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount, + uint256 amount0, + uint256 amount1 + ); + event MintRange( address indexed recipient, int24 lower, @@ -30,21 +40,29 @@ library MintRangeCall { } function perform( - mapping(uint256 => RangePoolStructs.RangePosition) - storage positions, + mapping(uint256 => RangePoolStructs.RangePosition) storage positions, mapping(int24 => PoolsharkStructs.Tick) storage ticks, PoolsharkStructs.TickMap storage tickMap, RangePoolStructs.Sample[65535] storage samples, PoolsharkStructs.GlobalState storage globalState, RangePoolStructs.MintRangeCache memory cache, RangePoolStructs.MintRangeParams memory params - ) external { + ) + external + returns ( + int256, // amount0Delta + int256 // amount1Delta + ) + { // check for invalid receiver - if (params.to == address(0)) - require(false, "CollectToZeroAddress()"); + if (params.to == address(0)) require(false, 'CollectToZeroAddress()'); // validate position ticks - ConstantProduct.checkTicks(params.lower, params.upper, cache.constants.tickSpacing); + ConstantProduct.checkTicks( + params.lower, + params.upper, + cache.constants.tickSpacing + ); cache.state = globalState; @@ -54,8 +72,13 @@ library MintRangeCall { cache.position = positions[params.positionId]; if (cache.position.liquidity == 0) require(false, 'PositionNotFound()'); - if (PositionTokens.balanceOf(cache.constants, params.to, params.positionId) == 0) - require(false, 'PositionOwnerMismatch()'); + if ( + PositionTokens.balanceOf( + cache.constants, + params.to, + params.positionId + ) == 0 + ) require(false, 'PositionOwnerMismatch()'); // existing position cache.owner = params.to; // set bounds as defined by position @@ -67,16 +90,16 @@ library MintRangeCall { cache.feesAccrued0, cache.feesAccrued1 ) = RangePositions.update( - ticks, - cache.position, - cache.state, - cache.constants, - RangePoolStructs.UpdateParams( - params.lower, - params.upper, - params.positionId, - 0 - ) + ticks, + cache.position, + cache.state, + cache.constants, + RangePoolStructs.UpdateParams( + params.lower, + params.upper, + params.positionId, + 0 + ) ); } else { // create a new position @@ -89,8 +112,14 @@ library MintRangeCall { cache.owner = params.to; } // set cache based on bounds - cache.priceLower = ConstantProduct.getPriceAtTick(cache.position.lower, cache.constants); - cache.priceUpper = ConstantProduct.getPriceAtTick(cache.position.upper, cache.constants); + cache.priceLower = ConstantProduct.getPriceAtTick( + cache.position.lower, + cache.constants + ); + cache.priceUpper = ConstantProduct.getPriceAtTick( + cache.position.upper, + cache.constants + ); // validate input amounts (params, cache) = RangePositions.validate(params, cache); @@ -100,6 +129,16 @@ library MintRangeCall { cache.amount0 -= params.amount0.toInt128(); cache.amount1 -= params.amount1.toInt128(); + emit Mint( + msg.sender, + cache.owner, + cache.position.lower, + cache.position.upper, + cache.liquidityMinted.toUint128(), + params.amount0, + params.amount1 + ); + emit MintRange( cache.owner, cache.position.lower, @@ -107,26 +146,22 @@ library MintRangeCall { params.positionId, cache.liquidityMinted.toUint128(), -(cache.amount0 + cache.feesAccrued0), /// @dev - emit token0 balance delta - -(cache.amount1 + cache.feesAccrued1) /// @dev - emit token1 balance delta + -(cache.amount1 + cache.feesAccrued1) /// @dev - emit token1 balance delta ); // update position with latest fees accrued - cache = RangePositions.add( - ticks, - samples, - tickMap, - cache, - params - ); + cache = RangePositions.add(ticks, samples, tickMap, cache, params); // save changes to storage before transfer out save(positions, globalState, cache, params.positionId); // transfer positive amounts back to user if (cache.feesAccrued0 > 0 || cache.feesAccrued1 > 0) - Collect.range( + CollectLib.range( + cache.position, cache.constants, cache.owner, + cache.owner, cache.feesAccrued0, cache.feesAccrued1 ); @@ -145,16 +180,24 @@ library MintRangeCall { // check balance after callback if (cache.amount0 < 0) - if (balance0(cache) < startBalance.amount0 + (-cache.amount0).toUint128()) - require(false, 'MintInputAmount0TooLow()'); + if ( + balance0(cache) < + startBalance.amount0 + (-cache.amount0).toUint128() + ) require(false, 'MintInputAmount0TooLow()'); if (cache.amount1 < 0) - if (balance1(cache) < startBalance.amount1 + (-cache.amount1).toUint128()) - require(false, 'MintInputAmount1TooLow()'); + if ( + balance1(cache) < + startBalance.amount1 + (-cache.amount1).toUint128() + ) require(false, 'MintInputAmount1TooLow()'); + + return ( + cache.amount0 + cache.feesAccrued0, + cache.amount1 + cache.feesAccrued1 + ); } function save( - mapping(uint256 => RangePoolStructs.RangePosition) - storage positions, + mapping(uint256 => RangePoolStructs.RangePosition) storage positions, PoolsharkStructs.GlobalState storage globalState, RangePoolStructs.MintRangeCache memory cache, uint32 positionId @@ -165,36 +208,32 @@ library MintRangeCall { globalState.positionIdNext = cache.state.positionIdNext; } - function balance0( - RangePoolStructs.MintRangeCache memory cache - ) private view returns (uint256) { - ( - bool success, - bytes memory data - ) = (cache.constants.token0) - .staticcall( - abi.encodeWithSelector( - IERC20Minimal.balanceOf.selector, - address(this) - ) - ); + function balance0(RangePoolStructs.MintRangeCache memory cache) + private + view + returns (uint256) + { + (bool success, bytes memory data) = (cache.constants.token0).staticcall( + abi.encodeWithSelector( + IERC20Minimal.balanceOf.selector, + address(this) + ) + ); require(success && data.length >= 32); return abi.decode(data, (uint256)); } - function balance1( - RangePoolStructs.MintRangeCache memory cache - ) private view returns (uint256) { - ( - bool success, - bytes memory data - ) = (cache.constants.token1) - .staticcall( - abi.encodeWithSelector( - IERC20Minimal.balanceOf.selector, - address(this) - ) - ); + function balance1(RangePoolStructs.MintRangeCache memory cache) + private + view + returns (uint256) + { + (bool success, bytes memory data) = (cache.constants.token1).staticcall( + abi.encodeWithSelector( + IERC20Minimal.balanceOf.selector, + address(this) + ) + ); require(success && data.length >= 32); return abi.decode(data, (uint256)); } diff --git a/contracts/libraries/range/pool/SnapshotRangeCall.sol b/contracts/libraries/range/pool/SnapshotRangeCall.sol index 1639348e..1a050455 100644 --- a/contracts/libraries/range/pool/SnapshotRangeCall.sol +++ b/contracts/libraries/range/pool/SnapshotRangeCall.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +// SPDX-License-Identifier: BUSL-1.1 +pragma solidity 0.8.18; import '../../../interfaces/structs/LimitPoolStructs.sol'; import '../RangePositions.sol'; @@ -19,27 +19,30 @@ library SnapshotRangeCall { ); function perform( - mapping(uint256 => RangePoolStructs.RangePosition) - storage positions, + mapping(uint256 => RangePoolStructs.RangePosition) storage positions, mapping(int24 => PoolsharkStructs.Tick) storage ticks, PoolsharkStructs.GlobalState memory state, PoolsharkStructs.LimitImmutables memory constants, uint32 positionId - ) external view returns ( - int56, - uint160, - uint128, - uint128 ) + external + view + returns ( + int56, + uint160, + uint128, + uint128 + ) { if (state.unlocked == _ENTERED) require(false, 'ReentrancyGuardReadOnlyReentrantCall()'); - return RangePositions.snapshot( - positions, - ticks, - state, - constants, - positionId - ); + return + RangePositions.snapshot( + positions, + ticks, + state, + constants, + positionId + ); } } diff --git a/contracts/libraries/utils/Bytes.sol b/contracts/libraries/utils/Bytes.sol index e221f5bd..32ab63d3 100644 --- a/contracts/libraries/utils/Bytes.sol +++ b/contracts/libraries/utils/Bytes.sol @@ -1,8 +1,8 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; library Bytes { - bytes16 private constant alphabet = "0123456789abcdef"; + bytes16 private constant alphabet = '0123456789abcdef'; function from(string memory source) internal pure returns (bytes32 result) { bytes memory tempEmptyStringTest = bytes(source); @@ -15,9 +15,13 @@ library Bytes { } } - function bytes32ToString(bytes32 _bytes32) internal pure returns (string memory) { + function bytes32ToString(bytes32 _bytes32) + internal + pure + returns (string memory) + { uint8 i = 0; - while(i < 32 && _bytes32[i] != 0) { + while (i < 32 && _bytes32[i] != 0) { i++; } bytes memory bytesArray = new bytes(i); @@ -26,4 +30,4 @@ library Bytes { } return string(bytesArray); } -} \ No newline at end of file +} diff --git a/contracts/libraries/utils/Collect.sol b/contracts/libraries/utils/Collect.sol index 4b345377..419f256e 100644 --- a/contracts/libraries/utils/Collect.sol +++ b/contracts/libraries/utils/Collect.sol @@ -1,23 +1,27 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import '../../interfaces/structs/LimitPoolStructs.sol'; import '../limit/LimitPositions.sol'; import '../utils/SafeTransfers.sol'; -library Collect { +library CollectLib { using SafeCast for int128; + using SafeCast for uint128; - event CollectRange0( - uint128 amount0 - ); - - event CollectRange1( + event Collect( + address indexed owner, + address recipient, + int24 indexed tickLower, + int24 indexed tickUpper, + uint128 amount0, uint128 amount1 ); function range( + RangePoolStructs.RangePosition memory position, PoolsharkStructs.LimitImmutables memory constants, + address owner, address recipient, int128 amount0, int128 amount1 @@ -25,22 +29,40 @@ library Collect { /// @dev - negative balances will revert if (amount0 > 0) { /// @dev - cast to ensure user doesn't owe the pool balance - SafeTransfers.transferOut(recipient, constants.token0, amount0.toUint128()); - emit CollectRange0(amount0.toUint128()); + SafeTransfers.transferOut( + recipient, + constants.token0, + amount0.toUint128() + ); } if (amount1 > 0) { /// @dev - cast to ensure user doesn't owe the pool balance - SafeTransfers.transferOut(recipient, constants.token1, amount1.toUint128()); - emit CollectRange1(amount1.toUint128()); + SafeTransfers.transferOut( + recipient, + constants.token1, + amount1.toUint128() + ); } + emit Collect( + owner, + recipient, + position.lower, + position.upper, + amount0 > 0 ? amount0.toUint128() : 0, + amount1 > 0 ? amount1.toUint128() : 0 + ); } function burnLimit( LimitPoolStructs.BurnLimitCache memory cache, PoolsharkStructs.BurnLimitParams memory params - ) internal returns ( - LimitPoolStructs.BurnLimitCache memory - ) + ) + internal + returns ( + LimitPoolStructs.BurnLimitCache memory, + int128 amount0Delta, + int128 amount1Delta + ) { uint128 amount0 = params.zeroForOne ? cache.amountOut : cache.amountIn; uint128 amount1 = params.zeroForOne ? cache.amountIn : cache.amountOut; @@ -48,12 +70,20 @@ library Collect { /// zero out balances and transfer out if (amount0 > 0) { cache.amountIn = 0; - SafeTransfers.transferOut(params.to, cache.constants.token0, amount0); + SafeTransfers.transferOut( + params.to, + cache.constants.token0, + amount0 + ); } if (amount1 > 0) { cache.amountOut = 0; - SafeTransfers.transferOut(params.to, cache.constants.token1, amount1); + SafeTransfers.transferOut( + params.to, + cache.constants.token1, + amount1 + ); } - return cache; + return (cache, amount0.toInt128(), amount1.toInt128()); } } diff --git a/contracts/libraries/utils/PositionTokens.sol b/contracts/libraries/utils/PositionTokens.sol index ebfe777d..b9b40cbd 100644 --- a/contracts/libraries/utils/PositionTokens.sol +++ b/contracts/libraries/utils/PositionTokens.sol @@ -1,12 +1,12 @@ -// SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import './Bytes.sol'; import './String.sol'; -import "../math/OverflowMath.sol"; +import '../math/OverflowMath.sol'; import '../../interfaces/IPositionERC1155.sol'; -import "../../interfaces/range/IRangePoolFactory.sol"; -import "../../interfaces/structs/RangePoolStructs.sol"; +import '../../interfaces/range/IRangePoolFactory.sol'; +import '../../interfaces/structs/RangePoolStructs.sol'; import '@openzeppelin/contracts/token/ERC20/ERC20.sol'; /// @notice Token library for ERC-1155 calls. @@ -17,30 +17,38 @@ library PositionTokens { PoolsharkStructs.LimitImmutables memory constants, address owner, uint32 positionId - ) internal view returns ( - uint256 - ) - { - return IPositionERC1155(constants.poolToken).balanceOf(owner, positionId); + ) internal view returns (uint256) { + return + IPositionERC1155(constants.poolToken).balanceOf(owner, positionId); } - function name(address token0, address token1) internal view returns (bytes32 result) { + function name(address token0, address token1) + internal + view + returns (bytes32 result) + { string memory nameString = string.concat( 'Poolshark ', - ERC20(token0).symbol(), '-', + ERC20(token0).symbol(), + '-', ERC20(token1).symbol() ); result = Bytes.from(nameString); } - function symbol(address token0, address token1) internal view returns (bytes32 result) { + function symbol(address token0, address token1) + internal + view + returns (bytes32 result) + { string memory symbolString = string.concat( 'PSHARK-', - ERC20(token0).symbol(), '-', + ERC20(token0).symbol(), + '-', ERC20(token1).symbol() ); result = Bytes.from(symbolString); } -} \ No newline at end of file +} diff --git a/contracts/libraries/utils/SafeCast.sol b/contracts/libraries/utils/SafeCast.sol index 7f9a9086..d64b3d5f 100644 --- a/contracts/libraries/utils/SafeCast.sol +++ b/contracts/libraries/utils/SafeCast.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: GPL-2.0-or-later -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; /// @title Safe casting methods /// @notice Contains methods for safely casting between types @@ -8,14 +8,15 @@ library SafeCast { /// @param y The uint256 to be downcasted /// @return z The downcasted integer, now type uint128 function toUint128(uint256 y) internal pure returns (uint128 z) { - if((z = uint128(y)) != y) require(false, 'Uint256ToUint128:Overflow()'); + if ((z = uint128(y)) != y) + require(false, 'Uint256ToUint128:Overflow()'); } /// @notice Cast a uint256 to a uint128, revert on overflow /// @param y The uint256 to be downcasted /// @return z The downcasted integer, now type uint128 function toUint128(int128 y) internal pure returns (uint128 z) { - if(y < 0) require(false, 'Int128ToUint128:Underflow()'); + if (y < 0) require(false, 'Int128ToUint128:Underflow()'); z = uint128(y); } @@ -23,14 +24,15 @@ library SafeCast { /// @param y The uint256 to be downcasted /// @return z The downcasted integer, now type uint160 function toUint160(uint256 y) internal pure returns (uint160 z) { - if((z = uint160(y)) != y) require(false, 'Uint256ToUint160:Overflow()'); + if ((z = uint160(y)) != y) + require(false, 'Uint256ToUint160:Overflow()'); } /// @notice Cast a uint256 to a uint160, revert on overflow /// @param y The uint256 to be downcasted /// @return z The downcasted integer, now type uint160 function toUint32(uint256 y) internal pure returns (uint32 z) { - if((z = uint32(y)) != y) require(false, 'Uint256ToUint32:Overflow()'); + if ((z = uint32(y)) != y) require(false, 'Uint256ToUint32:Overflow()'); } /// @notice Cast a int256 to a int128, revert on overflow or underflow @@ -44,7 +46,8 @@ library SafeCast { /// @param y The int256 to be downcasted /// @return z The downcasted integer, now type int128 function toInt128(uint128 y) internal pure returns (int128 z) { - if(y > uint128(type(int128).max)) require(false, 'Uint128ToInt128:Overflow()'); + if (y > uint128(type(int128).max)) + require(false, 'Uint128ToInt128:Overflow()'); z = int128(y); } @@ -52,7 +55,8 @@ library SafeCast { /// @param y The uint256 to be casted /// @return z The casted integer, now type int256 function toInt256(uint256 y) internal pure returns (int256 z) { - if(y > uint256(type(int256).max)) require(false, 'Uint256ToInt256:Overflow()'); + if (y > uint256(type(int256).max)) + require(false, 'Uint256ToInt256:Overflow()'); z = int256(y); } @@ -60,7 +64,7 @@ library SafeCast { /// @param y The uint256 to be downcasted /// @return z The downcasted integer, now type uint128 function toUint256(int256 y) internal pure returns (uint256 z) { - if(y < 0) require(false, 'Int256ToUint256:Underflow()'); + if (y < 0) require(false, 'Int256ToUint256:Underflow()'); z = uint256(y); } @@ -68,6 +72,6 @@ library SafeCast { /// @param y The uint256 to be downcasted /// @return z The downcasted integer, now type uint128 function toUint16(uint256 y) internal pure returns (uint16 z) { - if((z = uint16(y)) != y) require(false, 'Uint256ToUint16:Overflow()'); + if ((z = uint16(y)) != y) require(false, 'Uint256ToUint16:Overflow()'); } -} \ No newline at end of file +} diff --git a/contracts/libraries/utils/SafeTransfers.sol b/contracts/libraries/utils/SafeTransfers.sol index bc1e2696..d3c46e50 100644 --- a/contracts/libraries/utils/SafeTransfers.sol +++ b/contracts/libraries/utils/SafeTransfers.sol @@ -1,5 +1,5 @@ -//SPDX-License-Identifier: Unlicense -pragma solidity 0.8.13; +//SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import '@openzeppelin/contracts/token/ERC20/ERC20.sol'; @@ -20,12 +20,12 @@ library SafeTransfers { uint256 amount ) internal { bool success; + if (amount == 0) return; if (token == address(0)) { - (success, ) = to.call{value: amount}(""); - if (!success) require(false, "SafeTransfers::EthTransferFailed()"); + (success, ) = to.call{value: amount}(''); + if (!success) require(false, 'SafeTransfers::EthTransferFailed()'); return; } - if (amount == 0) return; IERC20 erc20Token = IERC20(token); // ? We are checking the transfer, but since we are doing so in an assembly block // ? Slither does not pick up on that and results in a hit @@ -49,7 +49,8 @@ library SafeTransfers { success := 0 } } - if (!success) require(false, 'TransferFailed(address(this), msg.sender'); + if (!success) + require(false, 'TransferFailed(address(this), msg.sender'); } /** @@ -62,13 +63,15 @@ library SafeTransfers { * See here: https://medium.com/coinmonks/missing-return-value-bug-at-least-130-tokens-affected-d67bf08521ca */ // slither-disable-next-line assembly - function transferInto(address token, address sender, uint256 amount) internal returns (uint256) { + function transferInto( + address token, + address sender, + uint256 amount + ) internal { if (token == address(0)) { - if (msg.value < amount) require(false, 'SafeTransfers::LowEthAmountSent()'); - return amount; + require(false, 'SafeTransfers::CannotTransferInEth()'); } IERC20 erc20Token = IERC20(token); - uint256 balanceBefore = IERC20(token).balanceOf(address(this)); /// @dev - msg.sender here is the pool erc20Token.transferFrom(sender, msg.sender, amount); @@ -90,11 +93,7 @@ library SafeTransfers { success := 0 } } - if (!success) require(false, 'TransferFailed(msg.sender, address(this)'); - - // Calculate the amount that was *actually* transferred - uint256 balanceAfter = IERC20(token).balanceOf(address(this)); - - return balanceAfter - balanceBefore; // underflow already checked above, just subtract + if (!success) + require(false, 'TransferFailed(msg.sender, address(this)'); } } diff --git a/contracts/libraries/utils/String.sol b/contracts/libraries/utils/String.sol index 0f8136e8..50472658 100644 --- a/contracts/libraries/utils/String.sol +++ b/contracts/libraries/utils/String.sol @@ -1,18 +1,18 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; library String { - bytes16 private constant alphabet = "0123456789abcdef"; + bytes16 private constant alphabet = '0123456789abcdef'; - function from(bytes32 value) internal pure returns(string memory) { + function from(bytes32 value) internal pure returns (string memory) { return toString(abi.encodePacked(value)); } - function from(address account) internal pure returns(string memory) { + function from(address account) internal pure returns (string memory) { return toString(abi.encodePacked(account)); } - function from(uint256 value) internal pure returns(string memory) { + function from(uint256 value) internal pure returns (string memory) { unchecked { uint256 length = log10(value) + 1; string memory buffer = new string(length); @@ -35,7 +35,7 @@ library String { } function from(int256 value) internal pure returns (string memory) { - return string(abi.encodePacked(value < 0 ? "-" : "", from(abs(value)))); + return string(abi.encodePacked(value < 0 ? '-' : '', from(abs(value)))); } function abs(int256 n) internal pure returns (uint256) { @@ -48,48 +48,48 @@ library String { function log10(uint256 value) internal pure returns (uint256) { uint256 result = 0; unchecked { - if (value >= 10 ** 64) { - value /= 10 ** 64; + if (value >= 10**64) { + value /= 10**64; result += 64; } - if (value >= 10 ** 32) { - value /= 10 ** 32; + if (value >= 10**32) { + value /= 10**32; result += 32; } - if (value >= 10 ** 16) { - value /= 10 ** 16; + if (value >= 10**16) { + value /= 10**16; result += 16; } - if (value >= 10 ** 8) { - value /= 10 ** 8; + if (value >= 10**8) { + value /= 10**8; result += 8; } - if (value >= 10 ** 4) { - value /= 10 ** 4; + if (value >= 10**4) { + value /= 10**4; result += 4; } - if (value >= 10 ** 2) { - value /= 10 ** 2; + if (value >= 10**2) { + value /= 10**2; result += 2; } - if (value >= 10 ** 1) { + if (value >= 10**1) { result += 1; } } return result; } - function toString(bytes memory data) internal pure returns(string memory) { + function toString(bytes memory data) internal pure returns (string memory) { bytes memory str = new bytes(2 + data.length * 2); - str[0] = "0"; - str[1] = "x"; - for (uint i = 0; i < data.length;) { - str[2+i*2] = alphabet[uint(uint8(data[i] >> 4))]; - str[3+i*2] = alphabet[uint(uint8(data[i] & 0x0f))]; + str[0] = '0'; + str[1] = 'x'; + for (uint256 i = 0; i < data.length; ) { + str[2 + i * 2] = alphabet[uint256(uint8(data[i] >> 4))]; + str[3 + i * 2] = alphabet[uint256(uint8(data[i] & 0x0f))]; unchecked { ++i; } } return string(str); } -} \ No newline at end of file +} diff --git a/contracts/staking/RangeStaker.sol b/contracts/staking/RangeStaker.sol index 1c53d8a8..04a12a96 100644 --- a/contracts/staking/RangeStaker.sol +++ b/contracts/staking/RangeStaker.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import '../interfaces/IPool.sol'; import '../interfaces/IPositionERC1155.sol'; @@ -18,11 +18,7 @@ import '../external/openzeppelin/security/ReentrancyGuard.sol'; /** * @dev Defines the actions which can be executed by the factory admin. */ -contract RangeStaker is - RangeStakerEvents, - PoolsharkStructs, - ReentrancyGuard -{ +contract RangeStaker is RangeStakerEvents, PoolsharkStructs, ReentrancyGuard { address public immutable limitPoolFactory; uint32 public immutable startTimestamp; uint32 public immutable endTimestamp; @@ -50,9 +46,7 @@ contract RangeStaker is uint32 endTime; } - constructor( - RangeStakerParams memory params - ) { + constructor(RangeStakerParams memory params) { owner = msg.sender; feeTo = msg.sender; limitPoolFactory = params.limitPoolFactory; @@ -76,14 +70,11 @@ contract RangeStaker is uint32 positionIdNext; } - function stakeRange( - StakeRangeParams memory params - ) external nonReentrant() { - + function stakeRange(StakeRangeParams memory params) external nonReentrant { // load pool constants StakeRangeLocals memory locals; locals.constants = ILimitPoolView(params.pool).immutables(); - + // Checks: validate deterministic address canonicalLimitPoolsOnly(params.pool, locals.constants); @@ -92,41 +83,43 @@ contract RangeStaker is locals.stake.positionId = params.positionId; } else { // grab positionIdNext from pool - (,,,,locals.positionIdNext,,) = ILimitPoolStorageView(params.pool).globalState(); + (, , , , locals.positionIdNext, , ) = ILimitPoolStorageView( + params.pool + ).globalState(); locals.stake.positionId = locals.positionIdNext - 1; } // stake info locals.stake.pool = params.pool; locals.poolToken = locals.constants.poolToken; - locals.stakeKey = keccak256(abi.encode( - locals.stake.pool, - locals.stake.positionId - )); + locals.stakeKey = keccak256( + abi.encode(locals.stake.pool, locals.stake.positionId) + ); // load previous fee growth and staked flag locals.stake.isStaked = rangeStakes[locals.stakeKey].isStaked; // check position exists if (!locals.stake.isStaked) { - ( - ,, - locals.stake.liquidity,, - ) = IRangePool(params.pool).positions(locals.stake.positionId); + (, , locals.stake.liquidity, , ) = IRangePool(params.pool) + .positions(locals.stake.positionId); } else { locals.stake.owner = rangeStakes[locals.stakeKey].owner; locals.stake.liquidity = rangeStakes[locals.stakeKey].liquidity; if (locals.stake.owner != params.to) { - require(false, "RangeStake::PositionOwnerMismatch()"); + require(false, 'RangeStake::PositionOwnerMismatch()'); } } if (locals.stake.liquidity == 0) { - require(false, "RangeStake::PositionNotFound()"); + require(false, 'RangeStake::PositionNotFound()'); } // check if transfer needed - locals.positionBalance = IPositionERC1155(locals.poolToken).balanceOf(address(this), locals.stake.positionId); + locals.positionBalance = IPositionERC1155(locals.poolToken).balanceOf( + address(this), + locals.stake.positionId + ); if (locals.positionBalance == 0) { // position not staked and balance not held @@ -141,15 +134,19 @@ contract RangeStaker is // start tracking fee growth from after compound if (!locals.stake.isStaked) { // compound position to avoid including old fees accrued - IRangePool(params.pool).burnRange(BurnRangeParams({ - to: params.to, - positionId: locals.stake.positionId, - burnPercent: 0 - })); + IRangePool(params.pool).burnRange( + BurnRangeParams({ + to: params.to, + positionId: locals.stake.positionId, + burnPercent: 0 + }) + ); ( locals.stake.feeGrowthInside0Last, locals.stake.feeGrowthInside1Last, - ,, + , + , + ) = IRangePool(params.pool).positions(locals.stake.positionId); // mark position as staked @@ -157,10 +154,7 @@ contract RangeStaker is locals.stake.owner = params.to; } else { // load previous fee growth - ( - locals.feeGrowthInside0Start, - locals.feeGrowthInside1Start - ) = ( + (locals.feeGrowthInside0Start, locals.feeGrowthInside1Start) = ( rangeStakes[locals.stakeKey].feeGrowthInside0Last, rangeStakes[locals.stakeKey].feeGrowthInside1Last ); @@ -168,23 +162,32 @@ contract RangeStaker is ( locals.stake.feeGrowthInside0Last, locals.stake.feeGrowthInside1Last, - locals.newPositionLiquidity,, + locals.newPositionLiquidity, + , + ) = IRangePool(params.pool).positions(params.positionId); // increment fee growth accrued if inside reward period locals.feeGrowth0Accrued = OverflowMath.mulDiv( - locals.stake.feeGrowthInside0Last - locals.feeGrowthInside0Start, + locals.stake.feeGrowthInside0Last - + locals.feeGrowthInside0Start, locals.stake.liquidity, Q128 ); locals.feeGrowth1Accrued = OverflowMath.mulDiv( - locals.stake.feeGrowthInside1Last - locals.feeGrowthInside1Start, + locals.stake.feeGrowthInside1Last - + locals.feeGrowthInside1Start, locals.stake.liquidity, Q128 ); - if (block.timestamp > startTimestamp && block.timestamp <= endTimestamp) { - if (locals.feeGrowth0Accrued > 0 || locals.feeGrowth1Accrued > 0) + if ( + block.timestamp > startTimestamp && + block.timestamp <= endTimestamp + ) { + if ( + locals.feeGrowth0Accrued > 0 || locals.feeGrowth1Accrued > 0 + ) emit StakeRangeAccrued( locals.stake.pool, locals.stake.positionId, @@ -211,63 +214,78 @@ contract RangeStaker is // Interactions: transfer out fees accrued if (locals.feeGrowth0Accrued > 0) - SafeTransfers.transferOut(locals.stake.owner, locals.constants.token0, locals.feeGrowth0Accrued); + SafeTransfers.transferOut( + locals.stake.owner, + locals.constants.token0, + locals.feeGrowth0Accrued + ); if (locals.feeGrowth1Accrued > 0) - SafeTransfers.transferOut(locals.stake.owner, locals.constants.token1, locals.feeGrowth1Accrued); + SafeTransfers.transferOut( + locals.stake.owner, + locals.constants.token1, + locals.feeGrowth1Accrued + ); } - function unstakeRange( - UnstakeRangeParams memory params - ) external nonReentrant() { - + function unstakeRange(UnstakeRangeParams memory params) + external + nonReentrant + { StakeRangeLocals memory locals; locals.poolToken = IPool(params.pool).poolToken(); - locals.stakeKey = keccak256(abi.encode( - params.pool, - params.positionId - )); + locals.stakeKey = keccak256(abi.encode(params.pool, params.positionId)); // load previous stake locals.stake = rangeStakes[locals.stakeKey]; if (locals.stake.pool == address(0)) { - require(false, "RangeUnstake::StakeNotFound()"); + require(false, 'RangeUnstake::StakeNotFound()'); } else if (locals.stake.owner != msg.sender) { - require(false, "RangeUnstake::PositionOwnerMisMatch()"); + require(false, 'RangeUnstake::PositionOwnerMisMatch()'); } else if (!locals.stake.isStaked) { - require(false, "RangeUnstake::PositionAlreadyUnstaked()"); + require(false, 'RangeUnstake::PositionAlreadyUnstaked()'); } ( locals.feeGrowthInside0Start, locals.feeGrowthInside1Start, - ,, + , + , + ) = IRangePool(params.pool).positions(params.positionId); // compound position to reward user for staked period - IRangePool(params.pool).burnRange(BurnRangeParams({ - to: params.to, - positionId: params.positionId, - burnPercent: 0 - })); + IRangePool(params.pool).burnRange( + BurnRangeParams({ + to: params.to, + positionId: params.positionId, + burnPercent: 0 + }) + ); // start tracking fee growth from after compound ( locals.stake.feeGrowthInside0Last, locals.stake.feeGrowthInside1Last, - ,, + , + , + ) = IRangePool(params.pool).positions(params.positionId); - if (block.timestamp > startTimestamp && block.timestamp <= endTimestamp) { + if ( + block.timestamp > startTimestamp && block.timestamp <= endTimestamp + ) { // increment fee growth accrued if inside reward period locals.feeGrowth0Accrued = OverflowMath.mulDiv( - locals.stake.feeGrowthInside0Last - locals.feeGrowthInside0Start, + locals.stake.feeGrowthInside0Last - + locals.feeGrowthInside0Start, locals.stake.liquidity, Q128 ); locals.feeGrowth1Accrued = OverflowMath.mulDiv( - locals.stake.feeGrowthInside1Last - locals.feeGrowthInside1Start, + locals.stake.feeGrowthInside1Last - + locals.feeGrowthInside1Start, locals.stake.liquidity, Q128 ); @@ -286,7 +304,7 @@ contract RangeStaker is params.positionId, 1 ); - + // mark position unstaked locals.stake.isStaked = false; @@ -300,32 +318,31 @@ contract RangeStaker is rangeStakes[locals.stakeKey] = locals.stake; } - function burnRangeStake( - address pool, - BurnRangeParams memory params - ) external nonReentrant() { + function burnRangeStake(address pool, BurnRangeParams memory params) + external + nonReentrant + { StakeRangeLocals memory locals; - locals.stakeKey = keccak256(abi.encode( - pool, - params.positionId - )); + locals.stakeKey = keccak256(abi.encode(pool, params.positionId)); // load previous stake locals.stake = rangeStakes[locals.stakeKey]; if (locals.stake.pool == address(0)) { - require(false, "BurnRangeStake::StakeNotFound()"); + require(false, 'BurnRangeStake::StakeNotFound()'); } else if (locals.stake.owner != msg.sender) { - require(false, "BurnRangeStake::PositionOwnerMismatch()"); + require(false, 'BurnRangeStake::PositionOwnerMismatch()'); } else if (!locals.stake.isStaked) { - require(false, "BurnRangeStake::PositionAlreadyUnstaked()"); + require(false, 'BurnRangeStake::PositionAlreadyUnstaked()'); } ( locals.feeGrowthInside0Start, locals.feeGrowthInside1Start, - ,, + , + , + ) = IRangePool(pool).positions(params.positionId); // compound position to reward user for staked period @@ -335,18 +352,24 @@ contract RangeStaker is ( locals.stake.feeGrowthInside0Last, locals.stake.feeGrowthInside1Last, - locals.stake.liquidity,, + locals.stake.liquidity, + , + ) = IRangePool(pool).positions(params.positionId); - if (block.timestamp > startTimestamp && block.timestamp <= endTimestamp) { + if ( + block.timestamp > startTimestamp && block.timestamp <= endTimestamp + ) { // increment fee growth accrued if inside reward period locals.feeGrowth0Accrued = OverflowMath.mulDiv( - locals.stake.feeGrowthInside0Last - locals.feeGrowthInside0Start, + locals.stake.feeGrowthInside0Last - + locals.feeGrowthInside0Start, locals.stake.liquidity, Q128 ); locals.feeGrowth1Accrued = OverflowMath.mulDiv( - locals.stake.feeGrowthInside1Last - locals.feeGrowthInside1Start, + locals.stake.feeGrowthInside1Last - + locals.feeGrowthInside1Start, locals.stake.liquidity, Q128 ); @@ -384,12 +407,14 @@ contract RangeStaker is * Can only be called by the current owner. */ function transferOwner(address newOwner) public virtual onlyOwner { - if(newOwner == address(0)) require (false, 'TransferredToZeroAddress()'); + if (newOwner == address(0)) + require(false, 'TransferredToZeroAddress()'); _transferOwner(newOwner); } function transferFeeTo(address newFeeTo) public virtual onlyFeeTo { - if(newFeeTo == address(0)) require (false, 'TransferredToZeroAddress()'); + if (newFeeTo == address(0)) + require(false, 'TransferredToZeroAddress()'); _transferFeeTo(newFeeTo); } @@ -417,19 +442,24 @@ contract RangeStaker is * @dev Throws if the sender is not the owner. */ function _checkOwner() internal view { - if (owner != msg.sender) require (false, 'OwnerOnly()'); + if (owner != msg.sender) require(false, 'OwnerOnly()'); } /** * @dev Throws if the sender is not the feeTo. */ function _checkFeeTo() internal view { - if (feeTo != msg.sender) require (false, 'FeeToOnly()'); + if (feeTo != msg.sender) require(false, 'FeeToOnly()'); } - function supportsInterface(bytes4 interfaceId) external pure returns (bool) { - return interfaceId == 0x01ffc9a7 || // ERC-165 support - interfaceId == 0xd9b67a26; // ERC-1155 support + function supportsInterface(bytes4 interfaceId) + external + pure + returns (bool) + { + return + interfaceId == 0x01ffc9a7 || // ERC-165 support + interfaceId == 0xd9b67a26; // ERC-1155 support } function canonicalLimitPoolsOnly( @@ -437,12 +467,14 @@ contract RangeStaker is PoolsharkStructs.LimitImmutables memory constants ) private view { // generate key for pool - bytes32 key = keccak256(abi.encode( - constants.poolImpl, - constants.token0, - constants.token1, - constants.swapFee - )); + bytes32 key = keccak256( + abi.encode( + constants.poolImpl, + constants.token0, + constants.token1, + constants.swapFee + ) + ); // compute address address predictedAddress = LibClone.predictDeterministicAddress( @@ -456,10 +488,13 @@ contract RangeStaker is if (pool != predictedAddress) require(false, 'InvalidCallerAddress()'); } - function encodeLimit( - LimitImmutables memory constants - ) private pure returns (bytes memory) { - return abi.encodePacked( + function encodeLimit(LimitImmutables memory constants) + private + pure + returns (bytes memory) + { + return + abi.encodePacked( constants.owner, constants.token0, constants.token1, @@ -469,6 +504,6 @@ contract RangeStaker is constants.genesisTime, constants.tickSpacing, constants.swapFee - ); + ); } -} \ No newline at end of file +} diff --git a/contracts/test/Token20.sol b/contracts/test/Token20.sol index 23c062b8..f387b8dd 100644 --- a/contracts/test/Token20.sol +++ b/contracts/test/Token20.sol @@ -1,10 +1,12 @@ //SPDX-License-Identifier: Unlicense -pragma solidity 0.8.13; +pragma solidity 0.8.18; +import '@openzeppelin/contracts/access/Ownable.sol'; import '@openzeppelin/contracts/token/ERC20/ERC20.sol'; import '@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol'; +import '../libraries/utils/SafeTransfers.sol'; -contract Token20 is ERC20, ERC20Burnable { +contract Token20 is ERC20, ERC20Burnable, Ownable { uint8 _decimals; constructor( @@ -12,6 +14,7 @@ contract Token20 is ERC20, ERC20Burnable { string memory tokenSymbol, uint8 decimals_ ) ERC20(tokenName, tokenSymbol) { + _transferOwnership(msg.sender); _decimals = decimals_; } @@ -22,4 +25,8 @@ contract Token20 is ERC20, ERC20Burnable { function decimals() public view override returns (uint8) { return _decimals; } + + function transferIntoTest() external { + SafeTransfers.transferInto(address(0), address(0), 0); + } } diff --git a/contracts/test/WETH9.sol b/contracts/test/WETH9.sol index 9f64e454..8b5c74b6 100644 --- a/contracts/test/WETH9.sol +++ b/contracts/test/WETH9.sol @@ -1,19 +1,19 @@ -//SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +//SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; contract WETH9 { - string public name = "Wrapped Ether"; - string public symbol = "WETH"; - uint8 public decimals = 18; + string public name = 'Wrapped Ether'; + string public symbol = 'WETH'; + uint8 public decimals = 18; address public immutable owner; - event Approval(address indexed src, address indexed guy, uint wad); - event Transfer(address indexed src, address indexed dst, uint wad); - event Deposit(address indexed dst, uint wad); - event Withdrawal(address indexed src, uint wad); + event Approval(address indexed src, address indexed guy, uint256 wad); + event Transfer(address indexed src, address indexed dst, uint256 wad); + event Deposit(address indexed dst, uint256 wad); + event Withdrawal(address indexed src, uint256 wad); - mapping (address => uint) public balanceOf; - mapping (address => mapping (address => uint)) public allowance; + mapping(address => uint256) public balanceOf; + mapping(address => mapping(address => uint256)) public allowance; modifier ownerOnly() { _onlyOwner(); @@ -37,34 +37,38 @@ contract WETH9 { emit Deposit(msg.sender, msg.value); } - function withdraw(uint wad) public { + function withdraw(uint256 wad) public { require(balanceOf[msg.sender] >= wad); balanceOf[msg.sender] -= wad; payable(msg.sender).transfer(wad); emit Withdrawal(msg.sender, wad); } - function totalSupply() public view returns (uint) { + function totalSupply() public view returns (uint256) { return address(this).balance; } - function approve(address guy, uint wad) public returns (bool) { + function approve(address guy, uint256 wad) public returns (bool) { allowance[msg.sender][guy] = wad; emit Approval(msg.sender, guy, wad); return true; } - function transfer(address dst, uint wad) public returns (bool) { + function transfer(address dst, uint256 wad) public returns (bool) { return transferFrom(msg.sender, dst, wad); } - function transferFrom(address src, address dst, uint wad) - public - returns (bool) - { + function transferFrom( + address src, + address dst, + uint256 wad + ) public returns (bool) { require(balanceOf[src] >= wad); - if (src != msg.sender && allowance[src][msg.sender] != uint(int(-1))) { + if ( + src != msg.sender && + allowance[src][msg.sender] != uint256(int256(-1)) + ) { require(allowance[src][msg.sender] >= wad); allowance[src][msg.sender] -= wad; } @@ -78,9 +82,9 @@ contract WETH9 { } function _mint(address account, uint256 amount) internal { - balanceOf[account] += amount; + balanceOf[account] += amount; - emit Transfer(address(0), account, amount); + emit Transfer(address(0), account, amount); } function _onlyOwner() private view { @@ -88,9 +92,6 @@ contract WETH9 { } } - - - /* GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007 @@ -767,4 +768,4 @@ the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. But first, please read . -*/ \ No newline at end of file +*/ diff --git a/contracts/utils/LimitPoolManager.sol b/contracts/utils/LimitPoolManager.sol index f555534f..16ef578a 100644 --- a/contracts/utils/LimitPoolManager.sol +++ b/contracts/utils/LimitPoolManager.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import '../interfaces/IPool.sol'; import '../interfaces/limit/ILimitPool.sol'; @@ -8,15 +8,60 @@ import '../interfaces/limit/ILimitPoolManager.sol'; import '../base/events/LimitPoolManagerEvents.sol'; import '../libraries/utils/SafeCast.sol'; +/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#%@@@@%@@@@%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@%%%%%%@@%==========+++**#@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@@@@@@@@%%##%%%@@%%%@@%%*-=====+++**#**+*#*#*#@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@=+===========+#@@@@@%#@%%%%%%*#%%%%%##%@@@@@@@@#****++#%#**#%=%@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@+-==+++***++++=++@@%%%%%%%%*****#%#*#@%@@@@@@@@@@@@@%#+#%#*##*=#@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@+=+*******+++%%##%%####++++++**@@%%@@@@@@@@@@@@@@@###*#%#+==@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@%=**##**+#%###########+====*@%@@%%@@@@@@@@@@@@@%###%@+%=+=*@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@#%%####*##*##*#**#**+=+=-=*%%%###%@@@@@@@@@@@####@#=====+*@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@#%#***#***#*****=*+=+==--=@@%#**###@@@@@@@@@@@###@@%=+++=++#@@@@*@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@##*********+***##++*+#%@*%@@@@@####@@@@#=%@@@@@@@@@@@+==++=+=%%#+*@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@##*****+**+*********++=%%%@@@@@@@@@*@@@%===@@@@@@@@@*===++=+++**#%#@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@#**++++*+++++===+***+==#%%#@@@@@@@@*##@*===+%@@@@%+==+=+++++++*%@%%@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@**++++++++====----+++====+###@@@@@@@%#%%*==++*+#+===+++++++++++*%%@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%###*++++++====---=*@@@@+===-%@**%@@@@@@@#%%%===*+****+++*++++*+*+*#@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@*+++*+++++===--=-*@@@@@@@@=--=@@@#%@@@@@@@@#%%*++++**#+++*++**++***#*@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@%===+++++=+++===*@@@@@@@@@@=-=%@@@@@%#@@@@@@@*===*++=++*++++****+**#%+@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@*=+@@#==+==+=-=@@@%%@@@@@@@@@@@@#***%%@@@@%+===+++*+*******+**++*#%*#@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@======-==#***%#@@@@@@@@@@@##%%%%%%@#====****+*****#**###*+++#*@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%-===*=++**###@@@@@@@@@@@@@+#%%%%@*======+*********###+++++++++%@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%-=+#%%+*###%@@@@@@@@@@@@@@@#%#@%++=+++===**#*#****#%=====+++++++%@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@-=#****####@@@@@@@@@@@@@@@@#%@*++*#++++=+#*#####*#%%==============*@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@*=#**+*###@@@@@@@@@@@@@@@@@#%+**###**+++######%%#+-==--=-=------======#@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@++#++++*##%@@@@@@@@@@@@@@@%+*#%%######*##%#+=-=#@@@@@*----------------*@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@%+#*+++==+***#@@@@@@@@@@@%#%%####%%#*+#*++%@@@@@@@@@@@@@%+=======*@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@*+**+========------=-=%@#%%%%%%%%%%%%#%#%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@#=====-==-------=#@@@@########%%%%%%##++#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%##%%@@@@@@@@@@@@%#######%%#*+=++@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#*####++++=@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%##**+=#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@***%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/ + /** - * @dev Defines the actions which can be executed by the factory admin. + * @title LimitPoolManager + * @notice The manager for all limit pools + * @author Poolshark + * @author @alphak3y */ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { address public owner; address public feeTo; address public factory; - uint16 public constant MAX_PROTOCOL_SWAP_FEE = 1e4; /// @dev - max protocol swap fee of 100% - uint16 public constant MAX_PROTOCOL_FILL_FEE = 1e2; /// @dev - max protocol fill fee of 1% + // fee delta const for dynamic fees + uint16 public feeDeltaConst; + mapping(address => uint16) poolFeeDeltaConsts; + // max protocol fees + uint16 public constant MAX_PROTOCOL_SWAP_FEE = 1e4; /// @dev - max protocol swap fee of 100% + uint16 public constant MAX_PROTOCOL_FILL_FEE = 1e2; /// @dev - max protocol fill fee of 1% // impl name => impl address bytes32[] _poolTypeNames; mapping(uint256 => address) internal _poolImpls; @@ -48,6 +93,10 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { emit FeeTierEnabled(1000, 10); emit FeeTierEnabled(3000, 30); emit FeeTierEnabled(10000, 100); + + // set initial fee delta const + feeDeltaConst = 0; + emit FeeDeltaConstChanged(0, 0); } /** @@ -68,12 +117,14 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { * Can only be called by the current owner. */ function transferOwner(address newOwner) public virtual onlyOwner { - if(newOwner == address(0)) require (false, 'TransferredToZeroAddress()'); + if (newOwner == address(0)) + require(false, 'TransferredToZeroAddress()'); _transferOwner(newOwner); } function transferFeeTo(address newFeeTo) public virtual onlyOwner { - if(newFeeTo == address(0)) require (false, 'TransferredToZeroAddress()'); + if (newFeeTo == address(0)) + require(false, 'TransferredToZeroAddress()'); _transferFeeTo(newFeeTo); } @@ -97,10 +148,10 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { emit FeeToTransfer(oldFeeTo, newFeeTo); } - function enableFeeTier( - uint16 swapFee, - int16 tickSpacing - ) external onlyOwner { + function enableFeeTier(uint16 swapFee, int16 tickSpacing) + external + onlyOwner + { if (_feeTiers[swapFee] != 0) revert FeeTierAlreadyEnabled(); if (tickSpacing <= 0) revert InvalidTickSpacing(); if (tickSpacing % 2 != 0) revert InvalidTickSpacing(); @@ -127,32 +178,52 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { emit PoolTypeEnabled(poolTypeName_, poolImpl_, tokenImpl_, poolTypeId_); } - function setFactory( - address factory_ - ) external onlyOwner { - if (factory != address(0)) require (false, 'FactoryAlreadySet()'); + function setFactory(address factory_) external onlyOwner { + if (factory != address(0)) require(false, 'FactoryAlreadySet()'); emit FactoryChanged(factory, factory_); factory = factory_; } - function collectProtocolFees( - address[] calldata pools - ) external onlyOwnerOrFeeTo { - if (pools.length == 0) require (false, 'EmptyPoolsArray()'); + function setFeeDeltaConst(address pool, uint16 feeDeltaConst_) + external + onlyOwner + { + if (feeDeltaConst_ > 10000) + require(false, 'FeeDeltaConstCeilingExceeded()'); + if (pool == address(0)) { + emit FeeDeltaConstChanged(feeDeltaConst, feeDeltaConst_); + feeDeltaConst = feeDeltaConst_; + } else { + emit PoolFeeDeltaConstChanged( + pool, + poolFeeDeltaConsts[pool], + feeDeltaConst_ + ); + poolFeeDeltaConsts[pool] = feeDeltaConst_; + } + } + + function collectProtocolFees(address[] calldata pools) + external + onlyOwnerOrFeeTo + { + if (pools.length == 0) require(false, 'EmptyPoolsArray()'); uint128[] memory token0FeesCollected = new uint128[](pools.length); uint128[] memory token1FeesCollected = new uint128[](pools.length); // pass empty fees params FeesParams memory feesParams; - for (uint i; i < pools.length;) { - ( - token0FeesCollected[i], - token1FeesCollected[i] - ) = IPool(pools[i]).fees(feesParams); + for (uint256 i; i < pools.length; ) { + (token0FeesCollected[i], token1FeesCollected[i]) = IPool(pools[i]) + .fees(feesParams); unchecked { ++i; } } - emit ProtocolFeesCollected(pools, token0FeesCollected, token1FeesCollected); + emit ProtocolFeesCollected( + pools, + token0FeesCollected, + token1FeesCollected + ); } // protocol fee flags @@ -165,9 +236,9 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { address[] calldata pools, FeesParams[] calldata feesParams ) external onlyOwner { - if (pools.length == 0) require (false, 'EmptyPoolsArray()'); + if (pools.length == 0) require(false, 'EmptyPoolsArray()'); if (pools.length != feesParams.length) { - require (false, 'MismatchedArrayLengths()'); + require(false, 'MismatchedArrayLengths()'); } uint128[] memory token0FeesCollected = new uint128[](pools.length); uint128[] memory token1FeesCollected = new uint128[](pools.length); @@ -175,13 +246,9 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { int16[] memory protocolSwapFees1 = new int16[](pools.length); int16[] memory protocolFillFees0 = new int16[](pools.length); int16[] memory protocolFillFees1 = new int16[](pools.length); - for (uint i; i < pools.length;) { - ( - token0FeesCollected[i], - token1FeesCollected[i] - ) = IPool(pools[i]).fees( - feesParams[i] - ); + for (uint256 i; i < pools.length; ) { + (token0FeesCollected[i], token1FeesCollected[i]) = IPool(pools[i]) + .fees(feesParams[i]); if ((feesParams[i].setFeesFlags & PROTOCOL_SWAP_FEE_0) > 0) { protocolSwapFees0[i] = int16(feesParams[i].protocolSwapFee0); } else { @@ -228,29 +295,37 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { ); } - function poolTypes( - uint16 poolTypeId - ) external view returns ( - address, - address - ) { + function feeDeltaConsts(address pool) external view returns (uint16) { + uint16 poolFeeDeltaConst = poolFeeDeltaConsts[pool]; + if (poolFeeDeltaConst != 0) { + // use custom value if set + return poolFeeDeltaConst; + } + // else use default value + return feeDeltaConst; + } + + function poolTypes(uint16 poolTypeId) + external + view + returns (address, address) + { return (_poolImpls[poolTypeId], _tokenImpls[poolTypeId]); } - function feeTiers( - uint16 swapFee - ) external view returns ( - int16 tickSpacing - ) { + function feeTiers(uint16 swapFee) + external + view + returns (int16 tickSpacing) + { return _feeTiers[swapFee]; } - + /** * @dev Throws if the sender is not the owner. */ function _checkOwner() internal view { - if (owner != msg.sender) - require (false, 'OwnerOnly()'); + if (owner != msg.sender) require(false, 'OwnerOnly()'); } /** @@ -258,6 +333,6 @@ contract LimitPoolManager is ILimitPoolManager, LimitPoolManagerEvents { */ function _checkFeeToAndOwner() internal view { if (feeTo != msg.sender && owner != msg.sender) - require (false, 'OwnerOrFeeToOnly()'); + require(false, 'OwnerOrFeeToOnly()'); } -} \ No newline at end of file +} diff --git a/contracts/utils/PoolsharkRouter.sol b/contracts/utils/PoolsharkRouter.sol index 81582926..485110a2 100644 --- a/contracts/utils/PoolsharkRouter.sol +++ b/contracts/utils/PoolsharkRouter.sol @@ -1,5 +1,5 @@ -// SPDX-License-Identifier: GPLv3 -pragma solidity 0.8.13; +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; import '../interfaces/IPool.sol'; import '../interfaces/staking/IRangeStaker.sol'; @@ -17,6 +17,12 @@ import '../libraries/utils/SafeCast.sol'; import '../interfaces/structs/PoolsharkStructs.sol'; import '../external/solady/LibClone.sol'; +/** + * @title PoolsharkRouter + * @notice The router for all limit and cover pools + * @author Poolshark + * @author @alphak3y + */ contract PoolsharkRouter is PoolsharkStructs, ILimitPoolMintRangeCallback, @@ -73,14 +79,14 @@ contract PoolsharkRouter is limitPoolFactory = limitPoolFactory_; coverPoolFactory = coverPoolFactory_; wethAddress = wethAddress_; - emit RouterDeployed( - address(this), - limitPoolFactory, - coverPoolFactory - ); + emit RouterDeployed(address(this), limitPoolFactory, coverPoolFactory); } - receive() external payable {} + receive() external payable { + if (msg.sender != wethAddress) { + require(false, 'PoolsharkRouter::ReceiveInvalid()'); + } + } /// @inheritdoc ILimitPoolSwapCallback function limitPoolSwapCallback( @@ -88,7 +94,9 @@ contract PoolsharkRouter is int256 amount1Delta, bytes calldata data ) external override { - PoolsharkStructs.LimitImmutables memory constants = ILimitPoolView(msg.sender).immutables(); + PoolsharkStructs.LimitImmutables memory constants = ILimitPoolView( + msg.sender + ).immutables(); // validate sender is a canonical limit pool canonicalLimitPoolsOnly(constants); @@ -101,14 +109,22 @@ contract PoolsharkRouter is if (constants.token0 == wethAddress && _data.wrapped) { wrapEth(uint256(-amount0Delta)); } else { - SafeTransfers.transferInto(constants.token0, _data.sender, uint256(-amount0Delta)); + SafeTransfers.transferInto( + constants.token0, + _data.sender, + uint256(-amount0Delta) + ); } } if (amount1Delta < 0) { if (constants.token1 == wethAddress && _data.wrapped) { wrapEth(uint256(-amount1Delta)); } else { - SafeTransfers.transferInto(constants.token1, _data.sender, uint256(-amount1Delta)); + SafeTransfers.transferInto( + constants.token1, + _data.sender, + uint256(-amount1Delta) + ); } } // transfer to swap caller @@ -132,27 +148,37 @@ contract PoolsharkRouter is int256 amount1Delta, bytes calldata data ) external override { - PoolsharkStructs.CoverImmutables memory constants = ICoverPool(msg.sender).immutables(); + PoolsharkStructs.CoverImmutables memory constants = ICoverPool( + msg.sender + ).immutables(); // validate sender is a canonical cover pool canonicalCoverPoolsOnly(constants); // decode original sender SwapCallbackData memory _data = abi.decode(data, (SwapCallbackData)); - + // transfer from swap caller if (amount0Delta < 0) { if (constants.token0 == wethAddress && _data.wrapped) { wrapEth(uint256(-amount0Delta)); } else { - SafeTransfers.transferInto(constants.token0, _data.sender, uint256(-amount0Delta)); + SafeTransfers.transferInto( + constants.token0, + _data.sender, + uint256(-amount0Delta) + ); } } if (amount1Delta < 0) { if (constants.token1 == wethAddress && _data.wrapped) { wrapEth(uint256(-amount1Delta)); } else { - SafeTransfers.transferInto(constants.token1, _data.sender, uint256(-amount1Delta)); + SafeTransfers.transferInto( + constants.token1, + _data.sender, + uint256(-amount1Delta) + ); } } if (amount0Delta > 0) { @@ -175,20 +201,41 @@ contract PoolsharkRouter is int256 amount1Delta, bytes calldata data ) external override { - PoolsharkStructs.LimitImmutables memory constants = ILimitPoolView(msg.sender).immutables(); + PoolsharkStructs.LimitImmutables memory constants = ILimitPoolView( + msg.sender + ).immutables(); // validate sender is a canonical limit pool canonicalLimitPoolsOnly(constants); // decode original sender - MintRangeCallbackData memory _data = abi.decode(data, (MintRangeCallbackData)); + MintRangeCallbackData memory _data = abi.decode( + data, + (MintRangeCallbackData) + ); - // transfer from mint caller + // transfer from swap caller if (amount0Delta < 0) { - SafeTransfers.transferInto(constants.token0, _data.sender, uint256(-amount0Delta)); + if (constants.token0 == wethAddress && _data.wrapped) { + wrapEth(uint256(-amount0Delta)); + } else { + SafeTransfers.transferInto( + constants.token0, + _data.sender, + uint256(-amount0Delta) + ); + } } if (amount1Delta < 0) { - SafeTransfers.transferInto(constants.token1, _data.sender, uint256(-amount1Delta)); + if (constants.token1 == wethAddress && _data.wrapped) { + wrapEth(uint256(-amount1Delta)); + } else { + SafeTransfers.transferInto( + constants.token1, + _data.sender, + uint256(-amount1Delta) + ); + } } } @@ -198,27 +245,40 @@ contract PoolsharkRouter is int256 amount1Delta, bytes calldata data ) external override { - PoolsharkStructs.LimitImmutables memory constants = ILimitPoolView(msg.sender).immutables(); + PoolsharkStructs.LimitImmutables memory constants = ILimitPoolView( + msg.sender + ).immutables(); // validate sender is a canonical limit pool canonicalLimitPoolsOnly(constants); // decode original sender - MintLimitCallbackData memory _data = abi.decode(data, (MintLimitCallbackData)); - + MintLimitCallbackData memory _data = abi.decode( + data, + (MintLimitCallbackData) + ); + // transfer from swap caller if (amount0Delta < 0) { if (constants.token0 == wethAddress && _data.wrapped) { wrapEth(uint256(-amount0Delta)); } else { - SafeTransfers.transferInto(constants.token0, _data.sender, uint256(-amount0Delta)); + SafeTransfers.transferInto( + constants.token0, + _data.sender, + uint256(-amount0Delta) + ); } } if (amount1Delta < 0) { if (constants.token1 == wethAddress && _data.wrapped) { wrapEth(uint256(-amount1Delta)); } else { - SafeTransfers.transferInto(constants.token1, _data.sender, uint256(-amount1Delta)); + SafeTransfers.transferInto( + constants.token1, + _data.sender, + uint256(-amount1Delta) + ); } } } @@ -229,27 +289,40 @@ contract PoolsharkRouter is int256 amount1Delta, bytes calldata data ) external override { - PoolsharkStructs.CoverImmutables memory constants = ICoverPool(msg.sender).immutables(); + PoolsharkStructs.CoverImmutables memory constants = ICoverPool( + msg.sender + ).immutables(); // validate sender is a canonical cover pool canonicalCoverPoolsOnly(constants); // decode original sender - MintCoverCallbackData memory _data = abi.decode(data, (MintCoverCallbackData)); + MintCoverCallbackData memory _data = abi.decode( + data, + (MintCoverCallbackData) + ); // transfer from swap caller if (amount0Delta < 0) { if (constants.token0 == wethAddress && _data.wrapped) { wrapEth(uint256(-amount0Delta)); } else { - SafeTransfers.transferInto(constants.token0, _data.sender, uint256(-amount0Delta)); + SafeTransfers.transferInto( + constants.token0, + _data.sender, + uint256(-amount0Delta) + ); } } if (amount1Delta < 0) { if (constants.token1 == wethAddress && _data.wrapped) { wrapEth(uint256(-amount1Delta)); } else { - SafeTransfers.transferInto(constants.token1, _data.sender, uint256(-amount1Delta)); + SafeTransfers.transferInto( + constants.token1, + _data.sender, + uint256(-amount1Delta) + ); } } } @@ -258,37 +331,41 @@ contract PoolsharkRouter is address[] memory pools, MintLimitParams[] memory params ) external payable { - if (pools.length != params.length) require(false, 'InputArrayLengthsMismatch()'); - for (uint i = 0; i < pools.length;) { - params[i].callbackData = abi.encode(MintLimitCallbackData({ - sender: msg.sender, - wrapped: msg.value > 0 - })); + if (pools.length != params.length) + require(false, 'InputArrayLengthsMismatch()'); + for (uint256 i = 0; i < pools.length; ) { + params[i].callbackData = abi.encode( + MintLimitCallbackData({ + sender: msg.sender, + wrapped: msg.value > 0 + }) + ); ILimitPool(pools[i]).mintLimit(params[i]); unchecked { ++i; } } - if (address(this).balance > 0) { - // return eth balance to msg.sender - SafeTransfers.transferOut(msg.sender, ethAddress, address(this).balance); - } + refundEth(); } function multiMintRange( address[] memory pools, MintRangeParams[] memory params ) external payable { - if (pools.length != params.length) require(false, 'InputArrayLengthsMismatch()'); - for (uint i = 0; i < pools.length;) { + if (pools.length != params.length) + require(false, 'InputArrayLengthsMismatch()'); + for (uint256 i = 0; i < pools.length; ) { address staker; { - MintRangeCallbackData memory callbackData = MintRangeCallbackData({ - sender: msg.sender, - recipient: params[i].to, - wrapped: msg.value > 0 - }); - staker = abi.decode(params[i].callbackData, (MintRangeInputData)).staker; + MintRangeCallbackData + memory callbackData = MintRangeCallbackData({ + sender: msg.sender, + recipient: params[i].to, + wrapped: msg.value > 0 + }); + staker = abi + .decode(params[i].callbackData, (MintRangeInputData)) + .staker; if (staker != address(0)) { params[i].to = staker; } @@ -296,11 +373,18 @@ contract PoolsharkRouter is } IRangePool(pools[i]).mintRange(params[i]); if (staker != address(0)) { - IRangeStaker(staker).stakeRange(StakeRangeParams({ - to: abi.decode(params[i].callbackData, (MintRangeCallbackData)).recipient, - pool: pools[i], - positionId: params[i].positionId - })); + IRangeStaker(staker).stakeRange( + StakeRangeParams({ + to: abi + .decode( + params[i].callbackData, + (MintRangeCallbackData) + ) + .recipient, + pool: pools[i], + positionId: params[i].positionId + }) + ); } // call to staking contract using positionId returned from mintRange // fees and staked position will go to params.to @@ -308,49 +392,45 @@ contract PoolsharkRouter is ++i; } } - if (address(this).balance > 0) { - // return eth balance to msg.sender - SafeTransfers.transferOut(msg.sender, ethAddress, address(this).balance); - } + refundEth(); } function multiMintCover( address[] memory pools, PoolsharkStructs.MintCoverParams[] memory params ) external payable { - if (pools.length != params.length) require(false, 'InputArrayLengthsMismatch()'); - for (uint i = 0; i < pools.length;) { - params[i].callbackData = abi.encode(MintCoverCallbackData({ - sender: msg.sender, - wrapped: msg.value > 0 - })); - try ICoverPool(pools[i]).mint(params[i]) { - } catch {} + if (pools.length != params.length) + require(false, 'InputArrayLengthsMismatch()'); + for (uint256 i = 0; i < pools.length; ) { + params[i].callbackData = abi.encode( + MintCoverCallbackData({ + sender: msg.sender, + wrapped: msg.value > 0 + }) + ); + try ICoverPool(pools[i]).mint(params[i]) {} catch {} unchecked { ++i; } } - if (address(this).balance > 0) { - // return eth balance to msg.sender - SafeTransfers.transferOut(msg.sender, ethAddress, address(this).balance); - } + refundEth(); } function multiQuote( address[] memory pools, QuoteParams[] memory params, - bool sortResults - ) external view returns ( - QuoteResults[] memory results - ) - { - if (pools.length != params.length) require(false, 'InputArrayLengthsMismatch()'); + bool sortResults + ) external view returns (QuoteResults[] memory results) { + if (pools.length != params.length) + require(false, 'InputArrayLengthsMismatch()'); if (sortResults) { // if sorting results check for matching params - for (uint i = 0; i < pools.length;) { + for (uint256 i = 0; i < pools.length; ) { if (i > 0) { - if (params[i].zeroForOne != params[0].zeroForOne) require (false, 'ZeroForOneParamMismatch()'); - if (params[i].exactIn != params[0].exactIn) require(false, 'ExactInParamMismatch()'); + if (params[i].zeroForOne != params[0].zeroForOne) + require(false, 'ZeroForOneParamMismatch()'); + if (params[i].exactIn != params[0].exactIn) + require(false, 'ExactInParamMismatch()'); /// @dev - amount and priceLimit values are allowed to be different } unchecked { @@ -359,7 +439,7 @@ contract PoolsharkRouter is } } results = new QuoteResults[](pools.length); - for (uint i = 0; i < pools.length;) { + for (uint256 i = 0; i < pools.length; ) { results[i].pool = pools[i]; ( results[i].amountIn, @@ -376,43 +456,52 @@ contract PoolsharkRouter is } } - function multiSwapSplit( - address[] memory pools, - SwapParams[] memory params - ) external payable { - if (pools.length != params.length) require(false, 'InputArrayLengthsMismatch()'); - for (uint i = 0; i < pools.length;) { + function multiSwapSplit(address[] memory pools, SwapParams[] memory params) + external + payable + { + if (pools.length != params.length) + require(false, 'InputArrayLengthsMismatch()'); + for (uint256 i = 0; i < pools.length; ) { if (i > 0) { - if (params[i].zeroForOne != params[0].zeroForOne) require (false, 'ZeroForOneParamMismatch()'); - if (params[i].exactIn != params[0].exactIn) require(false, 'ExactInParamMismatch()'); - if (params[i].amount != params[0].amount) require(false, 'AmountParamMisMatch()'); + if (params[i].zeroForOne != params[0].zeroForOne) + require(false, 'ZeroForOneParamMismatch()'); + if (params[i].exactIn != params[0].exactIn) + require(false, 'ExactInParamMismatch()'); + if (params[i].amount != params[0].amount) + require(false, 'AmountParamMisMatch()'); } unchecked { ++i; } } - for (uint i = 0; i < pools.length && params[0].amount > 0;) { + for (uint256 i = 0; i < pools.length && params[0].amount > 0; ) { // if msg.value > 0 we either need to wrap or unwrap the native gas token - params[i].callbackData = abi.encode(SwapCallbackData({ - sender: msg.sender, - recipient: params[i].to, - wrapped: msg.value > 0 - })); + params[i].callbackData = abi.encode( + SwapCallbackData({ + sender: msg.sender, + recipient: params[i].to, + wrapped: msg.value > 0 + }) + ); if (msg.value > 0) { IPool pool = IPool(pools[i]); - address tokenIn = params[i].zeroForOne ? pool.token0() : pool.token1(); - address tokenOut = params[i].zeroForOne ? pool.token1() : pool.token0(); + address tokenIn = params[i].zeroForOne + ? pool.token0() + : pool.token1(); + address tokenOut = params[i].zeroForOne + ? pool.token1() + : pool.token0(); if (tokenOut == wethAddress) { // send weth to router for unwrapping params[i].to = address(this); } else if (tokenIn != wethAddress) { - require (false, "NonNativeTokenPair()"); + require(false, 'NonNativeTokenPair()'); } } - ( - int256 amount0Delta, - int256 amount1Delta - ) = IPool(pools[i]).swap(params[i]); + (int256 amount0Delta, int256 amount1Delta) = IPool(pools[i]).swap( + params[i] + ); // if there is another pool to swap against if ((i + 1) < pools.length) { // calculate amount left and set for next call @@ -425,30 +514,29 @@ contract PoolsharkRouter is } else if (!params[0].zeroForOne && params[0].exactIn) { params[0].amount -= (-amount1Delta).toUint256().toUint128(); } - params[i+1].amount = params[0].amount; + params[i + 1].amount = params[0].amount; } unchecked { ++i; } } - if (address(this).balance > 0) { - // return eth balance to msg.sender - SafeTransfers.transferOut(msg.sender, ethAddress, address(this).balance); - } + refundEth(); } function multiSnapshotLimit( address[] memory pools, - SnapshotLimitParams[] memory params - ) external view returns( - uint128[] memory amountIns, - uint128[] memory amountOuts - ) { + SnapshotLimitParams[] memory params + ) + external + view + returns (uint128[] memory amountIns, uint128[] memory amountOuts) + { amountIns = new uint128[](pools.length); amountOuts = new uint128[](pools.length); - for (uint i = 0; i < pools.length;) { - if (pools[i] == address(0)) require(false, "InvalidPoolAddress()"); - (amountIns[i], amountOuts[i]) = ILimitPoolView(pools[i]).snapshotLimit(params[i]); + for (uint256 i = 0; i < pools.length; ) { + if (pools[i] == address(0)) require(false, 'InvalidPoolAddress()'); + (amountIns[i], amountOuts[i]) = ILimitPoolView(pools[i]) + .snapshotLimit(params[i]); unchecked { ++i; } @@ -459,15 +547,9 @@ contract PoolsharkRouter is ILimitPoolFactory.LimitPoolParams memory params, MintRangeParams[] memory mintRangeParams, MintLimitParams[] memory mintLimitParams - ) external payable returns ( - address pool, - address poolToken - ) { + ) external payable returns (address pool, address poolToken) { // check if pool exists - ( - pool, - poolToken - ) = ILimitPoolFactory(limitPoolFactory).getLimitPool( + (pool, poolToken) = ILimitPoolFactory(limitPoolFactory).getLimitPool( params.tokenIn, params.tokenOut, params.swapFee, @@ -475,100 +557,95 @@ contract PoolsharkRouter is ); // create if pool doesn't exist if (pool == address(0)) { - ( - pool, - poolToken - ) = ILimitPoolFactory(limitPoolFactory).createLimitPool( - params - ); + (pool, poolToken) = ILimitPoolFactory(limitPoolFactory) + .createLimitPool(params); } // mint initial range positions - for (uint i = 0; i < mintRangeParams.length;) { + for (uint256 i = 0; i < mintRangeParams.length; ) { address staker; { mintRangeParams[i].positionId = 0; - MintRangeCallbackData memory callbackData = MintRangeCallbackData({ - sender: msg.sender, - recipient: mintRangeParams[i].to, - wrapped: msg.value > 0 - }); - staker = abi.decode(mintRangeParams[i].callbackData, (MintRangeInputData)).staker; + MintRangeCallbackData + memory callbackData = MintRangeCallbackData({ + sender: msg.sender, + recipient: mintRangeParams[i].to, + wrapped: msg.value > 0 + }); + staker = abi + .decode( + mintRangeParams[i].callbackData, + (MintRangeInputData) + ) + .staker; if (staker != address(0)) { mintRangeParams[i].to = staker; } mintRangeParams[i].callbackData = abi.encode(callbackData); } - try IRangePool(pool).mintRange(mintRangeParams[i]) { - } catch {} + try IRangePool(pool).mintRange(mintRangeParams[i]) {} catch {} if (staker != address(0)) { - IRangeStaker(staker).stakeRange(StakeRangeParams({ - to: abi.decode(mintRangeParams[i].callbackData, (MintRangeCallbackData)).recipient, - pool: pool, - positionId: 0 - })); + IRangeStaker(staker).stakeRange( + StakeRangeParams({ + to: abi + .decode( + mintRangeParams[i].callbackData, + (MintRangeCallbackData) + ) + .recipient, + pool: pool, + positionId: 0 + }) + ); } unchecked { ++i; } } // mint initial limit positions - for (uint i = 0; i < mintLimitParams.length;) { + for (uint256 i = 0; i < mintLimitParams.length; ) { mintLimitParams[i].positionId = 0; - mintLimitParams[i].callbackData = abi.encode(MintLimitCallbackData({ - sender: msg.sender, - wrapped: msg.value > 0 - })); + mintLimitParams[i].callbackData = abi.encode( + MintLimitCallbackData({ + sender: msg.sender, + wrapped: msg.value > 0 + }) + ); ILimitPool(pool).mintLimit(mintLimitParams[i]); unchecked { ++i; } } - if (address(this).balance > 0) { - // send remaining eth to msg.sender - SafeTransfers.transferOut(msg.sender, ethAddress, address(this).balance); - } + refundEth(); } function createCoverPoolAndMint( ICoverPoolFactory.CoverPoolParams memory params, MintCoverParams[] memory mintCoverParams - ) external payable returns ( - address pool, - address poolToken - ) { + ) external payable returns (address pool, address poolToken) { // check if pool exists - ( - pool, - poolToken - ) = ICoverPoolFactory(coverPoolFactory).getCoverPool( + (pool, poolToken) = ICoverPoolFactory(coverPoolFactory).getCoverPool( params ); // create if pool doesn't exist if (pool == address(0)) { - ( - pool, - poolToken - ) = ICoverPoolFactory(coverPoolFactory).createCoverPool( - params - ); + (pool, poolToken) = ICoverPoolFactory(coverPoolFactory) + .createCoverPool(params); } // mint initial cover positions - for (uint i = 0; i < mintCoverParams.length;) { + for (uint256 i = 0; i < mintCoverParams.length; ) { mintCoverParams[i].positionId = 0; - mintCoverParams[i].callbackData = abi.encode(MintCoverCallbackData({ - sender: msg.sender, - wrapped: msg.value > 0 - })); - try ICoverPool(pool).mint(mintCoverParams[i]) { - } catch {} + mintCoverParams[i].callbackData = abi.encode( + MintCoverCallbackData({ + sender: msg.sender, + wrapped: msg.value > 0 + }) + ); + try ICoverPool(pool).mint(mintCoverParams[i]) {} catch {} unchecked { ++i; } } - if (address(this).balance > 0) { - // send remaining eth to msg.sender - SafeTransfers.transferOut(msg.sender, ethAddress, address(this).balance); - } + refundEth(); } struct SortQuoteResultsLocals { @@ -584,28 +661,34 @@ contract PoolsharkRouter is function sortQuoteResults( QuoteParams[] memory params, QuoteResults[] memory results - ) internal pure returns ( - QuoteResults[] memory - ) { + ) internal pure returns (QuoteResults[] memory) { SortQuoteResultsLocals memory locals; locals.sortedResults = new QuoteResults[](results.length); locals.sortedFlags = new bool[](results.length); locals.emptyResults = 0; - for (uint sorted = 0; sorted < results.length;) { + for (uint256 sorted = 0; sorted < results.length; ) { // if exactIn, sort by most output // if exactOut, sort by most output then least input - locals.sortAmount = params[0].exactIn ? int256(0) : type(int256).max; + locals.sortAmount = params[0].exactIn + ? int256(0) + : type(int256).max; locals.sortIndex = type(uint256).max; - for (uint index = 0; index < results.length;) { + for (uint256 index = 0; index < results.length; ) { // check if result already sorted if (!locals.sortedFlags[index]) { if (params[0].exactIn) { - if (results[index].amountOut > 0 && results[index].amountOut >= locals.sortAmount) { + if ( + results[index].amountOut > 0 && + results[index].amountOut >= locals.sortAmount + ) { locals.sortIndex = index; locals.sortAmount = results[index].amountOut; } } else { - if (results[index].amountIn > 0 && results[index].amountIn <= locals.sortAmount) { + if ( + results[index].amountIn > 0 && + results[index].amountIn <= locals.sortAmount + ) { locals.sortIndex = index; locals.sortAmount = results[index].amountIn; } @@ -618,10 +701,17 @@ contract PoolsharkRouter is } if (locals.sortIndex != type(uint256).max) { // add the sorted result - locals.sortedResults[sorted].pool = results[locals.sortIndex].pool; - locals.sortedResults[sorted].amountIn = results[locals.sortIndex].amountIn; - locals.sortedResults[sorted].amountOut = results[locals.sortIndex].amountOut; - locals.sortedResults[sorted].priceAfter = results[locals.sortIndex].priceAfter; + locals.sortedResults[sorted].pool = results[locals.sortIndex] + .pool; + locals.sortedResults[sorted].amountIn = results[ + locals.sortIndex + ].amountIn; + locals.sortedResults[sorted].amountOut = results[ + locals.sortIndex + ].amountOut; + locals.sortedResults[sorted].priceAfter = results[ + locals.sortIndex + ].priceAfter; // indicate this result was already sorted locals.sortedFlags[locals.sortIndex] = true; @@ -635,12 +725,15 @@ contract PoolsharkRouter is } // if any results were empty, prune them if (locals.emptyResults > 0) { - locals.prunedResults = new QuoteResults[](results.length - locals.emptyResults); + locals.prunedResults = new QuoteResults[]( + results.length - locals.emptyResults + ); locals.prunedIndex = 0; - for (uint sorted = 0; sorted < results.length;) { + for (uint256 sorted = 0; sorted < results.length; ) { // empty results are omitted if (locals.sortedResults[sorted].pool != address(0)) { - locals.prunedResults[locals.prunedIndex] = locals.sortedResults[sorted]; + locals.prunedResults[locals.prunedIndex] = locals + .sortedResults[sorted]; unchecked { ++locals.prunedIndex; } @@ -655,13 +748,19 @@ contract PoolsharkRouter is return locals.prunedResults; } - function multiCall( - address[] memory pools, - SwapParams[] memory params - ) external { - if (pools.length != params.length) require(false, 'InputArrayLengthsMismatch()'); - for (uint i = 0; i < pools.length;) { - params[i].callbackData = abi.encode(SwapCallbackData({sender: msg.sender, recipient: params[i].to, wrapped: true})); + function multiCall(address[] memory pools, SwapParams[] memory params) + external + { + if (pools.length != params.length) + require(false, 'InputArrayLengthsMismatch()'); + for (uint256 i = 0; i < pools.length; ) { + params[i].callbackData = abi.encode( + SwapCallbackData({ + sender: msg.sender, + recipient: params[i].to, + wrapped: true + }) + ); ICoverPool(pools[i]).swap(params[i]); unchecked { ++i; @@ -669,16 +768,84 @@ contract PoolsharkRouter is } } + function deployTge(address tgePool, address staker) external { + // read pool price + RangePoolState memory tgePoolState; + (tgePoolState, , , , , , ) = IPool(tgePool).globalState(); + uint160 expectedPoolPrice = 2172618421097231267834892073346; + if (tgePoolState.price < expectedPoolPrice) { + // move pool price up if below + SwapParams memory swapParams = SwapParams({ + to: msg.sender, + priceLimit: expectedPoolPrice, + amount: 1000e18, + exactIn: true, + zeroForOne: false, + callbackData: abi.encode( + SwapCallbackData({ + sender: msg.sender, + recipient: msg.sender, + wrapped: false + }) + ) + }); + IPool(tgePool).swap(swapParams); + } else if (tgePoolState.price > expectedPoolPrice) { + // move pool price down if above + SwapParams memory swapParams = SwapParams({ + to: msg.sender, + priceLimit: expectedPoolPrice, + amount: 1e18, + exactIn: true, + zeroForOne: true, + callbackData: abi.encode( + SwapCallbackData({ + sender: msg.sender, + recipient: msg.sender, + wrapped: false + }) + ) + }); + IPool(tgePool).swap(swapParams); + } + // read pool price + (tgePoolState, , , , , , ) = IPool(tgePool).globalState(); + if (tgePoolState.price != expectedPoolPrice) + require(false, 'PoolPriceMismatch()'); + MintRangeCallbackData memory callbackData = MintRangeCallbackData({ + sender: msg.sender, + recipient: staker != address(0) ? staker : msg.sender, + wrapped: false + }); + MintRangeParams memory mintRangeParams = MintRangeParams({ + to: staker, + lower: 54000, + upper: 77040, + positionId: 0, + amount0: 39168000000000000000, + amount1: 33000000000000000000000, + callbackData: abi.encode(callbackData) + }); + IRangePool(tgePool).mintRange(mintRangeParams); + if (staker != address(0)) { + IRangeStaker(staker).stakeRange( + StakeRangeParams({to: msg.sender, pool: tgePool, positionId: 0}) + ); + } + } + function canonicalLimitPoolsOnly( PoolsharkStructs.LimitImmutables memory constants ) private view { // generate key for pool - bytes32 key = keccak256(abi.encode( - constants.poolImpl, - constants.token0, - constants.token1, - constants.swapFee - )); + bytes32 key = keccak256( + abi.encode( + constants.poolImpl, + constants.token0, + constants.token1, + constants.swapFee + ) + ); // compute address address predictedAddress = LibClone.predictDeterministicAddress( @@ -689,21 +856,24 @@ contract PoolsharkRouter is ); // revert on sender mismatch - if (msg.sender != predictedAddress) require(false, 'InvalidCallerAddress()'); + if (msg.sender != predictedAddress) + require(false, 'InvalidCallerAddress()'); } function canonicalCoverPoolsOnly( PoolsharkStructs.CoverImmutables memory constants ) private view { // generate key for pool - bytes32 key = keccak256(abi.encode( - constants.token0, - constants.token1, - constants.source, - constants.inputPool, - constants.tickSpread, - constants.twapLength - )); + bytes32 key = keccak256( + abi.encode( + constants.token0, + constants.token1, + constants.source, + constants.inputPool, + constants.tickSpread, + constants.twapLength + ) + ); // compute address address predictedAddress = LibClone.predictDeterministicAddress( @@ -714,13 +884,17 @@ contract PoolsharkRouter is ); // revert on sender mismatch - if (msg.sender != predictedAddress) require(false, 'InvalidCallerAddress()'); + if (msg.sender != predictedAddress) + require(false, 'InvalidCallerAddress()'); } - function encodeLimit( - LimitImmutables memory constants - ) private pure returns (bytes memory) { - return abi.encodePacked( + function encodeLimit(LimitImmutables memory constants) + private + pure + returns (bytes memory) + { + return + abi.encodePacked( constants.owner, constants.token0, constants.token1, @@ -730,12 +904,14 @@ contract PoolsharkRouter is constants.genesisTime, constants.tickSpacing, constants.swapFee - ); + ); } - function encodeCover( - CoverImmutables memory constants - ) private pure returns (bytes memory) { + function encodeCover(CoverImmutables memory constants) + private + pure + returns (bytes memory) + { bytes memory value1 = abi.encodePacked( constants.owner, constants.token0, @@ -766,7 +942,8 @@ contract PoolsharkRouter is function wrapEth(uint256 amount) private { // wrap necessary amount of WETH IWETH9 weth = IWETH9(wethAddress); - if (amount > address(this).balance) require(false, 'WrapEth::LowEthBalance()'); + if (amount > address(this).balance) + require(false, 'WrapEth::LowEthBalance()'); weth.deposit{value: amount}(); // transfer weth into pool SafeTransfers.transferOut(msg.sender, wethAddress, amount); @@ -779,4 +956,18 @@ contract PoolsharkRouter is // send balance to recipient SafeTransfers.transferOut(recipient, ethAddress, amount); } -} \ No newline at end of file + + function refundEth() private { + if (address(this).balance > 0) { + if (address(this).balance >= msg.value) { + SafeTransfers.transferOut(msg.sender, ethAddress, msg.value); + } else { + SafeTransfers.transferOut( + msg.sender, + ethAddress, + address(this).balance + ); + } + } + } +} diff --git a/contracts/utils/PositionERC1155.sol b/contracts/utils/PositionERC1155.sol index bfc5ce6b..f441de75 100644 --- a/contracts/utils/PositionERC1155.sol +++ b/contracts/utils/PositionERC1155.sol @@ -1,25 +1,20 @@ -// SPDX-License-Identifier: MIT +// SPDX-License-Identifier: SSPL-1.0 -pragma solidity 0.8.13; +pragma solidity 0.8.18; import '../interfaces/IPool.sol'; import '../base/storage/PositionERC1155Immutables.sol'; -import "../interfaces/IPositionERC1155.sol"; +import '../interfaces/IPositionERC1155.sol'; import '../external/solady/LibClone.sol'; import '../libraries/utils/String.sol'; import '../libraries/utils/SafeTransfers.sol'; import '../libraries/utils/PositionTokens.sol'; -contract PositionERC1155 is - IPositionERC1155, - PositionERC1155Immutables -{ +contract PositionERC1155 is IPositionERC1155, PositionERC1155Immutables { address public immutable factory; address public immutable original; - constructor( - address factory_ - ) { + constructor(address factory_) { factory = factory_; original = address(this); } @@ -30,8 +25,8 @@ contract PositionERC1155 is /// @dev owner => spender => approved mapping(address => mapping(address => bool)) private _spenderApprovals; - /// @dev token id => total supply - mapping(uint256 => uint256) private _totalSupplyById; + /// @dev total supply for all token ids + uint256 public totalSupply; // eth address for safe withdrawal address public constant ethAddress = address(0); @@ -39,31 +34,54 @@ contract PositionERC1155 is modifier onlyCanonicalClones( PoolsharkStructs.LimitImmutables memory constants ) { - if(!_onlyCanonicalPools(constants)) require (false, 'CanoncialPoolsOnly()'); - if(!_onlyCanonicalPoolTokens(constants)) require (false, 'CanoncialPoolTokensOnly()'); + if (!_onlyCanonicalPools(constants)) + require(false, 'CanoncialPoolsOnly()'); + if (!_onlyCanonicalPoolTokens(constants)) + require(false, 'CanoncialPoolTokensOnly()'); _; } modifier checkApproval(address _from, address _spender) { if (_from != _spender) - if(!_isApprovedForAll(_from, _spender)) - require(false, string.concat('SpenderNotApproved(', String.from(_from), ', ', String.from(_spender), ')')); + if (!_isApprovedForAll(_from, _spender)) + require( + false, + string.concat( + 'SpenderNotApproved(', + String.from(_from), + ', ', + String.from(_spender), + ')' + ) + ); _; } modifier checkAddresses(address _from, address _to) { - if (_from == address(0) || _to == address(0)) require(false, 'TransferFromOrToAddress0()'); + if (_from == address(0) || _to == address(0)) + require(false, 'TransferFromOrToAddress0()'); if (_from == _to) require(false, 'TransferToSelf()'); _; } modifier checkLength(uint256 _lengthA, uint256 _lengthB) { - if (_lengthA != _lengthB) require(false, string.concat('LengthMismatch(', String.from(_lengthA), ', ', String.from(_lengthB), ')')); + if (_lengthA != _lengthB) + require( + false, + string.concat( + 'LengthMismatch(', + String.from(_lengthA), + ', ', + String.from(_lengthB), + ')' + ) + ); _; } modifier checkERC1155Support(address recipient) { - if (!_verifyERC1155Support(recipient)) require(false, 'ERC1155NotSupported()'); + if (!_verifyERC1155Support(recipient)) + require(false, 'ERC1155NotSupported()'); _; } @@ -72,10 +90,7 @@ contract PositionERC1155 is uint256 _id, uint256 _amount, PoolsharkStructs.LimitImmutables memory constants - ) external - onlyCanonicalClones(constants) - checkERC1155Support(_account) - { + ) external onlyCanonicalClones(constants) checkERC1155Support(_account) { _mint(_account, _id, _amount); } @@ -84,13 +99,15 @@ contract PositionERC1155 is uint256 _id, uint256 _amount, PoolsharkStructs.LimitImmutables memory constants - ) external - onlyCanonicalClones(constants) - { + ) external onlyCanonicalClones(constants) { _burn(_account, _id, _amount); } - function setApprovalForAll(address _spender, bool _approved) public virtual override { + function setApprovalForAll(address _spender, bool _approved) + public + virtual + override + { _setApprovalForAll(msg.sender, _spender, _approved); } @@ -99,7 +116,10 @@ contract PositionERC1155 is address _to, uint256 _id, uint256 _amount - ) public virtual override + ) + public + virtual + override checkAddresses(_from, _to) checkApproval(_from, msg.sender) checkERC1155Support(_to) @@ -114,7 +134,10 @@ contract PositionERC1155 is address _to, uint256[] calldata _ids, uint256[] calldata _amounts - ) public virtual override + ) + public + virtual + override checkLength(_ids.length, _amounts.length) checkAddresses(_from, _to) checkApproval(_from, msg.sender) @@ -131,19 +154,28 @@ contract PositionERC1155 is function withdrawEth( address recipient, PoolsharkStructs.LimitImmutables memory constants - ) external - onlyCanonicalClones(constants) - { + ) external onlyCanonicalClones(constants) { SafeTransfers.transferOut(recipient, ethAddress, address(this).balance); } - function isApprovedForAll(address _owner, address _spender) public view virtual override returns (bool) { + function isApprovedForAll(address _owner, address _spender) + public + view + virtual + override + returns (bool) + { return _isApprovedForAll(_owner, _spender); } - function supportsInterface(bytes4 interfaceID) external pure returns (bool) { - return interfaceID == 0x01ffc9a7 || // ERC-165 support - interfaceID == 0xd9b67a26; // ERC-1155 support + function supportsInterface(bytes4 interfaceID) + external + pure + returns (bool) + { + return + interfaceID == 0x01ffc9a7 || // ERC-165 support + interfaceID == 0xd9b67a26; // ERC-1155 support } function name() public pure virtual override returns (string memory) { @@ -154,18 +186,24 @@ contract PositionERC1155 is return Bytes.bytes32ToString(tokenSymbol()); } - function totalSupply(uint256 _id) public view virtual override returns (uint256) { - return _totalSupplyById[_id]; - } - - function balanceOf(address _account, uint256 _id) public view virtual override returns (uint256) { + function balanceOf(address _account, uint256 _id) + public + view + virtual + override + returns (uint256) + { return _tokenBalances[_id][_account]; } function balanceOfBatch( address[] calldata _accounts, uint256[] calldata _ids - ) public view virtual override + ) + public + view + virtual + override checkLength(_accounts.length, _ids.length) returns (uint256[] memory batchBalances) { @@ -184,9 +222,9 @@ contract PositionERC1155 is ) internal virtual { if (_account == address(0)) require(false, 'MintToAddress0()'); _beforeTokenTransfer(address(0), _account, _id, _amount); - _totalSupplyById[_id] += _amount; uint256 _accountBalance = _tokenBalances[_id][_account]; unchecked { + totalSupply += _amount; _tokenBalances[_id][_account] = _accountBalance + _amount; } emit TransferSingle(msg.sender, address(0), _account, _id, _amount); @@ -199,11 +237,23 @@ contract PositionERC1155 is ) internal virtual { if (_account == address(0)) require(false, 'BurnFromAddress0()'); uint256 _accountBalance = _tokenBalances[_id][_account]; - if (_accountBalance < _amount) require(false, string.concat('BurnExceedsBalance(', String.from(_account), ', ', String.from(_id), ', ', String.from(_amount), ')')); + if (_accountBalance < _amount) + require( + false, + string.concat( + 'BurnExceedsBalance(', + String.from(_account), + ', ', + String.from(_id), + ', ', + String.from(_amount), + ')' + ) + ); _beforeTokenTransfer(_account, address(0), _id, _amount); unchecked { + totalSupply -= _amount; _tokenBalances[_id][_account] = _accountBalance - _amount; - _totalSupplyById[_id] -= _amount; } emit TransferSingle(msg.sender, _account, address(0), _id, _amount); } @@ -215,7 +265,19 @@ contract PositionERC1155 is uint256 _amount ) internal virtual { uint256 _fromBalance = _tokenBalances[_id][_from]; - if (_fromBalance < _amount) require(false, string.concat('TransferExceedsBalance(', String.from(_from), ', ', String.from(_id), ', ', String.from(_amount), ')')); + if (_fromBalance < _amount) + require( + false, + string.concat( + 'TransferExceedsBalance(', + String.from(_from), + ', ', + String.from(_id), + ', ', + String.from(_amount), + ')' + ) + ); _beforeTokenTransfer(_from, _to, _id, _amount); unchecked { _tokenBalances[_id][_from] = _fromBalance - _amount; @@ -231,12 +293,21 @@ contract PositionERC1155 is address _spender, bool _approved ) internal virtual { - if (_owner == _spender) require(false, string.concat('SelfApproval(', String.from(_owner), ')')); + if (_owner == _spender) + require( + false, + string.concat('SelfApproval(', String.from(_owner), ')') + ); _spenderApprovals[_owner][_spender] = _approved; emit ApprovalForAll(_owner, _spender, _approved); } - function _isApprovedForAll(address _owner, address _spender) internal view virtual returns (bool) { + function _isApprovedForAll(address _owner, address _spender) + internal + view + virtual + returns (bool) + { return _owner == _spender || _spenderApprovals[_owner][_spender]; } @@ -252,20 +323,19 @@ contract PositionERC1155 is PoolsharkStructs.LimitImmutables memory constants ) private view returns (bool) { // generate key for pool - bytes32 key = keccak256(abi.encode( - constants.poolImpl, - constants.token0, - constants.token1, - constants.swapFee - )); + bytes32 key = keccak256( + abi.encode( + constants.poolImpl, + constants.token0, + constants.token1, + constants.swapFee + ) + ); // compute address address predictedAddress = LibClone.predictDeterministicAddress( original, - abi.encodePacked( - tokenName(), - tokenSymbol() - ), + abi.encodePacked(tokenName(), tokenSymbol()), key, factory ); @@ -279,12 +349,14 @@ contract PositionERC1155 is PoolsharkStructs.LimitImmutables memory constants ) private view returns (bool) { // generate key for pool - bytes32 key = keccak256(abi.encode( - constants.poolImpl, - constants.token0, - constants.token1, - constants.swapFee - )); + bytes32 key = keccak256( + abi.encode( + constants.poolImpl, + constants.token0, + constants.token1, + constants.swapFee + ) + ); // compute address address predictedAddress = LibClone.predictDeterministicAddress( @@ -312,14 +384,20 @@ contract PositionERC1155 is /// @notice Return if the `_target` contract supports ERC-1155 interface /// @param _target The address of the contract /// @return supported Whether the contract is supported (true) or not (false) - function _verifyERC1155Support(address _target) private view returns (bool supported) { + function _verifyERC1155Support(address _target) + private + view + returns (bool supported) + { if (_target.code.length == 0) return true; bytes memory encodedParams = abi.encodeWithSelector( IERC165.supportsInterface.selector, bytes4(0xd9b67a26) // ERC-1155 support ); - (bool success, bytes memory result) = _target.staticcall{gas: 30_000}(encodedParams); + (bool success, bytes memory result) = _target.staticcall{gas: 30_000}( + encodedParams + ); if (result.length < 32) return false; return success && abi.decode(result, (bool)); } -} \ No newline at end of file +} diff --git a/contracts/utils/TickQuoter.sol b/contracts/utils/TickQuoter.sol new file mode 100644 index 00000000..cb833008 --- /dev/null +++ b/contracts/utils/TickQuoter.sol @@ -0,0 +1,191 @@ +// SPDX-License-Identifier: SSPL-1.0 +pragma solidity 0.8.18; + +import '../interfaces/limit/ILimitPoolView.sol'; +import '../interfaces/limit/ILimitPoolStorageView.sol'; +import '../interfaces/structs/PoolsharkStructs.sol'; +import '../libraries/math/ConstantProduct.sol'; +import '../external/solady/LibClone.sol'; + +/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#%@@@@%@@@@%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@%%%%%%@@%==========+++**#@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@@@@@@@@%%##%%%@@%%%@@%%*-=====+++**#**+*#*#*#@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@=+===========+#@@@@@%#@%%%%%%*#%%%%%##%@@@@@@@@#****++#%#**#%=%@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@+-==+++***++++=++@@%%%%%%%%*****#%#*#@%@@@@@@@@@@@@@%#+#%#*##*=#@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@+=+*******+++%%##%%####++++++**@@%%@@@@@@@@@@@@@@@###*#%#+==@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@%=**##**+#%###########+====*@%@@%%@@@@@@@@@@@@@%###%@+%=+=*@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@#%%####*##*##*#**#**+=+=-=*%%%###%@@@@@@@@@@@####@#=====+*@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@#%#***#***#*****=*+=+==--=@@%#**###@@@@@@@@@@@###@@%=+++=++#@@@@*@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@##*********+***##++*+#%@*%@@@@@####@@@@#=%@@@@@@@@@@@+==++=+=%%#+*@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@##*****+**+*********++=%%%@@@@@@@@@*@@@%===@@@@@@@@@*===++=+++**#%#@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@#**++++*+++++===+***+==#%%#@@@@@@@@*##@*===+%@@@@%+==+=+++++++*%@%%@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@**++++++++====----+++====+###@@@@@@@%#%%*==++*+#+===+++++++++++*%%@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%###*++++++====---=*@@@@+===-%@**%@@@@@@@#%%%===*+****+++*++++*+*+*#@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@*+++*+++++===--=-*@@@@@@@@=--=@@@#%@@@@@@@@#%%*++++**#+++*++**++***#*@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@%===+++++=+++===*@@@@@@@@@@=-=%@@@@@%#@@@@@@@*===*++=++*++++****+**#%+@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@*=+@@#==+==+=-=@@@%%@@@@@@@@@@@@#***%%@@@@%+===+++*+*******+**++*#%*#@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@======-==#***%#@@@@@@@@@@@##%%%%%%@#====****+*****#**###*+++#*@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%-===*=++**###@@@@@@@@@@@@@+#%%%%@*======+*********###+++++++++%@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@%-=+#%%+*###%@@@@@@@@@@@@@@@#%#@%++=+++===**#*#****#%=====+++++++%@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@-=#****####@@@@@@@@@@@@@@@@#%@*++*#++++=+#*#####*#%%==============*@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@*=#**+*###@@@@@@@@@@@@@@@@@#%+**###**+++######%%#+-==--=-=------======#@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@++#++++*##%@@@@@@@@@@@@@@@%+*#%%######*##%#+=-=#@@@@@*----------------*@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@%+#*+++==+***#@@@@@@@@@@@%#%%####%%#*+#*++%@@@@@@@@@@@@@%+=======*@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@*+**+========------=-=%@#%%%%%%%%%%%%#%#%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@#=====-==-------=#@@@@########%%%%%%##++#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%##%%@@@@@@@@@@@@%#######%%#*+=++@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#*####++++=@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%##**+=#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@***%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ +@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/ + +/** + * @title TickQuoter + * @notice A contract to query tick data + * @author Poolshark + * @author @alphak3y + */ +contract TickQuoter is PoolsharkStructs { + address public immutable limitPoolFactory; + + struct TickData { + /** + * @custom:field tick + * @notice The index of the tick + */ + int24 tick; + + /** + * @custom:field liquidityDelta + * @notice The +/- liquidity change at the tick + * @notice Delta applied for upward crosses + * @notice Opposite delta applied for downward crosses + */ + int128 liquidityDelta; + + /** + * @custom:field liquidityAbsolute + * @notice The absolute value of liquidity at the tick + */ + uint128 liquidityAbsolute; + } + + constructor( + address limitPoolFactory_ + ) { + limitPoolFactory = limitPoolFactory_; + } + + function getTickDataInWord(address pool, int16 tickBitmapIndex) + public + view + returns (TickData[] memory populatedTicks) + { + // read constants from pool + LimitImmutables memory constants = ILimitPoolView(pool).immutables(); + + // validate address is a canonical limit pool + canonicalLimitPoolsOnly(pool, constants); + + int16 tickSpacing = constants.tickSpacing; + int24 startTick = (int24(tickBitmapIndex) << 8) * (tickSpacing / 2); + uint8 ticksCount; + + // array for tick data found; max size of 256 + TickData[] memory foundTicks = new TickData[](256); + + for (int24 i = 0; i < 256;) { + // offset currentTick from startTick + int24 currentTick = startTick + i * (tickSpacing / 2); + + // read tick from storage + RangeTick memory rangeTick; + LimitTick memory limitTick; + (rangeTick, limitTick) = ILimitPoolStorageView(pool).ticks( + currentTick + ); + + // check for non-zero liquidity + if (rangeTick.liquidityAbsolute + limitTick.liquidityAbsolute > 0) { + // push active tick to array + foundTicks[ticksCount] = TickData({ + tick: currentTick, + liquidityDelta: rangeTick.liquidityDelta + + limitTick.liquidityDelta, + liquidityAbsolute: rangeTick.liquidityAbsolute + + limitTick.liquidityAbsolute + }); + unchecked { + // count number of active ticks + ++ticksCount; + } + } + unchecked { + ++i; + } + } + + // resize array based on ticksCount + populatedTicks = new TickData[](ticksCount); + + // push tick data to returned array + for (uint256 i; i < ticksCount; ) { + populatedTicks[i] = foundTicks[ticksCount - i - 1]; + unchecked { + ++i; + } + } + } + + function canonicalLimitPoolsOnly( + address pool, + PoolsharkStructs.LimitImmutables memory constants + ) private view { + // generate key for pool + bytes32 key = keccak256( + abi.encode( + constants.poolImpl, + constants.token0, + constants.token1, + constants.swapFee + ) + ); + + // compute address + address predictedAddress = LibClone.predictDeterministicAddress( + constants.poolImpl, + encodeLimit(constants), + key, + limitPoolFactory + ); + + // revert on sender mismatch + if (pool != predictedAddress) + require(false, 'InvalidPoolAddress()'); + } + + function encodeLimit(LimitImmutables memory constants) + private + pure + returns (bytes memory) + { + return + abi.encodePacked( + constants.owner, + constants.token0, + constants.token1, + constants.poolToken, + constants.bounds.min, + constants.bounds.max, + constants.genesisTime, + constants.tickSpacing, + constants.swapFee + ); + } +} \ No newline at end of file diff --git a/hardhat.config.ts b/hardhat.config.ts index 896e2777..2486caaf 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -14,7 +14,7 @@ const config: HardhatUserConfig = { solidity: { compilers: [ { - version: '0.8.13', + version: '0.8.18', settings: { optimizer: { enabled: true, @@ -39,10 +39,23 @@ const config: HardhatUserConfig = { accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [], timeout: 60000, }, + arb_sepolia: { + chainId: 421614, + gasPrice: 15_000_000_000, + url: process.env.ARBITRUM_SEPOLIA_URL || '', + accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [], + timeout: 60000, + }, + arb_one: { + chainId: 42161, + url: process.env.ARBITRUM_ONE_URL || '', + accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [], + timeout: 60000, + }, scrollSepolia: { chainId: 534351, + gasPrice: 2_000_000_000, url: "https://sepolia-rpc.scroll.io/" || "", - gasPrice: 1_500_000_000, accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [], }, op_goerli: { @@ -55,18 +68,28 @@ const config: HardhatUserConfig = { }, etherscan: { apiKey: { + arbitrumOne: process.env.ARBITRUM_ONE_API_KEY, arbitrumGoerli: process.env.ARBITRUM_GOERLI_API_KEY, - scrollSepolia: 'D62920783A4311EE9D6600155D570C742E', + arb_sepolia: process.env.ARBITRUM_SEPOLIA_API_KEY, + scrollSepolia: process.env.SCROLL_SEPOLIA_API_KEY, }, customChains: [ { network: 'scrollSepolia', chainId: 534351, urls: { - apiURL: 'https://api-sepolia.scrollscan.dev/api', - browserURL: 'https://api-sepolia.scrollscan.dev', + apiURL: 'https://api-sepolia.scrollscan.com/api', + browserURL: 'https://sepolia.scrollscan.com/', }, }, + { + network: 'arb_sepolia', + chainId: 421614, + urls: { + apiURL: 'https://api-sepolia.arbiscan.io/api', + browserURL: 'https://sepolia.arbiscan.io/', + }, + }, ], }, } diff --git a/package.json b/package.json index 4cd0ec25..9eb024a0 100644 --- a/package.json +++ b/package.json @@ -7,9 +7,11 @@ "clean": "rm -rf ./artifacts ./cache ./typechain; npx hardhat clean", "compile": "npx hardhat compile", "test": "npx hardhat test", - "deploy": "rm -rf ./artifacts ./cache ./typechain; npx hardhat clean; npx hardhat deploy-limitpools --network arb_goerli;", + "deploy-test": "rm -rf ./artifacts ./cache ./typechain; npx hardhat clean; npx hardhat compile; npx hardhat deploy-limitpools --network arb_goerli;", + "deploy-main": "npx hardhat deploy-limitpools --network arb_one", "verify": "npx hardhat verify-contracts", - "all": "yarn clean; yarn compile; yarn test; yarn deploy;" + "all": "yarn clean; yarn compile; yarn test; yarn deploy;", + "prettier": "prettier --write --plugin=prettier-plugin-solidity contracts/**/*.sol" }, "devDependencies": { "@nomicfoundation/hardhat-network-helpers": "1.0.8", @@ -40,8 +42,8 @@ "hardhat-contract-sizer": "2.6.1", "hardhat-gas-reporter": "1.0.9", "mythxjs": "1.3.13", - "prettier": "2.7.1", - "prettier-plugin-solidity": "1.0.0-beta.24", + "prettier": "^2.7.1", + "prettier-plugin-solidity": "^1.0.0-beta.24", "solhint": "3.3.7", "solidity-coverage": "0.7.22", "ts-node": "10.9.1", diff --git a/scripts/autogen/contract-deployments-keys.ts b/scripts/autogen/contract-deployments-keys.ts index 16c5e7fc..d500f8da 100644 --- a/scripts/autogen/contract-deployments-keys.ts +++ b/scripts/autogen/contract-deployments-keys.ts @@ -2,23 +2,7 @@ import { ContractDeploymentsKey } from '../util/files/contractDeploymentsJson' export const CONTRACT_DEPLOYMENT_KEYS: ContractDeploymentsKey[] = [ { - networkName: 'arb_goerli', - objectName: 'poolRouter' - }, - { - networkName: 'arb_goerli', - objectName: 'rangeStaker' - }, - { - networkName: 'arb_goerli', - objectName: 'poolRouter' - }, - { - networkName: 'arb_goerli', - objectName: 'poolRouter' - }, - { - networkName: 'arb_goerli', + networkName: 'arb_sepolia', objectName: 'poolRouter' }, ]; \ No newline at end of file diff --git a/scripts/autogen/contract-deployments.json b/scripts/autogen/contract-deployments.json index 436d7ea2..ba3a29c8 100644 --- a/scripts/autogen/contract-deployments.json +++ b/scripts/autogen/contract-deployments.json @@ -1,205 +1,207 @@ { - "arb_goerli": { + "arb_one": { "token0": { "contractName": "Token20", - "contractAddress": "0xebff7a98149b4774c9743c5d1f382305fe5422c9", + "contractAddress": "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1", "constructorArguments": [ - "USD Coin", - "USDC", - 6 + "Wrapped Ether", + "WETH", + 18 ], - "created": "2023-08-23T15:56:24.723Z" + "created": "2023-08-23T15:56:24.715Z" }, "token1": { "contractName": "Token20", - "contractAddress": "0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7", + "contractAddress": "0x903CA00944D0b51e50D9F4fC96167c89F211542A", "constructorArguments": [ - "Wrapped Ether", - "WETH", + "Poolshark", + "FIN", 18 ], - "created": "2023-08-23T15:56:24.715Z" + "created": "2023-08-23T15:56:24.723Z" + }, + "tokenPay": { + "contractName": "Token20", + "contractAddress": "0x3788ce0817512e33130b1ea0490d2957b4ffc406", + "constructorArguments": [ + "Payout Token", + "PAY", + 18 + ], + "created": "2023-12-10T00:19:39.570Z" + }, + "tokenQuote": { + "contractName": "Token20", + "contractAddress": "0xeb590779e7f68e12a48f8b146252a0b0200df16f", + "constructorArguments": [ + "Quote Token", + "QUOTE", + 18 + ], + "created": "2023-12-10T00:19:45.000Z" + }, + "weth9": { + "contractName": "WETH9", + "contractAddress": "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1", + "constructorArguments": [], + "created": "2023-11-25T12:32:49.206Z" + }, + "coverPoolFactory": { + "contractName": "CoverPoolFactory", + "contractAddress": "0x0000000000000000000000000000000000000000", + "constructorArguments": [ + "0x7877A681D1290d80ed0272A920b9a5CeE8Fd2fAC" + ], + "created": "2023-10-01T13:30:41.488Z" }, "tickMapLib": { "contractName": "TickMap", - "contractAddress": "0xfc2bb8b23eecd9592ecb84453a41578a39008314", + "contractAddress": "0xf682a699eac19d8dc7c80f3d3239abea981407e2", "constructorArguments": [], - "created": "2023-11-25T12:44:23.611Z" + "created": "2024-01-08T04:21:38.601Z" }, "ticksLib": { "contractName": "Ticks", - "contractAddress": "0x8551385fffec5f0266093a2d6c5a08b917bc781e", + "contractAddress": "0x29fca90fa30b696cdaa9068b75fa46114647121c", "constructorArguments": [], - "created": "2023-11-25T12:44:25.752Z" - }, - "rangePositionsLib": { - "contractName": "RangePositions", - "contractAddress": "0x4dfea8437c7eb1667805ddf95c096cd2ab925f56", - "constructorArguments": [], - "created": "2023-11-25T12:44:27.302Z" + "created": "2024-01-08T04:21:44.314Z" }, "limitPositionsLib": { "contractName": "LimitPositions", - "contractAddress": "0x436f4c3fc60369cd0eb99acd716de4e303c30f36", + "contractAddress": "0x6620cd0c464c380cf02f8fe60e2d522e61856390", "constructorArguments": [], - "created": "2023-11-25T12:44:28.911Z" + "created": "2024-01-08T04:21:49.836Z" }, "limitPoolManager": { "contractName": "LimitPoolManager", - "contractAddress": "0x6d3af137e75097b892683832a2c0132f99625d0e", + "contractAddress": "0xc773049bf7131a5ddd85daded4397a1fd4264a3d", "constructorArguments": [], - "created": "2023-11-25T12:44:30.623Z" + "created": "2024-01-08T04:21:51.713Z" }, "limitPoolFactory": { "contractName": "LimitPoolFactory", - "contractAddress": "0x1b215002e688135549cc0290d6cf1f94e3aa425c", + "contractAddress": "0x8bb5db1625adb4ae4beb94a188d33062303f8fb7", "constructorArguments": [ - "0x6D3AF137E75097B892683832a2c0132f99625d0e" + "0xC773049bF7131A5Ddd85DadED4397a1fd4264a3D" ], - "created": "2023-11-25T12:44:32.085Z" + "created": "2024-01-08T04:21:52.913Z" }, "swapCall": { "contractName": "SwapCall", - "contractAddress": "0x4017cade72ea448a46ee0176eacef49c0e91d6f5", + "contractAddress": "0x3ec67dc00ba6672e9ab03d3942beee3279029af6", "constructorArguments": [], - "created": "2023-11-25T12:44:33.671Z" + "created": "2024-01-08T04:21:58.377Z" }, "mintRangeCall": { "contractName": "MintRangeCall", - "contractAddress": "0x767ace5b40c9c70159051b55895f1dc4d5ad770d", + "contractAddress": "0x61ece2c7b9e48d8d32e35238c488a592f0fe1bc4", "constructorArguments": [], - "created": "2023-11-25T12:44:35.331Z" + "created": "2024-01-08T04:22:04.134Z" }, "burnRangeCall": { "contractName": "BurnRangeCall", - "contractAddress": "0x9d9102bb05a935c5aa2cd350e7fa956aed2e283c", + "contractAddress": "0x9e5791c31fc427ff2e90727aa08cf88dfe0e956c", "constructorArguments": [], - "created": "2023-11-25T12:44:36.992Z" + "created": "2024-01-08T04:22:09.951Z" }, "mintLimitCall": { "contractName": "MintLimitCall", - "contractAddress": "0x57b6524810baaacd9726c26942bfe04500569271", + "contractAddress": "0x1bbe2d602da38077708544dce2b0c9b3309944de", "constructorArguments": [], - "created": "2023-11-25T12:44:38.947Z" + "created": "2024-01-08T04:22:15.780Z" }, "burnLimitCall": { "contractName": "BurnLimitCall", - "contractAddress": "0x581a56e3140beb3ff2f34d549daff05ac5553d36", + "contractAddress": "0xaa786d3a63a03cdd2d5857012276b3ef9d1dbfb5", "constructorArguments": [], - "created": "2023-11-25T12:44:40.594Z" + "created": "2024-01-08T04:22:21.430Z" }, "snapshotLimitCall": { "contractName": "SnapshotLimitCall", - "contractAddress": "0x33bb5144306c7e29abf6fc5663cf2f370648f651", + "contractAddress": "0x248fef9576bbdd1b961d8df4c5812fa0288308d2", "constructorArguments": [], - "created": "2023-11-25T12:44:42.153Z" + "created": "2024-01-08T04:22:27.137Z" }, "quoteCall": { "contractName": "QuoteCall", - "contractAddress": "0x1bcb9a43ea1a76c326341167e2d3b06267740dad", + "contractAddress": "0x54dabefa8feada05b8c00b6c8591511e86db2892", "constructorArguments": [], - "created": "2023-11-25T12:44:43.948Z" + "created": "2024-01-08T04:22:32.825Z" }, "feesCall": { "contractName": "FeesCall", - "contractAddress": "0x22c2269dc214d93d61afceb7e9228ae8e3d27eac", + "contractAddress": "0x32e40d1f6de8ec0de03a854b954c59e152f7d4b6", "constructorArguments": [], - "created": "2023-11-25T12:44:45.406Z" + "created": "2024-01-08T04:22:33.851Z" }, "sampleCall": { "contractName": "SampleCall", - "contractAddress": "0x41fc7610d53358fb9aa75e7e4c1c4558a3509905", + "contractAddress": "0x2b9fae71f0f0175904583351a03207f476602f66", "constructorArguments": [], - "created": "2023-11-25T12:44:47.083Z" + "created": "2024-01-08T04:22:39.184Z" }, "snapshotRangeCall": { "contractName": "SnapshotRangeCall", - "contractAddress": "0xf6485573b3e88e9906d87bc51f5e526990a8209d", + "contractAddress": "0xa2a22ea722317d8cd338345ed7cba99dcab5ec7b", "constructorArguments": [], - "created": "2023-11-25T12:44:48.554Z" + "created": "2024-01-08T04:22:40.812Z" }, "limitPoolImpl": { "contractName": "LimitPool", - "contractAddress": "0x50903014ec42471b8f628ce137acec4f860b2d8c", + "contractAddress": "0xb9a8cf9d5f31ba815ddaeba4098a40e643318826", "constructorArguments": [ - "0x1b215002E688135549CC0290d6Cf1F94E3AA425c" + "0x8bB5Db1625ADB4ae4bEb94A188D33062303F8Fb7" ], - "created": "2023-11-25T12:44:50.187Z" + "created": "2024-01-08T04:22:46.296Z" }, "positionERC1155": { "contractName": "PositionERC1155", - "contractAddress": "0xcf5b954159719a3eb768d18b16d23d85d516ec39", + "contractAddress": "0x0d8b0e3ab967b3cb34af0e5321490debe6597737", "constructorArguments": [ - "0x1b215002E688135549CC0290d6Cf1F94E3AA425c" + "0x8bB5Db1625ADB4ae4bEb94A188D33062303F8Fb7" ], - "created": "2023-11-25T12:44:51.809Z" - }, - "poolRouter": { - "contractName": "PoolsharkRouter", - "contractAddress": "0x24757e9d68bfcc99a9dba0a62737703cb1a32e06", - "constructorArguments": [ - "0x1b215002e688135549cc0290d6cf1f94e3aa425c", - "0xf8f75a39663e97d36da4be2882608513512f4cf0", - "0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7" - ], - "created": "2023-11-29T19:48:26.040Z" + "created": "2024-01-08T04:22:52.040Z" }, "limitPool": { "contractName": "LimitPool", - "contractAddress": "0x4998545e13a668a884272aaebff14ab21c5b4e89", + "contractAddress": "0xa43ddbcc4b78512c316bd7091b4c60f06db0fe42", "constructorArguments": [ - "0xebff7a98149b4774c9743c5d1f382305fe5422c9", - "0xEfb283eF3167CA2eE9D93B201af15e2af3f6e8c7", - "1000", + "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1", + "0x903CA00944D0b51e50D9F4fC96167c89F211542A", + "3000", 0 ], - "created": "2023-11-25T12:45:02.690Z" - }, - "coverPoolFactory": { - "contractName": "CoverPoolFactory", - "contractAddress": "0xf8f75a39663e97d36da4be2882608513512f4cf0", - "constructorArguments": [ - "0x7877A681D1290d80ed0272A920b9a5CeE8Fd2fAC" - ], - "created": "2023-10-01T13:30:41.488Z" + "created": "2024-01-08T04:22:55.562Z" }, - "tokenA": { - "contractName": "Token20", - "contractAddress": "0xd1c4abd91dc01684ac47bf26c5109db19b636c15", - "constructorArguments": [ - "ChainLink Token", - "LINK", - 18 - ], - "created": "2023-10-20T14:15:25.994Z" - }, - "tokenB": { - "contractName": "Token20", - "contractAddress": "0xaf412fe6adac79b04f22140ecb583b2fed3a0d8e", + "poolRouter": { + "contractName": "PoolsharkRouter", + "contractAddress": "0x12b7a6dd3a3dfde6a0f112a1bd876f704d933915", "constructorArguments": [ - "Wrapped BTC", - "WBTC", - 8 + "0x8bb5db1625adb4ae4beb94a188d33062303f8fb7", + "0x0000000000000000000000000000000000000000", + "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1" ], - "created": "2023-10-20T14:15:30.017Z" - }, - "weth9": { - "contractName": "WETH9", - "contractAddress": "0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7", - "constructorArguments": [], - "created": "2023-11-25T12:32:49.206Z" + "created": "2024-01-08T13:48:09.773Z" }, "rangeStaker": { "contractName": "RangeStaker", - "contractAddress": "0xe5e2e95a986ce078606c403593593b18ed98f4d6", + "contractAddress": "0x0e2b069fa52064a7e0b5a044ba25142203210a13", "constructorArguments": [ { - "limitPoolFactory": "0x1b215002e688135549cc0290d6cf1f94e3aa425c", + "limitPoolFactory": "0x8bb5db1625adb4ae4beb94a188d33062303f8fb7", "startTime": 0, "endTime": 2000707154 } ], - "created": "2023-11-28T22:19:18.570Z" + "created": "2024-01-08T04:23:06.825Z" + }, + "tickQuoter": { + "contractName": "TickQuoter", + "contractAddress": "0xc7cdb74fbc16fc9e7c57836d00ef2eb5e9c499bc", + "constructorArguments": [ + "0x8bb5db1625adb4ae4beb94a188d33062303f8fb7" + ], + "created": "2024-01-14T02:58:50.837Z" } }, "scrollSepolia": { @@ -225,128 +227,128 @@ }, "tickMapLib": { "contractName": "TickMap", - "contractAddress": "0x1fc002005790e473b2cd4f93f1d24a20432da75d", + "contractAddress": "0x3788ce0817512e33130b1ea0490d2957b4ffc406", "constructorArguments": [], - "created": "2023-09-27T23:07:01.909Z" + "created": "2024-01-08T22:52:07.199Z" }, "ticksLib": { "contractName": "Ticks", - "contractAddress": "0x0bdcd4d288eb481dd3cd81a2a35ad68cd5ded30d", + "contractAddress": "0xeb590779e7f68e12a48f8b146252a0b0200df16f", "constructorArguments": [], - "created": "2023-09-27T23:07:07.419Z" - }, - "rangePositionsLib": { - "contractName": "RangePositions", - "contractAddress": "0x29f6547dd4ba11e169173c8d210db7138d09054f", - "constructorArguments": [], - "created": "2023-09-27T23:07:16.463Z" + "created": "2024-01-08T22:52:13.123Z" }, "limitPositionsLib": { "contractName": "LimitPositions", - "contractAddress": "0x599372abed45a59140d85cc25618381103c245fa", + "contractAddress": "0x3b76c880dbf5f660dc0c8290435d7f85e4fa8e2b", "constructorArguments": [], - "created": "2023-09-27T23:07:22.458Z" + "created": "2024-01-08T22:52:18.542Z" }, "limitPoolManager": { "contractName": "LimitPoolManager", - "contractAddress": "0x4e2f0a57ed373261451c1ef30215232be352020e", + "contractAddress": "0x7113aec7595accbaea1e61bc59f97e35d3a87bd5", "constructorArguments": [], - "created": "2023-09-27T23:07:27.645Z" + "created": "2024-01-08T22:52:23.767Z" }, "limitPoolFactory": { "contractName": "LimitPoolFactory", - "contractAddress": "0x55feb5281c4097142870745462775b93c5eef412", + "contractAddress": "0xd506193e48f13438e35a1edc4ef5394876b5efa4", "constructorArguments": [ - "0x4e2f0a57eD373261451C1ef30215232bE352020E" + "0x7113AEC7595aCcbaEa1E61Bc59F97e35D3a87bD5" ], - "created": "2023-09-27T23:07:32.660Z" + "created": "2024-01-08T22:52:28.806Z" }, "swapCall": { "contractName": "SwapCall", - "contractAddress": "0x139e58aca96cd776e3d8e3e20dfc30154c485b46", + "contractAddress": "0xf71889221f81e4475285c2b96dbf04efda194d56", "constructorArguments": [], - "created": "2023-09-27T23:07:37.787Z" + "created": "2024-01-08T22:52:33.873Z" }, "mintRangeCall": { "contractName": "MintRangeCall", - "contractAddress": "0x358242aac3e75da72a4c091c091c80a84f115966", + "contractAddress": "0x86d7513c703767595a2b33f6379e2a7a591f3c6e", "constructorArguments": [], - "created": "2023-09-27T23:07:46.994Z" + "created": "2024-01-08T22:52:39.404Z" }, "burnRangeCall": { "contractName": "BurnRangeCall", - "contractAddress": "0x5f7d98a4443517c3928cb925d7202412b1e34886", + "contractAddress": "0x69781d0ef32aef5052def4da479523536a1e31f8", "constructorArguments": [], - "created": "2023-09-27T23:07:52.436Z" + "created": "2024-01-08T22:52:44.921Z" }, "mintLimitCall": { "contractName": "MintLimitCall", - "contractAddress": "0xef8be53e21994fa411c1a6fb9cbc9c8ad6125f52", + "contractAddress": "0xe25140cb94de68b0ee88342463e64a9294597ed3", "constructorArguments": [], - "created": "2023-09-27T23:08:01.808Z" + "created": "2024-01-08T22:52:50.768Z" }, "burnLimitCall": { "contractName": "BurnLimitCall", - "contractAddress": "0x33df95efe07a3b3e69ba31438ae511d360d89b32", + "contractAddress": "0x30eca6e3b3df59b00f8b6530d0e82c8640a5bdce", "constructorArguments": [], - "created": "2023-09-27T23:08:11.042Z" + "created": "2024-01-08T22:52:56.294Z" }, "snapshotLimitCall": { "contractName": "SnapshotLimitCall", - "contractAddress": "0x62e0671022af1b2e705f08b282767c57d29c7c4c", + "contractAddress": "0x6d4c6630bd48d0a048c5ee7167287df283c0ce1f", "constructorArguments": [], - "created": "2023-09-27T23:08:20.200Z" + "created": "2024-01-08T22:53:01.697Z" }, "quoteCall": { "contractName": "QuoteCall", - "contractAddress": "0xcd453b942f35adf0364d89c05a892518825c1c3b", + "contractAddress": "0x965ab254e03e0faa6c4bcc88d0385b9a52069f57", "constructorArguments": [], - "created": "2023-09-27T23:08:29.653Z" + "created": "2024-01-08T22:53:07.368Z" }, "feesCall": { "contractName": "FeesCall", - "contractAddress": "0x61e80b7f57f3613ce30153c85d29ad571e7e9032", + "contractAddress": "0xe404640ff22538f0725773fb87894870a2e7d0b1", "constructorArguments": [], - "created": "2023-09-27T23:08:38.692Z" + "created": "2024-01-08T22:53:12.421Z" }, "sampleCall": { "contractName": "SampleCall", - "contractAddress": "0x5f6de12756b3e6df4db21bcc4855b4eb52a5d794", + "contractAddress": "0x589b1a293d4797ff467200897c7e61ede8f445ae", "constructorArguments": [], - "created": "2023-09-27T23:08:47.875Z" + "created": "2024-01-08T22:53:17.502Z" }, "snapshotRangeCall": { "contractName": "SnapshotRangeCall", - "contractAddress": "0xd47965e2357f60b4e2062df945afdcc655b5ce3a", + "contractAddress": "0xcf0c39c21dcf16fd9ce84c2cea314c05c96d0f09", "constructorArguments": [], - "created": "2023-09-27T23:08:53.010Z" + "created": "2024-01-08T22:53:22.612Z" }, "limitPoolImpl": { "contractName": "LimitPool", - "contractAddress": "0x0d4371c6d53e10986ba23929e02de11e60ba3b3c", + "contractAddress": "0xcf5784a9f8c8b0604a63c79c71460934f6d62ef6", "constructorArguments": [ - "0x55FEB5281C4097142870745462775B93C5EeF412" + "0xd506193e48f13438e35A1Edc4ef5394876b5eFA4" ], - "created": "2023-09-27T23:09:02.338Z" + "created": "2024-01-08T22:53:28.030Z" }, "positionERC1155": { "contractName": "PositionERC1155", - "contractAddress": "0xed0ef8101900dfed208ac99ce47b723debb6bfe6", + "contractAddress": "0x581b8c5f68c68219e15ca9258e03f92ae35320e8", "constructorArguments": [ - "0x55FEB5281C4097142870745462775B93C5EeF412" + "0xd506193e48f13438e35A1Edc4ef5394876b5eFA4" ], - "created": "2023-09-27T23:09:11.427Z" + "created": "2024-01-08T22:53:28.940Z" }, "limitPool": { "contractName": "LimitPool", - "contractAddress": "0xA3374d366C33E803A3fF4a3db4B38e5Aa4A1f2E5", + "contractAddress": "0xea578474c65f859739c8ec362543eedc04f0d48e", "constructorArguments": [ - "0x434f4e5354414e542d50524f4455435400000000000000000000000000000000", - "0x26323C7e4F000227932d3ed640EA79D6027701DE", - "0x912c3e7d140e4EC85eb8e3910447ACDA8654D523", - "500" + "0x681cfAC3f265b6041FF4648A1CcB214F1c0DcF38", + "0xa9e1ab5e6878621F80E03A4a5F8FB3705F4FFA2B", + "3000", + 0 ], - "created": "2023-09-27T23:09:50.038Z" + "created": "2024-01-08T22:53:41.198Z" + }, + "weth9": { + "contractName": "WETH9", + "contractAddress": "0xf04bf4e3e8157ba5b91bfda16e21be770e7ac790", + "constructorArguments": [], + "created": "2024-01-08T22:55:37.005Z" }, "coverPoolFactory": { "contractName": "CoverPoolFactory", @@ -358,12 +360,202 @@ }, "poolRouter": { "contractName": "PoolsharkRouter", - "contractAddress": "0xf9aa2fb38bf96903b578cc98e6579b5d96d37b89", + "contractAddress": "0x14ce728bf96d5ec0976f332605fe80a42ec0b244", + "constructorArguments": [ + "0xd506193e48f13438e35a1edc4ef5394876b5efa4", + "0x4b0288914ea8ab81da888e74f93da0062212ddcc", + "0xf04bf4e3e8157ba5b91bfda16e21be770e7ac790" + ], + "created": "2024-01-08T13:48:09.773Z" + }, + "rangeStaker": { + "contractName": "RangeStaker", + "contractAddress": "0x83eea57c3536a7ec64ead2c67d5b6bfeb6593720", + "constructorArguments": [ + { + "limitPoolFactory": "0xd506193e48f13438e35a1edc4ef5394876b5efa4", + "startTime": 0, + "endTime": 2000707154 + } + ], + "created": "2024-01-08T22:55:37.005Z" + } + }, + "arb_sepolia": { + "weth9": { + "contractName": "WETH9", + "contractAddress": "0x414b73f989e7ca0653b5c98186749a348405e6d5", + "constructorArguments": [], + "created": "2024-01-09T01:02:26.457Z" + }, + "token0": { + "contractName": "Token20", + "contractAddress": "0x9f479560cd8a531e6c0fe04521cb246264fe6b71", + "constructorArguments": [ + "Dai Stablecoin", + "DAI", + 18 + ], + "created": "2024-01-09T00:58:39.313Z" + }, + "token1": { + "contractName": "Token20", + "contractAddress": "0x414b73f989e7ca0653b5c98186749a348405e6d5", + "constructorArguments": [ + "Wrapped Ether", + "WETH", + 18 + ], + "created": "2024-01-09T00:58:39.323Z" + }, + "tickMapLib": { + "contractName": "TickMap", + "contractAddress": "0x96c2815f7c750eb15d8d85c522dff85eadaa1cd5", + "constructorArguments": [], + "created": "2024-01-09T01:02:27.708Z" + }, + "ticksLib": { + "contractName": "Ticks", + "contractAddress": "0xd190cf03ec10dec87f51a18285af3851815505e8", + "constructorArguments": [], + "created": "2024-01-09T01:02:29.163Z" + }, + "limitPositionsLib": { + "contractName": "LimitPositions", + "contractAddress": "0x4b6918454f16c991ee5e090940f9209eed3b7097", + "constructorArguments": [], + "created": "2024-01-09T01:02:30.316Z" + }, + "coverPoolFactory": { + "contractName": "CoverPoolFactory", + "contractAddress": "0x0000000000000000000000000000000000000000", + "constructorArguments": [ + "0xF76088F4C5Ad2a138f68870449f019e0E93E1510" + ], + "created": "2023-09-25T01:59:46.159Z" + }, + "limitPoolManager": { + "contractName": "LimitPoolManager", + "contractAddress": "0x0385d0a8d169b1f1510bb5fa0be264463002f6f6", + "constructorArguments": [], + "created": "2024-01-09T01:02:31.636Z" + }, + "limitPoolFactory": { + "contractName": "LimitPoolFactory", + "contractAddress": "0x8e40c68b7546efd009a1a300c92e25da3c8725dc", + "constructorArguments": [ + "0x0385d0a8d169B1F1510BB5fa0be264463002F6f6" + ], + "created": "2024-01-09T01:02:32.869Z" + }, + "swapCall": { + "contractName": "SwapCall", + "contractAddress": "0xe0c1e03b85b22e10718cb20c3e91dd34dd73905e", + "constructorArguments": [], + "created": "2024-01-09T01:02:34.301Z" + }, + "mintRangeCall": { + "contractName": "MintRangeCall", + "contractAddress": "0x38cb66ea3d5b5341201e6a0b1b150660880e7d5d", + "constructorArguments": [], + "created": "2024-01-09T01:02:35.557Z" + }, + "burnRangeCall": { + "contractName": "BurnRangeCall", + "contractAddress": "0xe35d5aa1bac86d9eeaef63e7013094192e57ed21", + "constructorArguments": [], + "created": "2024-01-09T01:02:36.767Z" + }, + "mintLimitCall": { + "contractName": "MintLimitCall", + "contractAddress": "0xc2271a012fbba8098e569be9fa893a1255d73b0f", + "constructorArguments": [], + "created": "2024-01-09T01:02:38.031Z" + }, + "burnLimitCall": { + "contractName": "BurnLimitCall", + "contractAddress": "0x4372ee46dfb52d271139fd40aeda61c5da0c35e5", + "constructorArguments": [], + "created": "2024-01-09T01:02:39.240Z" + }, + "snapshotLimitCall": { + "contractName": "SnapshotLimitCall", + "contractAddress": "0x1fc002005790e473b2cd4f93f1d24a20432da75d", + "constructorArguments": [], + "created": "2024-01-09T01:02:40.378Z" + }, + "quoteCall": { + "contractName": "QuoteCall", + "contractAddress": "0x0bdcd4d288eb481dd3cd81a2a35ad68cd5ded30d", + "constructorArguments": [], + "created": "2024-01-09T01:02:41.639Z" + }, + "feesCall": { + "contractName": "FeesCall", + "contractAddress": "0x29f6547dd4ba11e169173c8d210db7138d09054f", + "constructorArguments": [], + "created": "2024-01-09T01:02:42.855Z" + }, + "sampleCall": { + "contractName": "SampleCall", + "contractAddress": "0x599372abed45a59140d85cc25618381103c245fa", + "constructorArguments": [], + "created": "2024-01-09T01:02:44.139Z" + }, + "snapshotRangeCall": { + "contractName": "SnapshotRangeCall", + "contractAddress": "0x4e2f0a57ed373261451c1ef30215232be352020e", + "constructorArguments": [], + "created": "2024-01-09T01:02:45.390Z" + }, + "limitPoolImpl": { + "contractName": "LimitPool", + "contractAddress": "0x55feb5281c4097142870745462775b93c5eef412", + "constructorArguments": [ + "0x8E40c68b7546efd009a1A300C92e25Da3c8725dc" + ], + "created": "2024-01-09T01:02:46.629Z" + }, + "positionERC1155": { + "contractName": "PositionERC1155", + "contractAddress": "0x139e58aca96cd776e3d8e3e20dfc30154c485b46", + "constructorArguments": [ + "0x8E40c68b7546efd009a1A300C92e25Da3c8725dc" + ], + "created": "2024-01-09T01:02:47.732Z" + }, + "limitPool": { + "contractName": "LimitPool", + "contractAddress": "0x02225f6a3d83648d7906a23856331c819265394d", + "constructorArguments": [ + "0x9f479560cd8a531e6c0fe04521cb246264fe6b71", + "0x414b73f989e7ca0653b5c98186749a348405e6d5", + "3000", + 0 + ], + "created": "2024-01-09T01:02:52.948Z" + }, + "poolRouter": { + "contractName": "PoolsharkRouter", + "contractAddress": "0x73ac9a2e665925719d9c272a3df60b97dbc3e50d", "constructorArguments": [ - "0x55feb5281c4097142870745462775b93c5eef412", - "0x4b0288914ea8ab81da888e74f93da0062212ddcc" + "0x8e40c68b7546efd009a1a300c92e25da3c8725dc", + "0x0000000000000000000000000000000000000000", + "0x414b73f989e7ca0653b5c98186749a348405e6d5" + ], + "created": "2024-01-17T00:16:04.942Z" + }, + "rangeStaker": { + "contractName": "RangeStaker", + "contractAddress": "0x62e0671022af1b2e705f08b282767c57d29c7c4c", + "constructorArguments": [ + { + "limitPoolFactory": "0x8e40c68b7546efd009a1a300c92e25da3c8725dc", + "startTime": 0, + "endTime": 2000707154 + } ], - "created": "2023-10-02T23:25:47.853Z" + "created": "2024-01-09T01:02:55.607Z" } } } \ No newline at end of file diff --git a/scripts/config/networkConfigs.ts b/scripts/config/networkConfigs.ts index fc490818..5f97f68b 100644 --- a/scripts/config/networkConfigs.ts +++ b/scripts/config/networkConfigs.ts @@ -40,6 +40,13 @@ export const NETWORK_CONFIGS: NetworkConfigs = { url: process.env.ARBITRUM_GOERLI_URL || '', accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [], }, + arb_one: { + chainId: 42161, + gas: 9000000, + gasPrice: 1_000_000_000, + url: process.env.ARBITRUM_ONE_URL || '', + accounts: process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [], + }, scrollSepolia : { chainId: 534353, gas: 9000000, diff --git a/scripts/constants/supportedNetworks.ts b/scripts/constants/supportedNetworks.ts index 86a43fdf..9f2c0fce 100644 --- a/scripts/constants/supportedNetworks.ts +++ b/scripts/constants/supportedNetworks.ts @@ -5,12 +5,15 @@ export enum SUPPORTED_NETWORKS { /* Testnet Supported Networks */ GOERLI = 'goerli', ARB_GOERLI = 'arb_goerli', + ARB_SEPOLIA = 'arb_sepolia', + ARB_ONE = 'arb_one', SCROLL_SEPOLIA = 'scrollSepolia' } export enum TESTNET_NETWORKS { GOERLI = 'goerli', ARB_GOERLI = 'arb_goerli', + ARB_SEPOLIA = 'arb_sepolia', SCROLL_SEPOLIA = 'scrollSepolia' } diff --git a/subgraph/LICENSE b/subgraph/LICENSE index 8aeaeeee..e72bfdda 100644 --- a/subgraph/LICENSE +++ b/subgraph/LICENSE @@ -1,21 +1,674 @@ -MIT License - -Copyright (c) 2022 Poolshark - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + Copyright (C) + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. \ No newline at end of file diff --git a/subgraph/README.md b/subgraph/README.md index 95e0ac2c..5fde2737 100644 --- a/subgraph/README.md +++ b/subgraph/README.md @@ -1 +1 @@ -# oceanbook-v1-subgraph +# Poolshark Limit Subgraph diff --git a/subgraph/abis/LimitPool.json b/subgraph/abis/LimitPool.json index cf430cb0..c95053f3 100644 --- a/subgraph/abis/LimitPool.json +++ b/subgraph/abis/LimitPool.json @@ -1,4 +1,47 @@ [ + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "internalType": "int24", + "name": "tickLower", + "type": "int24" + }, + { + "indexed": true, + "internalType": "int24", + "name": "tickUpper", + "type": "int24" + }, + { + "indexed": false, + "internalType": "uint128", + "name": "amount", + "type": "uint128" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount0", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount1", + "type": "uint256" + } + ], + "name": "Burn", + "type": "event" + }, { "anonymous": false, "inputs": [ @@ -106,19 +149,36 @@ { "anonymous": false, "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": false, + "internalType": "address", + "name": "recipient", + "type": "address" + }, + { + "indexed": true, + "internalType": "int24", + "name": "tickLower", + "type": "int24" + }, + { + "indexed": true, + "internalType": "int24", + "name": "tickUpper", + "type": "int24" + }, { "indexed": false, "internalType": "uint128", "name": "amount0", "type": "uint128" - } - ], - "name": "CollectRange0", - "type": "event" - }, - { - "anonymous": false, - "inputs": [ + }, { "indexed": false, "internalType": "uint128", @@ -126,7 +186,7 @@ "type": "uint128" } ], - "name": "CollectRange1", + "name": "Collect", "type": "event" }, { @@ -148,6 +208,25 @@ "name": "CompoundRange", "type": "event" }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "uint160", + "name": "price", + "type": "uint160" + }, + { + "indexed": false, + "internalType": "int24", + "name": "tick", + "type": "int24" + } + ], + "name": "Initialize", + "type": "event" + }, { "anonymous": false, "inputs": [ @@ -176,7 +255,56 @@ "type": "int24" } ], - "name": "Initialize", + "name": "InitializeLimit", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "sender", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "internalType": "int24", + "name": "tickLower", + "type": "int24" + }, + { + "indexed": true, + "internalType": "int24", + "name": "tickUpper", + "type": "int24" + }, + { + "indexed": false, + "internalType": "uint128", + "name": "amount", + "type": "uint128" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount0", + "type": "uint256" + }, + { + "indexed": false, + "internalType": "uint256", + "name": "amount1", + "type": "uint256" + } + ], + "name": "Mint", "type": "event" }, { @@ -315,6 +443,55 @@ "name": "SampleRecorded", "type": "event" }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "sender", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "recipient", + "type": "address" + }, + { + "indexed": false, + "internalType": "int256", + "name": "amount0", + "type": "int256" + }, + { + "indexed": false, + "internalType": "int256", + "name": "amount1", + "type": "int256" + }, + { + "indexed": false, + "internalType": "uint160", + "name": "price", + "type": "uint160" + }, + { + "indexed": false, + "internalType": "uint128", + "name": "liquidity", + "type": "uint128" + }, + { + "indexed": false, + "internalType": "int24", + "name": "tickAtPrice", + "type": "int24" + } + ], + "name": "Swap", + "type": "event" + }, { "anonymous": false, "inputs": [ @@ -385,7 +562,7 @@ "type": "bool" } ], - "name": "Swap", + "name": "SwapLimit", "type": "event" }, { diff --git a/subgraph/abis/LimitPoolFactory.json b/subgraph/abis/LimitPoolFactory.json index 17e9a4ac..a4bdcc3b 100644 --- a/subgraph/abis/LimitPoolFactory.json +++ b/subgraph/abis/LimitPoolFactory.json @@ -45,6 +45,43 @@ "type": "uint16" } ], + "name": "LimitPoolCreated", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "token0", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "token1", + "type": "address" + }, + { + "indexed": true, + "internalType": "uint24", + "name": "fee", + "type": "uint24" + }, + { + "indexed": false, + "internalType": "int24", + "name": "tickSpacing", + "type": "int24" + }, + { + "indexed": false, + "internalType": "address", + "name": "pool", + "type": "address" + } + ], "name": "PoolCreated", "type": "event" } diff --git a/subgraph/abis/v3Pool.json b/subgraph/abis/v3Pool.json new file mode 100644 index 00000000..f245f073 --- /dev/null +++ b/subgraph/abis/v3Pool.json @@ -0,0 +1,1020 @@ +[ + { + "inputs":[ + + ], + "stateMutability":"nonpayable", + "type":"constructor" + }, + { + "anonymous":false, + "inputs":[ + { + "indexed":true, + "internalType":"address", + "name":"owner", + "type":"address" + }, + { + "indexed":true, + "internalType":"int24", + "name":"tickLower", + "type":"int24" + }, + { + "indexed":true, + "internalType":"int24", + "name":"tickUpper", + "type":"int24" + }, + { + "indexed":false, + "internalType":"uint128", + "name":"amount", + "type":"uint128" + }, + { + "indexed":false, + "internalType":"uint256", + "name":"amount0", + "type":"uint256" + }, + { + "indexed":false, + "internalType":"uint256", + "name":"amount1", + "type":"uint256" + } + ], + "name":"Burn", + "type":"event" + }, + { + "anonymous":false, + "inputs":[ + { + "indexed":true, + "internalType":"address", + "name":"owner", + "type":"address" + }, + { + "indexed":false, + "internalType":"address", + "name":"recipient", + "type":"address" + }, + { + "indexed":true, + "internalType":"int24", + "name":"tickLower", + "type":"int24" + }, + { + "indexed":true, + "internalType":"int24", + "name":"tickUpper", + "type":"int24" + }, + { + "indexed":false, + "internalType":"uint128", + "name":"amount0", + "type":"uint128" + }, + { + "indexed":false, + "internalType":"uint128", + "name":"amount1", + "type":"uint128" + } + ], + "name":"Collect", + "type":"event" + }, + { + "anonymous":false, + "inputs":[ + { + "indexed":true, + "internalType":"address", + "name":"sender", + "type":"address" + }, + { + "indexed":true, + "internalType":"address", + "name":"recipient", + "type":"address" + }, + { + "indexed":false, + "internalType":"uint128", + "name":"amount0", + "type":"uint128" + }, + { + "indexed":false, + "internalType":"uint128", + "name":"amount1", + "type":"uint128" + } + ], + "name":"CollectProtocol", + "type":"event" + }, + { + "anonymous":false, + "inputs":[ + { + "indexed":true, + "internalType":"address", + "name":"sender", + "type":"address" + }, + { + "indexed":true, + "internalType":"address", + "name":"recipient", + "type":"address" + }, + { + "indexed":false, + "internalType":"uint256", + "name":"amount0", + "type":"uint256" + }, + { + "indexed":false, + "internalType":"uint256", + "name":"amount1", + "type":"uint256" + }, + { + "indexed":false, + "internalType":"uint256", + "name":"paid0", + "type":"uint256" + }, + { + "indexed":false, + "internalType":"uint256", + "name":"paid1", + "type":"uint256" + } + ], + "name":"Flash", + "type":"event" + }, + { + "anonymous":false, + "inputs":[ + { + "indexed":false, + "internalType":"uint16", + "name":"observationCardinalityNextOld", + "type":"uint16" + }, + { + "indexed":false, + "internalType":"uint16", + "name":"observationCardinalityNextNew", + "type":"uint16" + } + ], + "name":"IncreaseObservationCardinalityNext", + "type":"event" + }, + { + "anonymous":false, + "inputs":[ + { + "indexed":false, + "internalType":"uint160", + "name":"sqrtPriceX96", + "type":"uint160" + }, + { + "indexed":false, + "internalType":"int24", + "name":"tick", + "type":"int24" + } + ], + "name":"Initialize", + "type":"event" + }, + { + "anonymous":false, + "inputs":[ + { + "indexed":false, + "internalType":"address", + "name":"sender", + "type":"address" + }, + { + "indexed":true, + "internalType":"address", + "name":"owner", + "type":"address" + }, + { + "indexed":true, + "internalType":"int24", + "name":"tickLower", + "type":"int24" + }, + { + "indexed":true, + "internalType":"int24", + "name":"tickUpper", + "type":"int24" + }, + { + "indexed":false, + "internalType":"uint128", + "name":"amount", + "type":"uint128" + }, + { + "indexed":false, + "internalType":"uint256", + "name":"amount0", + "type":"uint256" + }, + { + "indexed":false, + "internalType":"uint256", + "name":"amount1", + "type":"uint256" + } + ], + "name":"Mint", + "type":"event" + }, + { + "anonymous":false, + "inputs":[ + { + "indexed":false, + "internalType":"uint8", + "name":"feeProtocol0Old", + "type":"uint8" + }, + { + "indexed":false, + "internalType":"uint8", + "name":"feeProtocol1Old", + "type":"uint8" + }, + { + "indexed":false, + "internalType":"uint8", + "name":"feeProtocol0New", + "type":"uint8" + }, + { + "indexed":false, + "internalType":"uint8", + "name":"feeProtocol1New", + "type":"uint8" + } + ], + "name":"SetFeeProtocol", + "type":"event" + }, + { + "anonymous":false, + "inputs":[ + { + "indexed":true, + "internalType":"address", + "name":"sender", + "type":"address" + }, + { + "indexed":true, + "internalType":"address", + "name":"recipient", + "type":"address" + }, + { + "indexed":false, + "internalType":"int256", + "name":"amount0", + "type":"int256" + }, + { + "indexed":false, + "internalType":"int256", + "name":"amount1", + "type":"int256" + }, + { + "indexed":false, + "internalType":"uint160", + "name":"sqrtPriceX96", + "type":"uint160" + }, + { + "indexed":false, + "internalType":"uint128", + "name":"liquidity", + "type":"uint128" + }, + { + "indexed":false, + "internalType":"int24", + "name":"tick", + "type":"int24" + } + ], + "name":"Swap", + "type":"event" + }, + { + "inputs":[ + { + "internalType":"int24", + "name":"tickLower", + "type":"int24" + }, + { + "internalType":"int24", + "name":"tickUpper", + "type":"int24" + }, + { + "internalType":"uint128", + "name":"amount", + "type":"uint128" + } + ], + "name":"burn", + "outputs":[ + { + "internalType":"uint256", + "name":"amount0", + "type":"uint256" + }, + { + "internalType":"uint256", + "name":"amount1", + "type":"uint256" + } + ], + "stateMutability":"nonpayable", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"address", + "name":"recipient", + "type":"address" + }, + { + "internalType":"int24", + "name":"tickLower", + "type":"int24" + }, + { + "internalType":"int24", + "name":"tickUpper", + "type":"int24" + }, + { + "internalType":"uint128", + "name":"amount0Requested", + "type":"uint128" + }, + { + "internalType":"uint128", + "name":"amount1Requested", + "type":"uint128" + } + ], + "name":"collect", + "outputs":[ + { + "internalType":"uint128", + "name":"amount0", + "type":"uint128" + }, + { + "internalType":"uint128", + "name":"amount1", + "type":"uint128" + } + ], + "stateMutability":"nonpayable", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"address", + "name":"recipient", + "type":"address" + }, + { + "internalType":"uint128", + "name":"amount0Requested", + "type":"uint128" + }, + { + "internalType":"uint128", + "name":"amount1Requested", + "type":"uint128" + } + ], + "name":"collectProtocol", + "outputs":[ + { + "internalType":"uint128", + "name":"amount0", + "type":"uint128" + }, + { + "internalType":"uint128", + "name":"amount1", + "type":"uint128" + } + ], + "stateMutability":"nonpayable", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"factory", + "outputs":[ + { + "internalType":"address", + "name":"", + "type":"address" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"fee", + "outputs":[ + { + "internalType":"uint24", + "name":"", + "type":"uint24" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"feeGrowthGlobal0X128", + "outputs":[ + { + "internalType":"uint256", + "name":"", + "type":"uint256" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"feeGrowthGlobal1X128", + "outputs":[ + { + "internalType":"uint256", + "name":"", + "type":"uint256" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"address", + "name":"recipient", + "type":"address" + }, + { + "internalType":"uint256", + "name":"amount0", + "type":"uint256" + }, + { + "internalType":"uint256", + "name":"amount1", + "type":"uint256" + }, + { + "internalType":"bytes", + "name":"data", + "type":"bytes" + } + ], + "name":"flash", + "outputs":[ + + ], + "stateMutability":"nonpayable", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"uint16", + "name":"observationCardinalityNext", + "type":"uint16" + } + ], + "name":"increaseObservationCardinalityNext", + "outputs":[ + + ], + "stateMutability":"nonpayable", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"uint160", + "name":"sqrtPriceX96", + "type":"uint160" + } + ], + "name":"initialize", + "outputs":[ + + ], + "stateMutability":"nonpayable", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"liquidity", + "outputs":[ + { + "internalType":"uint128", + "name":"", + "type":"uint128" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"maxLiquidityPerTick", + "outputs":[ + { + "internalType":"uint128", + "name":"", + "type":"uint128" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"address", + "name":"recipient", + "type":"address" + }, + { + "internalType":"int24", + "name":"tickLower", + "type":"int24" + }, + { + "internalType":"int24", + "name":"tickUpper", + "type":"int24" + }, + { + "internalType":"uint128", + "name":"amount", + "type":"uint128" + }, + { + "internalType":"bytes", + "name":"data", + "type":"bytes" + } + ], + "name":"mint", + "outputs":[ + { + "internalType":"uint256", + "name":"amount0", + "type":"uint256" + }, + { + "internalType":"uint256", + "name":"amount1", + "type":"uint256" + } + ], + "stateMutability":"nonpayable", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"uint256", + "name":"", + "type":"uint256" + } + ], + "name":"observations", + "outputs":[ + { + "internalType":"uint32", + "name":"blockTimestamp", + "type":"uint32" + }, + { + "internalType":"int56", + "name":"tickCumulative", + "type":"int56" + }, + { + "internalType":"uint160", + "name":"secondsPerLiquidityCumulativeX128", + "type":"uint160" + }, + { + "internalType":"bool", + "name":"initialized", + "type":"bool" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"uint32[]", + "name":"secondsAgos", + "type":"uint32[]" + } + ], + "name":"observe", + "outputs":[ + { + "internalType":"int56[]", + "name":"tickCumulatives", + "type":"int56[]" + }, + { + "internalType":"uint160[]", + "name":"secondsPerLiquidityCumulativeX128s", + "type":"uint160[]" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"bytes32", + "name":"", + "type":"bytes32" + } + ], + "name":"positions", + "outputs":[ + { + "internalType":"uint128", + "name":"liquidity", + "type":"uint128" + }, + { + "internalType":"uint256", + "name":"feeGrowthInside0LastX128", + "type":"uint256" + }, + { + "internalType":"uint256", + "name":"feeGrowthInside1LastX128", + "type":"uint256" + }, + { + "internalType":"uint128", + "name":"tokensOwed0", + "type":"uint128" + }, + { + "internalType":"uint128", + "name":"tokensOwed1", + "type":"uint128" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"protocolFees", + "outputs":[ + { + "internalType":"uint128", + "name":"token0", + "type":"uint128" + }, + { + "internalType":"uint128", + "name":"token1", + "type":"uint128" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"uint8", + "name":"feeProtocol0", + "type":"uint8" + }, + { + "internalType":"uint8", + "name":"feeProtocol1", + "type":"uint8" + } + ], + "name":"setFeeProtocol", + "outputs":[ + + ], + "stateMutability":"nonpayable", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"slot0", + "outputs":[ + { + "internalType":"uint160", + "name":"sqrtPriceX96", + "type":"uint160" + }, + { + "internalType":"int24", + "name":"tick", + "type":"int24" + }, + { + "internalType":"uint16", + "name":"observationIndex", + "type":"uint16" + }, + { + "internalType":"uint16", + "name":"observationCardinality", + "type":"uint16" + }, + { + "internalType":"uint16", + "name":"observationCardinalityNext", + "type":"uint16" + }, + { + "internalType":"uint8", + "name":"feeProtocol", + "type":"uint8" + }, + { + "internalType":"bool", + "name":"unlocked", + "type":"bool" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"int24", + "name":"tickLower", + "type":"int24" + }, + { + "internalType":"int24", + "name":"tickUpper", + "type":"int24" + } + ], + "name":"snapshotCumulativesInside", + "outputs":[ + { + "internalType":"int56", + "name":"tickCumulativeInside", + "type":"int56" + }, + { + "internalType":"uint160", + "name":"secondsPerLiquidityInsideX128", + "type":"uint160" + }, + { + "internalType":"uint32", + "name":"secondsInside", + "type":"uint32" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"address", + "name":"recipient", + "type":"address" + }, + { + "internalType":"bool", + "name":"zeroForOne", + "type":"bool" + }, + { + "internalType":"int256", + "name":"amountSpecified", + "type":"int256" + }, + { + "internalType":"uint160", + "name":"sqrtPriceLimitX96", + "type":"uint160" + }, + { + "internalType":"bytes", + "name":"data", + "type":"bytes" + } + ], + "name":"swap", + "outputs":[ + { + "internalType":"int256", + "name":"amount0", + "type":"int256" + }, + { + "internalType":"int256", + "name":"amount1", + "type":"int256" + } + ], + "stateMutability":"nonpayable", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"int16", + "name":"", + "type":"int16" + } + ], + "name":"tickBitmap", + "outputs":[ + { + "internalType":"uint256", + "name":"", + "type":"uint256" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"tickSpacing", + "outputs":[ + { + "internalType":"int24", + "name":"", + "type":"int24" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + { + "internalType":"int24", + "name":"", + "type":"int24" + } + ], + "name":"ticks", + "outputs":[ + { + "internalType":"uint128", + "name":"liquidityGross", + "type":"uint128" + }, + { + "internalType":"int128", + "name":"liquidityNet", + "type":"int128" + }, + { + "internalType":"uint256", + "name":"feeGrowthOutside0X128", + "type":"uint256" + }, + { + "internalType":"uint256", + "name":"feeGrowthOutside1X128", + "type":"uint256" + }, + { + "internalType":"int56", + "name":"tickCumulativeOutside", + "type":"int56" + }, + { + "internalType":"uint160", + "name":"secondsPerLiquidityOutsideX128", + "type":"uint160" + }, + { + "internalType":"uint32", + "name":"secondsOutside", + "type":"uint32" + }, + { + "internalType":"bool", + "name":"initialized", + "type":"bool" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"token0", + "outputs":[ + { + "internalType":"address", + "name":"", + "type":"address" + } + ], + "stateMutability":"view", + "type":"function" + }, + { + "inputs":[ + + ], + "name":"token1", + "outputs":[ + { + "internalType":"address", + "name":"", + "type":"address" + } + ], + "stateMutability":"view", + "type":"function" + } + ] \ No newline at end of file diff --git a/subgraph/abis/vFIN.json b/subgraph/abis/vFIN.json new file mode 100644 index 00000000..efd37f34 --- /dev/null +++ b/subgraph/abis/vFIN.json @@ -0,0 +1,684 @@ +[ + { + "inputs": [ + { + "internalType": "address", + "name": "_owner", + "type": "address" + }, + { + "internalType": "address", + "name": "_finAddress", + "type": "address" + }, + { + "internalType": "address", + "name": "_tellerAddress", + "type": "address" + } + ], + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "inputs": [], + "name": "AccountBalanceOverflow", + "type": "error" + }, + { + "inputs": [], + "name": "BalanceQueryForZeroAddress", + "type": "error" + }, + { + "inputs": [], + "name": "NotOwnerNorApproved", + "type": "error" + }, + { + "inputs": [], + "name": "TokenAlreadyExists", + "type": "error" + }, + { + "inputs": [], + "name": "TokenDoesNotExist", + "type": "error" + }, + { + "inputs": [], + "name": "TransferFromIncorrectOwner", + "type": "error" + }, + { + "inputs": [], + "name": "TransferToNonERC721ReceiverImplementer", + "type": "error" + }, + { + "inputs": [], + "name": "TransferToZeroAddress", + "type": "error" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "account", + "type": "address" + }, + { + "indexed": true, + "internalType": "uint256", + "name": "id", + "type": "uint256" + } + ], + "name": "Approval", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "operator", + "type": "address" + }, + { + "indexed": false, + "internalType": "bool", + "name": "isApproved", + "type": "bool" + } + ], + "name": "ApprovalForAll", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "previousOwner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "newOwner", + "type": "address" + } + ], + "name": "OwnershipTransferred", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "indexed": true, + "internalType": "uint256", + "name": "id", + "type": "uint256" + } + ], + "name": "Transfer", + "type": "event" + }, + { + "inputs": [], + "name": "BOND_TOKEN_ID", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "BOND_TOTAL_SUPPLY", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "account", + "type": "address" + }, + { + "internalType": "uint256", + "name": "id", + "type": "uint256" + } + ], + "name": "approve", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "owner", + "type": "address" + } + ], + "name": "balanceOf", + "outputs": [ + { + "internalType": "uint256", + "name": "result", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint32", + "name": "positionId", + "type": "uint32" + } + ], + "name": "claim", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + }, + { + "internalType": "uint32", + "name": "positionId", + "type": "uint32" + } + ], + "name": "exchangeBond", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "finAddress", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "id", + "type": "uint256" + } + ], + "name": "getApproved", + "outputs": [ + { + "internalType": "address", + "name": "result", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "owner", + "type": "address" + }, + { + "internalType": "address", + "name": "operator", + "type": "address" + } + ], + "name": "isApprovedForAll", + "outputs": [ + { + "internalType": "bool", + "name": "result", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "name", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "operator", + "type": "address" + }, + { + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "internalType": "uint256", + "name": "id", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "value", + "type": "uint256" + }, + { + "internalType": "bytes", + "name": "data", + "type": "bytes" + } + ], + "name": "onERC1155Received", + "outputs": [ + { + "internalType": "bytes4", + "name": "", + "type": "bytes4" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "owner", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "id", + "type": "uint256" + } + ], + "name": "ownerOf", + "outputs": [ + { + "internalType": "address", + "name": "result", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "renounceOwnership", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "id", + "type": "uint256" + } + ], + "name": "safeTransferFrom", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "id", + "type": "uint256" + }, + { + "internalType": "bytes", + "name": "data", + "type": "bytes" + } + ], + "name": "safeTransferFrom", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "operator", + "type": "address" + }, + { + "internalType": "bool", + "name": "isApproved", + "type": "bool" + } + ], + "name": "setApprovalForAll", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "startLinearVest", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bytes4", + "name": "interfaceId", + "type": "bytes4" + } + ], + "name": "supportsInterface", + "outputs": [ + { + "internalType": "bool", + "name": "", + "type": "bool" + } + ], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [], + "name": "symbol", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [], + "name": "tellerAddress", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "id", + "type": "uint256" + } + ], + "name": "tokenURI", + "outputs": [ + { + "internalType": "string", + "name": "", + "type": "string" + } + ], + "stateMutability": "pure", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "from", + "type": "address" + }, + { + "internalType": "address", + "name": "to", + "type": "address" + }, + { + "internalType": "uint256", + "name": "id", + "type": "uint256" + } + ], + "name": "transferFrom", + "outputs": [], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "newOwner", + "type": "address" + } + ], + "name": "transferOwnership", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [], + "name": "vestEndTime", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "name": "vestPositions", + "outputs": [ + { + "internalType": "uint128", + "name": "amount", + "type": "uint128" + }, + { + "internalType": "uint32", + "name": "lastClaimTimestamp", + "type": "uint32" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "vestStartTime", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "vestState", + "outputs": [ + { + "internalType": "uint32", + "name": "idNext", + "type": "uint32" + }, + { + "internalType": "bool", + "name": "started", + "type": "bool" + }, + { + "internalType": "bool", + "name": "ended", + "type": "bool" + }, + { + "internalType": "bool", + "name": "withdrawn", + "type": "bool" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "uint32", + "name": "positionId", + "type": "uint32" + } + ], + "name": "viewClaim", + "outputs": [ + { + "internalType": "uint256", + "name": "vestedAmount", + "type": "uint256" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bool", + "name": "redeem", + "type": "bool" + } + ], + "name": "withdraw", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + } +] \ No newline at end of file diff --git a/subgraph/config/arbitrum-goerli.json b/subgraph/config/arbitrum-goerli.json new file mode 100644 index 00000000..a9400f22 --- /dev/null +++ b/subgraph/config/arbitrum-goerli.json @@ -0,0 +1,48 @@ +{ + "Network": "arbitrum-goerli", + "Contracts": { + "Weth": "0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7", + "StablePool": { + "Address": "0x4998545e13a668a884272aaebff14ab21c5b4e89", + "StableIsToken0": true + }, + "RangeStaker": "0xe5e2E95A986CE078606C403593593b18Ed98f4d6", + "LimitPoolFactory": "0x1b215002e688135549cc0290d6cf1f94e3aa425c", + "LimitPoolManager": "0x6d3af137e75097b892683832a2c0132f99625d0e", + "Stablecoins": [ + "0x19bee8e887a5db5cf20a841eb4daacbcacf14b1b", + "0xebff7a98149b4774c9743c5d1f382305fe5422c9" + ], + "WhitelistedTokens": [ + "0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7", + "0x19bee8e887a5db5cf20a841eb4daacbcacf14b1b", + "0xebff7a98149b4774c9743c5d1f382305fe5422c9" + ], + "WhitelistedPairs": [ + "0xebff7a98149b4774c9743c5d1f382305fe5422c9-0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7" + ] + }, + "StartBlocks": { + "LimitPoolFactory": 56821935, + "LimitPoolManager": 56821935 + }, + "Symbols": { + "Stablecoins": [ + "DAI", + "USDC" + ], + "WhitelistedTokens": [ + "WETH", + "DAI", + "USDC" + ], + + "WhitelistedPairs": [ + "WETH-USDC" + ] + }, + "Season1": { + "startTime": "0", + "endTime": "2000707154" + } + } \ No newline at end of file diff --git a/subgraph/package.json b/subgraph/package.json index 358e261f..3cc54d57 100644 --- a/subgraph/package.json +++ b/subgraph/package.json @@ -1,5 +1,5 @@ { - "name": "poolsharkhedgepool-subgraph", + "name": "poolshark-limit-subgraph", "version": "0.0.1", "repository": "https://github.com/poolshark-protocol/limit", "license": "MIT", @@ -17,32 +17,27 @@ "codegen": "graph codegen", "build": "graph build", "deploy": "graph deploy --product hosted-service alphak3y/poolshark-limit", - "deploy-sats": "graph deploy limit-arbitrumGoerli --version-label v0.1.0 --node https://app.satsuma.xyz/api/subgraphs/deploy --deploy-key 7NoUUXPcOGfBX --ipfs https://ipfs.satsuma.xyz", - "deploy-staging": "graph deploy --version-label v0.3.1 --node https://api.graph-eu.p2pify.com/cc955503f93d46512a78fb9a70796dac/deploy --ipfs https://api.graph-eu.p2pify.com/cc955503f93d46512a78fb9a70796dac/ipfs staging-limit-arbitrumGoerli", - "deploy-beta2": "graph deploy --version-label v0.4.0 --node https://api.graph-ams.p2pify.com/3bd8a0adf573b40768f68ef92c7bd843/deploy --ipfs https://api.graph-ams.p2pify.com/3bd8a0adf573b40768f68ef92c7bd843/ipfs limit-arbitrumGoerli-beta2", - "deploy-test": "graph deploy --version-label v0.3.0 --node https://api.graph-ams.p2pify.com/abafff8142f8181262d18b7dfeac1236/deploy --ipfs https://api.graph-ams.p2pify.com/abafff8142f8181262d18b7dfeac1236/ipfs limit-arbitrumGoerli-test", - "deploy-ordertest": "graph deploy --version-label v0.1.0 --node https://api.graph-ams.p2pify.com/719c33840eec545dba4ed49d362448f0/deploy --ipfs https://api.graph-ams.p2pify.com/719c33840eec545dba4ed49d362448f0/ipfs limit-arbitrumGoerli-order-testing", - "deploy-chainstack": "graph deploy --node https://api.graph-eu.p2pify.com/cc955503f93d46512a78fb9a70796dac/deploy --ipfs https://api.graph-eu.p2pify.com/cc955503f93d46512a78fb9a70796dac/ipfs staging-limit-arbitrumGoerli", - "deploy-op-test": "graph deploy --product hosted-service alphak3y/poolshark-cover-op-goerli", - "deploy-hs": "graph deploy --product hosted-service alphak3y/poolshark-limit", - "deploy-local": "graph deploy example --ipfs http://localhost:5001 --node http://127.0.0.1:8020", - "test": "graph codegen; graph test -v 0.2.0" - }, - "devDependencies": { - "@graphprotocol/graph-cli": "0.25.1", - "@graphprotocol/graph-ts": "0.24.1", - "eslint": "7.32.0", - "eslint-config-prettier": "8.5.0", - "eslint-config-standard": "16.0.3", - "eslint-plugin-prettier": "3.4.1", - "libpq": "1.8.12", - "matchstick-as": "0.2.0" + "deploy-scroll": "graph deploy --product hosted-service alphak3y/poolshark-limit-scroll-mainnet", + "deploy-scroll-sepolia": "graph deploy --product hosted-service alphak3y/limit-scroll-sepolia", + "test": "graph codegen; graph test -v 0.2.0", + "prepare:subgraph": "mustache ./config/${NETWORK}.json ./templates/${BLOCKCHAIN}.subgraph.template.yaml > subgraph.yaml && rm -rf generated && npm run generate:schema && npm run subgraph:codegen && npm run subgraph:build", + "deploy:chainstack:arb-goerli": "BLOCKCHAIN=arbitrum =arbitrum-goerli env-cmd npm run prepare:subgraph && SLUG=aave/protocol-v2-goerli env-cmd npm run subgraph:deploy:chainstack:ordertest" }, "dependencies": { - "assemblyscript": "0.20.19", - "assemblyscript-json": "1.1.0", - "json-as": "0.2.6", - "node-gyp": "9.1.0", - "source-map-support": "0.5.21" + "babel-polyfill": "^6.26.0", + "babel-register": "^6.26.0" + }, + "devDependencies": { + "@graphprotocol/graph-cli": "^0.60.0", + "@graphprotocol/graph-ts": "^0.31.0", + "@trivago/prettier-plugin-sort-imports": "4.1.0", + "@typescript-eslint/eslint-plugin": "^5.59.11", + "@typescript-eslint/parser": "^5.59.11", + "eslint": "^8.25.0", + "eslint-config-prettier": "^8.5.0", + "mustache": "^4.2.0", + "prettier": "^2.8.8", + "rimraf": "^3.0.2", + "typescript": "5.1.3" } } diff --git a/subgraph/schema.graphql b/subgraph/schema.graphql index ee1a45f7..374b2522 100644 --- a/subgraph/schema.graphql +++ b/subgraph/schema.graphql @@ -201,7 +201,7 @@ type Transaction @entity { timestamp: BigInt! gasLimit: BigInt! gasPrice: BigInt! - swaps: [Swap!]! @derivedFrom(field: "transaction") + swaps: [Swap!]! } type LimitPoolType @entity { @@ -371,3 +371,12 @@ type UserSeasonReward @entity { stakingPoints: BigInt! } +type VFinPosition @entity { + # vFinAddress + positionId + id: ID! + + owner: Bytes! + positionId: BigInt! + vFinAddress: Bytes! +} + diff --git a/subgraph/src/constants/arbitrum-sepolia.ts b/subgraph/src/constants/arbitrum-sepolia.ts new file mode 100644 index 00000000..aa0c929b --- /dev/null +++ b/subgraph/src/constants/arbitrum-sepolia.ts @@ -0,0 +1,53 @@ +/* eslint-disable */ +import { BigInt, BigDecimal, Address } from '@graphprotocol/graph-ts' +import { LimitPoolFactory as FactoryContract } from '../../generated/templates/LimitPoolTemplate/LimitPoolFactory' + +// -------------- START REQUIRED CONFIG PER DEPLOYMENT -------------- +export let FACTORY_ADDRESS = '0x8e40c68b7546efd009a1a300c92e25da3c8725dc' +export let RANGE_STAKER_ADDRESS = '0x62e0671022af1b2e705f08b282767c57d29c7c4c' +// used for safe eth pricing +export const STABLE_POOL_ADDRESS = '0x02225f6a3d83648d7906a23856331c819265394d' +// -------------- END REQUIRED CONFIG PER DEPLOYMENT -------------- + +// determines which token to use for eth <-> usd rate, true means stable is token0 in pool above +export const STABLE_IS_TOKEN_0 = false +export const STABLE_TOKEN_DECIMALS = '18' + +// chain WETH address +export let WETH_ADDRESS = '0x414b73f989e7ca0653b5c98186749a348405e6d5' + +// tokens where USD value is safe to use for globals +export let WHITELISTED_TOKENS: string[] = [ + '0x414b73f989e7ca0653b5c98186749a348405e6d5', // WETH + '0x9f479560cd8a531e6c0fe04521cb246264fe6b71', // DAI +] + +//TODO: incentivizing only FIN - WETH 0.3% for now + +export let WHITELISTED_PAIRS: string[] = [ +] + +export let SEASON_1_START_TIME = BigInt.fromString('1574345600') // 11-1-2019 0:00 GMT +export let SEASON_1_END_TIME = BigInt.fromString('1684713600') // 5-22-2024 0:00 GMT + +// used for safe eth pricing +export let STABLE_COINS: string[] = [ + '0x9f479560cd8a531e6c0fe04521cb246264fe6b71', // DAI +] + +// minimum eth required in pool to count usd values towards global prices +export let MINIMUM_ETH_LOCKED = BigDecimal.fromString('0') + +// pool that breaks with subgraph logic +export const ERROR_POOL = '0x0000000000000000000000000000000000000000' + +export let ZERO_ADDRESS = '0x0000000000000000000000000000000000000000' + +export let ZERO_BI = BigInt.fromI32(0) +export let ONE_BI = BigInt.fromI32(1) +export let ZERO_BD = BigDecimal.fromString('0') +export let ONE_BD = BigDecimal.fromString('1') +export let TWO_BD = BigDecimal.fromString('2') +export let BI_18 = BigInt.fromI32(18) + +export let factoryContract = FactoryContract.bind(Address.fromString(FACTORY_ADDRESS)) \ No newline at end of file diff --git a/subgraph/src/constants/arbitrum.ts b/subgraph/src/constants/arbitrum.ts new file mode 100644 index 00000000..662446d7 --- /dev/null +++ b/subgraph/src/constants/arbitrum.ts @@ -0,0 +1,66 @@ +/* eslint-disable */ +import { BigInt, BigDecimal, Address } from '@graphprotocol/graph-ts' +import { LimitPoolFactory as FactoryContract } from '../../generated/templates/LimitPoolTemplate/LimitPoolFactory' + +// -------------- START REQUIRED CONFIG PER DEPLOYMENT -------------- +export let FACTORY_ADDRESS = '0x8bb5db1625adb4ae4beb94a188d33062303f8fb7' +export let RANGE_STAKER_ADDRESS = '0x0e2b069fa52064a7e0b5a044ba25142203210a13' +// used for safe eth pricing +export const STABLE_POOL_ADDRESS = '0xc31e54c7a869b9fcbecc14363cf510d1c41fa443' +// -------------- END REQUIRED CONFIG PER DEPLOYMENT -------------- + +// determines which token to use for eth <-> usd rate, true means stable is token0 in pool above +export const STABLE_IS_TOKEN_0 = false +export const STABLE_TOKEN_DECIMALS = '6' + +// chain WETH address +export let WETH_ADDRESS = '0x82af49447d8a07e3bd95bd0d56f35241523fbab1' + +// tokens where USD value is safe to use for globals +export let WHITELISTED_TOKENS: string[] = [ + '0x82af49447d8a07e3bd95bd0d56f35241523fbab1', // WETH + '0xda10009cbd5d07dd0cecc66161fc93d7c9000da1', // DAI + '0xaf88d065e77c8cc2239327c5edb3a432268e5831', // USDC + '0xff970a61a04b1ca14834a43f5de4533ebddb5cc8', // USDC.e + '0x5979d7b546e38e414f7e9822514be443a4800529', // wstETH + '0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9' // USDT +] + +//TODO: incentivizing only FIN - WETH 0.3% for now + +export let WHITELISTED_PAIRS: string[] = [ + '0x82af49447d8a07e3bd95bd0d56f35241523fbab1-0xaf88d065e77c8cc2239327c5edb3a432268e5831', // WETH - USDC + '0xaf88d065e77c8cc2239327c5edb3a432268e5831-0xff970a61a04b1ca14834a43f5de4533ebddb5cc8', // USDC - USDC.e + '0x5979d7b546e38e414f7e9822514be443a4800529-0x82af49447d8a07e3bd95bd0d56f35241523fbab1', // wstETH - WETH + '0x82af49447d8a07e3bd95bd0d56f35241523fbab1-0x912ce59144191c1204e64559fe8253a0e49e6548', // WETH - ARB + '0x912ce59144191c1204e64559fe8253a0e49e6548-0xaf88d065e77c8cc2239327c5edb3a432268e5831', // ARB - USDC + '0xaf88d065e77c8cc2239327c5edb3a432268e5831-0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9' // USDT - USDC +] + +export let SEASON_1_START_TIME = BigInt.fromString('1674345600') // 1-22-2024 0:00 GMT +export let SEASON_1_END_TIME = BigInt.fromString('1684713600') // 5-22-2024 0:00 GMT + +// used for safe eth pricing +export let STABLE_COINS: string[] = [ + '0xda10009cbd5d07dd0cecc66161fc93d7c9000da1', // DAI + '0xaf88d065e77c8cc2239327c5edb3a432268e5831', // USDC + '0xff970a61a04b1ca14834a43f5de4533ebddb5cc8', // USDC.e + '0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9' // USDT +] + +// minimum eth required in pool to count usd values towards global prices +export let MINIMUM_ETH_LOCKED = BigDecimal.fromString('0') + +// pool that breaks with subgraph logic +export const ERROR_POOL = '0x0000000000000000000000000000000000000000' + +export let ZERO_ADDRESS = '0x0000000000000000000000000000000000000000' + +export let ZERO_BI = BigInt.fromI32(0) +export let ONE_BI = BigInt.fromI32(1) +export let ZERO_BD = BigDecimal.fromString('0') +export let ONE_BD = BigDecimal.fromString('1') +export let TWO_BD = BigDecimal.fromString('2') +export let BI_18 = BigInt.fromI32(18) + +export let factoryContract = FactoryContract.bind(Address.fromString(FACTORY_ADDRESS)) \ No newline at end of file diff --git a/subgraph/src/constants/constants.ts b/subgraph/src/constants/constants.ts index 5df8faa0..04d6d883 100644 --- a/subgraph/src/constants/constants.ts +++ b/subgraph/src/constants/constants.ts @@ -1,49 +1,5 @@ -/* eslint-disable */ -import { BigInt, BigDecimal, Address } from '@graphprotocol/graph-ts' -import { LimitPoolFactory as FactoryContract } from '../../generated/templates/LimitPoolTemplate/LimitPoolFactory' -export let FACTORY_ADDRESS = '0x1b215002e688135549cc0290d6cf1f94e3aa425c' -export let RANGE_STAKER_ADDRESS = '0xe5e2E95A986CE078606C403593593b18Ed98f4d6' -export let WETH_ADDRESS = '0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7' +import { + STABLE_IS_TOKEN_0, FACTORY_ADDRESS, RANGE_STAKER_ADDRESS, STABLE_POOL_ADDRESS, STABLE_TOKEN_DECIMALS, WETH_ADDRESS, WHITELISTED_TOKENS, WHITELISTED_PAIRS, SEASON_1_END_TIME, SEASON_1_START_TIME, STABLE_COINS, MINIMUM_ETH_LOCKED, ERROR_POOL, ZERO_ADDRESS, ZERO_BI, ONE_BI, ONE_BD, ZERO_BD, TWO_BD, BI_18, factoryContract +} from "./arbitrum-sepolia"; -// tokens where USD value is safe to use for globals -export let WHITELISTED_TOKENS: string[] = [ - '0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7', // WETH - '0x19bee8e887a5db5cf20a841eb4daacbcacf14b1b', // DAI - '0xebff7a98149b4774c9743c5d1f382305fe5422c9' // USDC -] - -export let WHITELISTED_PAIRS: string[] = [ - '0xebff7a98149b4774c9743c5d1f382305fe5422c9-0xefb283ef3167ca2ee9d93b201af15e2af3f6e8c7' // WETH - USDC -] - -export let SEASON_1_START_TIME = BigInt.fromString('0') -export let SEASON_1_END_TIME = BigInt.fromString('2000707154') - -// used for safe eth pricing -export let STABLE_COINS: string[] = [ - '0x19bee8e887a5db5cf20a841eb4daacbcacf14b1b', //DAI - '0xebff7a98149b4774c9743c5d1f382305fe5422c9' //USDC -] - -// used for safe eth pricing -export const STABLE_POOL_ADDRESS = '0x4998545e13a668a884272aaebff14ab21c5b4e89' - -// determines which token to use for eth <-> usd rate, true means stable is token0 in pool above -export const STABLE_IS_TOKEN_0 = true - -// minimum eth required in pool to count usd values towards global prices -export let MINIMUM_ETH_LOCKED = BigDecimal.fromString('0') - -// pool that breaks with subgraph logic -export const ERROR_POOL = '0x0000000000000000000000000000000000000000' - -export let ZERO_ADDRESS = '0x0000000000000000000000000000000000000000' - -export let ZERO_BI = BigInt.fromI32(0) -export let ONE_BI = BigInt.fromI32(1) -export let ZERO_BD = BigDecimal.fromString('0') -export let ONE_BD = BigDecimal.fromString('1') -export let TWO_BD = BigDecimal.fromString('2') -export let BI_18 = BigInt.fromI32(18) - -export let factoryContract = FactoryContract.bind(Address.fromString(FACTORY_ADDRESS)) \ No newline at end of file +export { STABLE_IS_TOKEN_0, FACTORY_ADDRESS, RANGE_STAKER_ADDRESS, STABLE_POOL_ADDRESS, STABLE_TOKEN_DECIMALS, WETH_ADDRESS, WHITELISTED_TOKENS, WHITELISTED_PAIRS, SEASON_1_END_TIME, SEASON_1_START_TIME, STABLE_COINS, MINIMUM_ETH_LOCKED, ERROR_POOL, ZERO_ADDRESS, ZERO_BI, ONE_BI, ONE_BD, ZERO_BD, TWO_BD, BI_18, factoryContract}; \ No newline at end of file diff --git a/subgraph/src/constants/scroll-sepolia.ts b/subgraph/src/constants/scroll-sepolia.ts new file mode 100644 index 00000000..156302a8 --- /dev/null +++ b/subgraph/src/constants/scroll-sepolia.ts @@ -0,0 +1,52 @@ +/* eslint-disable */ +import { BigInt, BigDecimal, Address } from '@graphprotocol/graph-ts' +import { LimitPoolFactory as FactoryContract } from '../../generated/templates/LimitPoolTemplate/LimitPoolFactory' + +// -------------- START REQUIRED CONFIG PER DEPLOYMENT -------------- +export let FACTORY_ADDRESS = '0xd506193e48f13438e35a1edc4ef5394876b5efa4' +export let RANGE_STAKER_ADDRESS = '0x83eea57c3536a7ec64ead2c67d5b6bfeb6593720' +// used for safe eth pricing +export const STABLE_POOL_ADDRESS = '0xea578474c65f859739c8ec362543eedc04f0d48e' +// -------------- END REQUIRED CONFIG PER DEPLOYMENT -------------- + +// determines which token to use for eth <-> usd rate, true means stable is token0 in pool above +export const STABLE_IS_TOKEN_0 = false + +// chain WETH address +export let WETH_ADDRESS = '0xf04bf4e3e8157ba5b91bfda16e21be770e7ac790' + +// tokens where USD value is safe to use for globals +export let WHITELISTED_TOKENS: string[] = [ + '0xf04bf4e3e8157ba5b91bfda16e21be770e7ac790', // WETH + '0xda10009cbd5d07dd0cecc66161fc93d7c9000da1', // DAI +] + +//TODO: incentivizing only FIN - WETH 0.3% for now + +export let WHITELISTED_PAIRS: string[] = [ +] + +export let SEASON_1_START_TIME = BigInt.fromString('1674345600') // 1-22-2024 0:00 GMT +export let SEASON_1_END_TIME = BigInt.fromString('1684713600') // 5-22-2024 0:00 GMT + +// used for safe eth pricing +export let STABLE_COINS: string[] = [ + '0xa9e1ab5e6878621F80E03A4a5F8FB3705F4FFA2B', // DAI +] + +// minimum eth required in pool to count usd values towards global prices +export let MINIMUM_ETH_LOCKED = BigDecimal.fromString('0') + +// pool that breaks with subgraph logic +export const ERROR_POOL = '0x0000000000000000000000000000000000000000' + +export let ZERO_ADDRESS = '0x0000000000000000000000000000000000000000' + +export let ZERO_BI = BigInt.fromI32(0) +export let ONE_BI = BigInt.fromI32(1) +export let ZERO_BD = BigDecimal.fromString('0') +export let ONE_BD = BigDecimal.fromString('1') +export let TWO_BD = BigDecimal.fromString('2') +export let BI_18 = BigInt.fromI32(18) + +export let factoryContract = FactoryContract.bind(Address.fromString(FACTORY_ADDRESS)) \ No newline at end of file diff --git a/subgraph/src/mappings/limitpool.ts b/subgraph/src/mappings/limitpool.ts index e982600c..27ddf7ee 100644 --- a/subgraph/src/mappings/limitpool.ts +++ b/subgraph/src/mappings/limitpool.ts @@ -11,15 +11,15 @@ import { handleMintRange as handleMintRangeHelper } from './range/mint' import { handleBurnRange as handleBurnRangeHelper } from './range/burn' import { handleCompoundRange as handleCompoundRangeHelper } from './range/compound' import { handleSyncRangeTick as handleSyncRangeTickHelper } from './range/synctick' -import { BurnLimit, BurnRange, CollectRange0, CollectRange1, CompoundRange, Initialize, MintLimit, MintRange, SampleCountIncreased, SampleRecorded, Swap, SyncLimitLiquidity, SyncLimitPool, SyncLimitTick, SyncRangeTick } from '../../generated/LimitPoolFactory/LimitPool' -import { handleCollectRange0 as handleCollectRange0Helper, handleCollectRange1 as handleCollectRange1Helper } from './range/collect' +import { BurnLimit, BurnRange, Collect, CompoundRange, InitializeLimit, MintLimit, MintRange, SampleCountIncreased, SampleRecorded, Swap, SwapLimit, SyncLimitLiquidity, SyncLimitPool, SyncLimitTick, SyncRangeTick } from '../../generated/LimitPoolFactory/LimitPool' +import { handleCollect as handleCollectHelper } from './range/collect' // pool -export function handleInitialize(event: Initialize): void { +export function handleInitialize(event: InitializeLimit): void { handleInitializeHelper(event) } -export function handleSwap(event: Swap): void { +export function handleSwap(event: SwapLimit): void { handleSwapHelper(event) } @@ -49,12 +49,8 @@ export function handleSyncRangeTick(event: SyncRangeTick): void { handleSyncRangeTickHelper(event) } -export function handleCollectRange0(event: CollectRange0): void { - handleCollectRange0Helper(event) -} - -export function handleCollectRange1(event: CollectRange1): void { - handleCollectRange1Helper(event) +export function handleCollect(event: Collect): void { + handleCollectHelper(event) } // limit positions diff --git a/subgraph/src/mappings/limitpoolfactory.ts b/subgraph/src/mappings/limitpoolfactory.ts index 608f466f..8afaa4bd 100644 --- a/subgraph/src/mappings/limitpoolfactory.ts +++ b/subgraph/src/mappings/limitpoolfactory.ts @@ -1,5 +1,5 @@ import { Address, log } from '@graphprotocol/graph-ts' -import { PoolCreated } from '../../generated/LimitPoolFactory/LimitPoolFactory' +import { LimitPoolCreated, PoolCreated } from '../../generated/LimitPoolFactory/LimitPoolFactory' import { LimitPoolTemplate, PositionERC1155Template, RangeStakerTemplate } from '../../generated/templates' import { fetchTokenSymbol, @@ -11,7 +11,7 @@ import { safeLoadLimitPool, safeLoadLimitPoolFactory, safeLoadLimitPoolToken, sa import { BigInt } from '@graphprotocol/graph-ts' import { FACTORY_ADDRESS, ONE_BI, RANGE_STAKER_ADDRESS, STABLE_COINS, WETH_ADDRESS } from '../constants/constants' -export function handlePoolCreated(event: PoolCreated): void { +export function handlePoolCreated(event: LimitPoolCreated): void { // grab event parameters let poolAddressParam = event.params.pool let poolTokenParam = event.params.token diff --git a/subgraph/src/mappings/limitpoolmanager.ts b/subgraph/src/mappings/limitpoolmanager.ts index 62aa4a88..c5278bfb 100644 --- a/subgraph/src/mappings/limitpoolmanager.ts +++ b/subgraph/src/mappings/limitpoolmanager.ts @@ -2,6 +2,7 @@ import { safeLoadManager, safeLoadLimitPoolFactory, safeLoadFeeTier } from './ut import { BigInt, log } from '@graphprotocol/graph-ts' import { FACTORY_ADDRESS } from '../constants/constants' import { FactoryChanged, FeeTierEnabled, FeeToTransfer, PoolTypeEnabled, OwnerTransfer, ProtocolSwapFeesModified, ProtocolFeesCollected, ProtocolFillFeesModified } from '../../generated/LimitPoolManager/LimitPoolManager' +import { FeeTier } from '../../generated/schema' export function handleFeeTierEnabled(event: FeeTierEnabled): void { let swapFeeParam = event.params.swapFee @@ -72,10 +73,9 @@ export function handleOwnerTransfer(event: OwnerTransfer): void { if(!loadManager.exists) { manager.feeTo = newOwnerParam - // manager.factory = FACTORY_ADDRESS } if(!loadFactory.exists) { - //factory.owner = manager.id + factory.manager = manager.id } manager.owner = newOwnerParam diff --git a/subgraph/src/mappings/pool/initialize.ts b/subgraph/src/mappings/pool/initialize.ts index f3a012cb..4adb0cb3 100644 --- a/subgraph/src/mappings/pool/initialize.ts +++ b/subgraph/src/mappings/pool/initialize.ts @@ -1,9 +1,9 @@ import { BigInt, log } from "@graphprotocol/graph-ts" -import { Initialize } from "../../../generated/LimitPoolFactory/LimitPool" +import { Initialize, InitializeLimit } from "../../../generated/LimitPoolFactory/LimitPool" import { safeLoadBasePrice, safeLoadLimitPool, safeLoadLimitTick, safeLoadRangeTick, safeLoadToken } from "../utils/loads" import { sqrtPriceX96ToTokenPrices, findEthPerToken } from "../utils/price" -export function handleInitialize(event: Initialize): void { +export function handleInitialize(event: InitializeLimit): void { let minTickParam = event.params.minTick let maxTickParam = event.params.maxTick let startPriceParam = event.params.startPrice diff --git a/subgraph/src/mappings/pool/swap.ts b/subgraph/src/mappings/pool/swap.ts index 5fdf0f4a..0bfa4a01 100644 --- a/subgraph/src/mappings/pool/swap.ts +++ b/subgraph/src/mappings/pool/swap.ts @@ -4,10 +4,10 @@ import { BIGDECIMAL_ZERO, BIGINT_ONE, BIGINT_ZERO, convertTokenToDecimal } from import { ZERO_BD, TWO_BD, ONE_BI, FACTORY_ADDRESS, SEASON_1_END_TIME, SEASON_1_START_TIME } from "../../constants/constants" import { AmountType, findEthPerToken, getAdjustedAmounts, getEthPriceInUSD, sqrtPriceX96ToTokenPrices } from "../utils/price" import { updateDerivedTVLAmounts } from "../utils/tvl" -import { Swap } from "../../../generated/LimitPoolFactory/LimitPool" +import { Swap, SwapLimit } from "../../../generated/LimitPoolFactory/LimitPool" import { safeDiv } from "../utils/math" -export function handleSwap(event: Swap): void { +export function handleSwap(event: SwapLimit): void { let recipientParam = event.params.recipient let amountInParam = event.params.amountIn let amountOutParam = event.params.amountOut diff --git a/subgraph/src/mappings/range/collect.ts b/subgraph/src/mappings/range/collect.ts index e845200d..73b5a996 100644 --- a/subgraph/src/mappings/range/collect.ts +++ b/subgraph/src/mappings/range/collect.ts @@ -1,121 +1,51 @@ -import { safeLoadLimitPool, safeLoadLimitPoolFactory, safeLoadToken, safeLoadBasePrice, safeLoadTvlUpdateLog } from "../utils/loads" +import { safeLoadLimitPool, safeLoadLimitPoolFactory, safeLoadToken, safeLoadBasePrice } from "../utils/loads" import { BIGINT_ZERO, convertTokenToDecimal } from "../utils/helpers" -import { CollectRange0, CollectRange1 } from "../../../generated/LimitPoolFactory/LimitPool" import { findEthPerToken } from "../utils/price" import { updateDerivedTVLAmounts } from "../utils/tvl" - -export function handleCollectRange0(event: CollectRange0): void { - // let amount0Param = event.params.amount0 - // let amount1Param = BIGINT_ZERO - // let poolAddress = event.address.toHex() - // let senderParam = event.transaction.from - - // let loadLimitPool = safeLoadLimitPool(poolAddress) - // let pool = loadLimitPool.entity // 1 - - // let loadLimitPoolFactory = safeLoadLimitPoolFactory(pool.factory) - // let loadToken0 = safeLoadToken(pool.token0) - // let loadToken1 = safeLoadToken(pool.token1) - // let loadBasePrice = safeLoadBasePrice('eth') - - // let factory = loadLimitPoolFactory.entity // 2 - // let token0 = loadToken0.entity // 3 - // let token1 = loadToken1.entity // 4 - // let basePrice = loadBasePrice.entity // 5 - - // let amount0 = convertTokenToDecimal(amount0Param, token0.decimals) - // let amount1 = convertTokenToDecimal(amount1Param, token1.decimals) - // let amountUsd = amount0 - // .times(token0.ethPrice.times(basePrice.USD)) - // .plus(amount1.times(token1.ethPrice.times(basePrice.USD))) - - // // eth price updates - // token0.ethPrice = findEthPerToken(token0, token1, basePrice) - // token1.ethPrice = findEthPerToken(token1, token0, basePrice) - // token0.usdPrice = token0.ethPrice.times(basePrice.USD) - // token1.usdPrice = token1.ethPrice.times(basePrice.USD) - - // // tvl updates - // let oldPoolTotalValueLockedEth = pool.totalValueLockedEth - // token0.totalValueLocked = token0.totalValueLocked.minus(amount0) - // token1.totalValueLocked = token1.totalValueLocked.minus(amount1) - // pool.totalValueLocked0 = pool.totalValueLocked0.minus(amount0) - // pool.totalValueLocked1 = pool.totalValueLocked1.minus(amount1) - // let updateTvlRet = updateDerivedTVLAmounts(token0, token1, pool, factory, basePrice, oldPoolTotalValueLockedEth) - // token0 = updateTvlRet.token0 - // token1 = updateTvlRet.token1 - // pool = updateTvlRet.pool - // factory = updateTvlRet.factory - - // let loadTvlUpdateLog = safeLoadTvlUpdateLog(event.transaction.hash, poolAddress) - // let tvlUpdateLog = loadTvlUpdateLog.entity - - // tvlUpdateLog.pool = poolAddress - // tvlUpdateLog.eventName = "CollectRange" - // tvlUpdateLog.txnHash = event.transaction.hash - // tvlUpdateLog.txnBlockNumber = event.block.number - // tvlUpdateLog.amount0Change = amount0.neg() - // tvlUpdateLog.amount1Change = amount1.neg() - // tvlUpdateLog.amount0Total = pool.totalValueLocked0 - // tvlUpdateLog.amount1Total = pool.totalValueLocked1 - // tvlUpdateLog.token0UsdPrice = token0.usdPrice - // tvlUpdateLog.token1UsdPrice = token1.usdPrice - // tvlUpdateLog.amountUsdChange = amount0 - // .times(token0.ethPrice.times(basePrice.USD)) - // .plus(amount1.times(token1.ethPrice.times(basePrice.USD))).neg() - // tvlUpdateLog.amountUsdTotal = pool.totalValueLockedUsd - - // tvlUpdateLog.save() - - // pool.save() - // token0.save() - // token1.save() - // factory.save() - // basePrice.save() -} - -export function handleCollectRange1(event: CollectRange1): void { - // let amount0Param = BIGINT_ZERO - // let amount1Param = event.params.amount1 - // let poolAddress = event.address.toHex() - // let senderParam = event.transaction.from - - // let loadLimitPool = safeLoadLimitPool(poolAddress) - // let pool = loadLimitPool.entity // 1 - - // let loadLimitPoolFactory = safeLoadLimitPoolFactory(pool.factory) - // let loadToken0 = safeLoadToken(pool.token0) - // let loadToken1 = safeLoadToken(pool.token1) - // let loadBasePrice = safeLoadBasePrice('eth') - - // let factory = loadLimitPoolFactory.entity // 2 - // let token0 = loadToken0.entity // 3 - // let token1 = loadToken1.entity // 4 - // let basePrice = loadBasePrice.entity // 5 - - // let amount0 = convertTokenToDecimal(amount0Param, token0.decimals) - // let amount1 = convertTokenToDecimal(amount1Param, token1.decimals) - // let amountUsd = amount0 - // .times(token0.ethPrice.times(basePrice.USD)) - // .plus(amount1.times(token1.ethPrice.times(basePrice.USD))) - - // // eth price updates - // token0.ethPrice = findEthPerToken(token0, token1, basePrice) - // token1.ethPrice = findEthPerToken(token1, token0, basePrice) - // token0.usdPrice = token0.ethPrice.times(basePrice.USD) - // token1.usdPrice = token1.ethPrice.times(basePrice.USD) - - // // tvl updates - // let oldPoolTotalValueLockedEth = pool.totalValueLockedEth - // token0.totalValueLocked = token0.totalValueLocked.minus(amount0) - // token1.totalValueLocked = token1.totalValueLocked.minus(amount1) - // pool.totalValueLocked0 = pool.totalValueLocked0.minus(amount0) - // pool.totalValueLocked1 = pool.totalValueLocked1.minus(amount1) - // let updateTvlRet = updateDerivedTVLAmounts(token0, token1, pool, factory, basePrice, oldPoolTotalValueLockedEth) - // token0 = updateTvlRet.token0 - // token1 = updateTvlRet.token1 - // pool = updateTvlRet.pool - // factory = updateTvlRet.factory +import { Collect } from "../../../generated/LimitPoolFactory/LimitPool" + +export function handleCollect(event: Collect): void { + let amount0Param = event.params.amount0 + let amount1Param = BIGINT_ZERO + let poolAddress = event.address.toHex() + let senderParam = event.transaction.from + + let loadLimitPool = safeLoadLimitPool(poolAddress) + let pool = loadLimitPool.entity // 1 + + let loadLimitPoolFactory = safeLoadLimitPoolFactory(pool.factory) + let loadToken0 = safeLoadToken(pool.token0) + let loadToken1 = safeLoadToken(pool.token1) + let loadBasePrice = safeLoadBasePrice('eth') + + let factory = loadLimitPoolFactory.entity // 2 + let token0 = loadToken0.entity // 3 + let token1 = loadToken1.entity // 4 + let basePrice = loadBasePrice.entity // 5 + + let amount0 = convertTokenToDecimal(amount0Param, token0.decimals) + let amount1 = convertTokenToDecimal(amount1Param, token1.decimals) + let amountUsd = amount0 + .times(token0.ethPrice.times(basePrice.USD)) + .plus(amount1.times(token1.ethPrice.times(basePrice.USD))) + + // eth price updates + token0.ethPrice = findEthPerToken(token0, token1, pool, basePrice) + token1.ethPrice = findEthPerToken(token1, token0, pool, basePrice) + token0.usdPrice = token0.ethPrice.times(basePrice.USD) + token1.usdPrice = token1.ethPrice.times(basePrice.USD) + + // tvl updates + let oldPoolTotalValueLockedEth = pool.totalValueLockedEth + token0.totalValueLocked = token0.totalValueLocked.minus(amount0) + token1.totalValueLocked = token1.totalValueLocked.minus(amount1) + pool.totalValueLocked0 = pool.totalValueLocked0.minus(amount0) + pool.totalValueLocked1 = pool.totalValueLocked1.minus(amount1) + let updateTvlRet = updateDerivedTVLAmounts(token0, token1, pool, factory, basePrice, oldPoolTotalValueLockedEth) + token0 = updateTvlRet.token0 + token1 = updateTvlRet.token1 + pool = updateTvlRet.pool + factory = updateTvlRet.factory // let loadTvlUpdateLog = safeLoadTvlUpdateLog(event.transaction.hash, poolAddress) // let tvlUpdateLog = loadTvlUpdateLog.entity @@ -137,9 +67,9 @@ export function handleCollectRange1(event: CollectRange1): void { // tvlUpdateLog.save() - // pool.save() - // token0.save() - // token1.save() - // factory.save() - // basePrice.save() + pool.save() // 1 + token0.save() // 2 + token1.save() // 3 + factory.save() // 4 + basePrice.save() // 5 } \ No newline at end of file diff --git a/subgraph/src/mappings/utils/loads.ts b/subgraph/src/mappings/utils/loads.ts index eb24229e..8c701450 100644 --- a/subgraph/src/mappings/utils/loads.ts +++ b/subgraph/src/mappings/utils/loads.ts @@ -1,108 +1,102 @@ import { Address, BigDecimal, BigInt, Bytes, ethereum, log } from '@graphprotocol/graph-ts' -import { LimitPool, LimitPoolFactory, LimitPoolManager, LimitPosition, Token, FeeTier, BasePrice, RangePosition, RangeTick, Transaction, LimitTick, Swap, CompoundRangeLog, MintRangeLog, BurnRangeLog, PoolRouter, TvlUpdateLog, HistoricalOrder, TotalSeasonReward, UserSeasonReward, LimitPoolToken } from '../../../generated/schema' -import { ONE_BD } from '../../constants/constants' +import { LimitPool, LimitPoolFactory, LimitPoolManager, LimitPosition, Token, FeeTier, BasePrice, RangePosition, RangeTick, Transaction, LimitTick, Swap, CompoundRangeLog, MintRangeLog, BurnRangeLog, PoolRouter, TvlUpdateLog, HistoricalOrder, TotalSeasonReward, UserSeasonReward, LimitPoolToken, VFinPosition } from '../../../generated/schema' +import { FACTORY_ADDRESS, ONE_BD } from '../../constants/constants' import { fetchTokenSymbol, fetchTokenName, fetchTokenDecimals, BIGINT_ZERO, BIGDECIMAL_ZERO, + BIGINT_ONE, } from './helpers' import { bigDecimalExponated, safeDiv } from './math' import { getEthPriceInUSD } from './price' +import { ZERO_ADDRESS } from '../../constants/constants' -class LoadBasePriceRet { - entity: BasePrice +class LoadLimitPoolFactoryRet { + entity: LimitPoolFactory exists: boolean } -export function safeLoadBasePrice(name: string): LoadBasePriceRet { +export function safeLoadLimitPoolFactory(factoryAddress: string): LoadLimitPoolFactoryRet { let exists = true + let limitPoolFactoryEntity = LimitPoolFactory.load(factoryAddress) - let basePriceEntity = BasePrice.load(name) - - if (!basePriceEntity) { - basePriceEntity = new BasePrice(name) - exists = false - } - - basePriceEntity.USD = getEthPriceInUSD() - - return { - entity: basePriceEntity, - exists: exists, - } -} - -class LoadTransactionRet { - entity: Transaction - exists: boolean -} -export function safeLoadTransaction(event: ethereum.Event): LoadTransactionRet { - let exists = true + if (!limitPoolFactoryEntity) { + limitPoolFactoryEntity = new LimitPoolFactory(factoryAddress) - let transactionEntity = Transaction.load(event.transaction.hash.toHex()) + limitPoolFactoryEntity.manager = ZERO_ADDRESS + limitPoolFactoryEntity.poolCount = BIGINT_ZERO + limitPoolFactoryEntity.txnCount = BIGINT_ZERO + limitPoolFactoryEntity.volumeEthTotal = BIGDECIMAL_ZERO + limitPoolFactoryEntity.volumeUsdTotal = BIGDECIMAL_ZERO + limitPoolFactoryEntity.feesUsdTotal = BIGDECIMAL_ZERO + limitPoolFactoryEntity.feesEthTotal = BIGDECIMAL_ZERO + limitPoolFactoryEntity.totalValueLockedUsd = BIGDECIMAL_ZERO + limitPoolFactoryEntity.totalValueLockedEth = BIGDECIMAL_ZERO - if (!transactionEntity) { - transactionEntity = new Transaction(event.transaction.hash.toHex()) - transactionEntity.sender = event.transaction.from - transactionEntity.blockNumber = event.block.number - transactionEntity.timestamp = event.block.timestamp - transactionEntity.gasLimit = event.transaction.gasLimit - transactionEntity.gasPrice = event.transaction.gasPrice exists = false } return { - entity: transactionEntity, + entity: limitPoolFactoryEntity, exists: exists, } } -class LoadTokenRet { - entity: Token +class LoadManagerRet { + entity: LimitPoolManager exists: boolean } -export function safeLoadToken(address: string): LoadTokenRet { +export function safeLoadManager(address: string): LoadManagerRet { let exists = true - let tokenEntity = Token.load(address) + let managerEntity = LimitPoolManager.load(address) - if (!tokenEntity) { - tokenEntity = new Token(address) - log.info('{}', [address]) - let tokenAddress = Address.fromString(address) - tokenEntity.symbol = fetchTokenSymbol(tokenAddress) - tokenEntity.name = fetchTokenName(tokenAddress) - tokenEntity.decimals = fetchTokenDecimals(tokenAddress) + if (!managerEntity) { + managerEntity = new LimitPoolManager(address) + managerEntity.owner = Bytes.fromHexString(ZERO_ADDRESS) + managerEntity.feeTo = Bytes.fromHexString(ZERO_ADDRESS) + managerEntity.factory = FACTORY_ADDRESS + managerEntity.feeTiers = new Array() + managerEntity.poolTypes = new Array() exists = false } return { - entity: tokenEntity, + entity: managerEntity, exists: exists, } } -class LoadManagerRet { - entity: LimitPoolManager +class LoadBasePriceRet { + entity: BasePrice exists: boolean } -export function safeLoadManager(address: string): LoadManagerRet { +export function safeLoadBasePrice(name: string, stablePool: LimitPool | null = null): LoadBasePriceRet { let exists = true - let managerEntity = LimitPoolManager.load(address) + let basePriceEntity = BasePrice.load(name) - if (!managerEntity) { - managerEntity = new LimitPoolManager(address) + if (!basePriceEntity) { + basePriceEntity = new BasePrice(name) exists = false } + if (stablePool !== null) { + log.info("pool data: {}", [stablePool.id]) + // only non-null when updating v3 pool price + basePriceEntity.USD = getEthPriceInUSD(stablePool) + } else { + log.info("pool data is null", []) + } + return { - entity: managerEntity, + entity: basePriceEntity, exists: exists, } } + class LoadFeeTierRet { entity: FeeTier exists: boolean @@ -117,6 +111,12 @@ export function safeLoadFeeTier(feeTier: BigInt): LoadFeeTierRet { if (!feeTierEntity) { feeTierEntity = new FeeTier(feeTierId) + feeTierEntity.feeAmount = BIGINT_ZERO + feeTierEntity.tickSpacing = BIGINT_ZERO + + feeTierEntity.createdAtBlockNumber = BIGINT_ZERO + feeTierEntity.createdAtTimestamp = BIGINT_ZERO + exists = false } @@ -126,79 +126,48 @@ export function safeLoadFeeTier(feeTier: BigInt): LoadFeeTierRet { } } -class LoadRangeTickRet { - entity: RangeTick +class LoadTokenRet { + entity: Token exists: boolean } -export function safeLoadRangeTick(address: string, index: BigInt): LoadRangeTickRet { +export function safeLoadToken(address: string): LoadTokenRet { let exists = true - let tickId = address - .concat(index.toString()) - - let tickEntity = RangeTick.load(tickId) - - if (!tickEntity) { - tickEntity = new RangeTick(tickId) - tickEntity.pool = address - tickEntity.index = index - // 1.0001^tick is token1/token0. - tickEntity.price0 = bigDecimalExponated(BigDecimal.fromString('1.0001'), BigInt.fromI32(tickEntity.index.toI32())) - tickEntity.price1 = safeDiv(ONE_BD, tickEntity.price0) - exists = false - } + let tokenEntity = Token.load(address) - return { - entity: tickEntity, - exists: exists, - } -} + if (!tokenEntity) { + tokenEntity = new Token(address) + log.info('{}', [address]) + let tokenAddress = Address.fromString(address) -class LoadLimitTickRet { - entity: LimitTick - exists: boolean -} -export function safeLoadLimitTick(address: string, index: BigInt): LoadLimitTickRet { - let exists = true + tokenEntity.symbol = fetchTokenSymbol(tokenAddress) + tokenEntity.name = fetchTokenName(tokenAddress) + tokenEntity.decimals = fetchTokenDecimals(tokenAddress) - let tickId = address - .concat(index.toString()) + tokenEntity.ethPrice = BIGDECIMAL_ZERO + tokenEntity.usdPrice = BIGDECIMAL_ZERO - let tickEntity = LimitTick.load(tickId) + tokenEntity.pools = new Array() + + tokenEntity.volume = BIGDECIMAL_ZERO + tokenEntity.volumeUsd = BIGDECIMAL_ZERO + tokenEntity.volumeEth = BIGDECIMAL_ZERO + tokenEntity.txnCount = BIGINT_ZERO - if (!tickEntity) { - tickEntity = new LimitTick(tickId) - tickEntity.pool = address - tickEntity.index = index - // 1.0001^tick is token1/token0. - tickEntity.price0 = bigDecimalExponated(BigDecimal.fromString('1.0001'), BigInt.fromI32(tickEntity.index.toI32())) - tickEntity.price1 = safeDiv(ONE_BD, tickEntity.price0) - tickEntity.active = true - exists = false - } + tokenEntity.feesUsdTotal = BIGDECIMAL_ZERO + tokenEntity.feesEthTotal = BIGDECIMAL_ZERO - return { - entity: tickEntity, - exists: exists, - } -} + tokenEntity.totalValueLocked = BIGDECIMAL_ZERO + tokenEntity.totalValueLockedEth = BIGDECIMAL_ZERO + tokenEntity.totalValueLockedUsd = BIGDECIMAL_ZERO -class LoadLimitPoolFactoryRet { - entity: LimitPoolFactory - exists: boolean -} -export function safeLoadLimitPoolFactory(factoryAddress: string): LoadLimitPoolFactoryRet { - let exists = true - let coverPoolFactoryEntity = LimitPoolFactory.load(factoryAddress) + tokenEntity.whitelistPools = new Array() - if (!coverPoolFactoryEntity) { - coverPoolFactoryEntity = new LimitPoolFactory(factoryAddress) - coverPoolFactoryEntity.poolCount = BIGINT_ZERO exists = false } return { - entity: coverPoolFactoryEntity, + entity: tokenEntity, exists: exists, } } @@ -213,6 +182,50 @@ export function safeLoadLimitPool(poolAddress: string): LoadLimitPoolRet { if (!limitPoolEntity) { limitPoolEntity = new LimitPool(poolAddress) + + limitPoolEntity.token0 = 'token0' + limitPoolEntity.token1 = 'token1' + + limitPoolEntity.feeTier = 'default' + limitPoolEntity.swapFee = BIGINT_ZERO + limitPoolEntity.tickSpacing = BIGINT_ZERO + limitPoolEntity.factory = ZERO_ADDRESS + limitPoolEntity.poolType = 'default' + limitPoolEntity.poolToken = Bytes.fromHexString(ZERO_ADDRESS) + + limitPoolEntity.liquidity = BIGINT_ZERO + limitPoolEntity.liquidityGlobal = BIGINT_ZERO + limitPoolEntity.positionIdNext = BIGINT_ONE + limitPoolEntity.epoch = BIGINT_ONE + limitPoolEntity.poolPrice = BIGINT_ZERO + limitPoolEntity.pool0Price = BIGINT_ZERO + limitPoolEntity.pool1Price = BIGINT_ZERO + limitPoolEntity.poolLiquidity = BIGINT_ZERO + limitPoolEntity.pool0Liquidity = BIGINT_ZERO + limitPoolEntity.pool1Liquidity = BIGINT_ZERO + limitPoolEntity.tickAtPrice = BIGINT_ZERO + + limitPoolEntity.feeGrowthGlobal0 = BIGINT_ZERO + limitPoolEntity.feeGrowthGlobal1 = BIGINT_ZERO + + limitPoolEntity.samplesLength = BigInt.fromString('5') + + limitPoolEntity.price0 = BIGDECIMAL_ZERO + limitPoolEntity.price1 = BIGDECIMAL_ZERO + + limitPoolEntity.volumeToken0 = BIGDECIMAL_ZERO + limitPoolEntity.volumeToken1 = BIGDECIMAL_ZERO + limitPoolEntity.volumeEth = BIGDECIMAL_ZERO + limitPoolEntity.volumeUsd = BIGDECIMAL_ZERO + limitPoolEntity.feesUsd = BIGDECIMAL_ZERO + limitPoolEntity.feesEth = BIGDECIMAL_ZERO + limitPoolEntity.txnCount = BIGINT_ZERO + + limitPoolEntity.totalValueLocked0 = BIGDECIMAL_ZERO + limitPoolEntity.totalValueLocked1 = BIGDECIMAL_ZERO + limitPoolEntity.totalValueLockedUsd = BIGDECIMAL_ZERO + limitPoolEntity.totalValueLockedEth = BIGDECIMAL_ZERO + exists = false } @@ -232,6 +245,9 @@ export function safeLoadLimitPoolToken(poolTokenAddress: string): LoadLimitPoolT if (!limitPoolTokenEntity) { limitPoolTokenEntity = new LimitPoolToken(poolTokenAddress) + + limitPoolTokenEntity.pool = 'default' + exists = false } @@ -241,43 +257,116 @@ export function safeLoadLimitPoolToken(poolTokenAddress: string): LoadLimitPoolT } } -class LoadPoolRouterRet { - entity: PoolRouter +class LoadLimitPositionRet { + entity: LimitPosition exists: boolean } -export function safeLoadPoolRouter(routerAddress: string): LoadPoolRouterRet { +export function safeLoadLimitPosition( + poolAddress: string, + positionId: BigInt +): LoadLimitPositionRet { let exists = true - let poolRouterEntity = PoolRouter.load(routerAddress) + let fromToken: string + + let limitPositionId = poolAddress + .concat(positionId.toString()) + + let positionEntity = LimitPosition.load(limitPositionId) + + if (!positionEntity) { + positionEntity = new LimitPosition(limitPositionId) + + positionEntity.positionId = BIGINT_ZERO + positionEntity.owner = Bytes.fromHexString(ZERO_ADDRESS) + positionEntity.lower = BIGINT_ZERO + positionEntity.upper = BIGINT_ZERO + positionEntity.zeroForOne = true + positionEntity.liquidity = BIGINT_ZERO + positionEntity.pool = 'default' + positionEntity.epochLast = BIGINT_ZERO + positionEntity.claimPriceLast = BIGINT_ZERO + positionEntity.amountIn = BIGINT_ZERO + positionEntity.amountFilled = BIGINT_ZERO + positionEntity.tokenIn = 'tokenIn' + positionEntity.tokenOut = 'tokenOut' + + positionEntity.txnHash = Bytes.fromHexString(ZERO_ADDRESS) + positionEntity.createdBy = Bytes.fromHexString(ZERO_ADDRESS) + positionEntity.createdAtTimestamp = BIGINT_ZERO - if (!poolRouterEntity) { - poolRouterEntity = new PoolRouter(routerAddress) exists = false } return { - entity: poolRouterEntity, + entity: positionEntity, exists: exists, } } -class LoadLimitPositionRet { - entity: LimitPosition +class LoadRangePositionRet { + entity: RangePosition exists: boolean } -export function safeLoadLimitPosition( +export function safeLoadRangePosition( poolAddress: string, positionId: BigInt -): LoadLimitPositionRet { +): LoadRangePositionRet { let exists = true let fromToken: string - let limitPositionId = poolAddress + let rangePositionId = poolAddress .concat(positionId.toString()) - let positionEntity = LimitPosition.load(limitPositionId) + let positionEntity = RangePosition.load(rangePositionId) if (!positionEntity) { - positionEntity = new LimitPosition(limitPositionId) + positionEntity = new RangePosition(rangePositionId) + + positionEntity.positionId = BIGINT_ZERO + positionEntity.owner = Bytes.fromHexString(ZERO_ADDRESS) + positionEntity.lower = BIGINT_ZERO + positionEntity.upper = BIGINT_ZERO + positionEntity.liquidity = BIGINT_ZERO + positionEntity.pool = 'default' + positionEntity.staked = false + + positionEntity.createdAtTimestamp = BIGINT_ZERO + positionEntity.createdAtBlockNumber = BIGINT_ZERO + positionEntity.updatedAtTimestamp = BIGINT_ZERO + positionEntity.updatedAtBlockNumber = BIGINT_ZERO + + exists = false + } + + return { + entity: positionEntity, + exists: exists, + } +} +export function safeLoadRangePositionById( + rangePositionId: string +): LoadRangePositionRet { + let exists = true + let fromToken: string + + let positionEntity = RangePosition.load(rangePositionId) + + if (!positionEntity) { + positionEntity = new RangePosition(rangePositionId) + + positionEntity.positionId = BIGINT_ZERO + positionEntity.owner = Bytes.fromHexString(ZERO_ADDRESS) + positionEntity.lower = BIGINT_ZERO + positionEntity.upper = BIGINT_ZERO + positionEntity.liquidity = BIGINT_ZERO + positionEntity.pool = 'default' + positionEntity.staked = false + + positionEntity.createdAtTimestamp = BIGINT_ZERO + positionEntity.createdAtBlockNumber = BIGINT_ZERO + positionEntity.updatedAtTimestamp = BIGINT_ZERO + positionEntity.updatedAtBlockNumber = BIGINT_ZERO + exists = false } @@ -301,7 +390,19 @@ export function safeLoadSwap(event: ethereum.Event, pool: LimitPool): LoadSwapRe if (!swapEntity) { swapEntity = new Swap(swapId) + + swapEntity.transaction = 'default' + swapEntity.recipient = Bytes.fromHexString(ZERO_ADDRESS) + swapEntity.timestamp = BIGINT_ZERO swapEntity.pool = pool.id + swapEntity.zeroForOne = true + swapEntity.amount0 = BIGDECIMAL_ZERO + swapEntity.amount1 = BIGDECIMAL_ZERO + swapEntity.amountUsd = BIGDECIMAL_ZERO + swapEntity.priceAfter = BIGINT_ZERO + swapEntity.tickAfter = BIGINT_ZERO + swapEntity.txnIndex = BIGINT_ZERO + exists = false } @@ -311,49 +412,151 @@ export function safeLoadSwap(event: ethereum.Event, pool: LimitPool): LoadSwapRe } } -class LoadRangePositionRet { - entity: RangePosition +class LoadTransactionRet { + entity: Transaction exists: boolean } -export function safeLoadRangePosition( - poolAddress: string, - positionId: BigInt -): LoadRangePositionRet { +export function safeLoadTransaction(event: ethereum.Event): LoadTransactionRet { let exists = true - let fromToken: string - let rangePositionId = poolAddress - .concat(positionId.toString()) + let transactionEntity = Transaction.load(event.transaction.hash.toHex()) - let positionEntity = RangePosition.load(rangePositionId) + if (!transactionEntity) { + transactionEntity = new Transaction(event.transaction.hash.toHex()) - if (!positionEntity) { - positionEntity = new RangePosition(rangePositionId) + transactionEntity.sender = event.transaction.from + transactionEntity.blockNumber = event.block.number + transactionEntity.timestamp = event.block.timestamp + transactionEntity.gasLimit = event.transaction.gasLimit + transactionEntity.gasPrice = event.transaction.gasPrice + transactionEntity.swaps = new Array() exists = false } return { - entity: positionEntity, + entity: transactionEntity, exists: exists, } } -export function safeLoadRangePositionById( - rangePositionId: string -): LoadRangePositionRet { + +class LoadRangeTickRet { + entity: RangeTick + exists: boolean +} +export function safeLoadRangeTick(address: string, index: BigInt): LoadRangeTickRet { let exists = true - let fromToken: string - let positionEntity = RangePosition.load(rangePositionId) + let tickId = address + .concat(index.toString()) - if (!positionEntity) { - positionEntity = new RangePosition(rangePositionId) + let tickEntity = RangeTick.load(tickId) + + if (!tickEntity) { + tickEntity = new RangeTick(tickId) + tickEntity.pool = address + tickEntity.index = index + // 1.0001^tick is token1/token0. + tickEntity.price0 = bigDecimalExponated(BigDecimal.fromString('1.0001'), BigInt.fromI32(tickEntity.index.toI32())) + tickEntity.price1 = safeDiv(ONE_BD, tickEntity.price0) + + tickEntity.liquidityDelta = BIGINT_ZERO + tickEntity.liquidityAbsolute = BIGINT_ZERO + + tickEntity.feeGrowthOutside0 = BIGINT_ZERO + tickEntity.feeGrowthOutside1 = BIGINT_ZERO exists = false } return { - entity: positionEntity, + entity: tickEntity, + exists: exists, + } +} + +class LoadLimitTickRet { + entity: LimitTick + exists: boolean +} +export function safeLoadLimitTick(address: string, index: BigInt): LoadLimitTickRet { + let exists = true + + let tickId = address + .concat(index.toString()) + + let tickEntity = LimitTick.load(tickId) + + if (!tickEntity) { + tickEntity = new LimitTick(tickId) + tickEntity.pool = address + tickEntity.index = index + // 1.0001^tick is token1/token0. + tickEntity.active = true + tickEntity.price0 = bigDecimalExponated(BigDecimal.fromString('1.0001'), BigInt.fromI32(tickEntity.index.toI32())) + tickEntity.price1 = safeDiv(ONE_BD, tickEntity.price0) + + tickEntity.liquidityDelta = BIGINT_ZERO + tickEntity.liquidityAbsolute = BIGINT_ZERO + + tickEntity.epochLast0 = BIGINT_ZERO + tickEntity.epochLast1 = BIGINT_ZERO + + exists = false + } + + return { + entity: tickEntity, + exists: exists, + } +} + +class LoadPoolRouterRet { + entity: PoolRouter + exists: boolean +} +export function safeLoadPoolRouter(routerAddress: string): LoadPoolRouterRet { + let exists = true + let poolRouterEntity = PoolRouter.load(routerAddress) + + if (!poolRouterEntity) { + poolRouterEntity = new PoolRouter(routerAddress) + + poolRouterEntity.limitPoolFactory = Bytes.fromHexString(ZERO_ADDRESS) + poolRouterEntity.coverPoolFactory = Bytes.fromHexString(ZERO_ADDRESS) + + exists = false + } + + return { + entity: poolRouterEntity, + exists: exists, + } +} + +class LoadVFinPositionRet { + entity: VFinPosition + exists: boolean +} +export function safeLoadVFinPosition(vFinAddress: string, positionId: BigInt): LoadVFinPositionRet { + let exists = true + + let vFinPositionId = vFinAddress.concat(positionId.toString()) + + let vFinPositionEntity = VFinPosition.load(vFinPositionId) + + if (!vFinPositionEntity) { + vFinPositionEntity = new VFinPosition(vFinPositionId) + + vFinPositionEntity.owner = Bytes.fromHexString(ZERO_ADDRESS) + vFinPositionEntity.positionId = BIGINT_ZERO + vFinPositionEntity.vFinAddress = Bytes.fromHexString(ZERO_ADDRESS) + + exists = false + } + + return { + entity: vFinPositionEntity, exists: exists, } } @@ -479,14 +682,24 @@ export function safeLoadHistoricalOrder(tokenInAddress: string, tokenOutAddress: if (!historicalOrderEntity) { historicalOrderEntity = new HistoricalOrder(historicalOrderId) -<<<<<<< HEAD - historicalOrderEntity.positionId = BIGINT_ZERO -======= ->>>>>>> master + + historicalOrderEntity.owner = Bytes.fromHexString(ZERO_ADDRESS) + + historicalOrderEntity.pool = 'default' historicalOrderEntity.tokenIn = tokenInAddress historicalOrderEntity.tokenOut = tokenOutAddress + historicalOrderEntity.txnHash = Bytes.fromHexString(ZERO_ADDRESS) + historicalOrderEntity.amountIn = BIGDECIMAL_ZERO historicalOrderEntity.amountOut = BIGDECIMAL_ZERO + historicalOrderEntity.averagePrice = BIGDECIMAL_ZERO + historicalOrderEntity.completedAtTimestamp = BIGINT_ZERO + + historicalOrderEntity.usdValue = BIGDECIMAL_ZERO + + historicalOrderEntity.positionId = BIGINT_ZERO + historicalOrderEntity.touches = BIGINT_ZERO + historicalOrderEntity.completed = true exists = false @@ -509,6 +722,12 @@ export function safeLoadTotalSeasonReward(factoryAddress: string): LoadTotalSeas if (!totalSeasonRewardEntity) { totalSeasonRewardEntity = new TotalSeasonReward(factoryAddress) + + totalSeasonRewardEntity.whitelistedFeesUsd = BIGDECIMAL_ZERO + totalSeasonRewardEntity.nonWhitelistedFeesUsd = BIGDECIMAL_ZERO + totalSeasonRewardEntity.volumeTradedUsd = BIGDECIMAL_ZERO + totalSeasonRewardEntity.stakingPoints = BIGINT_ZERO + exists = false } @@ -530,6 +749,11 @@ export function safeLoadUserSeasonReward(userAddress: string): LoadUserSeasonRew if (!userSeasonRewardEntity) { userSeasonRewardEntity = new UserSeasonReward(userAddress) + userSeasonRewardEntity.whitelistedFeesUsd = BIGDECIMAL_ZERO + userSeasonRewardEntity.nonWhitelistedFeesUsd = BIGDECIMAL_ZERO + userSeasonRewardEntity.volumeTradedUsd = BIGDECIMAL_ZERO + userSeasonRewardEntity.stakingPoints = BIGINT_ZERO + exists = false } diff --git a/subgraph/src/mappings/utils/price.ts b/subgraph/src/mappings/utils/price.ts index 0b953791..a0de90fc 100644 --- a/subgraph/src/mappings/utils/price.ts +++ b/subgraph/src/mappings/utils/price.ts @@ -9,6 +9,7 @@ import { safeLoadBasePrice, safeLoadLimitPool, safeLoadToken } from './loads' export function sqrtPriceX96ToTokenPrices(sqrtPriceX96: BigInt, token0: Token, token1: Token): BigDecimal[] { let num = sqrtPriceX96.times(sqrtPriceX96).toBigDecimal() let Q192 = BigInt.fromI32(2).pow(192).toBigDecimal() + log.warning("before decimals", []) let price1 = num .div(Q192) .times( @@ -17,7 +18,7 @@ export function sqrtPriceX96ToTokenPrices(sqrtPriceX96: BigInt, token0: Token, t exponentToBigDecimal(token1.decimals) ) ); - + log.warning("after decimals", []) let price0 = BIGDECIMAL_ZERO if (price1.gt(BIGDECIMAL_ZERO)) { price0 = safeDiv(BigDecimal.fromString('1'), price1); @@ -26,9 +27,8 @@ export function sqrtPriceX96ToTokenPrices(sqrtPriceX96: BigInt, token0: Token, t return [price0, price1] } -export function getEthPriceInUSD(): BigDecimal { +export function getEthPriceInUSD(stablePool: LimitPool): BigDecimal { // fetch eth prices for each stablecoin - let stablePool = LimitPool.load(STABLE_POOL_ADDRESS) // stable is token0 if (stablePool !== null) { if (STABLE_IS_TOKEN_0) { log.info('stable pool token0: {}', [stablePool.price0.toString()]) diff --git a/subgraph/src/mappings/v3pool.ts b/subgraph/src/mappings/v3pool.ts new file mode 100644 index 00000000..713237d5 --- /dev/null +++ b/subgraph/src/mappings/v3pool.ts @@ -0,0 +1,94 @@ +import { BigInt, log } from "@graphprotocol/graph-ts" +import { Token } from "../../generated/schema" +import { STABLE_POOL_ADDRESS, STABLE_IS_TOKEN_0, STABLE_TOKEN_DECIMALS } from "../constants/constants" +import { safeLoadBasePrice, safeLoadLimitPool } from "./utils/loads" +import { sqrtPriceX96ToTokenPrices } from "./utils/price" +import { Initialize, Swap } from "../../generated/v3EthUsdcPool/v3Pool" + +export function handleV3Initialize(event: Initialize): void { + let loadPool = safeLoadLimitPool(STABLE_POOL_ADDRESS) + + log.warning("swap event process", []) + + let pool = loadPool.entity + + log.warning("pool entity", []) + + pool.poolPrice = event.params.sqrtPriceX96 + + log.warning("price after", []) + + let token0: Token; let token1: Token; + + if (STABLE_IS_TOKEN_0) { + log.warning("usdc token", []) + token0 = new Token('STABLE') + log.warning("usdc decimals", []) + token0.decimals = BigInt.fromString(STABLE_TOKEN_DECIMALS) + log.warning("weth token", []) + token1 = new Token('WETH') + log.warning("weth decimals", []) + token1.decimals = BigInt.fromString('18') + } else { + token0 = new Token('WETH') + token0.decimals = BigInt.fromString('18') + token1 = new Token('STABLE') + token1.decimals = BigInt.fromString(STABLE_TOKEN_DECIMALS) + } + + log.warning("about to convert to prices", []) + + let prices = sqrtPriceX96ToTokenPrices(pool.poolPrice, token0, token1) + pool.price0 = prices[0] + pool.price1 = prices[1] + + log.warning("after price convert", []) + + let loadBasePrice = safeLoadBasePrice('eth', pool) + let basePrice = loadBasePrice.entity + basePrice.save() +} + +export function handleV3Swap(event: Swap): void { + let loadPool = safeLoadLimitPool(STABLE_POOL_ADDRESS) + + log.warning("swap event process", []) + + let pool = loadPool.entity + + log.warning("pool entity", []) + + pool.poolPrice = event.params.sqrtPriceX96 + + log.warning("price after", []) + + let token0: Token; let token1: Token; + + if (STABLE_IS_TOKEN_0) { + log.warning("usdc token", []) + token0 = new Token('USDC') + log.warning("usdc decimals", []) + token0.decimals = BigInt.fromString(STABLE_TOKEN_DECIMALS) + log.warning("weth token", []) + token1 = new Token('WETH') + log.warning("weth decimals", []) + token1.decimals = BigInt.fromString('18') + } else { + token0 = new Token('WETH') + token0.decimals = BigInt.fromString('18') + token1 = new Token('USDC') + token1.decimals = BigInt.fromString(STABLE_TOKEN_DECIMALS) + } + + log.warning("about to convert to prices", []) + + let prices = sqrtPriceX96ToTokenPrices(pool.poolPrice, token0, token1) + pool.price0 = prices[0] + pool.price1 = prices[1] + + log.warning("after price convert", []) + + let loadBasePrice = safeLoadBasePrice('eth', pool) + let basePrice = loadBasePrice.entity + basePrice.save() +} \ No newline at end of file diff --git a/subgraph/src/mappings/vfin.ts b/subgraph/src/mappings/vfin.ts new file mode 100644 index 00000000..ce864816 --- /dev/null +++ b/subgraph/src/mappings/vfin.ts @@ -0,0 +1,22 @@ +import { RouterDeployed } from "../../generated/PoolsharkRouter/PoolsharkRouter"; +import { Transfer } from "../../generated/vFIN/vFIN"; +import { safeLoadPoolRouter, safeLoadVFinPosition } from "./utils/loads"; + +export function handleTransfer(event: Transfer): void { + let fromParam = event.params.from + let toParam = event.params.to + let idParam = event.params.id + let vFinAddress = event.address + + let loadVFinPosition = safeLoadVFinPosition(vFinAddress.toHex(), idParam) + + let vFinPosition = loadVFinPosition.entity + + if (!loadVFinPosition.exists) { + vFinPosition.vFinAddress = vFinAddress + vFinPosition.positionId = idParam + } + vFinPosition.owner = toParam + + vFinPosition.save() +} \ No newline at end of file diff --git a/subgraph/subgraph.yaml b/subgraph/subgraph.yaml index 88fcc3d2..f15203dd 100644 --- a/subgraph/subgraph.yaml +++ b/subgraph/subgraph.yaml @@ -6,7 +6,7 @@ schema: templates: - kind: ethereum/contract name: LimitPoolTemplate - network: arbitrum-goerli + network: arbitrum-sepolia source: abi: LimitPool mapping: @@ -30,9 +30,9 @@ templates: - name: ERC20NameBytes file: ./abis/ERC20NameBytes.json eventHandlers: - - event: Initialize(int24,int24,uint160,int24) + - event: InitializeLimit(int24,int24,uint160,int24) handler: handleInitialize - - event: Swap(indexed address,uint256,uint256,uint200,uint200,uint160,uint128,uint128,int24,indexed bool,indexed bool) + - event: SwapLimit(indexed address,uint256,uint256,uint200,uint200,uint160,uint128,uint128,int24,indexed bool,indexed bool) handler: handleSwap - event: MintLimit(indexed address,int24,int24,bool,uint32,uint32,uint128,uint128) handler: handleMintLimit @@ -56,14 +56,12 @@ templates: handler: handleSyncLimitLiquidity - event: SyncLimitTick(uint32,int24,bool) handler: handleSyncLimitTick - - event: CollectRange0(uint128) - handler: handleCollectRange0 - - event: CollectRange1(uint128) - handler: handleCollectRange1 + - event: Collect(indexed address,address,indexed int24,indexed int24,uint128,uint128) + handler: handleCollect # ERC-1155 events - kind: ethereum/contract name: PositionERC1155Template - network: arbitrum-goerli + network: arbitrum-sepolia source: abi: PositionERC1155 mapping: @@ -86,9 +84,8 @@ templates: # RangeStaker events - kind: ethereum/contract name: RangeStakerTemplate - network: arbitrum-goerli + network: arbitrum-sepolia source: - # address: '0xe5e2E95A986CE078606C403593593b18Ed98f4d6' abi: RangeStaker mapping: kind: ethereum/events @@ -114,11 +111,11 @@ templates: dataSources: - kind: ethereum/contract name: LimitPoolFactory - network: arbitrum-goerli + network: arbitrum-sepolia source: - address: '0x1b215002e688135549cc0290d6cf1f94e3aa425c' + address: '0x8e40c68b7546efd009a1a300c92e25da3c8725dc' abi: LimitPoolFactory - startBlock: 56821935 + startBlock: 5789864 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -139,15 +136,15 @@ dataSources: - name: ERC20NameBytes file: ./abis/ERC20NameBytes.json eventHandlers: - - event: PoolCreated(address,address,indexed address,indexed address,indexed uint16,int16,uint16) + - event: LimitPoolCreated(address,address,indexed address,indexed address,indexed uint16,int16,uint16) handler: handlePoolCreated - kind: ethereum/contract name: LimitPoolManager - network: arbitrum-goerli + network: arbitrum-sepolia source: - address: '0x6d3af137e75097b892683832a2c0132f99625d0e' + address: '0x0385d0a8d169b1f1510bb5fa0be264463002f6f6' abi: LimitPoolManager - startBlock: 56821935 + startBlock: 5789859 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -175,14 +172,14 @@ dataSources: - event: ProtocolFillFeesModified(address[],int16[],int16[]) handler: handleProtocolFillFeesModified - event: ProtocolSwapFeesModified(address[],int16[],int16[]) - handler: ProtocolSwapFeesModified + handler: handleProtocolSwapFeesModified - kind: ethereum/contract name: PoolsharkRouter - network: arbitrum-goerli + network: arbitrum-sepolia source: - address: '0x4b5a159109bb997135e9b2bb6e486e2101466881' + address: '0x33df95efe07a3b3e69ba31438ae511d360d89b32' abi: PoolsharkRouter - startBlock: 56821935 + startBlock: 5789931 mapping: kind: ethereum/events apiVersion: 0.0.6 @@ -196,5 +193,45 @@ dataSources: eventHandlers: - event: RouterDeployed(address,address,address) handler: handleRouterDeployed - - + - kind: ethereum/contract + name: vFIN + network: arbitrum-sepolia + source: + address: '0xed0ef8101900dfed208ac99ce47b723debb6bfe6' + abi: vFIN + startBlock: 5795375 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/vfin.ts + entities: + - VestedFin + abis: + - name: vFIN + file: ./abis/vFIN.json + eventHandlers: + - event: Transfer(indexed address,indexed address,indexed uint256) + handler: handleTransfer + - kind: ethereum/contract + name: v3EthUsdcPool + network: arbitrum-sepolia + source: + address: '0x02225f6a3d83648d7906a23856331c819265394d' + abi: v3Pool + startBlock: 5789922 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/v3Pool.ts + entities: + - BasePrice + abis: + - name: v3Pool + file: ./abis/v3Pool.json + eventHandlers: + - event: Initialize(uint160,int24) + handler: handleV3Initialize + - event: Swap(indexed address,indexed address,int256,int256,uint160,uint128,int24) + handler: handleV3Swap \ No newline at end of file diff --git a/subgraph/templates/arbitrum-goerli.subgraph.template.yaml b/subgraph/templates/arbitrum-goerli.subgraph.template.yaml new file mode 100644 index 00000000..d36cf0bd --- /dev/null +++ b/subgraph/templates/arbitrum-goerli.subgraph.template.yaml @@ -0,0 +1,198 @@ +specVersion: 0.0.4 +description: Poolshark is a Directional Liquidity AMM allowing LPs to capture directional strength. +repository: https://github.com/poolshark-protocol/limit +schema: + file: ./schema.graphql +templates: + - kind: ethereum/contract + name: LimitPoolTemplate + network: arbitrum-goerli + source: + abi: LimitPool + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpool.ts + entities: + - Token + - LimitPool + - Position + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: Initialize(int24,int24,uint160,int24) + handler: handleInitialize + - event: Swap(indexed address,uint256,uint256,uint200,uint200,uint160,uint128,uint128,int24,indexed bool,indexed bool) + handler: handleSwap + - event: MintLimit(indexed address,int24,int24,bool,uint32,uint32,uint128,uint128) + handler: handleMintLimit + - event: SampleCountIncreased(uint16) + handler: handleSampleCountIncreased + - event: SampleRecorded(int56,uint160) + handler: handleSampleRecorded + - event: MintRange(indexed address,int24,int24,indexed uint32,uint128,int128,int128) + handler: handleMintRange + - event: BurnRange(indexed address,indexed uint256,uint128,int128,int128) + handler: handleBurnRange + - event: CompoundRange(indexed uint32,uint128) + handler: handleCompoundRange + - event: SyncRangeTick(uint200,uint200,int24) + handler: handleSyncRangeTick + - event: BurnLimit(indexed address,uint32,int24,int24,int24,int24,bool,uint128,uint128,uint128) + handler: handleBurnLimit + - event: SyncLimitPool(uint160,uint128,uint32,int24,bool) + handler: handleSyncLimitPool + - event: SyncLimitLiquidity(uint128,int24,bool) + handler: handleSyncLimitLiquidity + - event: SyncLimitTick(uint32,int24,bool) + handler: handleSyncLimitTick + - event: CollectRange0(uint128) + handler: handleCollectRange0 + - event: CollectRange1(uint128) + handler: handleCollectRange1 + # ERC-1155 events + - kind: ethereum/contract + name: PositionERC1155Template + network: arbitrum-goerli + source: + abi: PositionERC1155 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/positionerc1155.ts + entities: + - Token + - RangePool + - RangePosition + abis: + - name: PositionERC1155 + file: ./abis/PositionERC1155.json + eventHandlers: + - event: TransferSingle(indexed address,indexed address,indexed address,uint256,uint256) + handler: handleTransferSingle + - event: TransferBatch(indexed address,indexed address,indexed address,uint256[],uint256[]) + handler: handleTransferBatch + # RangeStaker events + - kind: ethereum/contract + name: RangeStakerTemplate + network: arbitrum-goerli + source: + # address: '0xe5e2E95A986CE078606C403593593b18Ed98f4d6' + abi: RangeStaker + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/staking/rangestaker.ts + entities: + - RangeStake + abis: + - name: RangeStaker + file: ./abis/RangeStaker.json + eventHandlers: + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: StakeRange(address,uint32,address,uint256,uint256,uint128) + handler: handleStakeRange + - event: StakeRangeAccrued(address,uint32,uint256,uint256) + handler: handleStakeRangeAccrued + - event: UnstakeRange(address,uint32,address) + handler: handleUnstakeRange +dataSources: + - kind: ethereum/contract + name: LimitPoolFactory + network: arbitrum-goerli + source: + address: '0x1b215002e688135549cc0290d6cf1f94e3aa425c' + abi: LimitPoolFactory + startBlock: 56821935 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolfactory.ts + entities: + - Token + - LimitPool + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: PoolCreated(address,address,indexed address,indexed address,indexed uint16,int16,uint16) + handler: handlePoolCreated + - kind: ethereum/contract + name: LimitPoolManager + network: arbitrum-goerli + source: + address: '0x6d3af137e75097b892683832a2c0132f99625d0e' + abi: LimitPoolManager + startBlock: 56821935 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolmanager.ts + entities: + - LimitPool + - LimitPoolFactory + abis: + - name: LimitPoolManager + file: ./abis/LimitPoolManager.json + eventHandlers: + - event: FeeTierEnabled(uint16,int16) + handler: handleFeeTierEnabled + - event: PoolTypeEnabled(bytes32,address,address,uint16) + handler: handlePoolTypeEnabled + - event: FactoryChanged(indexed address,indexed address) + handler: handleFactoryChanged + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: ProtocolFeesCollected(address[],uint128[],uint128[]) + handler: handleProtocolFeesCollected + - event: ProtocolFillFeesModified(address[],int16[],int16[]) + handler: handleProtocolFillFeesModified + - event: ProtocolSwapFeesModified(address[],int16[],int16[]) + handler: ProtocolSwapFeesModified + - kind: ethereum/contract + name: PoolsharkRouter + network: arbitrum-goerli + source: + address: '0x4b5a159109bb997135e9b2bb6e486e2101466881' + abi: PoolsharkRouter + startBlock: 56821935 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/poolsharkrouter.ts + entities: + - PoolsharkRouter + abis: + - name: PoolsharkRouter + file: ./abis/PoolsharkRouter.json + eventHandlers: + - event: RouterDeployed(address,address,address) + handler: handleRouterDeployed diff --git a/subgraph/templates/arbitrum-sepolia.subgraph.template.yaml b/subgraph/templates/arbitrum-sepolia.subgraph.template.yaml new file mode 100644 index 00000000..31a6beef --- /dev/null +++ b/subgraph/templates/arbitrum-sepolia.subgraph.template.yaml @@ -0,0 +1,237 @@ +specVersion: 0.0.4 +description: Poolshark is a Directional Liquidity AMM allowing LPs to capture directional strength. +repository: https://github.com/poolshark-protocol/limit +schema: + file: ./schema.graphql +templates: + - kind: ethereum/contract + name: LimitPoolTemplate + network: arbitrum-sepolia + source: + abi: LimitPool + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpool.ts + entities: + - Token + - LimitPool + - Position + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: InitializeLimit(int24,int24,uint160,int24) + handler: handleInitialize + - event: SwapLimit(indexed address,uint256,uint256,uint200,uint200,uint160,uint128,uint128,int24,indexed bool,indexed bool) + handler: handleSwap + - event: MintLimit(indexed address,int24,int24,bool,uint32,uint32,uint128,uint128) + handler: handleMintLimit + - event: SampleCountIncreased(uint16) + handler: handleSampleCountIncreased + - event: SampleRecorded(int56,uint160) + handler: handleSampleRecorded + - event: MintRange(indexed address,int24,int24,indexed uint32,uint128,int128,int128) + handler: handleMintRange + - event: BurnRange(indexed address,indexed uint256,uint128,int128,int128) + handler: handleBurnRange + - event: CompoundRange(indexed uint32,uint128) + handler: handleCompoundRange + - event: SyncRangeTick(uint200,uint200,int24) + handler: handleSyncRangeTick + - event: BurnLimit(indexed address,uint32,int24,int24,int24,int24,bool,uint128,uint128,uint128) + handler: handleBurnLimit + - event: SyncLimitPool(uint160,uint128,uint32,int24,bool) + handler: handleSyncLimitPool + - event: SyncLimitLiquidity(uint128,int24,bool) + handler: handleSyncLimitLiquidity + - event: SyncLimitTick(uint32,int24,bool) + handler: handleSyncLimitTick + - event: Collect(indexed address,address,indexed int24,indexed int24,uint128,uint128) + handler: handleCollect + # ERC-1155 events + - kind: ethereum/contract + name: PositionERC1155Template + network: arbitrum-sepolia + source: + abi: PositionERC1155 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/positionerc1155.ts + entities: + - Token + - RangePool + - RangePosition + abis: + - name: PositionERC1155 + file: ./abis/PositionERC1155.json + eventHandlers: + - event: TransferSingle(indexed address,indexed address,indexed address,uint256,uint256) + handler: handleTransferSingle + - event: TransferBatch(indexed address,indexed address,indexed address,uint256[],uint256[]) + handler: handleTransferBatch + # RangeStaker events + - kind: ethereum/contract + name: RangeStakerTemplate + network: arbitrum-sepolia + source: + abi: RangeStaker + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/staking/rangestaker.ts + entities: + - RangeStake + abis: + - name: RangeStaker + file: ./abis/RangeStaker.json + eventHandlers: + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: StakeRange(address,uint32,address,uint256,uint256,uint128) + handler: handleStakeRange + - event: StakeRangeAccrued(address,uint32,uint256,uint256) + handler: handleStakeRangeAccrued + - event: UnstakeRange(address,uint32,address) + handler: handleUnstakeRange +dataSources: + - kind: ethereum/contract + name: LimitPoolFactory + network: arbitrum-sepolia + source: + address: '0x8e40c68b7546efd009a1a300c92e25da3c8725dc' + abi: LimitPoolFactory + startBlock: 5789864 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolfactory.ts + entities: + - Token + - LimitPool + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: LimitPoolCreated(address,address,indexed address,indexed address,indexed uint16,int16,uint16) + handler: handlePoolCreated + - kind: ethereum/contract + name: LimitPoolManager + network: arbitrum-sepolia + source: + address: '0x0385d0a8d169b1f1510bb5fa0be264463002f6f6' + abi: LimitPoolManager + startBlock: 5789859 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolmanager.ts + entities: + - LimitPool + - LimitPoolFactory + abis: + - name: LimitPoolManager + file: ./abis/LimitPoolManager.json + eventHandlers: + - event: FeeTierEnabled(uint16,int16) + handler: handleFeeTierEnabled + - event: PoolTypeEnabled(bytes32,address,address,uint16) + handler: handlePoolTypeEnabled + - event: FactoryChanged(indexed address,indexed address) + handler: handleFactoryChanged + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: ProtocolFeesCollected(address[],uint128[],uint128[]) + handler: handleProtocolFeesCollected + - event: ProtocolFillFeesModified(address[],int16[],int16[]) + handler: handleProtocolFillFeesModified + - event: ProtocolSwapFeesModified(address[],int16[],int16[]) + handler: ProtocolSwapFeesModified + - kind: ethereum/contract + name: PoolsharkRouter + network: arbitrum-sepolia + source: + address: '0x33df95efe07a3b3e69ba31438ae511d360d89b32' + abi: PoolsharkRouter + startBlock: 5789931 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/poolsharkrouter.ts + entities: + - PoolsharkRouter + abis: + - name: PoolsharkRouter + file: ./abis/PoolsharkRouter.json + eventHandlers: + - event: RouterDeployed(address,address,address) + handler: handleRouterDeployed + - kind: ethereum/contract + name: vFIN + network: arbitrum-sepolia + source: + address: '0xed0ef8101900dfed208ac99ce47b723debb6bfe6' + abi: vFIN + startBlock: 5795375 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/vfin.ts + entities: + - VestedFin + abis: + - name: vFIN + file: ./abis/vFIN.json + eventHandlers: + - event: Transfer(indexed address,indexed address,indexed uint256) + handler: handleTransfer + - kind: ethereum/contract + name: v3EthUsdcPool + network: arbitrum-sepolia + source: + address: '0x02225f6a3d83648d7906a23856331c819265394d' + abi: v3Pool + startBlock: 5789922 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/v3Pool.ts + entities: + - BasePrice + abis: + - name: v3Pool + file: ./abis/v3Pool.json + eventHandlers: + - event: Initialize(uint160,int24) + handler: handleV3Initialize + - event: Swap(indexed address,indexed address,int256,int256,uint160,uint128,int24) + handler: handleV3Swap \ No newline at end of file diff --git a/subgraph/templates/arbitrum.subgraph.template.yaml b/subgraph/templates/arbitrum.subgraph.template.yaml new file mode 100644 index 00000000..ff6b5e7d --- /dev/null +++ b/subgraph/templates/arbitrum.subgraph.template.yaml @@ -0,0 +1,235 @@ +specVersion: 0.0.4 +description: Poolshark is a Directional Liquidity AMM allowing LPs to capture directional strength. +repository: https://github.com/poolshark-protocol/limit +schema: + file: ./schema.graphql +templates: + - kind: ethereum/contract + name: LimitPoolTemplate + network: arbitrum-one + source: + abi: LimitPool + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpool.ts + entities: + - Token + - LimitPool + - Position + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: InitializeLimit(int24,int24,uint160,int24) + handler: handleInitialize + - event: SwapLimit(indexed address,uint256,uint256,uint200,uint200,uint160,uint128,uint128,int24,indexed bool,indexed bool) + handler: handleSwap + - event: MintLimit(indexed address,int24,int24,bool,uint32,uint32,uint128,uint128) + handler: handleMintLimit + - event: SampleCountIncreased(uint16) + handler: handleSampleCountIncreased + - event: SampleRecorded(int56,uint160) + handler: handleSampleRecorded + - event: MintRange(indexed address,int24,int24,indexed uint32,uint128,int128,int128) + handler: handleMintRange + - event: BurnRange(indexed address,indexed uint256,uint128,int128,int128) + handler: handleBurnRange + - event: CompoundRange(indexed uint32,uint128) + handler: handleCompoundRange + - event: SyncRangeTick(uint200,uint200,int24) + handler: handleSyncRangeTick + - event: BurnLimit(indexed address,uint32,int24,int24,int24,int24,bool,uint128,uint128,uint128) + handler: handleBurnLimit + - event: SyncLimitPool(uint160,uint128,uint32,int24,bool) + handler: handleSyncLimitPool + - event: SyncLimitLiquidity(uint128,int24,bool) + handler: handleSyncLimitLiquidity + - event: SyncLimitTick(uint32,int24,bool) + handler: handleSyncLimitTick + - event: Collect(indexed address,address,indexed int24,indexed int24,uint128,uint128) + handler: handleCollect + # ERC-1155 events + - kind: ethereum/contract + name: PositionERC1155Template + network: arbitrum-one + source: + abi: PositionERC1155 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/positionerc1155.ts + entities: + - Token + - RangePool + - RangePosition + abis: + - name: PositionERC1155 + file: ./abis/PositionERC1155.json + eventHandlers: + - event: TransferSingle(indexed address,indexed address,indexed address,uint256,uint256) + handler: handleTransferSingle + - event: TransferBatch(indexed address,indexed address,indexed address,uint256[],uint256[]) + handler: handleTransferBatch + # RangeStaker events + - kind: ethereum/contract + name: RangeStakerTemplate + network: arbitrum-one + source: + abi: RangeStaker + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/staking/rangestaker.ts + entities: + - RangeStake + abis: + - name: RangeStaker + file: ./abis/RangeStaker.json + eventHandlers: + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: StakeRange(address,uint32,address,uint256,uint256,uint128) + handler: handleStakeRange + - event: StakeRangeAccrued(address,uint32,uint256,uint256) + handler: handleStakeRangeAccrued + - event: UnstakeRange(address,uint32,address) + handler: handleUnstakeRange +dataSources: + - kind: ethereum/contract + name: LimitPoolFactory + network: arbitrum-one + source: + address: '0x8bb5db1625adb4ae4beb94a188d33062303f8fb7' + abi: LimitPoolFactory + startBlock: 168223582 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolfactory.ts + entities: + - Token + - LimitPool + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: LimitPoolCreated(address,address,indexed address,indexed address,indexed uint16,int16,uint16) + handler: handlePoolCreated + - kind: ethereum/contract + name: LimitPoolManager + network: arbitrum-one + source: + address: '0xc773049bf7131a5ddd85daded4397a1fd4264a3d' + abi: LimitPoolManager + startBlock: 168223575 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolmanager.ts + entities: + - LimitPool + - LimitPoolFactory + abis: + - name: LimitPoolManager + file: ./abis/LimitPoolManager.json + eventHandlers: + - event: FeeTierEnabled(uint16,int16) + handler: handleFeeTierEnabled + - event: PoolTypeEnabled(bytes32,address,address,uint16) + handler: handlePoolTypeEnabled + - event: FactoryChanged(indexed address,indexed address) + handler: handleFactoryChanged + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: ProtocolFeesCollected(address[],uint128[],uint128[]) + handler: handleProtocolFeesCollected + - event: ProtocolFillFeesModified(address[],int16[],int16[]) + handler: handleProtocolFillFeesModified + - event: ProtocolSwapFeesModified(address[],int16[],int16[]) + handler: ProtocolSwapFeesModified + - kind: ethereum/contract + name: PoolsharkRouter + network: arbitrum-one + source: + address: '0x12b7a6dd3a3dfde6a0f112a1bd876f704d933915' + abi: PoolsharkRouter + startBlock: 168223826 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/poolsharkrouter.ts + entities: + - PoolsharkRouter + abis: + - name: PoolsharkRouter + file: ./abis/PoolsharkRouter.json + eventHandlers: + - event: RouterDeployed(address,address,address) + handler: handleRouterDeployed + - kind: ethereum/contract + name: vFIN + network: arbitrum-one + source: + address: '0xFA3e62Aae5DE014c4CD20377Ec90Eb8e59d31169' + abi: vFIN + startBlock: 161266335 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/vfin.ts + entities: + - VestedFin + abis: + - name: vFIN + file: ./abis/vFIN.json + eventHandlers: + - event: Transfer(indexed address,indexed address,indexed uint256) + handler: handleTransfer + - kind: ethereum/contract + name: v3EthUsdcPool + network: arbitrum-one + source: + address: '0xc31e54c7a869b9fcbecc14363cf510d1c41fa443' + abi: v3Pool + startBlock: 168223500 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/v3Pool.ts + entities: + - BasePrice + abis: + - name: v3Pool + file: ./abis/v3Pool.json + eventHandlers: + - event: Swap(indexed address,indexed address,int256,int256,uint160,uint128,int24) + handler: handleV3Swap \ No newline at end of file diff --git a/subgraph/templates/scroll-sepolia.subgraph.template.yaml b/subgraph/templates/scroll-sepolia.subgraph.template.yaml new file mode 100644 index 00000000..2c173b5b --- /dev/null +++ b/subgraph/templates/scroll-sepolia.subgraph.template.yaml @@ -0,0 +1,237 @@ +specVersion: 0.0.4 +description: Poolshark is a Directional Liquidity AMM allowing LPs to capture directional strength. +repository: https://github.com/poolshark-protocol/limit +schema: + file: ./schema.graphql +templates: + - kind: ethereum/contract + name: LimitPoolTemplate + network: scroll-sepolia + source: + abi: LimitPool + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpool.ts + entities: + - Token + - LimitPool + - Position + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: InitializeLimit(int24,int24,uint160,int24) + handler: handleInitialize + - event: SwapLimit(indexed address,uint256,uint256,uint200,uint200,uint160,uint128,uint128,int24,indexed bool,indexed bool) + handler: handleSwap + - event: MintLimit(indexed address,int24,int24,bool,uint32,uint32,uint128,uint128) + handler: handleMintLimit + - event: SampleCountIncreased(uint16) + handler: handleSampleCountIncreased + - event: SampleRecorded(int56,uint160) + handler: handleSampleRecorded + - event: MintRange(indexed address,int24,int24,indexed uint32,uint128,int128,int128) + handler: handleMintRange + - event: BurnRange(indexed address,indexed uint256,uint128,int128,int128) + handler: handleBurnRange + - event: CompoundRange(indexed uint32,uint128) + handler: handleCompoundRange + - event: SyncRangeTick(uint200,uint200,int24) + handler: handleSyncRangeTick + - event: BurnLimit(indexed address,uint32,int24,int24,int24,int24,bool,uint128,uint128,uint128) + handler: handleBurnLimit + - event: SyncLimitPool(uint160,uint128,uint32,int24,bool) + handler: handleSyncLimitPool + - event: SyncLimitLiquidity(uint128,int24,bool) + handler: handleSyncLimitLiquidity + - event: SyncLimitTick(uint32,int24,bool) + handler: handleSyncLimitTick + - event: Collect(indexed address,address,indexed int24,indexed int24,uint128,uint128) + handler: handleCollect + # ERC-1155 events + - kind: ethereum/contract + name: PositionERC1155Template + network: scroll-sepolia + source: + abi: PositionERC1155 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/positionerc1155.ts + entities: + - Token + - RangePool + - RangePosition + abis: + - name: PositionERC1155 + file: ./abis/PositionERC1155.json + eventHandlers: + - event: TransferSingle(indexed address,indexed address,indexed address,uint256,uint256) + handler: handleTransferSingle + - event: TransferBatch(indexed address,indexed address,indexed address,uint256[],uint256[]) + handler: handleTransferBatch + # RangeStaker events + - kind: ethereum/contract + name: RangeStakerTemplate + network: scroll-sepolia + source: + abi: RangeStaker + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/staking/rangestaker.ts + entities: + - RangeStake + abis: + - name: RangeStaker + file: ./abis/RangeStaker.json + eventHandlers: + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: StakeRange(address,uint32,address,uint256,uint256,uint128) + handler: handleStakeRange + - event: StakeRangeAccrued(address,uint32,uint256,uint256) + handler: handleStakeRangeAccrued + - event: UnstakeRange(address,uint32,address) + handler: handleUnstakeRange +dataSources: + - kind: ethereum/contract + name: LimitPoolFactory + network: scroll-sepolia + source: + address: '0xd506193e48f13438e35a1edc4ef5394876b5efa4' + abi: LimitPoolFactory + startBlock: 2764010 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolfactory.ts + entities: + - Token + - LimitPool + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: LimitPoolCreated(address,address,indexed address,indexed address,indexed uint16,int16,uint16) + handler: handlePoolCreated + - kind: ethereum/contract + name: LimitPoolManager + network: scroll-sepolia + source: + address: '0x7113aec7595accbaea1e61bc59f97e35d3a87bd5' + abi: LimitPoolManager + startBlock: 2764009 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolmanager.ts + entities: + - LimitPool + - LimitPoolFactory + abis: + - name: LimitPoolManager + file: ./abis/LimitPoolManager.json + eventHandlers: + - event: FeeTierEnabled(uint16,int16) + handler: handleFeeTierEnabled + - event: PoolTypeEnabled(bytes32,address,address,uint16) + handler: handlePoolTypeEnabled + - event: FactoryChanged(indexed address,indexed address) + handler: handleFactoryChanged + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: ProtocolFeesCollected(address[],uint128[],uint128[]) + handler: handleProtocolFeesCollected + - event: ProtocolFillFeesModified(address[],int16[],int16[]) + handler: handleProtocolFillFeesModified + - event: ProtocolSwapFeesModified(address[],int16[],int16[]) + handler: handleProtocolSwapFeesModified + - kind: ethereum/contract + name: PoolsharkRouter + network: scroll-sepolia + source: + address: '0x14ce728bf96d5ec0976f332605fe80a42ec0b244' + abi: PoolsharkRouter + startBlock: 2764065 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/poolsharkrouter.ts + entities: + - PoolsharkRouter + abis: + - name: PoolsharkRouter + file: ./abis/PoolsharkRouter.json + eventHandlers: + - event: RouterDeployed(address,address,address) + handler: handleRouterDeployed + - kind: ethereum/contract + name: vFIN + network: scroll-sepolia + source: + address: '0x1a16415867fde435d3f8a6631ef308ae7bf96b3b' + abi: vFIN + startBlock: 2764254 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/vfin.ts + entities: + - VestedFin + abis: + - name: vFIN + file: ./abis/vFIN.json + eventHandlers: + - event: Transfer(indexed address,indexed address,indexed uint256) + handler: handleTransfer + - kind: ethereum/contract + name: v3EthUsdcPool + network: scroll-sepolia + source: + address: '0xea578474c65f859739c8ec362543eedc04f0d48e' + abi: v3Pool + startBlock: 2764030 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/v3Pool.ts + entities: + - BasePrice + abis: + - name: v3Pool + file: ./abis/v3Pool.json + eventHandlers: + - event: Initialize(uint160,int24) + handler: handleV3Initialize + - event: Swap(indexed address,indexed address,int256,int256,uint160,uint128,int24) + handler: handleV3Swap \ No newline at end of file diff --git a/subgraph/templates/subgraph.template.yaml b/subgraph/templates/subgraph.template.yaml new file mode 100644 index 00000000..a96e5db0 --- /dev/null +++ b/subgraph/templates/subgraph.template.yaml @@ -0,0 +1,200 @@ +specVersion: 0.0.4 +description: Poolshark is a Directional Liquidity AMM allowing LPs to capture directional strength. +repository: https://github.com/poolshark-protocol/limit +schema: + file: ./schema.graphql +templates: + - kind: ethereum/contract + name: LimitPoolTemplate + network: {{network}} + source: + abi: LimitPool + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpool.ts + entities: + - Token + - LimitPool + - Position + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: Initialize(int24,int24,uint160,int24) + handler: handleInitialize + - event: Swap(indexed address,uint256,uint256,uint200,uint200,uint160,uint128,uint128,int24,indexed bool,indexed bool) + handler: handleSwap + - event: MintLimit(indexed address,int24,int24,bool,uint32,uint32,uint128,uint128) + handler: handleMintLimit + - event: SampleCountIncreased(uint16) + handler: handleSampleCountIncreased + - event: SampleRecorded(int56,uint160) + handler: handleSampleRecorded + - event: MintRange(indexed address,int24,int24,indexed uint32,uint128,int128,int128) + handler: handleMintRange + - event: BurnRange(indexed address,indexed uint256,uint128,int128,int128) + handler: handleBurnRange + - event: CompoundRange(indexed uint32,uint128) + handler: handleCompoundRange + - event: SyncRangeTick(uint200,uint200,int24) + handler: handleSyncRangeTick + - event: BurnLimit(indexed address,uint32,int24,int24,int24,int24,bool,uint128,uint128,uint128) + handler: handleBurnLimit + - event: SyncLimitPool(uint160,uint128,uint32,int24,bool) + handler: handleSyncLimitPool + - event: SyncLimitLiquidity(uint128,int24,bool) + handler: handleSyncLimitLiquidity + - event: SyncLimitTick(uint32,int24,bool) + handler: handleSyncLimitTick + - event: CollectRange0(uint128) + handler: handleCollectRange0 + - event: CollectRange1(uint128) + handler: handleCollectRange1 + # ERC-1155 events + - kind: ethereum/contract + name: PositionERC1155Template + network: arbitrum-goerli + source: + abi: PositionERC1155 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/positionerc1155.ts + entities: + - Token + - RangePool + - RangePosition + abis: + - name: PositionERC1155 + file: ./abis/PositionERC1155.json + eventHandlers: + - event: TransferSingle(indexed address,indexed address,indexed address,uint256,uint256) + handler: handleTransferSingle + - event: TransferBatch(indexed address,indexed address,indexed address,uint256[],uint256[]) + handler: handleTransferBatch + # RangeStaker events + - kind: ethereum/contract + name: RangeStakerTemplate + network: arbitrum-goerli + source: + # address: '0xe5e2E95A986CE078606C403593593b18Ed98f4d6' + abi: RangeStaker + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/staking/rangestaker.ts + entities: + - RangeStake + abis: + - name: RangeStaker + file: ./abis/RangeStaker.json + eventHandlers: + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: StakeRange(address,uint32,address,uint256,uint256,uint128) + handler: handleStakeRange + - event: StakeRangeAccrued(address,uint32,uint256,uint256) + handler: handleStakeRangeAccrued + - event: UnstakeRange(address,uint32,address) + handler: handleUnstakeRange +dataSources: + - kind: ethereum/contract + name: LimitPoolFactory + network: arbitrum-goerli + source: + address: '{{0x1b215002e688135549cc0290d6cf1f94e3aa425c}}' + abi: LimitPoolFactory + startBlock: 56821935 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolfactory.ts + entities: + - Token + - LimitPool + abis: + - name: LimitPool + file: ./abis/LimitPool.json + - name: LimitPoolFactory + file: ./abis/LimitPoolFactory.json + - name: ERC20 + file: ./abis/ERC20.json + - name: ERC20SymbolBytes + file: ./abis/ERC20SymbolBytes.json + - name: ERC20NameBytes + file: ./abis/ERC20NameBytes.json + eventHandlers: + - event: PoolCreated(address,address,indexed address,indexed address,indexed uint16,int16,uint16) + handler: handlePoolCreated + - kind: ethereum/contract + name: LimitPoolManager + network: arbitrum-goerli + source: + address: '0x6d3af137e75097b892683832a2c0132f99625d0e' + abi: LimitPoolManager + startBlock: 56821935 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/limitpoolmanager.ts + entities: + - LimitPool + - LimitPoolFactory + abis: + - name: LimitPoolManager + file: ./abis/LimitPoolManager.json + eventHandlers: + - event: FeeTierEnabled(uint16,int16) + handler: handleFeeTierEnabled + - event: PoolTypeEnabled(bytes32,address,address,uint16) + handler: handlePoolTypeEnabled + - event: FactoryChanged(indexed address,indexed address) + handler: handleFactoryChanged + - event: FeeToTransfer(indexed address,indexed address) + handler: handleFeeToTransfer + - event: OwnerTransfer(indexed address,indexed address) + handler: handleOwnerTransfer + - event: ProtocolFeesCollected(address[],uint128[],uint128[]) + handler: handleProtocolFeesCollected + - event: ProtocolFillFeesModified(address[],int16[],int16[]) + handler: handleProtocolFillFeesModified + - event: ProtocolSwapFeesModified(address[],int16[],int16[]) + handler: ProtocolSwapFeesModified + - kind: ethereum/contract + name: PoolsharkRouter + network: arbitrum-goerli + source: + address: '0x4b5a159109bb997135e9b2bb6e486e2101466881' + abi: PoolsharkRouter + startBlock: 56821935 + mapping: + kind: ethereum/events + apiVersion: 0.0.6 + language: wasm/assemblyscript + file: ./src/mappings/poolsharkrouter.ts + entities: + - PoolsharkRouter + abis: + - name: PoolsharkRouter + file: ./abis/PoolsharkRouter.json + eventHandlers: + - event: RouterDeployed(address,address,address) + handler: handleRouterDeployed + + diff --git a/tasks/deploy/deploy-limitpools.ts b/tasks/deploy/deploy-limitpools.ts index a2277bc7..0564ba6c 100644 --- a/tasks/deploy/deploy-limitpools.ts +++ b/tasks/deploy/deploy-limitpools.ts @@ -27,5 +27,8 @@ task(DEPLOY_LIMITPOOLS) await deployLimitPools.deployLimitPools.postDeployment() + const psharkAsciiArt = '@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#%@@@@%@@@@%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@%%%%%%@@%==========+++**#@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@%%@@@@@@@@@@@@@@@%%##%%%@@%%%@@%%*-=====+++**#**+*#*#*#@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@=+===========+#@@@@@%#@%%%%%%*#%%%%%##%@@@@@@@@#****++#%#**#%=%@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@+-==+++***++++=++@@%%%%%%%%*****#%#*#@%@@@@@@@@@@@@@%#+#%#*##*=#@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@+=+*******+++%%##%%####++++++**@@%%@@@@@@@@@@@@@@@###*#%#+==@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@%=**##**+#%###########+====*@%@@%%@@@@@@@@@@@@@%###%@+%=+=*@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@#%%####*##*##*#**#**+=+=-=*%%%###%@@@@@@@@@@@####@#=====+*@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@#%#***#***#*****=*+=+==--=@@%#**###@@@@@@@@@@@###@@%=+++=++#@@@@*@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@##*********+***##++*+#%@*%@@@@@####@@@@#=%@@@@@@@@@@@+==++=+=%%#+*@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@##*****+**+*********++=%%%@@@@@@@@@*@@@%===@@@@@@@@@*===++=+++**#%#@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@#**++++*+++++===+***+==#%%#@@@@@@@@*##@*===+%@@@@%+==+=+++++++*%@%%@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@**++++++++====----+++====+###@@@@@@@%#%%*==++*+#+===+++++++++++*%%@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@%###*++++++====---=*@@@@+===-%@**%@@@@@@@#%%%===*+****+++*++++*+*+*#@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@*+++*+++++===--=-*@@@@@@@@=--=@@@#%@@@@@@@@#%%*++++**#+++*++**++***#*@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@%===+++++=+++===*@@@@@@@@@@=-=%@@@@@%#@@@@@@@*===*++=++*++++****+**#%+@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@*=+@@#==+==+=-=@@@%%@@@@@@@@@@@@#***%%@@@@%+===+++*+*******+**++*#%*#@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@======-==#***%#@@@@@@@@@@@##%%%%%%@#====****+*****#**###*+++#*@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@%-===*=++**###@@@@@@@@@@@@@+#%%%%@*======+*********###+++++++++%@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@%-=+#%%+*###%@@@@@@@@@@@@@@@#%#@%++=+++===**#*#****#%=====+++++++%@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@-=#****####@@@@@@@@@@@@@@@@#%@*++*#++++=+#*#####*#%%==============*@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@*=#**+*###@@@@@@@@@@@@@@@@@#%+**###**+++######%%#+-==--=-=------======#@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@++#++++*##%@@@@@@@@@@@@@@@%+*#%%######*##%#+=-=#@@@@@*----------------*@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@%+#*+++==+***#@@@@@@@@@@@%#%%####%%#*+#*++%@@@@@@@@@@@@@%+=======*@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@*+**+========------=-=%@#%%%%%%%%%%%%#%#%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@#=====-==-------=#@@@@########%%%%%%##++#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@%%##%%@@@@@@@@@@@@%#######%%#*+=++@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%#*####++++=@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@%##**+=#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@***%@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\r\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@' + console.log(psharkAsciiArt) + console.log('Limit pool deployment complete.\n') }) diff --git a/tasks/deploy/utils/mintPosition.ts b/tasks/deploy/utils/mintPosition.ts index dec8ce86..cbfa2656 100644 --- a/tasks/deploy/utils/mintPosition.ts +++ b/tasks/deploy/utils/mintPosition.ts @@ -1,9 +1,9 @@ import { BigNumber } from 'ethers' -import { BN_ZERO, getLiquidity, getPrice, validateBurn, validateMint, validateSwap, } from '../../../test/utils/contracts/limitpool' +import { BN_ZERO, } from '../../../test/utils/contracts/limitpool' import { InitialSetup } from '../../../test/utils/setup/initialSetup' -import { validateMint as validateMintRange, validateBurn as validateBurnRange } from '../../../test/utils/contracts/rangepool' -import { mintSigners20 } from '../../../test/utils/token' import { getNonce } from '../../utils' +import { parseUnits } from 'ethers/lib/utils' +import { mintSigners20 } from '../../../test/utils/token' export class MintPosition { private initialSetup: InitialSetup @@ -31,12 +31,12 @@ export class MintPosition { console.log(this.nonce) await this.initialSetup.readLimitPoolSetup(this.nonce) console.log('read positions') - const token0Amount = ethers.utils.parseUnits('100', await hre.props.token0.decimals()) - const token1Amount = ethers.utils.parseUnits('100', await hre.props.token1.decimals()) - - await mintSigners20(hre.props.token0, token0Amount.mul(10000), [hre.props.alice]) - await mintSigners20(hre.props.token1, token1Amount.mul(10000), [hre.props.alice]) + const token0Amount = ethers.utils.parseUnits('1000', 18) + console.log('mint tokens', hre.props.alice.address) + // await mintSigners20(hre.props.token0, token0Amount.mul(10000), [hre.props.alice]) + await mintSigners20(hre.props.token1, token0Amount, [hre.props.alice]) + console.log('minted tokens') const liquidityAmount = '49802891105937278098768' // await getPrice(true) @@ -84,59 +84,70 @@ export class MintPosition { // ) // console.log('position snapshot', snapshot.feesOwed0.toString(), snapshot.feesOwed1.toString()) - + const amountIn = parseUnits('100', 18) + const signer = hre.props.alice + // let approveTxn = await hre.props.token0.connect(signer).approve(hre.props.poolRouter.address, amountIn) + console.log('approve tokens') + let approveTxn = await hre.props.token1.connect(signer).approve(hre.props.poolRouter.address, amountIn) + await approveTxn.wait() // const aliceId = await validateMintRange({ // signer: hre.props.alice, - // recipient: '0x9dA9409D17DeA285B078af06206941C049F692Dc', - // lower: '-400000', - // upper: '100000', - // amount0: token0Amount.mul(1000), - // amount1: token1Amount.mul(1000), - // balance0Decrease: BigNumber.from('624999999999999999'), - // balance1Decrease: token1Amount.mul(10).sub(1), + // recipient: hre.props.alice.address, + // lower: '-200200', + // upper: '-200000', + // amount0: BN_ZERO, + // amount1: amountIn, + // balance0Decrease: BN_ZERO, + // balance1Decrease: amountIn, // liquidityIncrease: BN_ZERO, // revertMessage: '', // }) // return - // const signer = hre.props.alice - // let approveTxn = await hre.props.token1.connect(signer).approve(hre.props.poolRouter.address, token1Amount.mul(1000)) - // await approveTxn.wait() - // // for (let i=0; i < 20; i++) { - - // const zeroForOne = false - // const amountIn = token1Amount.mul(1000) - // const priceLimit = BigNumber.from('3169126500570573503741758013440') - - // let txn = await hre.props.poolRouter - // .connect(signer) - // .multiSwapSplit( - // [hre.props.limitPool.address], - // [ - // { - // to: signer.address, - // priceLimit: priceLimit, - // amount: amountIn, - // zeroForOne: zeroForOne, - // exactIn: true, - // callbackData: ethers.utils.formatBytes32String('') - // }, - // ], {gasLimit: 3000000}) - // await txn.wait() + // const token0Amount = ethers.utils.parseUnits('1', 16) + + // const amountIn = token0Amount + + // // // for (let i=0; i < 20; i++) { + + // 0 => 1 + //0xe80dc9a69853483c745f8e32162f0bd5813c b291 => new factory + + const zeroForOne = true + + const priceLimit = BigNumber.from('4039859466863342510789667678182') + console.log('before swap') + let txn = await hre.props.poolRouter + .connect(signer) + .multiSwapSplit( + ['0x02225f6a3d83648d7906a23856331c819265394d'], + [ + { + to: signer.address, + priceLimit: priceLimit, + amount: amountIn, + zeroForOne: zeroForOne, + exactIn: true, + callbackData: ethers.utils.formatBytes32String('') + }, + ]) + await txn.wait() // } + // return + // const globalStateAfter = (await hre.props.limitPool.globalState()) // console.log('sample state', globalStateAfter.pool.samples.index, globalStateAfter.pool.samples.count, globalStateAfter.pool.samples.countMax, globalStateAfter.pool.tickAtPrice) - await validateSwap({ - signer: hre.props.alice, - recipient: hre.props.alice.address, - zeroForOne: false, - amountIn: token1Amount.mul(100), - priceLimit: BigNumber.from('3256300000000000000000000'), - balanceInDecrease: token1Amount.mul(10000), - balanceOutIncrease: '15641085361593105857', - revertMessage:'', - }) + // await validateSwap({ + // signer: hre.props.alice, + // recipient: hre.props.alice.address, + // zeroForOne: false, + // amountIn: token1Amount.mul(100), + // priceLimit: BigNumber.from('3256300000000000000000000'), + // balanceInDecrease: token1Amount.mul(10000), + // balanceOutIncrease: '15641085361593105857', + // revertMessage:'', + // }) // await validateBurn({ // signer: hre.props.alice, @@ -170,7 +181,7 @@ export class MintPosition { // await getPrice(false, true) // await getLiquidity(false, true) - console.log('position minted') + // console.log('position minted') } public async postDeployment() {} diff --git a/tasks/deploy/utils/mintTokens.ts b/tasks/deploy/utils/mintTokens.ts index 42a5eede..69eea2c2 100644 --- a/tasks/deploy/utils/mintTokens.ts +++ b/tasks/deploy/utils/mintTokens.ts @@ -1,5 +1,5 @@ import { InitialSetup } from '../../../test/utils/setup/initialSetup' -import { mintSigners20 } from '../../../test/utils/token' +import { mintSigners20, mintSigners20WithRecipient } from '../../../test/utils/token' import { getNonce } from '../../utils' export class MintTokens { @@ -28,8 +28,12 @@ export class MintTokens { await this.initialSetup.readLimitPoolSetup(this.nonce) const token0Amount = ethers.utils.parseUnits('100', await hre.props.token0.decimals()) const token1Amount = ethers.utils.parseUnits('100', await hre.props.token1.decimals()) - await mintSigners20(hre.props.token0, token0Amount.mul(100), [hre.props.alice]) - await mintSigners20(hre.props.token1, token1Amount.mul(100), [hre.props.alice]) + // 0x65f5B282E024e3d6CaAD112e848dEc3317dB0902 + // 0x1DcF623EDf118E4B21b4C5Dc263bb735E170F9B8 + // 0x9dA9409D17DeA285B078af06206941C049F692Dc + // 0xBd5db4c7D55C086107f4e9D17c4c34395D1B1E1E + await mintSigners20WithRecipient(hre.props.token0, token0Amount.mul(100), [hre.props.alice], '0x65f5B282E024e3d6CaAD112e848dEc3317dB0902') + // await mintSigners20WithRecipient(hre.props.token1, token1Amount.mul(100), [hre.props.alice], '0x65f5B282E024e3d6CaAD112e848dEc3317dB0902') // const token0Balance = await hre.props.token0.balanceOf( // '0x50924f626d1Ae4813e4a81E2c5589EC3882C13ca' diff --git a/test/contracts/libraries/samples.ts b/test/contracts/libraries/samples.ts index f5894765..5d579789 100644 --- a/test/contracts/libraries/samples.ts +++ b/test/contracts/libraries/samples.ts @@ -84,8 +84,8 @@ describe('Samples Tests', function () { if (debugMode) await getSample(debugMode) await validateSample({ - secondsPerLiquidityAccum: '2722258935367507707706996859454145691648', - tickSecondsAccum: '128760', + secondsPerLiquidityAccum: '3062541302288446171170371466885913903104', + tickSecondsAccum: '144855', averagePrice: '177157928842132501967358423881', averageLiquidity: '0', averageTick: 16095 @@ -107,8 +107,8 @@ describe('Samples Tests', function () { if (debugMode) await getSample(debugMode) await validateSample({ - secondsPerLiquidityAccum: '3743106036130323098097120681749450326016', - tickSecondsAccum: '177045', + secondsPerLiquidityAccum: '4083388403051261561560495289181218537472', + tickSecondsAccum: '193140', averagePrice: '177157928842132501967358423881', averageLiquidity: '0', averageTick: 16095 @@ -126,8 +126,8 @@ describe('Samples Tests', function () { }) await validateSample({ - secondsPerLiquidityAccum: '3743106036130323098112338571829045210211', - tickSecondsAccum: '209235', + secondsPerLiquidityAccum: '4083388403051261561575713179260813421667', + tickSecondsAccum: '225330', averagePrice: '177157928842132501967358423881', averageLiquidity: '30435780159189768390', averageTick: 16095 @@ -147,8 +147,8 @@ describe('Samples Tests', function () { }) await validateSample({ - secondsPerLiquidityAccum: '3743106036130323098127556461908640094406', - tickSecondsAccum: '209313', + secondsPerLiquidityAccum: '4083388403051261561590931069340408305862', + tickSecondsAccum: '225408', averagePrice: '79382800422362568253159300200', averageLiquidity: '30435780159189768390', averageTick: 39 @@ -168,8 +168,8 @@ describe('Samples Tests', function () { }) await validateSample({ - secondsPerLiquidityAccum: '3743106036130323098142774351988234978601', - tickSecondsAccum: '209471', + secondsPerLiquidityAccum: '4083388403051261561606148959420003190057', + tickSecondsAccum: '225566', averagePrice: '79541716941062961637430132579', averageLiquidity: '30435780159189768390', averageTick: 79 @@ -189,8 +189,8 @@ describe('Samples Tests', function () { }) await validateSample({ - secondsPerLiquidityAccum: '3743106036130323098142774351988234978601', - tickSecondsAccum: '209471', + secondsPerLiquidityAccum: '4083388403051261561606148959420003190057', + tickSecondsAccum: '225566', averagePrice: '79382800422362568253159300200', averageLiquidity: '15217890079594884196', averageTick: 39 diff --git a/test/contracts/limitpool.ts b/test/contracts/limitpool.ts index 3db63cc6..7ad94a83 100644 --- a/test/contracts/limitpool.ts +++ b/test/contracts/limitpool.ts @@ -6948,7 +6948,7 @@ describe('LimitPool Tests', function () { zeroForOne: false, balanceInIncrease: "364", balanceOutIncrease: "0", - lowerTickCleared: true, //TODO: double check this + lowerTickCleared: true, upperTickCleared: true, revertMessage: "", }); @@ -7225,7 +7225,7 @@ describe('LimitPool Tests', function () { balanceInDecrease: tokenAmount, positionLiquidityChange: '24296516120648317604804', liquidityIncrease: "46581025846542819175371", - balanceOutIncrease: "51167329561763357462", //TODO: why is this number different on this side + balanceOutIncrease: "51167329561763357462", upperTickCleared: false, lowerTickCleared: true, revertMessage: "", diff --git a/test/contracts/limitpoolmanager.ts b/test/contracts/limitpoolmanager.ts index cb07502c..37917dcd 100644 --- a/test/contracts/limitpoolmanager.ts +++ b/test/contracts/limitpoolmanager.ts @@ -207,19 +207,6 @@ describe('LimitPoolManager Tests', function () { ).to.be.revertedWith('InvalidSwapFee()') }) - /// @dev - pool type id is incremented each time - // it('Should not enable pool type a second time', async function () { - // await expect( - // hre.props.limitPoolManager - // .connect(hre.props.admin) - // .enablePoolType( - // hre.props.limitPoolImpl.address, - // hre.props.positionERC1155.address, - // constantProductString - // ) - // ).to.be.revertedWith('PoolTypeAlreadyExists()') - // }) - it('Should not enable pool type w/ invalid impl addresses', async function () { await expect( hre.props.limitPoolManager @@ -250,34 +237,6 @@ describe('LimitPoolManager Tests', function () { ).to.be.revertedWith('InvalidImplAddresses()') }) - // it('Should enable new twap source', async function () { - // await hre.props.limitPoolManager - // .connect(hre.props.admin) - // .enableTwapSource(psharkString, hre.props.uniswapV3Source.address, hre.props.uniswapV3Source.address) - - // const twapSource = await hre.props.limitPoolManager - // .twapSources(psharkString) - // expect(twapSource[0]).to.be.equal(hre.props.uniswapV3Source.address) - // expect(twapSource[1]).to.be.equal(hre.props.uniswapV3Source.address) - // }) - - - // it('Should not enable twap source with OwnerOnly()', async function () { - // await expect( - // hre.props.limitPoolManager - // .connect(hre.props.bob) - // .enableTwapSource(psharkString, hre.props.uniswapV3Source.address, hre.props.uniswapV3Source.address) - // ).to.be.revertedWith('OwnerOnly()') - // }) - - // it('Should not enable twap source with invalid string', async function () { - // await expect( - // hre.props.limitPoolManager - // .connect(hre.props.admin) - // .enableTwapSource(ethers.utils.formatBytes32String(''), hre.props.uniswapV3Source.address, hre.props.uniswapV3Source.address) - // ).to.be.revertedWith('TwapSourceNameInvalid()') - // }) - it('Should not update protocol fees on pool', async function () { let globalStateBefore = await hre.props.limitPool.globalState(); await expect( @@ -341,11 +300,129 @@ describe('LimitPoolManager Tests', function () { let globalStateAfter = await hre.props.limitPool.globalState(); expect(globalStateBefore.pool.protocolSwapFee0).to.be.equal(globalStateAfter.pool.protocolSwapFee0) expect(globalStateBefore.pool.protocolSwapFee1).to.be.equal(globalStateAfter.pool.protocolSwapFee1) - expect(globalStateBefore.pool0.protocolFillFee).to.be.equal(globalStateBefore.pool0.protocolFillFee) - expect(globalStateBefore.pool1.protocolFillFee).to.be.equal(globalStateBefore.pool1.protocolFillFee) + expect(globalStateBefore.pool0.protocolFillFee).to.be.equal(globalStateAfter.pool0.protocolFillFee) + expect(globalStateBefore.pool1.protocolFillFee).to.be.equal(globalStateAfter.pool1.protocolFillFee) // erc-20 balance should not change }) + it('Should update protocol fees on pool', async function () { + // protocol swap fee 0 + let globalStateBefore = await hre.props.limitPool.globalState(); + let txn = await hre.props.limitPoolManager + .connect(hre.props.admin) + .modifyProtocolFees( + [hre.props.limitPool.address], + [ + { + protocolSwapFee0: 500, + protocolSwapFee1: 500, + protocolFillFee0: 500, + protocolFillFee1: 500, + setFeesFlags: 1 + } + ] + ) + let globalStateAfter = await hre.props.limitPool.globalState(); + expect(globalStateBefore.pool.protocolSwapFee0).to.not.be.equal(globalStateAfter.pool.protocolSwapFee0) + expect(globalStateAfter.pool.protocolSwapFee0).to.be.equal(500) + expect(globalStateBefore.pool.protocolSwapFee1).to.be.equal(globalStateAfter.pool.protocolSwapFee1) + expect(globalStateBefore.pool0.protocolFillFee).to.be.equal(globalStateAfter.pool0.protocolFillFee) + expect(globalStateBefore.pool1.protocolFillFee).to.be.equal(globalStateAfter.pool1.protocolFillFee) + + // protocol swap fee 1 + globalStateBefore = globalStateAfter + txn = await hre.props.limitPoolManager + .connect(hre.props.admin) + .modifyProtocolFees( + [hre.props.limitPool.address], + [ + { + protocolSwapFee0: 500, + protocolSwapFee1: 500, + protocolFillFee0: 500, + protocolFillFee1: 500, + setFeesFlags: 2 + } + ] + ) + globalStateAfter = await hre.props.limitPool.globalState(); + expect(globalStateBefore.pool.protocolSwapFee0).to.be.equal(globalStateAfter.pool.protocolSwapFee0) + expect(globalStateBefore.pool.protocolSwapFee1).to.not.be.equal(globalStateAfter.pool.protocolSwapFee1) + expect(globalStateAfter.pool.protocolSwapFee1).to.be.equal(500) + expect(globalStateBefore.pool0.protocolFillFee).to.be.equal(globalStateAfter.pool0.protocolFillFee) + expect(globalStateBefore.pool1.protocolFillFee).to.be.equal(globalStateAfter.pool1.protocolFillFee) + + // protocol fill fee 0 + globalStateBefore = globalStateAfter + txn = await hre.props.limitPoolManager + .connect(hre.props.admin) + .modifyProtocolFees( + [hre.props.limitPool.address], + [ + { + protocolSwapFee0: 500, + protocolSwapFee1: 500, + protocolFillFee0: 50, + protocolFillFee1: 50, + setFeesFlags: 4 + } + ] + ) + globalStateAfter = await hre.props.limitPool.globalState(); + expect(globalStateBefore.pool.protocolSwapFee0).to.be.equal(globalStateAfter.pool.protocolSwapFee0) + expect(globalStateBefore.pool.protocolSwapFee1).to.be.equal(globalStateAfter.pool.protocolSwapFee1) + + expect(globalStateBefore.pool0.protocolFillFee).to.be.equal(globalStateAfter.pool0.protocolFillFee) + expect(globalStateBefore.pool1.protocolFillFee).to.not.be.equal(globalStateAfter.pool1.protocolFillFee) + expect(globalStateAfter.pool1.protocolFillFee).to.be.equal(50) + + // protocol fill fee 1 + globalStateBefore = globalStateAfter + txn = await hre.props.limitPoolManager + .connect(hre.props.admin) + .modifyProtocolFees( + [hre.props.limitPool.address], + [ + { + protocolSwapFee0: 500, + protocolSwapFee1: 500, + protocolFillFee0: 50, + protocolFillFee1: 50, + setFeesFlags: 8 + } + ] + ) + globalStateAfter = await hre.props.limitPool.globalState(); + expect(globalStateBefore.pool.protocolSwapFee0).to.be.equal(globalStateAfter.pool.protocolSwapFee0) + expect(globalStateBefore.pool.protocolSwapFee1).to.be.equal(globalStateAfter.pool.protocolSwapFee1) + + expect(globalStateBefore.pool0.protocolFillFee).to.not.be.equal(globalStateAfter.pool0.protocolFillFee) + expect(globalStateBefore.pool1.protocolFillFee).to.be.equal(globalStateAfter.pool1.protocolFillFee) + expect(globalStateAfter.pool0.protocolFillFee).to.be.equal(50) + + // reset all protocol fees + globalStateBefore = globalStateAfter + txn = await hre.props.limitPoolManager + .connect(hre.props.admin) + .modifyProtocolFees( + [hre.props.limitPool.address], + [ + { + protocolSwapFee0: 0, + protocolSwapFee1: 0, + protocolFillFee0: 0, + protocolFillFee1: 0, + setFeesFlags: 15 // 8 + 4 + 2 + 1 = 15 + } + ] + ) + globalStateAfter = await hre.props.limitPool.globalState(); + expect(globalStateAfter.pool.protocolSwapFee0).to.be.equal(0) + expect(globalStateAfter.pool.protocolSwapFee1).to.be.equal(0) + expect(globalStateAfter.pool0.protocolFillFee).to.be.equal(0) + expect(globalStateAfter.pool1.protocolFillFee).to.be.equal(0) + }) + // modify each protocol fee one by one and verify others do not change // mint position and complete a swap with protocol fee turned on diff --git a/test/contracts/rangepool.ts b/test/contracts/rangepool.ts index 169fc728..6cd9b81c 100644 --- a/test/contracts/rangepool.ts +++ b/test/contracts/rangepool.ts @@ -17,7 +17,8 @@ import { getRangeLiquidity, getTickLiquidity, } from '../utils/contracts/rangepool' -import { RangePoolState, ZERO_ADDRESS } from '../utils/contracts/limitpool' +import { RangePoolState, ZERO_ADDRESS, getLiquidity } from '../utils/contracts/limitpool' +import { parseUnits } from 'ethers/lib/utils' alice: SignerWithAddress describe('RangePool Exact In Tests', function () { @@ -172,6 +173,76 @@ describe('RangePool Exact In Tests', function () { } }) + it('pool - Should not skip crossing tick - kyber exploit', async function () { + if (debugMode) console.log('kyber pool:', hre.props.kyberPool.address, hre.props.kyberPoolToken.address) + await validateSwap({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + zeroForOne: true, + amount: tokenAmount.div(10), + sqrtPriceLimitX96: BigNumber.from('309485009821345068724781056'), // 2.50 USD per FIN + balanceInDecrease: BigNumber.from('0'), + balanceOutIncrease: BigNumber.from('0'), + poolContract: hre.props.kyberPool, + revertMessage: '', + }) + const aliceId = await validateMint({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + lower: '-111310', + upper: '-110908', + amount0: BigNumber.from('3408506672908522891'), + amount1: BigNumber.from('18870618627071590'), + balance0Decrease: BigNumber.from('3408506672908522891'), + balance1Decrease: BigNumber.from('18870618627071590'), + liquidityIncrease: BigNumber.from('243433600698313192894'), + poolContract: hre.props.kyberPool, + poolTokenContract: hre.props.kyberPoolToken, + revertMessage: '', + }) + await validateBurn({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + lower: '-111310', + upper: '-110908', + positionId: aliceId ?? 0, + liquidityAmount: BigNumber.from('40572266783052180174'), + balance0Increase: BigNumber.from('568084445484753554'), + balance1Increase: BigNumber.from('3145103104511930'), + poolContract: hre.props.kyberPool, + poolTokenContract: hre.props.kyberPoolToken, + revertMessage: '', + }) + await validateSwap({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + zeroForOne: true, + amount: BigNumber.from('3056056735638220799999'), + sqrtPriceLimitX96: BigNumber.from('303343357908702216014253965'), + balanceInDecrease: BigNumber.from('1051453208852302636974'), + balanceOutIncrease: BigNumber.from('15712935110141611'), + poolContract: hre.props.kyberPool, + revertMessage: '', + }) + await validateSwap({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + zeroForOne: false, + amount: BigNumber.from('15768859296842439'), + sqrtPriceLimitX96: BigNumber.from('320556756984076944269385500'), + balanceInDecrease: BigNumber.from('15768859296842439'), + balanceOutIncrease: BigNumber.from('1053450196174862625184'), + poolContract: hre.props.kyberPool, + revertMessage: '', + }) + if (debugMode) await getPrice(hre.props.kyberPool) + if (debugMode) await getRangeLiquidity() + if (balanceCheck) { + console.log('balance after token0:', (await hre.props.token0.balanceOf(hre.props.limitPool.address)).toString()) + console.log('balance after token1:', (await hre.props.token1.balanceOf(hre.props.limitPool.address)).toString()) + } + }) + it("pool0 - Should revert mint, burn, and swap when recipient is zero address", async function () { const aliceId = await validateMint({ signer: hre.props.alice, @@ -275,6 +346,36 @@ describe('RangePool Exact In Tests', function () { }) }); + it.skip('token1 - Should calculate liquidity for TGE position', async function () { + + await validateSwap({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + zeroForOne: false, + amount: tokenAmount.div(10), + sqrtPriceLimitX96: BigNumber.from('2358285847295149069702956253974'), // 2.50 USD per FIN + balanceInDecrease: BigNumber.from('0'), + balanceOutIncrease: BigNumber.from('0'), + revertMessage: '', + }) + + await mintSigners20(hre.props.token0, tokenAmount.mul(1000), [hre.props.alice, hre.props.bob]) + await mintSigners20(hre.props.token1, tokenAmount.mul(1000), [hre.props.alice, hre.props.bob]) + + const aliceId = await validateMint({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + lower: '44850', + upper: '77040', + amount0: parseUnits('52000', 18), + amount1: parseUnits('52000', 18), + balance0Decrease: BigNumber.from('31568903742987611804'), // 49.84 = ~110495.28 USD + balance1Decrease: BigNumber.from('51999999999999999999996'), // 40000 = 100000 USD + liquidityIncrease: BigNumber.from('2555287091759866264142'), + revertMessage: '', + }) + }) + it('token1 - Should mint, swap, and burn 14', async function () { const aliceId = await validateMint({ @@ -894,6 +995,7 @@ describe('RangePool Exact In Tests', function () { }) it('pool - Should mint position inside the other 17', async function () { + // await getPrice() const pool: RangePoolState = (await hre.props.limitPool.globalState()).pool const aliceLiquidity = BigNumber.from('7705754408611783555308') const bobLiquidity = BigNumber.from('12891478442546858467877') @@ -3221,5 +3323,4 @@ describe('RangePool Exact Out Tests', function () { console.log('balance after token1:', (await hre.props.token1.balanceOf(hre.props.limitPool.address)).toString()) } }) - //TODO: swapping to boundary price w/ exactOut }) \ No newline at end of file diff --git a/test/contracts/tgedeployer.ts b/test/contracts/tgedeployer.ts new file mode 100644 index 00000000..22ef1aa7 --- /dev/null +++ b/test/contracts/tgedeployer.ts @@ -0,0 +1,141 @@ +/* global describe it before ethers */ +const hardhat = require('hardhat') +const { expect } = require('chai') +import { gBefore } from '../utils/hooks.test' +import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers' +import { BigNumber } from 'ethers' +import { mintSigners20 } from '../utils/token' +import { + validateMint, + BN_ZERO, + validateSwap, + validateBurn, + getTickAtPrice, + getRangeBalanceOf, + getSnapshot, + getPrice, + getRangeLiquidity, + getTickLiquidity, +} from '../utils/contracts/rangepool' +import { RangePoolState, ZERO_ADDRESS } from '../utils/contracts/limitpool' +import { parseUnits } from 'ethers/lib/utils' +import { validateDeployTge } from '../utils/contracts/poolsharkrouter' + +alice: SignerWithAddress +describe('TGE Deployment Tests', function () { + let tokenAmount: BigNumber + let token0Decimals: number + let token1Decimals: number + let minPrice: BigNumber + let maxPrice: BigNumber + + let alice: SignerWithAddress + let bob: SignerWithAddress + let carol: SignerWithAddress + + ////////// DEBUG FLAGS ////////// + let debugMode = false + let balanceCheck = false + + const liquidityAmount = BigNumber.from('49902591570441687020675') + const liquidityAmount2 = BigNumber.from('50102591670431696268925') + const liquidityAmount3 = BigNumber.from('3852877204305891777654') + const minTickIdx = BigNumber.from('-887272') + const maxTickIdx = BigNumber.from('887272') + + before(async function () { + await gBefore() + let currentBlock = await ethers.provider.getBlockNumber() + const pool: RangePoolState = (await hre.props.limitPool.globalState()).pool + const liquidity = pool.liquidity + const feeGrowthGlobal0 = pool.feeGrowthGlobal0 + const feeGrowthGlobal1 = pool.feeGrowthGlobal1 + const price = pool.price + const nearestTick = pool.tickAtPrice + + expect(liquidity).to.be.equal(BN_ZERO) + + minPrice = BigNumber.from('4295128739') + maxPrice = BigNumber.from('1461446703485210103287273052203988822378723970341') + token0Decimals = await hre.props.token0.decimals() + token1Decimals = await hre.props.token1.decimals() + tokenAmount = ethers.utils.parseUnits('100', token0Decimals) + tokenAmount = ethers.utils.parseUnits('100', token1Decimals) + alice = hre.props.alice + bob = hre.props.bob + carol = hre.props.carol + }) + + this.beforeEach(async function () { + await mintSigners20(hre.props.token0, tokenAmount.mul(10), [hre.props.alice, hre.props.bob]) + await mintSigners20(hre.props.token1, tokenAmount.mul(10), [hre.props.alice, hre.props.bob]) + }) + + it.skip('token1 - Should calculate liquidity for TGE position', async function () { + + await validateSwap({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + zeroForOne: false, + amount: tokenAmount.div(10), + sqrtPriceLimitX96: BigNumber.from('2358285847295149069702956253974'), // 2.50 USD per FIN + balanceInDecrease: BigNumber.from('0'), + balanceOutIncrease: BigNumber.from('0'), + revertMessage: '', + }) + + await mintSigners20(hre.props.token0, tokenAmount.mul(1000), [hre.props.alice, hre.props.bob]) + await mintSigners20(hre.props.token1, tokenAmount.mul(1000), [hre.props.alice, hre.props.bob]) + + const aliceId = await validateMint({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + lower: '44850', + upper: '77040', + amount0: parseUnits('52000', 18), + amount1: parseUnits('52000', 18), + balance0Decrease: BigNumber.from('31568903742987611804'), // 49.84 = ~110495.28 USD + balance1Decrease: BigNumber.from('51999999999999999999996'), // 40000 = 100000 USD + liquidityIncrease: BigNumber.from('2555287091759866264142'), + revertMessage: '', + }) + }) + + it('token1 - Should deploy TGE position', async function () { + + await mintSigners20(hre.props.token0, tokenAmount.mul(55000), [hre.props.alice, hre.props.bob]) + await mintSigners20(hre.props.token1, tokenAmount.mul(55000), [hre.props.alice, hre.props.bob]) + + const aliceLiquidity = BigNumber.from('2572549719381782803480') + + const amount0 = BigNumber.from('39168000000000000000') + const amount1 = BigNumber.from('32271546804490624383438') + + const aliceId = await validateDeployTge({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + lower: '54000', // $10 per FIN + upper: '77040', // $1 per FIN + amount0: parseUnits('41', 18), + amount1: parseUnits('34000', 18), + balance0Decrease: amount0, // TODO: change to correct value + balance1Decrease: amount1, // 52k FIN + liquidityIncrease: aliceLiquidity, + revertMessage: '', + stake: true + }) + + await validateBurn({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + lower: '54000', // $10 per FIN + upper: '77040', // $1 per FIN, + positionId: aliceId, + liquidityAmount: aliceLiquidity, + balance0Increase: amount0.sub(1), // 39.168 ETH + balance1Increase: amount1.sub(1), // 52k FIN + revertMessage: '', + staked: true + }) + }) +}); \ No newline at end of file diff --git a/test/contracts/tickquoter.ts b/test/contracts/tickquoter.ts new file mode 100644 index 00000000..d15c4fc1 --- /dev/null +++ b/test/contracts/tickquoter.ts @@ -0,0 +1,186 @@ +/* global describe it before ethers */ +const hardhat = require('hardhat') +const { expect } = require('chai') +import { gBefore } from '../utils/hooks.test' +import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers' +import { BigNumber } from 'ethers' +import { mintSigners20 } from '../utils/token' +import { + validateMint, + BN_ZERO, + validateSwap, + validateBurn, + getTickAtPrice, + getRangeBalanceOf, + getSnapshot, + getPrice, + getRangeLiquidity, + getTickLiquidity, +} from '../utils/contracts/rangepool' +import { RangePoolState, ZERO_ADDRESS } from '../utils/contracts/limitpool' +import { parseUnits } from 'ethers/lib/utils' +import { validateDeployTge } from '../utils/contracts/poolsharkrouter' + +alice: SignerWithAddress +describe('TickQuoter Tests', function () { + let tokenAmount: BigNumber + let token0Decimals: number + let token1Decimals: number + let minPrice: BigNumber + let maxPrice: BigNumber + + let alice: SignerWithAddress + let bob: SignerWithAddress + let carol: SignerWithAddress + + ////////// DEBUG FLAGS ////////// + let debugMode = false + let balanceCheck = false + + const liquidityAmount = BigNumber.from('49902591570441687020675') + const liquidityAmount2 = BigNumber.from('50102591670431696268925') + const liquidityAmount3 = BigNumber.from('3852877204305891777654') + const minTickIdx = BigNumber.from('-887272') + const maxTickIdx = BigNumber.from('887272') + + before(async function () { + await gBefore() + let currentBlock = await ethers.provider.getBlockNumber() + const pool: RangePoolState = (await hre.props.limitPool.globalState()).pool + const liquidity = pool.liquidity + const feeGrowthGlobal0 = pool.feeGrowthGlobal0 + const feeGrowthGlobal1 = pool.feeGrowthGlobal1 + const price = pool.price + const nearestTick = pool.tickAtPrice + + expect(liquidity).to.be.equal(BN_ZERO) + + minPrice = BigNumber.from('4295128739') + maxPrice = BigNumber.from('1461446703485210103287273052203988822378723970341') + token0Decimals = await hre.props.token0.decimals() + token1Decimals = await hre.props.token1.decimals() + tokenAmount = ethers.utils.parseUnits('100', token0Decimals) + tokenAmount = ethers.utils.parseUnits('100', token1Decimals) + alice = hre.props.alice + bob = hre.props.bob + carol = hre.props.carol + }) + + this.beforeEach(async function () { + await mintSigners20(hre.props.token0, tokenAmount.mul(10), [hre.props.alice, hre.props.bob]) + await mintSigners20(hre.props.token1, tokenAmount.mul(10), [hre.props.alice, hre.props.bob]) + }) + + it('pool - Should quote ticks', async function () { + // 82169065928981720851271231910 + // 177159557114295710296101716160 + if (debugMode) await getPrice() + const pool: RangePoolState = (await hre.props.limitPool.globalState()).pool + const aliceLiquidity = BigNumber.from('7705754408611783555308') + const bobLiquidity = BigNumber.from('12891478442546858467877') + const bobLiquidity2 = BigNumber.from('4901161634764542438930') + + await validateSwap({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + zeroForOne: true, + amount: tokenAmount, + sqrtPriceLimitX96: BigNumber.from('82169065928981720851271231910'), + balanceInDecrease: BigNumber.from('0'), // token1 increase in pool + balanceOutIncrease: BigNumber.from('0'), // token0 decrease in pool + revertMessage: '', + }) + + if (debugMode) await getPrice() + const aliceId = await validateMint({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + lower: '500', + upper: '1000', + amount0: tokenAmount, + amount1: tokenAmount, + balance0Decrease: BigNumber.from('100000000000000000000'), + balance1Decrease: BigNumber.from('90970905615086187051'), + liquidityIncrease: aliceLiquidity, + revertMessage: '', + collectRevertMessage: '' + }) + + await validateSwap({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + zeroForOne: false, + amount: tokenAmount, + sqrtPriceLimitX96: BigNumber.from('82255474610179467046984074964'), + balanceInDecrease: BigNumber.from('8404133769503785680'), // token1 increase in pool + balanceOutIncrease: BigNumber.from('7801206245756322179'), // token0 decrease in pool + revertMessage: '', + }) + + const bobId = await validateMint({ + signer: hre.props.bob, + recipient: hre.props.bob.address, + lower: '600', + upper: '800', + amount0: tokenAmount, + amount1: tokenAmount, + balance0Decrease: BigNumber.from('31002239349424966834'), + balance1Decrease: BigNumber.from('100000000000000000000'), + liquidityIncrease: bobLiquidity, + revertMessage: '', + }) + + // quote ticks + const populatedTicks = await hre.props.tickQuoter.getTickDataInWord(hre.props.limitPool.address, 0) + + expect(populatedTicks[0][0]).to.be.equal(1000) + expect(populatedTicks[1][0]).to.be.equal(800) + expect(populatedTicks[2][0]).to.be.equal(600) + expect(populatedTicks[3][0]).to.be.equal(500) + expect(populatedTicks.length).to.be.equal(4) + + if (debugMode) await getSnapshot(bobId) + if (debugMode) console.log('FIRST BURN') + await validateBurn({ + signer: hre.props.bob, + lower: '600', + upper: '800', + positionId: bobId, + liquidityAmount: BigNumber.from('3867443532764057540363'), + burnPercent: ethers.utils.parseUnits('3', 37), + balance0Increase: BigNumber.from('9300671804827490049'), + balance1Increase: BigNumber.from('29999999999999999999'), + revertMessage: '', + }) + if (debugMode) await getSnapshot(bobId) + if (debugMode) console.log('SECOND BURN') + await validateBurn({ + signer: hre.props.bob, + lower: '600', + upper: '800', + positionId: bobId, + liquidityAmount: BigNumber.from('9024034909782800927514'), + burnPercent: ethers.utils.parseUnits('1', 38), + balance0Increase: BigNumber.from('21701567544597476783'), + balance1Increase: BigNumber.from('69999999999999999999'), + revertMessage: '', + }) + if (debugMode) await getSnapshot(aliceId) + await validateBurn({ + signer: hre.props.alice, + lower: '500', + upper: '1000', + positionId: aliceId, + liquidityAmount: aliceLiquidity, + burnPercent: ethers.utils.parseUnits('1', 38), + balance0Increase: BigNumber.from('92198793754243677819'), + balance1Increase: BigNumber.from('99375039384589972730'), + revertMessage: '', + }) + + if (balanceCheck) { + console.log('balance after token0:', (await hre.props.token0.balanceOf(hre.props.limitPool.address)).toString()) + console.log('balance after token1:', (await hre.props.token1.balanceOf(hre.props.limitPool.address)).toString()) + } + }) +}) \ No newline at end of file diff --git a/test/contracts/wethpool.ts b/test/contracts/wethpool.ts index b8bc9f3c..b931711a 100644 --- a/test/contracts/wethpool.ts +++ b/test/contracts/wethpool.ts @@ -5,6 +5,7 @@ import { SignerWithAddress } from '@nomiclabs/hardhat-ethers/signers' import { BigNumber } from 'ethers' import { mintSigners20 } from '../utils/token' import { + BN_ONE, BN_ZERO, LimitPoolState, getLiquidity, @@ -207,4 +208,74 @@ describe('WethPool Tests', function () { if (debugMode) console.log('pool eth balance after collect:', (await ethers.provider.getBalance(hre.props.limitPool.address)).toString()) if (debugMode) console.log('pool token eth balance after:', (await ethers.provider.getBalance(hre.props.limitPoolToken.address)).toString()) }) + + it('pool - send too low of ETH amount in msg.value', async function () { + + const wethToken0 = hre.props.weth9.address == (await hre.props.wethPool.token0()) + + if (wethToken0) { + let globalState = await hre.props.wethPool.globalState() + if (debugMode) console.log('weth pool start:', globalState.pool.price.toString(), globalState.pool.liquidity.toString()) + const aliceLiquidity = '10405966812730338361' + // mint should revert + const aliceId = await validateMintRange({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + lower: '69080', + upper: '80070', + amount0: BigNumber.from(tokenAmount), + amount1: BigNumber.from(tokenAmount), + balance0Decrease: BigNumber.from('7356461269128718'), + balance1Decrease: BigNumber.from('99999999999999999991'), + liquidityIncrease: BigNumber.from("5202983406365169180"), + revertMessage: '', + poolContract: hre.props.wethPool, + poolTokenContract: hre.props.wethPoolToken + }) + // globalState = await hre.props.wethPool.globalState() + // console.log('weth pool swap1:', globalState.pool.price.toString(), globalState.pool.liquidity.toString()) + + // no-op swap + await validateSwap({ + signer: hre.props.alice, + recipient: hre.props.alice.address, + zeroForOne: true, + amountIn: tokenAmountBn.mul(2), + priceLimit: minPrice, + balanceInDecrease: '396087570498016', // only gas is used; all other ETH is returned + balanceOutIncrease: '0', + revertMessage: 'WrapEth::LowEthBalance()', + nativeIn: true, + poolContract: hre.props.wethPool, + gasUsed: '396087570498016', + customMsgValue: BigNumber.from(2) + }) + + await expect( + hre.props.token0 + .connect(hre.props.alice) + .transferIntoTest() + ).to.be.revertedWith('SafeTransfers::CannotTransferInEth()'); + + await expect( + hre.props.alice.sendTransaction({ + to: hre.props.poolRouter.address, + value: ethers.utils.parseEther("50.0") + }) + ).to.be.revertedWith('PoolsharkRouter::ReceiveInvalid()'); + + await validateBurnRange({ + signer: hre.props.alice, + positionId: aliceId, + lower: '69080', + upper: '80070', + liquidityAmount: BigNumber.from("5202983406365169180"), + balance0Increase: BigNumber.from('7356461269128717'), + balance1Increase: BigNumber.from('99999999999999999990'), + revertMessage: '', + poolContract: hre.props.wethPool, + poolTokenContract: hre.props.wethPoolToken, + }) + } + }) }) \ No newline at end of file diff --git a/test/utils/contracts/limitpool.ts b/test/utils/contracts/limitpool.ts index 1ccf22e7..4a3c4e90 100644 --- a/test/utils/contracts/limitpool.ts +++ b/test/utils/contracts/limitpool.ts @@ -130,6 +130,7 @@ export interface ValidateSwapParams { nativeOut?: boolean poolContract?: LimitPool gasUsed?: string + customMsgValue?: BigNumber } export interface ValidateBurnParams { @@ -374,7 +375,7 @@ export async function validateSwap(params: ValidateSwapParams) { priceLimit: priceLimit, exactIn: true, callbackData: ethers.utils.formatBytes32String('') - }], {gasLimit: 3000000, value: getSwapMsgValue(nativeIn, nativeOut, amountIn)}) + }], {gasLimit: 3000000, value: params.customMsgValue ?? getSwapMsgValue(nativeIn, nativeOut, amountIn)}) ).to.be.revertedWith(revertMessage) return } diff --git a/test/utils/contracts/poolsharkrouter.ts b/test/utils/contracts/poolsharkrouter.ts index c856b11a..00340abe 100644 --- a/test/utils/contracts/poolsharkrouter.ts +++ b/test/utils/contracts/poolsharkrouter.ts @@ -1,3 +1,8 @@ +import { expect } from "chai" +import { BigNumber, Contract } from "ethers" +import { BN_ZERO, Position, ValidateMintParams } from "./rangepool" +import { RangeTick, RangeStake, GlobalState } from "./limitpool" + export function getMintRangeInputData(stake: boolean): any { if (stake) return ethers.utils.defaultAbiCoder.encode( @@ -22,4 +27,125 @@ export function getMintRangeInputData(stake: boolean): any { ) else return ethers.utils.formatBytes32String('') -} \ No newline at end of file +} + +export async function validateDeployTge(params: ValidateMintParams): Promise { + const signer = params.signer + const recipient = params.recipient + const lower = BigNumber.from(params.lower) + const upper = BigNumber.from(params.upper) + const amount0 = params.amount0 + const amount1 = params.amount1 + const balance0Decrease = params.balance0Decrease + const balance1Decrease = params.balance1Decrease + const liquidityIncrease = params.liquidityIncrease + const revertMessage = params.revertMessage + const collectRevertMessage = params.collectRevertMessage + const positionId = params.positionId ? params.positionId : 0 + const poolContract = params.poolContract ?? hre.props.limitPool + const poolTokenContract = params.poolTokenContract ?? hre.props.limitPoolToken + const expectedPositionId = params.positionId ? params.positionId + : (await poolContract.globalState()).positionIdNext + + const stake = params.stake ?? false + + let balance0Before + let balance1Before + const token0 = await hre.ethers.getContractAt('Token20', await poolContract.token0()) + const token1 = await hre.ethers.getContractAt('Token20', await poolContract.token1()) + balance0Before = await token0.balanceOf(params.signer.address) + balance1Before = await token1.balanceOf(params.signer.address) + const approve0Txn = await token0 + .connect(params.signer) + .approve(hre.props.poolRouter.address, amount0) + await approve0Txn.wait() + const approve1Txn = await token1 + .connect(params.signer) + .approve(hre.props.poolRouter.address, amount1) + await approve1Txn.wait() + + let lowerTickBefore: RangeTick + let upperTickBefore: RangeTick + let positionBefore: Position + let positionTokens: Contract + let positionTokenBalanceBefore: BigNumber + lowerTickBefore = (await poolContract.ticks(lower)).range + upperTickBefore = (await poolContract.ticks(upper)).range + + positionBefore = await poolContract.positions(positionId) + positionTokens = await hre.ethers.getContractAt('PositionERC1155', poolTokenContract.address); + positionTokenBalanceBefore = await positionTokens.balanceOf(stake ? hre.props.rangeStaker.address : signer.address, expectedPositionId); + // if (params.positionId && !stake) + // expect(positionTokenBalanceBefore).to.be.equal(1) + if (revertMessage == '') { + const txn = await hre.props.poolRouter + .connect(params.signer) + .deployTge(poolContract.address, hre.props.rangeStaker.address) + await txn.wait() + } else { + await expect( + hre.props.poolRouter + .connect(params.signer) + .multiMintRange( + [poolContract.address], + [ + { + to: recipient, + lower: lower, + upper: upper, + positionId: positionId, + amount0: amount0, + amount1: amount1, + callbackData: getMintRangeInputData(stake), + } + ]) + ).to.be.revertedWith(revertMessage) + return + } + let balance0After + let balance1After + balance0After = await token0.balanceOf(params.signer.address) + balance1After = await token1.balanceOf(params.signer.address) + + expect(balance0Before.sub(balance0After)).to.be.equal(balance0Decrease) + expect(balance1Before.sub(balance1After)).to.be.equal(balance1Decrease) + + let lowerTickAfter: RangeTick + let upperTickAfter: RangeTick + let positionAfter: Position + let positionTokenBalanceAfter: BigNumber + lowerTickAfter = (await poolContract.ticks(lower)).range + upperTickAfter = (await poolContract.ticks(upper)).range + + let globalStateAfter: GlobalState = await poolContract.globalState(); + expect(globalStateAfter.pool.price).to.be.equal(BigNumber.from('2172618421097231267834892073346')) + + positionAfter = await poolContract.positions(expectedPositionId) + positionTokens = await hre.ethers.getContractAt('PositionERC1155', poolTokenContract.address); + positionTokenBalanceAfter = await positionTokens.balanceOf(stake ? hre.props.rangeStaker.address : signer.address, expectedPositionId); + if (!params.positionId) + expect(positionTokenBalanceAfter.sub(positionTokenBalanceBefore)).to.be.equal(BigNumber.from(1)) + expect(lowerTickAfter.liquidityDelta.sub(lowerTickBefore.liquidityDelta)).to.be.equal( + liquidityIncrease + ) + expect(upperTickAfter.liquidityDelta.sub(upperTickBefore.liquidityDelta)).to.be.equal( + BN_ZERO.sub(liquidityIncrease) + ) + expect(positionAfter.liquidity.sub(positionBefore.liquidity)).to.be.equal(liquidityIncrease) + if (stake) { + // check fg0/1 and liquidity match + const stakeKey = ethers.utils.keccak256(ethers.utils.defaultAbiCoder.encode( + ["address", "uint32"], // encode as address array + [ poolContract.address, expectedPositionId ] + )) + const rangeStake: RangeStake = await hre.props.rangeStaker.rangeStakes(stakeKey) + expect(positionAfter.feeGrowthInside0Last).to.be.equal(rangeStake.feeGrowthInside0Last) + expect(positionAfter.feeGrowthInside1Last).to.be.equal(rangeStake.feeGrowthInside1Last) + expect(positionAfter.liquidity).to.be.equal(rangeStake.liquidity) + expect(rangeStake.positionId).to.be.equal(expectedPositionId) + expect(rangeStake.pool).to.be.equal(poolContract.address) + expect(rangeStake.isStaked).to.be.equal(true) + expect(rangeStake.owner).to.be.equal(params.recipient) + } + return expectedPositionId + } \ No newline at end of file diff --git a/test/utils/contracts/rangepool.ts b/test/utils/contracts/rangepool.ts index 34155515..4bc8e278 100644 --- a/test/utils/contracts/rangepool.ts +++ b/test/utils/contracts/rangepool.ts @@ -53,6 +53,7 @@ export interface ValidateMintParams { poolContract?: LimitPool poolTokenContract?: PositionERC1155 stake?: boolean + customMsgValue?: BigNumber } export interface ValidateSampleParams { @@ -73,6 +74,7 @@ export interface ValidateSwapParams { balanceOutIncrease: BigNumber revertMessage: string exactIn?: boolean + poolContract?: LimitPool } export interface ValidateBurnParams { @@ -118,8 +120,8 @@ export async function getTickAtPrice() { console.log('tick at price:', tickAtPrice) } -export async function getPrice() { - const poolPrice = (await hre.props.limitPool.globalState()).pool.price +export async function getPrice(poolContract?: LimitPool) { + const poolPrice = (await (poolContract ?? hre.props.limitPool).globalState()).pool.price console.log('pool price:', poolPrice.toString()) } @@ -213,14 +215,14 @@ export async function validateSwap(params: ValidateSwapParams) { const balanceInDecrease = params.balanceInDecrease const balanceOutIncrease = params.balanceOutIncrease const revertMessage = params.revertMessage - - const poolBefore: RangePoolState = (await hre.props.limitPool.globalState()).pool + const poolContract = params.poolContract ?? hre.props.limitPool + const poolBefore: RangePoolState = (await poolContract.globalState()).pool const liquidityBefore = poolBefore.liquidity const priceBefore = poolBefore.price const nearestTickBefore = poolBefore.tickAtPrice // quote pre-swap and validate balance changes match post-swap - const quote = await hre.props.limitPool.quote({ + const quote = await poolContract.quote({ zeroForOne: zeroForOne, amount: amount, exactIn: params.exactIn ?? true, @@ -258,7 +260,7 @@ export async function validateSwap(params: ValidateSwapParams) { let txn = await hre.props.poolRouter .connect(signer) .multiSwapSplit( - [hre.props.limitPool.address], + [poolContract.address], [{ to: signer.address, zeroForOne: zeroForOne, @@ -273,7 +275,7 @@ export async function validateSwap(params: ValidateSwapParams) { hre.props.poolRouter .connect(signer) .multiSwapSplit( - [hre.props.limitPool.address], + [poolContract.address], [{ to: signer.address, zeroForOne: zeroForOne, @@ -301,7 +303,7 @@ export async function validateSwap(params: ValidateSwapParams) { expect(balanceInBefore.sub(balanceInAfter)).to.be.equal(inAmount) expect(balanceOutAfter.sub(balanceOutBefore)).to.be.equal(outAmount) - const poolAfter: RangePoolState = (await hre.props.limitPool.globalState()).pool + const poolAfter: RangePoolState = (await poolContract.globalState()).pool const liquidityAfter = poolAfter.liquidity const priceAfter = poolAfter.price @@ -325,10 +327,11 @@ export async function validateMint(params: ValidateMintParams): Promise const revertMessage = params.revertMessage const collectRevertMessage = params.collectRevertMessage const positionId = params.positionId ? params.positionId : 0 - const expectedPositionId = params.positionId ? params.positionId - : (await hre.props.limitPool.globalState()).positionIdNext const poolContract = params.poolContract ?? hre.props.limitPool const poolTokenContract = params.poolTokenContract ?? hre.props.limitPoolToken + const expectedPositionId = params.positionId ? params.positionId + : (await poolContract.globalState()).positionIdNext + const stake = params.stake ?? false let balance0Before @@ -479,6 +482,9 @@ export async function validateBurn(params: ValidateBurnParams) { burnPercent = liquidityAmount .mul(ethers.utils.parseUnits('1', 38)) .div(positionBefore.liquidity) + liquidityAmount = burnPercent + .mul(positionBefore.liquidity) + .div(ethers.utils.parseUnits('1', 38)) } if (revertMessage == '') { positionSnapshot = await poolContract.snapshotRange(params.positionId) @@ -541,7 +547,7 @@ export async function validateBurn(params: ValidateBurnParams) { if (burnPercent.eq(ethers.utils.parseUnits('1', 38))) expect(positionTokenBalanceAfter.sub(positionTokenBalanceBefore)).to.be.equal(-1) expect(lowerTickAfter.liquidityDelta.sub(lowerTickBefore.liquidityDelta)).to.be.equal( - BN_ZERO.sub(params.liquidityAmount) + BN_ZERO.sub(liquidityAmount) ) expect(upperTickAfter.liquidityDelta.sub(upperTickBefore.liquidityDelta)).to.be.equal( liquidityAmount diff --git a/test/utils/setup/beforeEachProps.ts b/test/utils/setup/beforeEachProps.ts index 31a7a036..b0c36e43 100644 --- a/test/utils/setup/beforeEachProps.ts +++ b/test/utils/setup/beforeEachProps.ts @@ -19,6 +19,7 @@ import { SnapshotLimitCall, WETH9, RangeStaker, + TickQuoter, } from '../../../typechain' import { InitialSetup } from './initialSetup' import { MintRangeCall } from '../../../typechain' @@ -33,8 +34,11 @@ export interface BeforeEachProps { wethPoolToken: PositionERC1155 limitPoolImpl: LimitPool limitPoolToken: PositionERC1155 + kyberPool: LimitPool + kyberPoolToken: PositionERC1155 limitPoolManager: LimitPoolManager limitPoolFactory: LimitPoolFactory + tickQuoter: TickQuoter poolRouter: PoolsharkRouter ticksLib: Ticks tickMapLib: TickMap @@ -94,8 +98,11 @@ export class GetBeforeEach { let wethPoolToken: PositionERC1155 let limitPoolImpl: LimitPool let limitPoolToken: PositionERC1155 + let kyberPool: LimitPool + let kyberPoolToken: PositionERC1155 let limitPoolManager: LimitPoolManager let limitPoolFactory: LimitPoolFactory + let tickQuoter: TickQuoter let poolRouter: PoolsharkRouter let tickMapLib: TickMap let ticksLib: Ticks @@ -133,8 +140,11 @@ export class GetBeforeEach { wethPoolToken, limitPoolImpl, limitPoolToken, + kyberPool, + kyberPoolToken, limitPoolManager, limitPoolFactory, + tickQuoter, poolRouter, tickMapLib, ticksLib, diff --git a/test/utils/setup/initialSetup.ts b/test/utils/setup/initialSetup.ts index 2a214b31..6b93494c 100644 --- a/test/utils/setup/initialSetup.ts +++ b/test/utils/setup/initialSetup.ts @@ -1,8 +1,9 @@ +import { keccak256 } from 'ethers/lib/utils' import { SUPPORTED_NETWORKS } from '../../../scripts/constants/supportedNetworks' import { DeployAssist } from '../../../scripts/util/deployAssist' import { ContractDeploymentsKeys } from '../../../scripts/util/files/contractDeploymentKeys' import { ContractDeploymentsJson } from '../../../scripts/util/files/contractDeploymentsJson' -import { BurnLimitCall__factory, LimitPool__factory, MintLimitCall__factory, LimitPositions__factory, QuoteCall__factory, PositionERC1155__factory, LimitTicks__factory, FeesCall__factory, SampleCall__factory, SnapshotRangeCall__factory, SnapshotLimitCall__factory, WETH9__factory, RangeStaker, RangeStaker__factory } from '../../../typechain' +import { BurnLimitCall__factory, LimitPool__factory, MintLimitCall__factory, LimitPositions__factory, QuoteCall__factory, PositionERC1155__factory, LimitTicks__factory, FeesCall__factory, SampleCall__factory, SnapshotRangeCall__factory, SnapshotLimitCall__factory, WETH9__factory, RangeStaker, RangeStaker__factory, TickQuoter__factory } from '../../../typechain' import { BurnRangeCall__factory } from '../../../typechain' import { SwapCall__factory } from '../../../typechain' import { MintRangeCall__factory } from '../../../typechain' @@ -167,6 +168,16 @@ export class InitialSetup { ], ) + await this.deployAssist.deployContractWithRetry( + network, + // @ts-ignore + TickQuoter__factory, + 'tickQuoter', + [ + hre.props.limitPoolFactory.address + ], + ) + await this.deployAssist.deployContractWithRetry( network, // @ts-ignore @@ -323,6 +334,15 @@ export class InitialSetup { hre.nonce += 1; + // add 500 fee tier + enableFeeTierTxn = await hre.props.limitPoolManager.enableFeeTier( + 800, + 2 + ); + await enableFeeTierTxn.wait(); + + hre.nonce += 1; + // create first limit pool let createPoolTxn = await hre.props.limitPoolFactory.createLimitPool({ poolTypeId: 0, @@ -345,6 +365,18 @@ export class InitialSetup { }); await wethPoollTxn.wait(); + hre.nonce += 1; + + // create kyber test limit pool + let kyperPoolTxn = await hre.props.limitPoolFactory.createLimitPool({ + poolTypeId: 0, + tokenIn: hre.props.token0.address, + tokenOut: hre.props.token1.address, + swapFee: '800', + startPrice: '3266660825699135434887405499641' + }); + await kyperPoolTxn.wait(); + hre.nonce += 1; [limitPoolAddress, limitPoolTokenAddress] = await hre.props.limitPoolFactory.getLimitPool( @@ -364,27 +396,24 @@ export class InitialSetup { hre.props.wethPool = await hre.ethers.getContractAt('LimitPool', wethPoolAddress) hre.props.wethPoolToken = await hre.ethers.getContractAt('PositionERC1155', wethPoolTokenAddress) - await this.deployAssist.saveContractDeployment( - network, - 'LimitPool', - 'wethPool', - hre.props.wethPool, - [ - hre.props.weth9.address, - hre.props.token1.address, - '500', - '3266660825699135434887405499641', - 0 - ] + let [kyberPoolAddress, kyberPoolTokenAddress] = await hre.props.limitPoolFactory.getLimitPool( + hre.props.token0.address, + hre.props.token1.address, + '800', + 0 ) + + hre.props.kyberPool = await hre.ethers.getContractAt('LimitPool', kyberPoolAddress) + hre.props.kyberPoolToken = await hre.ethers.getContractAt('PositionERC1155', kyberPoolTokenAddress) + } else if (this.deployPools) { // create first limit pool let createPoolTxn = await hre.props.limitPoolFactory.createLimitPool({ poolTypeId: 0, tokenIn: hre.props.token0.address, tokenOut: hre.props.token1.address, - swapFee: '1000', - startPrice: '1738267302024796147492397123192298' + swapFee: '3000', + startPrice: '2565382193812633925403' }); await createPoolTxn.wait(); @@ -393,33 +422,32 @@ export class InitialSetup { [limitPoolAddress, limitPoolTokenAddress] = await hre.props.limitPoolFactory.getLimitPool( hre.props.token0.address, hre.props.token1.address, - '1000', + '3000', 0 ) + + // @dev - skip 0.3% and 1% fee tiers for now + // createPoolTxn = await hre.props.limitPoolFactory.createLimitPool({ + // poolTypeId: 0, + // tokenIn: hre.props.token0.address, + // tokenOut: hre.props.token1.address, + // swapFee: '3000', + // startPrice: '1738267302024796147492397123192298' + // }); + // await createPoolTxn.wait(); - hre.nonce += 1; - - createPoolTxn = await hre.props.limitPoolFactory.createLimitPool({ - poolTypeId: 0, - tokenIn: hre.props.token0.address, - tokenOut: hre.props.token1.address, - swapFee: '3000', - startPrice: '1738267302024796147492397123192298' - }); - await createPoolTxn.wait(); - - hre.nonce += 1; + // hre.nonce += 1; - createPoolTxn = await hre.props.limitPoolFactory.createLimitPool({ - poolTypeId: 0, - tokenIn: hre.props.token0.address, - tokenOut: hre.props.token1.address, - swapFee: '10000', - startPrice: '1738267302024796147492397123192298' - }); - await createPoolTxn.wait(); - - hre.nonce += 1; + // createPoolTxn = await hre.props.limitPoolFactory.createLimitPool({ + // poolTypeId: 0, + // tokenIn: hre.props.token0.address, + // tokenOut: hre.props.token1.address, + // swapFee: '10000', + // startPrice: '1738267302024796147492397123192298' + // }); + // await createPoolTxn.wait(); + + // hre.nonce += 1; } hre.props.limitPool = await hre.ethers.getContractAt('LimitPool', limitPoolAddress) @@ -436,7 +464,7 @@ export class InitialSetup { [ hre.props.token0.address, hre.props.token1.address, - hre.network.name == 'hardhat' ? '500' : '1000', + hre.network.name == 'hardhat' ? '500' : '3000', 0 ] ) diff --git a/test/utils/token.ts b/test/utils/token.ts index 87fa05dc..462eeeda 100644 --- a/test/utils/token.ts +++ b/test/utils/token.ts @@ -13,6 +13,18 @@ export async function mintSigners20( } } +export async function mintSigners20WithRecipient( + token: Contract, + amount: BigNumberish, + signers: SignerWithAddress[], + recipient: string +): Promise { + for (let signer of signers) { + const txn = await token.connect(hre.props.alice).mint(recipient, amount) + await txn.wait() + } +} + export async function mintAddresses20( token: Contract, amount: BigNumberish,