Skip to content

Commit dc0cdb4

Browse files
authored
update potatoswap v3 calculation (#16903)
1 parent b8348bb commit dc0cdb4

File tree

4 files changed

+114
-26
lines changed

4 files changed

+114
-26
lines changed

projects/potatoswap-v3/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const { v3Tvl } = require('./v3.js');
2+
3+
module.exports = {
4+
xlayer: {
5+
tvl: v3Tvl,
6+
},
7+
misrepresentedTokens: true,
8+
};

projects/potatoswap-v3/v3.js

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
const { sumTokens2 } = require('../helper/unwrapLPs');
2+
3+
// --- V3 (Dynamic NFPM Traversal Method) ---
4+
5+
// Contract Addresses
6+
const v3Factory = "0xa1415fAe79c4B196d087F02b8aD5a622B8A827E5"
7+
const v3NFPM = "0xE6b5d25cc857c956bA20B73f4e21a1F1397947d8" // NonfungiblePositionManager
8+
9+
// ABIs required for traversal
10+
const abi = {
11+
NFPM: {
12+
totalSupply: "uint256:totalSupply",
13+
tokenByIndex: "function tokenByIndex(uint256 index) view returns (uint256)",
14+
// positions (tokenId) returns: (nonce, operator, token0, token1, fee, ...)
15+
positions: "function positions(uint256 tokenId) view returns (uint96, address, address, address, uint24, int24, int24, uint128, uint256, uint256, uint128, uint128)"
16+
},
17+
Factory: {
18+
getPool: "function getPool(address tokenA, address tokenB, uint24 fee) view returns (address pool)"
19+
}
20+
}
21+
22+
/**
23+
* Dynamic V3 TVL function.
24+
*/
25+
async function getV3Tvl(api) {
26+
// 1. Get the total number of LP NFTs
27+
const nftTotalSupply = await api.call({
28+
target: v3NFPM,
29+
abi: abi.NFPM.totalSupply,
30+
});
31+
32+
// 2. Build multicall to get all NFT token IDs
33+
const nftTokenIds = await api.multiCall({
34+
target: v3NFPM,
35+
abi: abi.NFPM.tokenByIndex,
36+
calls: Array.from({ length: Number(nftTotalSupply) }, (_, i) => i),
37+
});
38+
39+
// 3. Build multicall to get position info (token0, token1, fee) for all NFTs
40+
const positionData = await api.multiCall({
41+
target: v3NFPM,
42+
abi: abi.NFPM.positions,
43+
calls: nftTokenIds,
44+
});
45+
46+
// 4. De-duplicate to find all unique pool "recipes" (token0, token1, fee)
47+
const uniquePools = new Map();
48+
positionData.forEach(pos => {
49+
// pos[2] is token0, pos[3] is token1, pos[4] is fee
50+
const key = `${pos[2]}-${pos[3]}-${pos[4]}`;
51+
if (!uniquePools.has(key)) {
52+
uniquePools.set(key, {
53+
token0: pos[2],
54+
token1: pos[3],
55+
fee: pos[4]
56+
});
57+
}
58+
});
59+
60+
// 5. Build multicall to get the actual pool addresses from the factory
61+
const poolAddresses = await api.multiCall({
62+
target: v3Factory,
63+
abi: abi.Factory.getPool,
64+
calls: Array.from(uniquePools.values()).map(p => ({
65+
params: [p.token0, p.token1, p.fee]
66+
}))
67+
});
68+
69+
// 6. Format the data for sumTokens2: [ [token0, token1], poolAddress ]
70+
const ownerTokens = [];
71+
const pools = Array.from(uniquePools.values());
72+
poolAddresses.forEach((poolAddress, i) => {
73+
ownerTokens.push([
74+
[pools[i].token0, pools[i].token1], poolAddress,
75+
]);
76+
});
77+
78+
// 7. Calculate TVL and convert values to float
79+
const balances = await sumTokens2({ api, ownerTokens, balances: {} });
80+
const floatBalances = Object.fromEntries(
81+
Object.entries(balances).map(([key, value]) => [key, parseFloat(value)])
82+
);
83+
return floatBalances;
84+
}
85+
86+
module.exports = {
87+
v3Tvl: getV3Tvl,
88+
};

projects/potatoswap/index.js

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,8 @@
1-
const { getUniTVL } = require("../helper/unknownTokens");
2-
const { uniV3Export } = require('../helper/uniswapV3');
3-
4-
const v2Factory = "0x630db8e822805c82ca40a54dae02dd5ac31f7fcf"
5-
const v2Tvl = getUniTVL({
6-
factory: v2Factory,
7-
useDefaultCoreAssets: true,
8-
});
9-
10-
const v3Factory = "0xa1415fAe79c4B196d087F02b8aD5a622B8A827E5"
11-
const v3FromBlock = 38145900
12-
const v3Tvl = uniV3Export({
13-
xlayer: {
14-
factory: v3Factory,
15-
fromBlock: v3FromBlock,
16-
}
17-
}).xlayer.tvl;
1+
const { v2Tvl } = require('./v2.js');
182

193
module.exports = {
20-
xlayer: {
21-
tvl: async (api) => {
22-
api.addBalances(await v2Tvl(api))
23-
api.addBalances(await v3Tvl(api))
24-
return api.getBalances()
25-
},
26-
},
27-
misrepresentedTokens: true,
28-
}
4+
xlayer: {
5+
tvl: v2Tvl,
6+
},
7+
misrepresentedTokens: true,
8+
};

projects/potatoswap/v2.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const { getUniTVL } = require("../helper/unknownTokens");
2+
3+
// --- V2 ---
4+
const v2Factory = "0x630db8e822805c82ca40a54dae02dd5ac31f7fcf"
5+
const v2Tvl = getUniTVL({
6+
factory: v2Factory,
7+
useDefaultCoreAssets: true,
8+
});
9+
10+
module.exports = {
11+
v2Tvl,
12+
};

0 commit comments

Comments
 (0)