@zoralabs/coins 0.9.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +179 -114
- package/CHANGELOG.md +46 -0
- package/abis/BaseCoin.json +26 -118
- package/abis/BaseTest.json +47 -0
- package/abis/BuySupplyWithSwapRouterHook.json +40 -0
- package/abis/Coin.json +171 -63
- package/abis/CoinDopplerMultiCurve.json +38 -0
- package/abis/CoinRewardsV4.json +54 -0
- package/abis/CoinTest.json +53 -20
- package/abis/CoinUniV4Test.json +1091 -0
- package/abis/CoinV4.json +234 -211
- package/abis/DeployScript.json +47 -0
- package/abis/DeployedCoinVersionLookup.json +21 -0
- package/abis/DeployedCoinVersionLookupTest.json +716 -0
- package/abis/DifferentNamespaceVersionLookup.json +39 -0
- package/abis/DopplerUniswapV3Test.json +49 -93
- package/abis/ERC20.json +310 -0
- package/abis/FactoryTest.json +85 -7
- package/abis/FeeEstimatorHook.json +1515 -0
- package/abis/HooksDeployment.json +23 -0
- package/abis/HooksTest.json +60 -0
- package/abis/ICoin.json +40 -71
- package/abis/ICoinV3.json +879 -0
- package/abis/ICoinV4.json +915 -0
- package/abis/IDeployedCoinVersionLookup.json +21 -0
- package/abis/IERC721.json +36 -36
- package/abis/IHasPoolKey.json +42 -0
- package/abis/IHasRewardsRecipients.json +54 -0
- package/abis/IHasSwapPath.json +60 -0
- package/abis/IMsgSender.json +15 -0
- package/abis/IPoolConfigEncoding.json +46 -0
- package/abis/ISwapPathRouter.json +92 -0
- package/abis/IUniversalRouter.json +61 -0
- package/abis/IUnlockCallback.json +21 -0
- package/abis/IV4Quoter.json +310 -0
- package/abis/IZoraFactory.json +210 -11
- package/abis/IZoraV4CoinHook.json +348 -4
- package/abis/MockERC20.json +21 -0
- package/abis/MultiOwnableTest.json +47 -0
- package/abis/{CoinConfigurationVersions.json → Position.json} +1 -1
- package/abis/PrintUpgradeCommand.json +9 -0
- package/abis/ProxyShim.json +24 -0
- package/abis/StateLibrary.json +80 -0
- package/abis/TestDeployedCoinVersionLookupImplementation.json +39 -0
- package/abis/TestV4Swap.json +9 -0
- package/abis/UpgradeCoinImpl.json +47 -0
- package/abis/UpgradesTest.json +81 -0
- package/abis/Vm.json +1482 -111
- package/abis/VmSafe.json +856 -32
- package/abis/ZoraFactoryImpl.json +339 -1
- package/abis/ZoraV4CoinHook.json +442 -5
- package/addresses/8453.json +7 -4
- package/addresses/84532.json +8 -5
- package/addresses/dev/8453.json +10 -0
- package/dist/index.cjs +1932 -167
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +1928 -167
- package/dist/index.js.map +1 -1
- package/dist/wagmiGenerated.d.ts +2606 -160
- package/dist/wagmiGenerated.d.ts.map +1 -1
- package/foundry.toml +1 -0
- package/package/wagmiGenerated.ts +1941 -164
- package/package.json +8 -3
- package/remappings.txt +6 -1
- package/script/Deploy.s.sol +1 -1
- package/script/DeployDevFactory.s.sol +21 -0
- package/script/DeployHooks.s.sol +1 -1
- package/script/PrintUpgradeCommand.s.sol +13 -0
- package/script/Simulate.s.sol +1 -10
- package/script/TestBackingCoinSwap.s.sol +147 -0
- package/script/TestV4Swap.s.sol +136 -0
- package/script/UpgradeCoinImpl.sol +2 -2
- package/script/UpgradeFactoryImpl.s.sol +2 -2
- package/src/BaseCoin.sol +190 -0
- package/src/Coin.sol +87 -202
- package/src/CoinV4.sol +121 -0
- package/src/ZoraFactoryImpl.sol +208 -36
- package/{script → src/deployment}/CoinsDeployerBase.sol +111 -17
- package/src/hooks/ZoraV4CoinHook.sol +212 -0
- package/src/hooks/{BaseCoinDeployHook.sol → deployment/BaseCoinDeployHook.sol} +3 -3
- package/src/hooks/deployment/BuySupplyWithSwapRouterHook.sol +140 -0
- package/src/interfaces/ICoin.sol +31 -39
- package/src/interfaces/ICoinV3.sol +71 -0
- package/src/interfaces/ICoinV4.sol +69 -0
- package/src/interfaces/IDeployedCoinVersionLookup.sol +11 -0
- package/src/interfaces/IMsgSender.sol +9 -0
- package/src/interfaces/IPoolConfigEncoding.sol +14 -0
- package/src/interfaces/ISwapPathRouter.sol +14 -0
- package/src/interfaces/IZoraFactory.sol +67 -28
- package/src/interfaces/IZoraV4CoinHook.sol +116 -0
- package/src/libs/CoinCommon.sol +15 -0
- package/src/libs/CoinConfigurationVersions.sol +116 -1
- package/src/libs/CoinConstants.sol +5 -0
- package/src/libs/CoinDopplerMultiCurve.sol +134 -0
- package/src/libs/CoinDopplerUniV3.sol +19 -171
- package/src/libs/CoinRewards.sol +195 -0
- package/src/libs/CoinRewardsV4.sol +179 -0
- package/src/libs/CoinSetup.sol +57 -0
- package/src/libs/CoinSetupV3.sol +6 -67
- package/src/libs/DopplerMath.sol +156 -0
- package/src/libs/HooksDeployment.sol +128 -0
- package/src/libs/MarketConstants.sol +4 -0
- package/src/libs/PoolStateReader.sol +22 -0
- package/src/libs/UniV3BuySell.sol +74 -292
- package/src/libs/UniV4SwapHelper.sol +65 -0
- package/src/libs/UniV4SwapToCurrency.sol +109 -0
- package/src/libs/V4Liquidity.sol +122 -0
- package/src/types/PoolConfiguration.sol +15 -0
- package/src/utils/DeployedCoinVersionLookup.sol +52 -0
- package/src/version/ContractVersionBase.sol +1 -1
- package/test/Coin.t.sol +78 -88
- package/test/CoinDopplerUniV3.t.sol +32 -171
- package/test/CoinUniV4.t.sol +777 -0
- package/test/{Hooks.t.sol → DeploymentHooks.t.sol} +53 -16
- package/test/Factory.t.sol +80 -47
- package/test/MultiOwnable.t.sol +6 -3
- package/test/Upgrades.t.sol +97 -5
- package/test/mocks/MockERC20.sol +12 -0
- package/test/utils/BaseTest.sol +162 -57
- package/test/utils/DeployedCoinVersionLookup.t.sol +127 -0
- package/test/utils/FeeEstimatorHook.sol +84 -0
- package/test/utils/ProxyShim.sol +17 -0
- package/wagmi.config.ts +4 -0
- package/.env +0 -1
- package/.turbo/turbo-update-contract-version.log +0 -22
- package/abis/CoinSetupV3.json +0 -7
- package/abis/HookDeployer.json +0 -68
- package/abis/IHookDeployer.json +0 -42
- package/src/hooks/BuySupplyWithSwapRouterHook.sol +0 -78
- package/src/libs/CoinLegacy.sol +0 -48
- package/src/libs/CoinLegacyMarket.sol +0 -182
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
pragma solidity ^0.8.23;
|
|
3
|
-
|
|
4
|
-
import {BaseCoinDeployHook} from "./BaseCoinDeployHook.sol";
|
|
5
|
-
import {ICoin} from "../interfaces/ICoin.sol";
|
|
6
|
-
import {IZoraFactory} from "../interfaces/IZoraFactory.sol";
|
|
7
|
-
import {ISwapRouter} from "../interfaces/ISwapRouter.sol";
|
|
8
|
-
import {IWETH} from "../interfaces/IWETH.sol";
|
|
9
|
-
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
|
10
|
-
|
|
11
|
-
/// @title BuySupplyWithSwapRouter
|
|
12
|
-
/// @notice A hook that buys supply for a coin that is priced in an erc20 token with ETH, using a Uniswap SwapRouter.
|
|
13
|
-
/// Supports both single-hop and multi-hop swaps using uniswap v3
|
|
14
|
-
contract BuySupplyWithSwapRouterHook is BaseCoinDeployHook {
|
|
15
|
-
ISwapRouter immutable swapRouter;
|
|
16
|
-
|
|
17
|
-
error Erc20NotReceived();
|
|
18
|
-
error InvalidSwapRouterCall();
|
|
19
|
-
error SwapReverted(bytes error);
|
|
20
|
-
error CoinBalanceNot0(uint256 balance);
|
|
21
|
-
|
|
22
|
-
constructor(IZoraFactory _factory, address _swapRouter) BaseCoinDeployHook(_factory) {
|
|
23
|
-
swapRouter = ISwapRouter(_swapRouter);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/// @notice Hook that buys supply for a coin that is priced in an erc20 token with ETH, using a Uniswap SwapRouter.
|
|
27
|
-
/// Returns abi encoded (uint256 amountCurrency, uint256 coinsPurchased) - amountCurrency is the amount of currency received from the swap and sent to the coin for the purchase,
|
|
28
|
-
/// and coinsPurchased is the amount of coins purchased using the amountCurrency that was received from the swap
|
|
29
|
-
function _afterCoinDeploy(address, ICoin coin, bytes calldata hookData) internal override returns (bytes memory) {
|
|
30
|
-
address currency = coin.currency();
|
|
31
|
-
|
|
32
|
-
(address buyRecipient, bytes memory swapRouterCall) = abi.decode(hookData, (address, bytes));
|
|
33
|
-
|
|
34
|
-
uint256 amountCurrency = _handleSwap(currency, swapRouterCall);
|
|
35
|
-
|
|
36
|
-
uint256 coinsPurchased = _handleBuy(buyRecipient, coin, amountCurrency);
|
|
37
|
-
|
|
38
|
-
return abi.encode(amountCurrency, coinsPurchased);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function _handleSwap(address currency_, bytes memory swapRouterCall) internal returns (uint256 amountCurrency) {
|
|
42
|
-
// call the swap router, with the msg.value
|
|
43
|
-
_validateSwapRouterCall(swapRouterCall);
|
|
44
|
-
|
|
45
|
-
(bool success, bytes memory result) = address(swapRouter).call{value: msg.value}(swapRouterCall);
|
|
46
|
-
|
|
47
|
-
require(success, SwapReverted(result));
|
|
48
|
-
|
|
49
|
-
amountCurrency = abi.decode(result, (uint256));
|
|
50
|
-
|
|
51
|
-
// validate that this contract received the correct amount of currency
|
|
52
|
-
require(IERC20(currency_).balanceOf(address(this)) == amountCurrency, Erc20NotReceived());
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
function _validateSwapRouterCall(bytes memory swapRouterCall) internal pure {
|
|
56
|
-
// validate that the swap router call is valid - only exactInput and exactInputSingle are supported
|
|
57
|
-
|
|
58
|
-
bytes4 selector = _getSelectorFromCall(swapRouterCall);
|
|
59
|
-
|
|
60
|
-
require(selector == ISwapRouter.exactInput.selector || selector == ISwapRouter.exactInputSingle.selector, InvalidSwapRouterCall());
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function _getSelectorFromCall(bytes memory _call) internal pure returns (bytes4 selector) {
|
|
64
|
-
assembly {
|
|
65
|
-
selector := mload(add(_call, 32))
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function _handleBuy(address buyRecipient, ICoin coin, uint256 amountCurrency) internal returns (uint256 coinsPurchased) {
|
|
70
|
-
IERC20(coin.currency()).approve(address(coin), amountCurrency);
|
|
71
|
-
|
|
72
|
-
(, coinsPurchased) = coin.buy(buyRecipient, amountCurrency, 0, 0, address(0));
|
|
73
|
-
|
|
74
|
-
// make sure that this contract has no balance of the coin remaining
|
|
75
|
-
uint256 coinBalance = IERC20(address(coin)).balanceOf(address(this));
|
|
76
|
-
require(coinBalance == 0, CoinBalanceNot0(coinBalance));
|
|
77
|
-
}
|
|
78
|
-
}
|
package/src/libs/CoinLegacy.sol
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
pragma solidity ^0.8.23;
|
|
3
|
-
|
|
4
|
-
import {PoolConfiguration, ICoin} from "../interfaces/ICoin.sol";
|
|
5
|
-
import {TickMath} from "../utils/uniswap/TickMath.sol";
|
|
6
|
-
import {MarketConstants} from "./MarketConstants.sol";
|
|
7
|
-
import {LiquidityAmounts} from "../utils/uniswap/LiquidityAmounts.sol";
|
|
8
|
-
import {LpPosition} from "../types/LpPosition.sol";
|
|
9
|
-
import {CoinConfigurationVersions} from "./CoinConfigurationVersions.sol";
|
|
10
|
-
|
|
11
|
-
library CoinLegacy {
|
|
12
|
-
function setupPool(
|
|
13
|
-
bool isCoinToken0,
|
|
14
|
-
bytes memory poolConfig_,
|
|
15
|
-
address weth
|
|
16
|
-
) internal pure returns (uint160 sqrtPriceX96, PoolConfiguration memory poolConfiguration) {
|
|
17
|
-
(, address currency, int24 tickLower_) = abi.decode(poolConfig_, (uint8, address, int24));
|
|
18
|
-
|
|
19
|
-
// If WETH is the pool's currency, validate the lower tick
|
|
20
|
-
if ((currency == weth || currency == address(0)) && tickLower_ > MarketConstants.LP_TICK_LOWER_WETH) {
|
|
21
|
-
revert ICoin.InvalidWethLowerTick();
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
int24 savedTickLower = isCoinToken0 ? tickLower_ : -MarketConstants.LP_TICK_UPPER;
|
|
25
|
-
int24 savedTickUpper = isCoinToken0 ? MarketConstants.LP_TICK_UPPER : -tickLower_;
|
|
26
|
-
|
|
27
|
-
sqrtPriceX96 = TickMath.getSqrtPriceAtTick(isCoinToken0 ? savedTickLower : savedTickUpper);
|
|
28
|
-
|
|
29
|
-
poolConfiguration = PoolConfiguration({
|
|
30
|
-
version: CoinConfigurationVersions.LEGACY_POOL_VERSION,
|
|
31
|
-
tickLower: savedTickLower,
|
|
32
|
-
tickUpper: savedTickUpper,
|
|
33
|
-
numPositions: 1,
|
|
34
|
-
maxDiscoverySupplyShare: 0
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function calculatePositions(bool isCoinToken0, int24 tickLower, int24 tickUpper) internal pure returns (LpPosition[] memory positions) {
|
|
39
|
-
positions = new LpPosition[](1);
|
|
40
|
-
|
|
41
|
-
uint160 sqrtPriceX96 = TickMath.getSqrtPriceAtTick(isCoinToken0 ? tickLower : tickUpper);
|
|
42
|
-
uint160 farSqrtPriceX96 = TickMath.getSqrtPriceAtTick(isCoinToken0 ? tickUpper : tickLower);
|
|
43
|
-
uint128 liquidity = isCoinToken0
|
|
44
|
-
? LiquidityAmounts.getLiquidityForAmount0(sqrtPriceX96, farSqrtPriceX96, MarketConstants.POOL_LAUNCH_SUPPLY)
|
|
45
|
-
: LiquidityAmounts.getLiquidityForAmount1(sqrtPriceX96, farSqrtPriceX96, MarketConstants.POOL_LAUNCH_SUPPLY);
|
|
46
|
-
positions[0] = LpPosition({tickLower: tickLower, tickUpper: tickUpper, liquidity: liquidity});
|
|
47
|
-
}
|
|
48
|
-
}
|
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
// SPDX-License-Identifier: MIT
|
|
2
|
-
pragma solidity ^0.8.23;
|
|
3
|
-
|
|
4
|
-
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
|
5
|
-
import {IUniswapV3Factory} from "../interfaces/IUniswapV3Factory.sol";
|
|
6
|
-
import {IUniswapV3Pool} from "../interfaces/IUniswapV3Pool.sol";
|
|
7
|
-
import {ISwapRouter} from "../interfaces/ISwapRouter.sol";
|
|
8
|
-
import {PoolConfiguration, ICoin} from "../interfaces/ICoin.sol";
|
|
9
|
-
import {TickMath} from "../utils/uniswap/TickMath.sol";
|
|
10
|
-
import {MarketConstants} from "./MarketConstants.sol";
|
|
11
|
-
import {LiquidityAmounts} from "../utils/uniswap/LiquidityAmounts.sol";
|
|
12
|
-
import {LpPosition} from "../types/LpPosition.sol";
|
|
13
|
-
import {CoinConfigurationVersions} from "./CoinConfigurationVersions.sol";
|
|
14
|
-
import {UniV3Errors} from "./UniV3Errors.sol";
|
|
15
|
-
|
|
16
|
-
library CoinLegacyMarket {
|
|
17
|
-
struct State {
|
|
18
|
-
address poolAddress;
|
|
19
|
-
uint160 sqrtPriceX96;
|
|
20
|
-
PoolConfiguration poolConfiguration;
|
|
21
|
-
address pairedCurrency;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
struct MarketConfig {
|
|
25
|
-
address uniswapv3Factory;
|
|
26
|
-
address weth;
|
|
27
|
-
address pairedCurrency;
|
|
28
|
-
int24 tickLower;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
function _validateMarketConfig(MarketConfig memory marketConfig) internal pure {
|
|
32
|
-
if (marketConfig.uniswapv3Factory == address(0)) {
|
|
33
|
-
revert UniV3Errors.InvalidUniswapV3Factory();
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
if (marketConfig.weth == address(0)) {
|
|
37
|
-
revert UniV3Errors.InvalidWeth();
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
address weth = marketConfig.weth;
|
|
41
|
-
address currency = marketConfig.pairedCurrency;
|
|
42
|
-
int24 tickLower = marketConfig.tickLower;
|
|
43
|
-
|
|
44
|
-
// If WETH is the pool's currency, validate the lower tick
|
|
45
|
-
if ((currency == weth || currency == address(0)) && tickLower > MarketConstants.LP_TICK_LOWER_WETH) {
|
|
46
|
-
revert ICoin.InvalidWethLowerTick();
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function _isCoinToken0(address coin, address currency) internal pure returns (bool isCoinToken0, address token0, address token1) {
|
|
51
|
-
token0 = coin < currency ? coin : currency;
|
|
52
|
-
token1 = token1 = coin < currency ? currency : coin;
|
|
53
|
-
isCoinToken0 = token0 == coin;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function setupMarket(bytes memory _marketConfig, address coin) internal returns (bytes memory) {
|
|
57
|
-
MarketConfig memory marketConfig = abi.decode(_marketConfig, (MarketConfig));
|
|
58
|
-
_validateMarketConfig(marketConfig);
|
|
59
|
-
|
|
60
|
-
(bool isCoinToken0, address token0, address token1) = _isCoinToken0(coin, marketConfig.pairedCurrency);
|
|
61
|
-
|
|
62
|
-
(uint160 sqrtPriceX96, PoolConfiguration memory poolConfiguration) = setupPool(
|
|
63
|
-
isCoinToken0,
|
|
64
|
-
marketConfig.pairedCurrency,
|
|
65
|
-
marketConfig.tickLower,
|
|
66
|
-
marketConfig.weth
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
address poolAddress = _createPool(token0, token1, sqrtPriceX96, marketConfig.uniswapv3Factory);
|
|
70
|
-
|
|
71
|
-
LpPosition[] memory positions = calculatePositions(isCoinToken0, poolConfiguration);
|
|
72
|
-
|
|
73
|
-
_mintPositions(positions, poolAddress);
|
|
74
|
-
|
|
75
|
-
State memory state = State({
|
|
76
|
-
poolAddress: poolAddress,
|
|
77
|
-
sqrtPriceX96: sqrtPriceX96,
|
|
78
|
-
poolConfiguration: poolConfiguration,
|
|
79
|
-
pairedCurrency: marketConfig.pairedCurrency
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
return abi.encode(state);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function buy(bytes memory _state, address recipient, uint256 orderSize, uint256 minAmountOut, bytes memory tradeData) internal returns (uint256, uint256) {
|
|
86
|
-
State memory state = abi.decode(_state, (State));
|
|
87
|
-
(uint160 sqrtPriceLimitX96, address swapRouter, ) = abi.decode(tradeData, (uint160, address, address));
|
|
88
|
-
|
|
89
|
-
// Calculate the trade reward
|
|
90
|
-
// uint256 tradeReward = _calculateReward(orderSize, TOTAL_FEE_BPS);
|
|
91
|
-
|
|
92
|
-
ISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams({
|
|
93
|
-
tokenIn: state.pairedCurrency,
|
|
94
|
-
tokenOut: address(this),
|
|
95
|
-
fee: MarketConstants.LP_FEE,
|
|
96
|
-
recipient: recipient,
|
|
97
|
-
amountIn: orderSize,
|
|
98
|
-
amountOutMinimum: minAmountOut,
|
|
99
|
-
sqrtPriceLimitX96: sqrtPriceLimitX96
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
uint256 amountOut = ISwapRouter(swapRouter).exactInputSingle(params);
|
|
103
|
-
|
|
104
|
-
return (orderSize, amountOut);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function sell(bytes memory _state, uint256 orderSize, uint256 minAmountOut, bytes memory tradeData) internal returns (uint256, uint256) {
|
|
108
|
-
State memory state = abi.decode(_state, (State));
|
|
109
|
-
(uint160 sqrtPriceLimitX96, address swapRouter) = abi.decode(tradeData, (uint160, address));
|
|
110
|
-
|
|
111
|
-
// Approve the swap router to spend the coin
|
|
112
|
-
IERC20(address(this)).approve(swapRouter, orderSize);
|
|
113
|
-
|
|
114
|
-
// Set the swap parameters
|
|
115
|
-
ISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams({
|
|
116
|
-
tokenIn: address(this),
|
|
117
|
-
tokenOut: state.pairedCurrency,
|
|
118
|
-
fee: MarketConstants.LP_FEE,
|
|
119
|
-
recipient: address(this),
|
|
120
|
-
amountIn: orderSize,
|
|
121
|
-
amountOutMinimum: minAmountOut,
|
|
122
|
-
sqrtPriceLimitX96: sqrtPriceLimitX96
|
|
123
|
-
});
|
|
124
|
-
|
|
125
|
-
// Execute the swap
|
|
126
|
-
uint256 amountOut = ISwapRouter(swapRouter).exactInputSingle(params);
|
|
127
|
-
|
|
128
|
-
return (orderSize, amountOut);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/// @dev Creates the Uniswap V3 pool for the coin/currency pair
|
|
132
|
-
function _createPool(address token0, address token1, uint160 sqrtPriceX96, address v3Factory) internal returns (address pool) {
|
|
133
|
-
pool = IUniswapV3Factory(v3Factory).createPool(token0, token1, MarketConstants.LP_FEE);
|
|
134
|
-
|
|
135
|
-
// This pool should be new, if it has already been initialized
|
|
136
|
-
// then we will fail the creation step prompting the user to try again.
|
|
137
|
-
IUniswapV3Pool(pool).initialize(sqrtPriceX96);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function setupPool(
|
|
141
|
-
bool isCoinToken0,
|
|
142
|
-
address currency,
|
|
143
|
-
int24 tickLower_,
|
|
144
|
-
address weth
|
|
145
|
-
) internal pure returns (uint160 sqrtPriceX96, PoolConfiguration memory poolConfiguration) {
|
|
146
|
-
// If WETH is the pool's currency, validate the lower tick
|
|
147
|
-
if ((currency == weth || currency == address(0)) && tickLower_ > MarketConstants.LP_TICK_LOWER_WETH) {
|
|
148
|
-
revert ICoin.InvalidWethLowerTick();
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
int24 savedTickLower = isCoinToken0 ? tickLower_ : -MarketConstants.LP_TICK_UPPER;
|
|
152
|
-
int24 savedTickUpper = isCoinToken0 ? MarketConstants.LP_TICK_UPPER : -tickLower_;
|
|
153
|
-
|
|
154
|
-
sqrtPriceX96 = TickMath.getSqrtPriceAtTick(isCoinToken0 ? savedTickLower : savedTickUpper);
|
|
155
|
-
|
|
156
|
-
poolConfiguration = PoolConfiguration({
|
|
157
|
-
version: CoinConfigurationVersions.LEGACY_POOL_VERSION,
|
|
158
|
-
tickLower: savedTickLower,
|
|
159
|
-
tickUpper: savedTickUpper,
|
|
160
|
-
numPositions: 1,
|
|
161
|
-
maxDiscoverySupplyShare: 0
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
function calculatePositions(bool isCoinToken0, PoolConfiguration memory poolConfiguration) internal pure returns (LpPosition[] memory positions) {
|
|
166
|
-
positions = new LpPosition[](1);
|
|
167
|
-
|
|
168
|
-
uint160 sqrtPriceX96 = TickMath.getSqrtPriceAtTick(isCoinToken0 ? poolConfiguration.tickLower : poolConfiguration.tickUpper);
|
|
169
|
-
uint160 farSqrtPriceX96 = TickMath.getSqrtPriceAtTick(isCoinToken0 ? poolConfiguration.tickUpper : poolConfiguration.tickLower);
|
|
170
|
-
uint128 liquidity = isCoinToken0
|
|
171
|
-
? LiquidityAmounts.getLiquidityForAmount0(sqrtPriceX96, farSqrtPriceX96, MarketConstants.POOL_LAUNCH_SUPPLY)
|
|
172
|
-
: LiquidityAmounts.getLiquidityForAmount1(sqrtPriceX96, farSqrtPriceX96, MarketConstants.POOL_LAUNCH_SUPPLY);
|
|
173
|
-
positions[0] = LpPosition({tickLower: poolConfiguration.tickLower, tickUpper: poolConfiguration.tickUpper, liquidity: liquidity});
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/// @dev Mints the calculated liquidity positions into the Uniswap V3 pool
|
|
177
|
-
function _mintPositions(LpPosition[] memory lbpPositions, address poolAddress) internal {
|
|
178
|
-
for (uint256 i; i < lbpPositions.length; i++) {
|
|
179
|
-
IUniswapV3Pool(poolAddress).mint(address(this), lbpPositions[i].tickLower, lbpPositions[i].tickUpper, lbpPositions[i].liquidity, "");
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|