Skip to content

Commit 31465ad

Browse files
authored
feat(projects/silo): add support for Silo V2 on Sonic & Arbitrum (DefiLlama#13034)
1 parent 1230a59 commit 31465ad

File tree

2 files changed

+147
-29
lines changed

2 files changed

+147
-29
lines changed

projects/silo-v2/index.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
const sdk = require('@defillama/sdk')
2+
const { sumTokens2 } = require('../helper/unwrapLPs')
3+
const { getLogs } = require('../helper/cache/getLogs')
4+
5+
const XAI = '0xd7c9f0e536dc865ae858b0c0453fe76d13c3beac'
6+
const blacklistedSilos = ["0x6543ee07cf5dd7ad17aeecf22ba75860ef3bbaaa",];
7+
8+
const getAssetAbiV2 = "address:asset";
9+
const getAssetStateAbiV2 = 'function getTotalAssetsStorage(uint8 _assetType) external view returns (uint256 totalAssetsByType)';
10+
11+
const configV2 = {
12+
sonic: {
13+
factories: [
14+
{
15+
START_BLOCK: 2672166,
16+
SILO_FACTORY: '0xa42001d6d2237d2c74108fe360403c4b796b7170', // Silo V2 Sonic (Main)
17+
}
18+
]
19+
},
20+
arbitrum: {
21+
factories: [
22+
{
23+
START_BLOCK: 291201890,
24+
SILO_FACTORY: '0xf7dc975C96B434D436b9bF45E7a45c95F0521442', // Silo V2 Arbitrum (Main)
25+
}
26+
]
27+
}
28+
}
29+
30+
async function tvl(api) {
31+
// Handle V2 silos
32+
let toaV2 = [];
33+
if(configV2[api.chain]) {
34+
const siloArrayV2 = await getSilosV2(api);
35+
const assetsV2 = await api.multiCall({
36+
abi: getAssetAbiV2,
37+
calls: siloArrayV2.map(i => ({ target: i })),
38+
});
39+
toaV2 = assetsV2.map((asset, i) => [[asset], siloArrayV2[i]]);
40+
}
41+
42+
return sumTokens2({ api, ownerTokens: toaV2, blacklistedTokens: [XAI], });
43+
}
44+
45+
async function borrowed(api) {
46+
if(configV2[api.chain]) {
47+
// Handle V2 silos
48+
const siloArrayV2 = await getSilosV2(api);
49+
50+
// Get asset address for each silo
51+
const siloAssets = await api.multiCall({
52+
abi: getAssetAbiV2,
53+
calls: siloArrayV2.map(i => ({ target: i })),
54+
});
55+
56+
// Get total borrow amount for each silo (AssetType.DEBT = 2)
57+
const borrowAmounts = await api.multiCall({
58+
abi: getAssetStateAbiV2,
59+
calls: siloArrayV2.map(i => ({ target: i, params: [2] })),
60+
});
61+
62+
// Add borrow amounts for V2 silos
63+
siloAssets.forEach((asset, index) => {
64+
if (asset.toLowerCase() === XAI) return;
65+
return api.add(asset, borrowAmounts[index])
66+
});
67+
}
68+
}
69+
70+
async function getSilosV2(api) {
71+
const chain = api.chain;
72+
let logs = [];
73+
let siloAddresses = [];
74+
if(configV2[chain]) {
75+
for(let factory of configV2[chain].factories) {
76+
const { SILO_FACTORY, START_BLOCK } = factory;
77+
let logChunk = await getLogs({
78+
api,
79+
target: SILO_FACTORY,
80+
fromBlock: START_BLOCK,
81+
eventAbi: 'event NewSilo(address indexed implementation, address indexed token0, address indexed token1, address silo0, address silo1, address siloConfig)',
82+
});
83+
logs = [...logs, ...logChunk];
84+
}
85+
86+
siloAddresses = logs.flatMap((log) => {
87+
88+
let silo0 = log.args[3];
89+
let silo1 = log.args[4];
90+
91+
return [silo0, silo1].filter(
92+
(address) => blacklistedSilos.indexOf(address.toLowerCase()) === -1
93+
);
94+
});
95+
96+
}
97+
98+
return siloAddresses;
99+
}
100+
101+
102+
module.exports = {
103+
methodology: `We calculate TVL by interacting with Silo Factory smart contracts on Ethereum, Arbitrum, Base & Optimism. For Ethereum, it queries Silo(Main-V2)(0xa42001d6d2237d2c74108fe360403c4b796b7170). On Arbitrum, we query the Silo Arbitrum factory (Main-V2)(0xf7dc975C96B434D436b9bF45E7a45c95F0521442), we query the factories to obtain the addresses of Silos, retrieve the assets of each Silo, and then calculate the sum of the deposited tokens, borrowed amounts are calculated separately from TVL.`,
104+
// ethereum: { tvl, borrowed, },
105+
arbitrum: { tvl, borrowed, },
106+
// optimism: { tvl, borrowed, },
107+
// base: { tvl, borrowed, },
108+
sonic: { tvl, borrowed, },
109+
hallmarks: []
110+
}

projects/silo/index.js

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
const sdk = require('@defillama/sdk')
22
const { sumTokens2 } = require('../helper/unwrapLPs')
33
const { getLogs } = require('../helper/cache/getLogs')
4-
const getAssetsAbi = "address[]:getAssets"
54

6-
const getAssetStateAbi = 'function getAssetsWithState() view returns (address[] assets, tuple(address collateralToken, address collateralOnlyToken, address debtToken, uint256 totalDeposits, uint256 collateralOnlyDeposits, uint256 totalBorrowAmount)[] assetsStorage)'
5+
const XAI = '0xd7c9f0e536dc865ae858b0c0453fe76d13c3beac'
6+
const blacklistedSilos = ["0x6543ee07cf5dd7ad17aeecf22ba75860ef3bbaaa",];
7+
8+
const getAssetsAbiV1 = "address[]:getAssets"
9+
const getAssetStateAbiV1 = 'function getAssetsWithState() view returns (address[] assets, tuple(address collateralToken, address collateralOnlyToken, address debtToken, uint256 totalDeposits, uint256 collateralOnlyDeposits, uint256 totalBorrowAmount)[] assetsStorage)'
710

8-
const config = {
11+
const configV1 = {
912
ethereum: {
1013
factories: [
1114
{
@@ -52,39 +55,43 @@ const config = {
5255
},
5356
}
5457

55-
const XAI = '0xd7c9f0e536dc865ae858b0c0453fe76d13c3beac'
56-
const fallbackBlacklist = ["0x6543ee07cf5dd7ad17aeecf22ba75860ef3bbaaa",];
57-
5858
async function tvl(api) {
59-
const siloArray = await getSilos(api)
60-
const assets = await api.multiCall({
61-
abi: getAssetsAbi,
62-
calls: siloArray,
63-
})
59+
// Handle V1 silos
60+
let toaV1 = [];
61+
if(configV1[api.chain]) {
62+
const siloArray = await getSilosV1(api);
63+
const assets = await api.multiCall({
64+
abi: getAssetsAbiV1,
65+
calls: siloArray,
66+
});
67+
toaV1 = assets.map((v, i) => ([v, siloArray[i]]));
68+
}
6469

65-
const toa = assets.map((v, i) => ([v, siloArray[i]]))
66-
return sumTokens2({ api, ownerTokens: toa, blacklistedTokens: [XAI], })
70+
return sumTokens2({ api, ownerTokens: toaV1, blacklistedTokens: [XAI], });
6771
}
6872

6973
async function borrowed(api) {
70-
const siloArray = await getSilos(api)
71-
const assetStates = await api.multiCall({
72-
abi: getAssetStateAbi,
73-
calls: siloArray.map(i => ({ target: i })),
74-
});
75-
assetStates.forEach(({ assets, assetsStorage }) => {
76-
assetsStorage
77-
.forEach((i, j) => {
78-
if (assets[j].toLowerCase() === XAI) return;
79-
return api.add(assets[j], i.totalBorrowAmount)
80-
})
81-
})
74+
// Handle V1 silos
75+
if(configV1[api.chain]) {
76+
const siloArray = await getSilosV1(api);
77+
const assetStates = await api.multiCall({
78+
abi: getAssetStateAbiV1,
79+
calls: siloArray.map(i => ({ target: i })),
80+
});
81+
assetStates.forEach(({ assets, assetsStorage }) => {
82+
assetsStorage
83+
.forEach((i, j) => {
84+
if (assets[j].toLowerCase() === XAI) return;
85+
return api.add(assets[j], i.totalBorrowAmount)
86+
})
87+
})
88+
}
8289
}
8390

84-
async function getSilos(api) {
91+
async function getSilosV1(api) {
8592
const chain = api.chain
8693
let logs = [];
87-
for(let factory of config[chain].factories) {
94+
for(let factory of configV1[chain].factories) {
8895
const { SILO_FACTORY, START_BLOCK, } = factory;
8996
let logChunk = await getLogs({
9097
api,
@@ -95,16 +102,17 @@ async function getSilos(api) {
95102
logs = [...logs, ...logChunk];
96103
}
97104

98-
return logs.map((log) => `0x${log.topics[1].substring(26)}`).filter((address) => fallbackBlacklist.indexOf(address.toLowerCase()) === -1);
105+
return logs.map((log) => `0x${log.topics[1].substring(26)}`).filter((address) => blacklistedSilos.indexOf(address.toLowerCase()) === -1);
99106
}
100107

101108

102109
module.exports = {
103-
methodology: `We calculate TVL by interacting with Silo Factory smart contracts on Ethereum and Arbitrum. For Ethereum, it queries Silo(Main)(0xB7d391192080674281bAAB8B3083154a5f64cd0a), (Legacy)(0x4D919CEcfD4793c0D47866C8d0a02a0950737589), (Convex Factory)(0x6d4A256695586F61b77B09bc3D28333A91114d5a), and (LLAMA Edition)(0x2c0fA05281730EFd3ef71172d8992500B36b56eA). On Arbitrum, we query the Silo Arbitrum factory(0x4166487056A922D784b073d4d928a516B074b719), On Optimism, we query the Silo Optimism factory(0x6B14c4450a29Dd9562c20259eBFF67a577b540b9), On Base, we query the Silo Base factory(0x408822E4E8682413666809b0655161093cd36f2b), we query the to obtain the addresses of Silos, retrieve the assets of each Silo, and then calculates the sum of the deposited tokens, borrowed amount are exported separately`,
110+
methodology: `We calculate TVL by interacting with Silo Factory smart contracts on Ethereum, Arbitrum, Base & Optimism. For Ethereum, it queries (Main-V1)(0xB7d391192080674281bAAB8B3083154a5f64cd0a), (Legacy-V1)(0x4D919CEcfD4793c0D47866C8d0a02a0950737589), (Convex Factory-V1)(0x6d4A256695586F61b77B09bc3D28333A91114d5a), and (LLAMA Edition-V1)(0x2c0fA05281730EFd3ef71172d8992500B36b56eA). On Arbitrum, we query the Silo Arbitrum factory (Main-V1)(0x4166487056A922D784b073d4d928a516B074b719), On Optimism, we query the Silo Optimism factory (Main-V1)(0x6B14c4450a29Dd9562c20259eBFF67a577b540b9), On Base, we query the Silo Base factory (Main-V1)(0x408822E4E8682413666809b0655161093cd36f2b), we query the to obtain the addresses of Silos, retrieve the assets of each Silo, and then calculates the sum of the deposited tokens, borrowed amount are exported separately.`,
104111
ethereum: { tvl, borrowed, },
105112
arbitrum: { tvl, borrowed, },
106113
optimism: { tvl, borrowed, },
107114
base: { tvl, borrowed, },
115+
sonic: { tvl, borrowed, },
108116
hallmarks: [
109117
[1692968400, "Launch CRV market"]
110118
]

0 commit comments

Comments
 (0)