وسائل لوڈ ہو رہے ہیں... لوڈنگ...

FMZ Quant Uniswap V3 ایکسچینج پول لیکویڈیٹی سے متعلقہ آپریشنز گائیڈ (حصہ 1)

مصنف:FMZ~Lydia, تخلیق: 2023-07-21 09:22:33, تازہ کاری: 2024-11-11 22:37:08

type:function},{inputs:[{internalType:address:uint24,name:"",type:uint24}],name:feeAmountTickSpacing,outputs:[{internalType:int24,name:"",type:int24}],stateMutability:view,type:function},{inputs:[{internalType:address:uint24,name:"",type:address:address},{internaladdaddType:address,addaddaddaddname:"address",type:address:int24,outputs:"",type:"",",type:"Mutability:"view",type:"address:"address:"add

// Uniswap معاہدے کا پتہ const UniswapV3RouterAddress = 0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45 const ContractV3Factory = 0x1F98431c8aD98523631AE4a59f267346ea31F984

// مشترکہ مستقل 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))

// پڑھنے کے قابل رقم میں تبدیل کریں فنکشن toAmount ((s، اعشاریہ) { واپسی نمبر (((BigDecimal(BigInt(s)) / BigDecimal ((Math.pow(10, اعشاریہ))).toString()) }

// پڑھنے کے قابل مقدار سے پیرامیٹرز اور حساب کے لئے استعمال ہونے والی رقم میں الٹ تبادلوں فنکشن toInnerAmount ((n، اعشاریہ) { واپسی (بگ ڈیسیمل ((ن) * بگ ڈیسیمل ((ریاضی.پو ((10, اعشاریہ))).toFixed(0) }

فنکشن subIn256 ((x، y) { var فرق = x - y اگر (فرق < صفر) { واپسی Q256 + فرق } دیگر { واپسی کا فرق } }

// ٹوکن کے بارے میں ریکارڈ کی معلومات کے ذریعے پوچھاcoingecko.comvar ٹوکن = [] تقریب init() { var res = JSON.parse ((HttpQuery(https://tokens.coingecko.com/uniswap/all.json)) log ((fetch، res.tokens.length، tokens سے،res.name) _.each ((res.tokens، فنکشن ((ٹوکن) { ٹوکنز.پوش نام:token.name علامت: ٹوکن۔ علامت، اعشاریہ نمبر: ٹوکن. اعشاریہ نمبر، ایڈریس: ٹوکن.ایڈریس }) }) لاگ (( ٹوکن:, ٹوکن) }

اہم فنکشن // تلاش کرنے کے لئے بٹوے کا پتہ // const walletAddress =exchange.IO("عنوان") const walletAddress = 0x28df8b987BE232bA33FdFB8Fc5058C1592A3db26

// Get the address of Uniswap V3's positionManager contract
exchange.IO("abi", UniswapV3RouterAddress, ABI_UniswapV3Router)
const NonfungiblePositionManagerAddress = exchange.IO("api", UniswapV3RouterAddress, "positionManager")
Log("NonfungiblePositionManagerAddress:", NonfungiblePositionManagerAddress)

// Register ABI for positionManager contracts
exchange.IO("abi", NonfungiblePositionManagerAddress, ABI_NonfungiblePositionManager)

// Query the number of Uniswap V3 positions NFT owned by the current account
var nftBalance = exchange.IO("api", NonfungiblePositionManagerAddress, "balanceOf", walletAddress)
Log("nftBalance:", nftBalance)

// Query the TokenId of these NFTs
var nftTokenIds = []
for (var i = 0 ; i < nftBalance; i++) {
    var nftTokenId = exchange.IO("api", NonfungiblePositionManagerAddress, "tokenOfOwnerByIndex", walletAddress, i)
    nftTokenIds.push(nftTokenId)
    Log("nftTokenId:", nftTokenId)
}

// Query liquidity position details based on the tokenId of the positions NFT
var positions = []
for (var i = 0; i < nftTokenIds.length; i++) {
    var pos = exchange.IO("api", NonfungiblePositionManagerAddress, "positions", nftTokenIds[i])        
    Log("pos:", pos)

    // Parsing position data
    positions.push(getFees(pos))
}

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

}

فنکشن getFees ((posData) { var token0Symbol = صفر var token1Symbol = صفر کے لئے (ٹوکن میں تبدیلی) { if (tokens[i].address.toLowerCase() == posData.token0.toLowerCase()) { token0Symbol = ٹوکن[i] } دوسری صورت میں اگر (ٹوکن[i].ایڈریس.لوئر کیس() == پوسٹ ڈیٹا.ٹوکن1.لوئر کیس()) { token1Symbol = ٹوکن[i] } }

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
}

// Registered factory contract ABI
exchange.IO("abi", ContractV3Factory, ABI_Factory)
// Call the getPool method of the factory contract to get the pool address
var poolAddress = exchange.IO("api", ContractV3Factory, "getPool", token0Address, token1Address, posData.fee)
if (!poolAddress) {
    throw "getPool failed"
}
Log("poolAddress:", poolAddress)

// ABI for registered pool contracts
exchange.IO("abi", poolAddress, ABI_Pool)

// Get the slot0 data in the exchange pool
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"
}

// Call the ticks method of the pool contract by taking tickLower , tickUpper from posData as parameters
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"
}

// Obtain feeGrowthOutside0X128 and feeGrowthOutside1X128 data for tickeLow and tickeHigh
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

// Converted to a BigInt type for calculating the
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

// Calculate the Fee to get token0 and token1
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/28d88187ae84ce5d5379c.png)

![img](/upload/asset/28dd7917ad3df05b6c272.png)

In the next post, we will conduct an exploration of increasing mobility and decreasing mobility on Uniswap V3 DEX.

مزید