加密日记【019】UniswapV3(3)
April 26th, 2023

学习过程中的解惑:

  • Liquidity=sqrt(x*y),当新增LP时,Liquidity是加法,计算此时的swap也是用当前全局的Liquidity。池子同样会跟踪 L(代码中的 liquidity 变量),即所有包含现价的价格区间提供的总流动性

  • uniswapV3SwapCallback通过接口调用,可以在不同的合约中编写不同的函数内容

    创建UniswapV3Pool的合约的时候用了同样的方式传入参数

    //src/UniswapV3Pool.sol 
    constructor() {
           (factory,token0,token1,tickSpacing) = IUniswapV3PoolDeployer(msg.sender).parameters;
        }
    //src/UniswapV3Factory.sol
     parameters = PoolParameters({
                factory: address(this),
                token0: tokenX,
                token1: tokenY,
                tickSpacing: tickSpacing
            });
    
            pool = address(
                new UniswapV3Pool{
                    salt: keccak256(abi.encodePacked(tokenX, tokenY, tickSpacing))
                }()
            );
    
    1. PoolParameters作为接口写入IUniswapV3PoolDeployer

    2. IUniswapV3PoolDeployer(msg.sender).parameters 对UniswapV3Factory的调用

  • 用assembly 节省内存,并且用revert返回的时候可以解析返回的内容

    assembly {
        let ptr := mload(0x40) //0x40空闲指针返回空闲的位置
        mstore(ptr, amountOut) //将amountOut存入空闲的位置
        mstore(add(ptr, 0x20), sqrtPriceX96After)//存入下一个32字节之后的位置
        mstore(add(ptr, 0x40), tickAfter)//存入下一个32字节之后的位置
        revert(ptr, 96)//返回 ptr 指向位置的 96 字节数据
    }
    
    try 
         ...
    {} catch (bytes memory reason){
         return abi.decode(reason,(uint256,uint160,int24));
    //这里将返回的数据进行解码
    }
    
    • CALL和CALLSTATIC的区别

      1. CALLSTATIC 是在 Solidity 0.5 版本中引入的新操作码,而 CALL 则一直存在于以太坊 EVM 中。

      2. CALLSTATIC 调用的合约不能修改状态(即不能改变任何存储状态变量),而 CALL 可以。这是因为 CALLSTATIC 在调用时会将当前的状态复制一份,然后在复制的状态上执行操作,而不是在原始状态上执行操作。这意味着 CALLSTATIC 调用不会影响当前合约的状态。

      3. CALLSTATIC 的 gas 成本较低,因为它不需要维护存储状态的复制。而 CALL 的 gas 成本较高,因为它需要在当前状态上执行操作,并且需要维护状态的复制。

      4. CALLSTATIC 只能调用当前 EVM 内的合约,而 CALL 可以调用当前 EVM 内或外的合约。

      5. CALLSTATIC 不会返回任何值,而 CALL 可以返回值。

      综上所述,如果您只需要调用另一个合约并检索结果,则可以考虑使用 CALLSTATIC。如果您需要调用另一个合约并修改状态,则必须使用 CALL。

Subscribe to 0x3c
Receive the latest updates directly to your inbox.
Nft graphic
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.
More from 0x3c

Skeleton

Skeleton

Skeleton