Skip to content

Commit 147c5da

Browse files
authored
Feature/update kyberswap tvl (#16725)
1 parent 2eadd0f commit 147c5da

File tree

2 files changed

+89
-38
lines changed

2 files changed

+89
-38
lines changed

projects/helper/coreAssets.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,8 @@
16251625
"USDC": "0x176211869cA2b568f2A7D4EE941E073a821EE1ff",
16261626
"USDT": "0xA219439258ca9da29E9Cc4cE5596924745e12B93",
16271627
"DAI": "0x4AF15ec2A0BD43Db75dd04E62FAA3B8EF36b00d5",
1628-
"rzETH": "0xbf5495efe5db9ce00f80364c8b423567e58d2110"
1628+
"rzETH": "0xbf5495efe5db9ce00f80364c8b423567e58d2110",
1629+
"BNB": "0xf5c6825015280cdfd0b56903f9f8b5a2233476f5"
16291630
},
16301631
"base": {
16311632
"WETH": "0x4200000000000000000000000000000000000006",

projects/kyber/index.js

Lines changed: 87 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,58 @@
11
const sdk = require("@defillama/sdk")
22
const { cachedGraphQuery } = require("../helper/cache");
33
const { sumTokens2 } = require('../helper/unwrapLPs')
4+
const ADDRESSES = require('../helper/coreAssets.json');
5+
6+
7+
const KYBER_STAKING_ADDRESS = "0xECf0bdB7B3F349AbfD68C3563678124c5e8aaea3";
8+
const KYBER_STAKING_ADDRESS_2 = "0xeadb96F1623176144EBa2B24e35325220972b3bD";
9+
const KNC = "0xdd974d5c2e2928dea5f71b9825b8b646686bd200";
10+
const KNC_V2 = "0xdeFA4e8a7bcBA345F687a2f1456F5Edd9CE97202";
11+
const UNISWAP_KNC = "0x49c4f9bc14884f6210F28342ceD592A633801a8b";
12+
const UNISWAP_KNC_V2 = "0xf49C43Ae0fAf37217bDcB00DF478cF793eDd6687";
13+
const UNISWAP_KNC_V3 = "0x32263442a49650D89B2AB3dCB46B8C8DeC612F4D";
14+
const KYBER_AGGREGATOR = "0x6E4141d33021b52C91c28608403db4A0FFB50Ec6";
15+
const KYBER_META_AGGREGATION_ROUTER_V2 = "0x6131b5fae19ea4f9d964eac0408e4408b66337b5";
16+
const KYBER_AGGREGATION_ROUTER_V2 = "0xdf1a1b60f2d438842916c0adc43748768353ec25";
417

518
const CONFIG = {
6-
ethereum: { graphId: "mainnet" },
7-
arbitrum: { graphId: "arbitrum-one", blacklistedTokens: ['0x0df5dfd95966753f01cb80e76dc20ea958238c46'] }, // rWETH
19+
ethereum: {
20+
graphId: "mainnet",
21+
owners: [ KYBER_META_AGGREGATION_ROUTER_V2 ],
22+
tokens: [ ADDRESSES.ethereum.WETH, ADDRESSES.null, ADDRESSES.ethereum.USDC, ADDRESSES.ethereum.USDT ],
23+
staking: sumTokens2.bind(null, {
24+
owners: [ KYBER_STAKING_ADDRESS, KYBER_STAKING_ADDRESS_2 ],
25+
tokens: [
26+
KNC,
27+
KNC_V2
28+
],
29+
chain: 'ethereum'
30+
})
31+
},
32+
arbitrum: { graphId: "arbitrum-one", blacklistedTokens: [ '0x0df5dfd95966753f01cb80e76dc20ea958238c46' ] }, // rWETH
833
polygon: { graphId: "matic" },
934
avax: { graphId: "avalanche" },
10-
bsc: { graphId: "bsc" },
35+
bsc: {
36+
graphId: "bsc",
37+
},
1138
fantom: { graphId: "fantom" },
1239
cronos: { graphId: "cronos" },
1340
optimism: { graphId: "optimism" },
14-
linea: { graphId: 'linea' },
15-
base: { graphId: 'base' },
16-
scroll: { graphId: 'scroll' }
41+
linea: {
42+
graphId: 'linea',
43+
owners: [ KYBER_META_AGGREGATION_ROUTER_V2 ],
44+
tokens: [ ADDRESSES.linea.WETH, ADDRESSES.null, ADDRESSES.linea.USDC, ADDRESSES.linea.USDT, ADDRESSES.linea.BNB ],
45+
},
46+
base: {
47+
// graphId: 'base', // not using standard subgraph due to issues
48+
owners: [ KYBER_META_AGGREGATION_ROUTER_V2 ],
49+
tokens: [ ADDRESSES.base.WETH, ADDRESSES.null, ADDRESSES.base.USDC, ADDRESSES.base.USDT ],
50+
},
51+
scroll: { graphId: 'scroll' },
1752
};
1853

1954
async function fetchPools(chain) {
20-
let url
21-
22-
switch (chain) {
23-
case "linea": url = 'https://graph-query.linea.build/subgraphs/name/kybernetwork/kyberswap-elastic-linea'; break;
24-
case "cronos": url = 'https://cronos-graph.kyberengineering.io/subgraphs/name/kybernetwork/kyberswap-elastic-cronos'; break;
25-
case "base": url = 'https://base-graph.kyberengineering.io/subgraphs/name/kybernetwork/kyberswap-elastic-base'; break;
26-
case "scroll": url = 'https://scroll-graph.kyberengineering.io/subgraphs/name/kybernetwork/kyberswap-elastic-scroll'; break;
27-
case "mainnet": url = sdk.graph.modifyEndpoint('4U9PxDR4asVvfXyoVy18fhuj6NHnQhLzZkjZ5Bmuc5xk'); break;
28-
case "arbitrum-one" : url = sdk.graph.modifyEndpoint('C36tj8jSpEHxcNbjM3z7ayUZHVjrk4HRqnpGMFuRgXs6'); break;
29-
case "avalanche": url = sdk.graph.modifyEndpoint('9oMJfc7CL8uDqqQ3T3NFBnFCz9JMwq2YhH9AqojECFWp'); break;
30-
case "bsc": url = sdk.graph.modifyEndpoint('FDEDgycFnTbPZ7PfrnWEZ4iR7T5De6BR69zx1i8gKQRa'); break;
31-
case "fantom": url = sdk.graph.modifyEndpoint('9aj6YZFVL647wFBQXnNKM72eiowP4fyzynQKwLrn5axL'); break;
32-
case "optimism": url = sdk.graph.modifyEndpoint('3Kpd8i7U94pTz3Mgdb8hyvT5o26fpwT7SUHAbTa6JzfZ'); break;
33-
default: url = `https://api.thegraph.com/subgraphs/name/kybernetwork/kyberswap-elastic-${chain}`;
34-
}
55+
const url = getGraphUrl(chain);
3556
let toa = [];
3657
const poolQuery = `
3758
query pools($lastId: String) {
@@ -45,34 +66,63 @@ async function fetchPools(chain) {
4566
}
4667
}
4768
}`;
48-
const pools = await cachedGraphQuery('kyber/'+chain, url, poolQuery, { fetchById: true, })
49-
pools.forEach(({ id, token0, token1}) => {
50-
toa.push([token0.id, id])
51-
toa.push([token1.id, id])
52-
})
53-
69+
const pools = await cachedGraphQuery('kyber/' + chain, url, poolQuery, { fetchById: true, })
70+
console.debug('pools', pools.length)
71+
pools.forEach?.(({ id, token0, token1 }) => {
72+
toa.push([ token0.id, id ])
73+
toa.push([ token1.id, id ])
74+
})
75+
5476
return toa;
5577
}
5678

57-
function elastic(graphId, blacklistedTokens = []) {
79+
function getGraphUrl(chain) {
80+
let url;
81+
82+
switch (chain) {
83+
case "linea": url = 'https://graph-query.linea.build/subgraphs/name/kybernetwork/kyberswap-elastic-linea'; break;
84+
case "cronos": url = 'https://cronos-graph.kyberengineering.io/subgraphs/name/kybernetwork/kyberswap-elastic-cronos'; break;
85+
case "base": url = 'https://base-graph.kyberengineering.io/subgraphs/name/kybernetwork/kyberswap-elastic-base'; break;
86+
case "scroll": url = 'https://scroll-graph.kyberengineering.io/subgraphs/name/kybernetwork/kyberswap-elastic-scroll'; break;
87+
case "mainnet": url = sdk.graph.modifyEndpoint('4U9PxDR4asVvfXyoVy18fhuj6NHnQhLzZkjZ5Bmuc5xk'); break;
88+
case "polygon": url = sdk.graph.modifyEndpoint('BcBYReG6ZKkrXrnZUqvTqjQGURg4vBMimEJm7TZFi4qV'); break;
89+
case "arbitrum-one": url = sdk.graph.modifyEndpoint('C36tj8jSpEHxcNbjM3z7ayUZHVjrk4HRqnpGMFuRgXs6'); break;
90+
case "avalanche": url = sdk.graph.modifyEndpoint('9oMJfc7CL8uDqqQ3T3NFBnFCz9JMwq2YhH9AqojECFWp'); break;
91+
case "bsc": url = sdk.graph.modifyEndpoint('FDEDgycFnTbPZ7PfrnWEZ4iR7T5De6BR69zx1i8gKQRa'); break;
92+
case "fantom": url = sdk.graph.modifyEndpoint('9aj6YZFVL647wFBQXnNKM72eiowP4fyzynQKwLrn5axL'); break;
93+
case "optimism": url = sdk.graph.modifyEndpoint('3Kpd8i7U94pTz3Mgdb8hyvT5o26fpwT7SUHAbTa6JzfZ'); break;
94+
default: url = `https://api.thegraph.com/subgraphs/name/kybernetwork/kyberswap-elastic-${chain}`;
95+
}
96+
return url;
97+
}
98+
99+
function tvl(config) {
100+
const { graphId, blacklistedTokens = [], owners, tokens } = config;
58101
return async (api) => {
59-
if (!graphId) return
60-
const pools = await fetchPools(graphId);
61-
return sumTokens2({ api, tokensAndOwners: pools, blacklistedTokens })
102+
const pools = graphId && await fetchPools(graphId) || []
103+
return sumTokens2({
104+
api,
105+
tokensAndOwners: pools,
106+
tokens,
107+
owners,
108+
blacklistedTokens
109+
})
62110
}
63111
}
64112

65113
module.exports = {
66114
timetravel: false,
115+
methodology: `TVL is calculated by querying KyberSwap's subgraphs to get all liquidity pools and summing the balances of all tokens held in these pools. It also includes tokens held in Kyber's Meta Aggregation Router contracts on specified chains.`,
67116
hallmarks: [
68-
['2023-04-17', 'Kyber team identified a vuln'],
117+
[ '2023-04-17', 'Kyber team identified a vuln' ],
69118
// [1700611200,'Protocol exploit'],
70119
],
71120
};
72121

73122
Object.keys(CONFIG).forEach((chain) => {
74-
const { graphId, blacklistedTokens } = CONFIG[chain]
75-
module.exports[chain] = { tvl: elastic(graphId, blacklistedTokens)}
76-
})
77-
78-
module.exports.base.tvl = () => ({}) // setting base to 0 for now as I could not find the graph endpoint
123+
const config = CONFIG[ chain ]
124+
module.exports[ chain ] = {
125+
tvl: tvl(config),
126+
staking: config?.staking || (() => ({})),
127+
}
128+
})

0 commit comments

Comments
 (0)