# Slingshot <> Ambient integration ## Questions - How pools are created (so that we can detect any new pools created on-chain, and get relevant Pool metadata) - I see a PoolRegistry.sol, but am unclear what events are emitted when a pool is created. - The pool metadata. i.e. Liquidity Data (ticks, etc), Token Balances, Tick Spacings, fees.., I see the Pool struct in PoolSpecs.sol, is this all the data? - Pool Math (so we can calculate the output amount given an input amount for a pool, provided that we have it's metadata) - I'm assuming since this using univ3 concentrated liquidity, the math would be exactly the same, but would like to confirm how the math would be done based on the data from the Pool struct (in PoolSpecs.sol) - Executing a Swap (so we can execute a swap on-chain, given the pool metadata and the input amount) - Usually we just interface with a router if available. - Otherwise we would need to understand the pool swap interface, but looks like you've already pointed that out earlier. ### Background Context Ambient is a single contract dex. It uses sidecar contracts (also called paths) which the main dex contract (CrocSwapDex) will delegate calls to. For slingshot, the relevant sidecar contracts will likely be: - coldpath (used create new pool templates and initialize new pools) - warmpath (used to mint/burn liquidity) - hotpath (probably the most important; used for swaps. This contract is inherited by CrocSwapDex, not delegated, in order to save on the gas cost of delegatecall) Since swaps are the most frequently used feature, the CrocSwapDex provides a simple swap function directly in the contract to save on the gas of a delegatecall: [link to swap function](https://github.com/Canto-Network/CrocSwap-protocol/blob/117f1fc9a41bce044e9ba3e5897a27d564679400/contracts/CrocSwapDex.sol#L80) ## Pools - There is currently no event emitted on pool creation. If this is necessary, we can add an event. - On pool metadata: - [Pool struct](https://github.com/Canto-Network/CrocSwap-protocol/blob/117f1fc9a41bce044e9ba3e5897a27d564679400/contracts/libraries/PoolSpecs.sol#L55-L63) - for Canto pools, protocolTake = 0, tickSize = 1, jitThresh = 2, knockoutBits = 1, oracleFlags = 0 - tokens should always be sorted by address, where addr(baseToken) < addr(quoteToken) for (baseToken, quoteToken) - to query live pool data, use the [lens contracts](https://github.com/Canto-Network/CrocSwap-protocol/tree/main/contracts/lens) - CrocQuery: get basic info on pools, such as price, liquidity, positions of specific addresses etc - CrocImpact: contract to calculate price impact of swaps - **POOL MATH** - generally same as uniV3 - 1 tick = 1 basis point (0.0001, or 0.01%) - ambient uses square root price, in the same way that univ3 does - prices are represented in Q64.64 representation, unlike uniV3 which uses Q64.96 Example: ```python3= # example for NOTE/USDC pool # NOTE has 18 decimals, USDC has 6 # get the current sqrtPrice in q64.64 representation by using # the CrocQuery contract (queryPrice function) sqrtPriceQ64_64 = 18450241666184948264507780 price = (sqrtPriceQ64_64 ** 2) / (2 ** 128) print(price) 1000379245686.0354 # 1 unit of USDC = 1000379245686.0354 units of NOTE # so 1 USDC = 1.0037 NOTE ``` ## Swaps - Call function [swap](https://github.com/Canto-Network/CrocSwap-protocol/blob/117f1fc9a41bce044e9ba3e5897a27d564679400/contracts/CrocSwapDex.sol#L80) in CrocSwapDex - tip and reserveFlags params should be set to 0 for Canto pools - check sample swap script [here](https://github.com/Canto-Network/CrocSwap-protocol/blob/main/misc/scripts/deploy_canto/swapTest.js) - in this script, we are trading 20 USDC for ~20 cNOTE. - we set the qty to 20000000 (denoting exact USDC to input) - we set limitPrice to 16602069666338596454400000 - convert sqrtPriceQ64_64 to price using above equation: we get 810000000000 - means we are willing to sell USDC until it becomes worth 0.81 NOTE - we set minOut to 19000000000000000000, meaning we will only take the swap if we get at least 19 NOTE back - limitPrice is a feature that doesn't need to be supported. As long as minOut is set, we can calculate swap slippage. That means limitPrice could be set to very low or very high depending on which way we are swapping.