update potatoswap v3 calculation (#16903)

This commit is contained in:
planD
2025-11-04 13:25:49 +00:00
committed by GitHub
parent b8348bb599
commit dc0cdb4c49
4 changed files with 114 additions and 26 deletions

View File

@@ -0,0 +1,8 @@
const { v3Tvl } = require('./v3.js');
module.exports = {
xlayer: {
tvl: v3Tvl,
},
misrepresentedTokens: true,
};

View File

@@ -0,0 +1,88 @@
const { sumTokens2 } = require('../helper/unwrapLPs');
// --- V3 (Dynamic NFPM Traversal Method) ---
// Contract Addresses
const v3Factory = "0xa1415fAe79c4B196d087F02b8aD5a622B8A827E5"
const v3NFPM = "0xE6b5d25cc857c956bA20B73f4e21a1F1397947d8" // NonfungiblePositionManager
// ABIs required for traversal
const abi = {
NFPM: {
totalSupply: "uint256:totalSupply",
tokenByIndex: "function tokenByIndex(uint256 index) view returns (uint256)",
// positions (tokenId) returns: (nonce, operator, token0, token1, fee, ...)
positions: "function positions(uint256 tokenId) view returns (uint96, address, address, address, uint24, int24, int24, uint128, uint256, uint256, uint128, uint128)"
},
Factory: {
getPool: "function getPool(address tokenA, address tokenB, uint24 fee) view returns (address pool)"
}
}
/**
* Dynamic V3 TVL function.
*/
async function getV3Tvl(api) {
// 1. Get the total number of LP NFTs
const nftTotalSupply = await api.call({
target: v3NFPM,
abi: abi.NFPM.totalSupply,
});
// 2. Build multicall to get all NFT token IDs
const nftTokenIds = await api.multiCall({
target: v3NFPM,
abi: abi.NFPM.tokenByIndex,
calls: Array.from({ length: Number(nftTotalSupply) }, (_, i) => i),
});
// 3. Build multicall to get position info (token0, token1, fee) for all NFTs
const positionData = await api.multiCall({
target: v3NFPM,
abi: abi.NFPM.positions,
calls: nftTokenIds,
});
// 4. De-duplicate to find all unique pool "recipes" (token0, token1, fee)
const uniquePools = new Map();
positionData.forEach(pos => {
// pos[2] is token0, pos[3] is token1, pos[4] is fee
const key = `${pos[2]}-${pos[3]}-${pos[4]}`;
if (!uniquePools.has(key)) {
uniquePools.set(key, {
token0: pos[2],
token1: pos[3],
fee: pos[4]
});
}
});
// 5. Build multicall to get the actual pool addresses from the factory
const poolAddresses = await api.multiCall({
target: v3Factory,
abi: abi.Factory.getPool,
calls: Array.from(uniquePools.values()).map(p => ({
params: [p.token0, p.token1, p.fee]
}))
});
// 6. Format the data for sumTokens2: [ [token0, token1], poolAddress ]
const ownerTokens = [];
const pools = Array.from(uniquePools.values());
poolAddresses.forEach((poolAddress, i) => {
ownerTokens.push([
[pools[i].token0, pools[i].token1], poolAddress,
]);
});
// 7. Calculate TVL and convert values to float
const balances = await sumTokens2({ api, ownerTokens, balances: {} });
const floatBalances = Object.fromEntries(
Object.entries(balances).map(([key, value]) => [key, parseFloat(value)])
);
return floatBalances;
}
module.exports = {
v3Tvl: getV3Tvl,
};

View File

@@ -1,28 +1,8 @@
const { getUniTVL } = require("../helper/unknownTokens");
const { uniV3Export } = require('../helper/uniswapV3');
const v2Factory = "0x630db8e822805c82ca40a54dae02dd5ac31f7fcf"
const v2Tvl = getUniTVL({
factory: v2Factory,
useDefaultCoreAssets: true,
});
const v3Factory = "0xa1415fAe79c4B196d087F02b8aD5a622B8A827E5"
const v3FromBlock = 38145900
const v3Tvl = uniV3Export({
xlayer: {
factory: v3Factory,
fromBlock: v3FromBlock,
}
}).xlayer.tvl;
const { v2Tvl } = require('./v2.js');
module.exports = {
xlayer: {
tvl: async (api) => {
api.addBalances(await v2Tvl(api))
api.addBalances(await v3Tvl(api))
return api.getBalances()
},
},
misrepresentedTokens: true,
}
xlayer: {
tvl: v2Tvl,
},
misrepresentedTokens: true,
};

12
projects/potatoswap/v2.js Normal file
View File

@@ -0,0 +1,12 @@
const { getUniTVL } = require("../helper/unknownTokens");
// --- V2 ---
const v2Factory = "0x630db8e822805c82ca40a54dae02dd5ac31f7fcf"
const v2Tvl = getUniTVL({
factory: v2Factory,
useDefaultCoreAssets: true,
});
module.exports = {
v2Tvl,
};