From dc0cdb4c4929bab1ee4f9bd7a54d645cde0a7bfb Mon Sep 17 00:00:00 2001 From: planD <43724111+wyf-ACCEPT@users.noreply.github.com> Date: Tue, 4 Nov 2025 13:25:49 +0000 Subject: [PATCH] update potatoswap v3 calculation (#16903) --- projects/potatoswap-v3/index.js | 8 +++ projects/potatoswap-v3/v3.js | 88 +++++++++++++++++++++++++++++++++ projects/potatoswap/index.js | 32 +++--------- projects/potatoswap/v2.js | 12 +++++ 4 files changed, 114 insertions(+), 26 deletions(-) create mode 100644 projects/potatoswap-v3/index.js create mode 100644 projects/potatoswap-v3/v3.js create mode 100644 projects/potatoswap/v2.js diff --git a/projects/potatoswap-v3/index.js b/projects/potatoswap-v3/index.js new file mode 100644 index 000000000..bec0d8139 --- /dev/null +++ b/projects/potatoswap-v3/index.js @@ -0,0 +1,8 @@ +const { v3Tvl } = require('./v3.js'); + +module.exports = { + xlayer: { + tvl: v3Tvl, + }, + misrepresentedTokens: true, +}; diff --git a/projects/potatoswap-v3/v3.js b/projects/potatoswap-v3/v3.js new file mode 100644 index 000000000..24ad92fc9 --- /dev/null +++ b/projects/potatoswap-v3/v3.js @@ -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, +}; diff --git a/projects/potatoswap/index.js b/projects/potatoswap/index.js index bbf32d804..b0c4f0030 100644 --- a/projects/potatoswap/index.js +++ b/projects/potatoswap/index.js @@ -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, -} \ No newline at end of file + xlayer: { + tvl: v2Tvl, + }, + misrepresentedTokens: true, +}; diff --git a/projects/potatoswap/v2.js b/projects/potatoswap/v2.js new file mode 100644 index 000000000..7cded5bf1 --- /dev/null +++ b/projects/potatoswap/v2.js @@ -0,0 +1,12 @@ +const { getUniTVL } = require("../helper/unknownTokens"); + +// --- V2 --- +const v2Factory = "0x630db8e822805c82ca40a54dae02dd5ac31f7fcf" +const v2Tvl = getUniTVL({ + factory: v2Factory, + useDefaultCoreAssets: true, +}); + +module.exports = { + v2Tvl, +}; \ No newline at end of file