Skip to content

Commit

Permalink
Origami add borrowed metric (DefiLlama#11612)
Browse files Browse the repository at this point in the history
Co-authored-by: g1ntoki <[email protected]>
  • Loading branch information
frost-ostrich and g1nt0ki authored Sep 13, 2024
1 parent 4130929 commit 013728e
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions projects/origami/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,26 @@ module.exports = {
}

Object.keys(GRAPH_URLS).forEach(chain => {
const endpoint = GRAPH_URLS[chain]
module.exports[chain] = {
tvl: async (api) => {
const { metrics: [{ investmentVaults }] } = await cachedGraphQuery('origami/' + chain, endpoint, '{ metrics { investmentVaults { id } } }')
const vaults = investmentVaults.map(vault => vault.id)
let tokens = await api.multiCall({ abi: 'address:reserveToken', calls: vaults })
if (chain === 'arbitrum')
tokens = await api.multiCall({ abi: 'address:baseToken', calls: tokens })

const decimals = await api.multiCall({ abi: 'uint8:decimals', calls: vaults })
const supplies = await api.multiCall({ abi: 'uint256:totalSupply', calls: vaults })
const reserves = await api.multiCall({ abi: 'uint256:reservesPerShare', calls: vaults })
const bals = reserves.map((reserve, i) => reserve * supplies[i] / 10 ** decimals[i])
api.add(tokens, bals)
}
}
module.exports[chain] = { tvl }
})

async function tvl(api) {
const { metrics: [{ investmentVaults }] } = await cachedGraphQuery('origami/' + api.chain, GRAPH_URLS[api.chain], '{ metrics { investmentVaults { id kinds } } }')

const isLeveraged = vault => !!vault.kinds.find(v => v === 'Leverage')
const levVaults = investmentVaults.filter(isLeveraged).map(v => v.id)
const nonLevVaults = investmentVaults.filter(v => !isLeveraged(v)).map(v => v.id)

let nonLevTokens = await api.multiCall({ abi: 'address:reserveToken', calls: nonLevVaults })
nonLevTokens = await api.multiCall({ abi: 'address:baseToken', calls: nonLevTokens })
const decimals = await api.multiCall({ abi: 'uint8:decimals', calls: nonLevVaults })
const supplies = await api.multiCall({ abi: 'uint256:totalSupply', calls: nonLevVaults })
const reserves = await api.multiCall({ abi: 'uint256:reservesPerShare', calls: nonLevVaults })
const bals = supplies.map((supply, idx) => reserves[idx] * supply / 10 ** decimals[idx])
api.add(nonLevTokens, bals)

const levReserveTokens = await api.multiCall({ calls: levVaults, abi: 'address:reserveToken' })
const assetsAndLiabilities = await api.multiCall({ abi: 'function assetsAndLiabilities() external view returns (uint256 assets,uint256 liabilities,uint256 ratio)', calls: levVaults })
const levBals = assetsAndLiabilities.map(({ assets, liabilities }) => assets - liabilities)
api.add(levReserveTokens, levBals)
}

0 comments on commit 013728e

Please sign in to comment.