Skip to content

Commit

Permalink
feat(projects/silo): add support for Silo V2 on Sonic & Arbitrum (#13034
Browse files Browse the repository at this point in the history
)
  • Loading branch information
JayWelsh authored Jan 9, 2025
1 parent 1230a59 commit 31465ad
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 29 deletions.
110 changes: 110 additions & 0 deletions projects/silo-v2/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
const sdk = require('@defillama/sdk')
const { sumTokens2 } = require('../helper/unwrapLPs')
const { getLogs } = require('../helper/cache/getLogs')

const XAI = '0xd7c9f0e536dc865ae858b0c0453fe76d13c3beac'
const blacklistedSilos = ["0x6543ee07cf5dd7ad17aeecf22ba75860ef3bbaaa",];

const getAssetAbiV2 = "address:asset";
const getAssetStateAbiV2 = 'function getTotalAssetsStorage(uint8 _assetType) external view returns (uint256 totalAssetsByType)';

const configV2 = {
sonic: {
factories: [
{
START_BLOCK: 2672166,
SILO_FACTORY: '0xa42001d6d2237d2c74108fe360403c4b796b7170', // Silo V2 Sonic (Main)
}
]
},
arbitrum: {
factories: [
{
START_BLOCK: 291201890,
SILO_FACTORY: '0xf7dc975C96B434D436b9bF45E7a45c95F0521442', // Silo V2 Arbitrum (Main)
}
]
}
}

async function tvl(api) {
// Handle V2 silos
let toaV2 = [];
if(configV2[api.chain]) {
const siloArrayV2 = await getSilosV2(api);
const assetsV2 = await api.multiCall({
abi: getAssetAbiV2,
calls: siloArrayV2.map(i => ({ target: i })),
});
toaV2 = assetsV2.map((asset, i) => [[asset], siloArrayV2[i]]);
}

return sumTokens2({ api, ownerTokens: toaV2, blacklistedTokens: [XAI], });
}

async function borrowed(api) {
if(configV2[api.chain]) {
// Handle V2 silos
const siloArrayV2 = await getSilosV2(api);

// Get asset address for each silo
const siloAssets = await api.multiCall({
abi: getAssetAbiV2,
calls: siloArrayV2.map(i => ({ target: i })),
});

// Get total borrow amount for each silo (AssetType.DEBT = 2)
const borrowAmounts = await api.multiCall({
abi: getAssetStateAbiV2,
calls: siloArrayV2.map(i => ({ target: i, params: [2] })),
});

// Add borrow amounts for V2 silos
siloAssets.forEach((asset, index) => {
if (asset.toLowerCase() === XAI) return;
return api.add(asset, borrowAmounts[index])
});
}
}

async function getSilosV2(api) {
const chain = api.chain;
let logs = [];
let siloAddresses = [];
if(configV2[chain]) {
for(let factory of configV2[chain].factories) {
const { SILO_FACTORY, START_BLOCK } = factory;
let logChunk = await getLogs({
api,
target: SILO_FACTORY,
fromBlock: START_BLOCK,
eventAbi: 'event NewSilo(address indexed implementation, address indexed token0, address indexed token1, address silo0, address silo1, address siloConfig)',
});
logs = [...logs, ...logChunk];
}

siloAddresses = logs.flatMap((log) => {

let silo0 = log.args[3];
let silo1 = log.args[4];

return [silo0, silo1].filter(
(address) => blacklistedSilos.indexOf(address.toLowerCase()) === -1
);
});

}

return siloAddresses;
}


module.exports = {
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.`,
// ethereum: { tvl, borrowed, },
arbitrum: { tvl, borrowed, },
// optimism: { tvl, borrowed, },
// base: { tvl, borrowed, },
sonic: { tvl, borrowed, },
hallmarks: []
}
66 changes: 37 additions & 29 deletions projects/silo/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const sdk = require('@defillama/sdk')
const { sumTokens2 } = require('../helper/unwrapLPs')
const { getLogs } = require('../helper/cache/getLogs')
const getAssetsAbi = "address[]:getAssets"

const getAssetStateAbi = 'function getAssetsWithState() view returns (address[] assets, tuple(address collateralToken, address collateralOnlyToken, address debtToken, uint256 totalDeposits, uint256 collateralOnlyDeposits, uint256 totalBorrowAmount)[] assetsStorage)'
const XAI = '0xd7c9f0e536dc865ae858b0c0453fe76d13c3beac'
const blacklistedSilos = ["0x6543ee07cf5dd7ad17aeecf22ba75860ef3bbaaa",];

const getAssetsAbiV1 = "address[]:getAssets"
const getAssetStateAbiV1 = 'function getAssetsWithState() view returns (address[] assets, tuple(address collateralToken, address collateralOnlyToken, address debtToken, uint256 totalDeposits, uint256 collateralOnlyDeposits, uint256 totalBorrowAmount)[] assetsStorage)'

const config = {
const configV1 = {
ethereum: {
factories: [
{
Expand Down Expand Up @@ -52,39 +55,43 @@ const config = {
},
}

const XAI = '0xd7c9f0e536dc865ae858b0c0453fe76d13c3beac'
const fallbackBlacklist = ["0x6543ee07cf5dd7ad17aeecf22ba75860ef3bbaaa",];

async function tvl(api) {
const siloArray = await getSilos(api)
const assets = await api.multiCall({
abi: getAssetsAbi,
calls: siloArray,
})
// Handle V1 silos
let toaV1 = [];
if(configV1[api.chain]) {
const siloArray = await getSilosV1(api);
const assets = await api.multiCall({
abi: getAssetsAbiV1,
calls: siloArray,
});
toaV1 = assets.map((v, i) => ([v, siloArray[i]]));
}

const toa = assets.map((v, i) => ([v, siloArray[i]]))
return sumTokens2({ api, ownerTokens: toa, blacklistedTokens: [XAI], })
return sumTokens2({ api, ownerTokens: toaV1, blacklistedTokens: [XAI], });
}

async function borrowed(api) {
const siloArray = await getSilos(api)
const assetStates = await api.multiCall({
abi: getAssetStateAbi,
calls: siloArray.map(i => ({ target: i })),
});
assetStates.forEach(({ assets, assetsStorage }) => {
assetsStorage
.forEach((i, j) => {
if (assets[j].toLowerCase() === XAI) return;
return api.add(assets[j], i.totalBorrowAmount)
})
})
// Handle V1 silos
if(configV1[api.chain]) {
const siloArray = await getSilosV1(api);
const assetStates = await api.multiCall({
abi: getAssetStateAbiV1,
calls: siloArray.map(i => ({ target: i })),
});
assetStates.forEach(({ assets, assetsStorage }) => {
assetsStorage
.forEach((i, j) => {
if (assets[j].toLowerCase() === XAI) return;
return api.add(assets[j], i.totalBorrowAmount)
})
})
}
}

async function getSilos(api) {
async function getSilosV1(api) {
const chain = api.chain
let logs = [];
for(let factory of config[chain].factories) {
for(let factory of configV1[chain].factories) {
const { SILO_FACTORY, START_BLOCK, } = factory;
let logChunk = await getLogs({
api,
Expand All @@ -95,16 +102,17 @@ async function getSilos(api) {
logs = [...logs, ...logChunk];
}

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


module.exports = {
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`,
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.`,
ethereum: { tvl, borrowed, },
arbitrum: { tvl, borrowed, },
optimism: { tvl, borrowed, },
base: { tvl, borrowed, },
sonic: { tvl, borrowed, },
hallmarks: [
[1692968400, "Launch CRV market"]
]
Expand Down

0 comments on commit 31465ad

Please sign in to comment.