交易所开发成品丨交易所系统开发(演示版)丨交易所APP源码设计

2023-02-21 15:26:07 浏览数 (3)

What is the exchange?

An exchange is an information platform for trading certain information and goods. A fixed place is called an exchange. The exchange, with the help of information platform, realizes the sharing of property rights information, long-distance trading, unified coordination, and balance of property rights trading market and various terms.

The blockchain exchange is a matchmaking trading platform, which is compatible with the matching engine of traditional matchmaking rules, and the way of fund custody and delivery is replaced by the blockchain. Such a blockchain exchange can solve the unsolved problems of the blockchain, namely the issue of supply and demand information release and liquidity.

创建交易对

创建交易对的调用流程如下:

用户首先调用 NonfungiblePositionManager 合约的 createAndInitializePoolIfNecessary 方法创建交易对, 传入的参数为交易对的 token0, token1, fee 和初始价格 P−−√P.

NonfungiblePositionManager 合约内部通过调用 UniswapV3Factory 的 createPool 方法完成交易对的创建,然后对交易对进行初始化,初始化的作用就是给交易对设置一个初始的价格。

createAndInitializePoolIfNecessary 如下:

代码语言:javascript复制
function createAndInitializePoolIfNecessary(
    address tokenA,
    address tokenB,
    uint24 fee,
    uint160 sqrtPriceX96
) external payable returns (address pool) {
    pool = IUniswapV3Factory(factory).getPool(tokenA, tokenB, fee);
    if (pool == address(0)) {
        pool = IUniswapV3Factory(factory).createPool(tokenA, tokenB, fee);
        IUniswapV3Pool(pool).initialize(sqrtPriceX96);
    } else {
        (uint160 sqrtPriceX96Existing, , , , , , ) = IUniswapV3Pool(pool).slot0();
        if (sqrtPriceX96Existing == 0) {
            IUniswapV3Pool(pool).initialize(sqrtPriceX96);
        }
    }
}

1 人点赞