-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
06fdd9a
commit 4d3797a
Showing
1 changed file
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
const sdk = require("@defillama/sdk"); | ||
const BigNumber = require("bignumber.js"); | ||
const { request, gql } = require("graphql-request"); | ||
|
||
const vaultAbis = { | ||
baseToken: "function baseToken() view returns (address)", | ||
}; | ||
|
||
const GRAPH_URLS = { | ||
ethereum: "https://api.thegraph.com/subgraphs/name/templedao/origami-mainnet", // ethereum | ||
arbitrum: "https://api.thegraph.com/subgraphs/name/templedao/origami-arb", // arbitrum | ||
}; | ||
|
||
const stableCoin = { | ||
arbitrum: { | ||
address: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831", | ||
decimals: 6, | ||
}, | ||
}; | ||
|
||
const metricsQuery = gql` | ||
{ | ||
metrics { | ||
investmentVaultsTvlUSD | ||
} | ||
} | ||
`; | ||
|
||
const vaultQuery = gql` | ||
{ | ||
metrics { | ||
investmentVaults { | ||
id | ||
} | ||
} | ||
} | ||
`; | ||
|
||
async function tvlFromGraph(api) { | ||
const { chain } = api; | ||
const graphUrl = GRAPH_URLS[chain]; | ||
|
||
const balances = {}; | ||
if (chain == "ethereum") { | ||
// fetch vaults first | ||
const vaultResp = await request(graphUrl, vaultQuery); | ||
const vaults = vaultResp.metrics[0].investmentVaults; | ||
|
||
const apyInfos = await Promise.all( | ||
vaults.map(async (vault) => { | ||
const vaultAddr = vault.id.toLowerCase(); | ||
|
||
const tvlQuery = gql` | ||
{ | ||
investmentVault(id: "${vaultAddr}") { | ||
tvl | ||
} | ||
} | ||
`; | ||
const tvlResp = await request(graphUrl, tvlQuery); | ||
|
||
const baseToken = await api.call({ | ||
abi: vaultAbis.baseToken, | ||
target: vaultAddr, | ||
}); | ||
|
||
return { | ||
tvl: BigNumber(tvlResp.investmentVault.tvl).times(1e18).toFixed(0), | ||
baseToken, | ||
}; | ||
}) | ||
); | ||
|
||
for (const apyInfo of apyInfos) { | ||
sdk.util.sumSingleBalance(balances, apyInfo.baseToken, apyInfo.tvl); | ||
} | ||
} else { | ||
const metricsResp = await request(graphUrl, metricsQuery); | ||
sdk.util.sumSingleBalance( | ||
balances, | ||
chain + ":" + stableCoin[chain].address.toLowerCase(), | ||
BigNumber(metricsResp.metrics[0].investmentVaultsTvlUSD) | ||
.times(10 ** stableCoin[chain].decimals) | ||
.toFixed(0) | ||
); | ||
} | ||
|
||
return balances; | ||
} | ||
|
||
module.exports = { | ||
ethereum: { | ||
tvl: tvlFromGraph, | ||
}, | ||
arbitrum: { | ||
tvl: tvlFromGraph, | ||
}, | ||
}; |