Sumber daya yang dimuat... Pemuatan...

FMZ Kuantitatif Uniswap V3 Panduan Operasi yang berkaitan dengan Likuiditas Kolam Pertukaran (1)

Penulis:Penemu Kuantitas - Mimpi Kecil, Dibuat: 2023-07-14 17:36:55, Diperbarui: 2024-11-11 22:38:18

Tidak dapat dibayar, type:function}]

// Alamat kontrak Uniswap const UniswapV3RouterAddress = 0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45 const KontrakV3Factory = 0x1F98431c8aD98523631AE4a59f267346ea31F984

/ Dosis yang biasa const ZERO = BigInt ((0) const TWO = BigInt ((2) const Q96 = (TWO ** BigInt ((96)) const Q128 = (TWO ** BigInt ((128)) const Q192 = (TWO ** BigInt ((96)) ** TWO const Q256 = (TWO ** BigInt ((256))

// Mengkonversi ke jumlah yang dapat dibaca fungsi toAmount ((s, decimals) { return Number (((BigDecimal ((BigInt)) s)) / BigDecimal ((Math.pow ((10, decimals))).toString (()) Aku tidak tahu.

// Mengkonversi jumlah yang dapat dibaca ke dalam jumlah yang digunakan untuk perhitungan fungsi toInnerAmount ((n, decimals) { return (BigDecimal ((n) * BigDecimal ((Math.pow ((10, decimals))).toFixed ((0) Aku tidak tahu.

fungsi subIn256 ((x, y) { perbedaan var = x - y jika (perbedaan < ZERO) { kembali Q256 + perbedaan {\cH00FFFF} lainnya perbedaan pengembalian {\cH00FFFF} {\cH00FFFF}

// Mencatat informasi token yang ditanyakan melalui coingecko.com var tokens = [] fungsi init (() { var res = JSON.parse ((HttpQuery))https://tokens.coingecko.com/uniswap/all.json)) Log ((get, res.tokens.length, tokens dari,res.name) _.each ((res.tokens, fungsi ((token) { token.push ((({ Nama:token.name... simbol: token.symbol, desimal: token.desimal, alamat: token.address {\cH00FFFF}) {\cH00FFFF}) Log (( token:, token) {\cH00FFFF}

fungsi main (() { // Alamat dompet yang ingin ditelusuri // const walletAddress =exchange.IO("Alamat") const walletAddress = 0x28df8b987BE232bA33FdFB8Fc5058C1592A3db26

// 获取Uniswap V3的positionManager合约的地址
exchange.IO("abi", UniswapV3RouterAddress, ABI_UniswapV3Router)
const NonfungiblePositionManagerAddress = exchange.IO("api", UniswapV3RouterAddress, "positionManager")
Log("NonfungiblePositionManagerAddress:", NonfungiblePositionManagerAddress)

// 注册positionManager合约的ABI
exchange.IO("abi", NonfungiblePositionManagerAddress, ABI_NonfungiblePositionManager)

// 查询当前账户拥有的Uniswap V3 positions NFT数量
var nftBalance = exchange.IO("api", NonfungiblePositionManagerAddress, "balanceOf", walletAddress)
Log("nftBalance:", nftBalance)

// 查询这些NFT的TokenId
var nftTokenIds = []
for (var i = 0 ; i < nftBalance; i++) {
    var nftTokenId = exchange.IO("api", NonfungiblePositionManagerAddress, "tokenOfOwnerByIndex", walletAddress, i)
    nftTokenIds.push(nftTokenId)
    Log("nftTokenId:", nftTokenId)
}

// 根据positions NFT的tokenId查询流动性头寸详细信息
var positions = []
for (var i = 0; i < nftTokenIds.length; i++) {
    var pos = exchange.IO("api", NonfungiblePositionManagerAddress, "positions", nftTokenIds[i])        
    Log("pos:", pos)

    // 解析头寸数据
    positions.push(getFees(pos))
}

var tbl = {
    type : "table",
    title : "LP-Fees",
    cols : ["token0", "token1", "fee", "token0Fee", "token1Fee"],
    rows : positions
}
LogStatus("`" + JSON.stringify(tbl) + "`")

}

fungsi getFees ((posData) { var token0Symbol = nol var token1Symbol = nol untuk (variasi dalam token) { if (tokens[i].address.toLowerCase() == posData.token0.toLowerCase()) { token0Symbol = token[i] } lain jika (tokens[i].address.toLowerCase() == posData.token1.toLowerCase()) { token1Simbol = token[i] {\cH00FFFF} {\cH00FFFF}

if (!token0Symbol || !token1Symbol) {
	Log("token0Symbol:", token0Symbol, ", token1Symbol:", token1Symbol)
    throw "token not found"
}

// get Pool
var token0Address = token0Symbol.address
var token1Address = token1Symbol.address
if (BigInt(token0Address) > BigInt(token1Address)) {
    var tmp = token0Address
    token0Address = token1Address
    token1Address = tmp
}

// 注册工厂合约ABI
exchange.IO("abi", ContractV3Factory, ABI_Factory)
// 调用工厂合约的getPool方法获取池地址
var poolAddress = exchange.IO("api", ContractV3Factory, "getPool", token0Address, token1Address, posData.fee)
if (!poolAddress) {
    throw "getPool failed"
}
Log("poolAddress:", poolAddress)

// 注册池合约的ABI
exchange.IO("abi", poolAddress, ABI_Pool)

// 获取兑换池中的slot0数据
var slot0 = exchange.IO("api", poolAddress, "slot0")
if (!slot0) {
	throw "get slot0 failed"
}    

// feeGrowthGlobal0X128 , feeGrowthGlobal1X128
var feeGrowthGlobal0 = exchange.IO("api", poolAddress, "feeGrowthGlobal0X128")
var feeGrowthGlobal1 = exchange.IO("api", poolAddress, "feeGrowthGlobal1X128")
if (!feeGrowthGlobal0 || !feeGrowthGlobal1) {
    throw "get feeGrowthGlobal failed"
}

// 把posData中的tickLower , tickUpper作为参数,调用池合约的ticks方法
var tickLow = exchange.IO("api", poolAddress, "ticks", posData.tickLower)
var tickHigh = exchange.IO("api", poolAddress, "ticks", posData.tickUpper)
if (!tickLow || !tickHigh) {
    throw "get tick failed"
}

// 拿到tickLow和tickHigh的feeGrowthOutside0X128、feeGrowthOutside1X128数据
var feeGrowth0Low = tickLow.feeGrowthOutside0X128
var feeGrowth0Hi = tickHigh.feeGrowthOutside0X128
var feeGrowth1Low = tickLow.feeGrowthOutside1X128
var feeGrowth1Hi = tickHigh.feeGrowthOutside1X128

// feeGrowthInside0 feeGrowthInside1
var feeGrowthInside0 = posData.feeGrowthInside0LastX128
var feeGrowthInside1 = posData.feeGrowthInside1LastX128

var liquidity = BigInt(posData.liquidity)
var tickLow = parseInt(posData.tickLower)
var tickHigh = parseInt(posData.tickUpper)
var tickCurrent = parseInt(slot0.tick)
var decimal0 = token0Symbol.decimals
var decimal1 = token1Symbol.decimals

// 转换为BigInt类型,用于计算
var feeGrowthGlobal_0 = BigInt(feeGrowthGlobal0)
var feeGrowthGlobal_1 = BigInt(feeGrowthGlobal1)

var tickLowerFeeGrowthOutside_0 = BigInt(feeGrowth0Low)
var tickLowerFeeGrowthOutside_1 = BigInt(feeGrowth1Low)

var tickUpperFeeGrowthOutside_0 = BigInt(feeGrowth0Hi)
var tickUpperFeeGrowthOutside_1 = BigInt(feeGrowth1Hi)

var tickLowerFeeGrowthBelow_0 = ZERO
var tickLowerFeeGrowthBelow_1 = ZERO
var tickUpperFeeGrowthAbove_0 = ZERO
var tickUpperFeeGrowthAbove_1 = ZERO

if (tickCurrent >= tickHigh) {
    tickUpperFeeGrowthAbove_0 = subIn256(feeGrowthGlobal_0, tickUpperFeeGrowthOutside_0)
    tickUpperFeeGrowthAbove_1 = subIn256(feeGrowthGlobal_1, tickUpperFeeGrowthOutside_1)
} else {
    tickUpperFeeGrowthAbove_0 = tickUpperFeeGrowthOutside_0
    tickUpperFeeGrowthAbove_1 = tickUpperFeeGrowthOutside_1
}

if (tickCurrent >= tickLow) {
    tickLowerFeeGrowthBelow_0 = tickLowerFeeGrowthOutside_0
    tickLowerFeeGrowthBelow_1 = tickLowerFeeGrowthOutside_1
} else {
    tickLowerFeeGrowthBelow_0 = subIn256(feeGrowthGlobal_0, tickLowerFeeGrowthOutside_0)
    tickLowerFeeGrowthBelow_1 = subIn256(feeGrowthGlobal_1, tickLowerFeeGrowthOutside_1)
}

var fr_t1_0 = subIn256(subIn256(feeGrowthGlobal_0, tickLowerFeeGrowthBelow_0), tickUpperFeeGrowthAbove_0)
var fr_t1_1 = subIn256(subIn256(feeGrowthGlobal_1, tickLowerFeeGrowthBelow_1), tickUpperFeeGrowthAbove_1)

var feeGrowthInsideLast_0 = BigInt(feeGrowthInside0)
var feeGrowthInsideLast_1 = BigInt(feeGrowthInside1)

var uncollectedFees_0 = (liquidity * subIn256(fr_t1_0, feeGrowthInsideLast_0)) / Q128
var uncollectedFees_1 = (liquidity * subIn256(fr_t1_1, feeGrowthInsideLast_1)) / Q128

// 计算得到token0和token1的Fee
var token0Fee = toAmount(uncollectedFees_0, decimal0)
var token1Fee = toAmount(uncollectedFees_1, decimal1)

return [token0Symbol.symbol, token1Symbol.symbol, posData.fee / 10000 + "%", token0Fee, token1Fee]

}


![img](/upload/asset/16b989dd92b246d34e74.png)


下一篇我们会进行在Uniswap V3 DEX上增加流动性、减少流动性的探究。

Berkaitan

Lebih banyak

Crypto JoeItu terlalu kuat!