Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prototype for defillama IST tvl calculation v0.0.1 🚀 #1

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Jovonni
Copy link
Collaborator

@Jovonni Jovonni commented Jun 10, 2024

Wanted to run these two approaches by you guys to calc tvl for ist for defi llama...

will fill out below when ready:

NOTE

Please enable "Allow edits by maintainers" while putting up the PR.


  • If you would like to add a volume adapter please submit the PR here.
  • If you would like to add a liquidations adapter, please refer to this readme document for details.
  1. Once your adapter has been merged, it takes time to show on the UI. If more than 24 hours have passed, please let us know in Discord.
  2. Sorry, We no longer accept fetch adapter for new projects, we prefer the tvl to computed from blockchain data, if you have trouble with creating a the adapter, please hop onto our discord, we are happy to assist you.
  3. Please fill the form below only if the PR is for listing a new protocol else it can be ignored/replaced with reason/details about the PR
  4. For updating listing info It is a different repo, you can find your listing in this file: https://github.com/DefiLlama/defillama-server/blob/master/defi/src/protocols/data2.ts, you can edit it there and put up a PR
  5. Do not edit/push package-lock.json file as part of your changes, we use lockfileVersion 2, and most use v1 and using that messes up our CI
  6. No need to go to our discord and announce that you've created a PR, we monitor all PRs and will review it asap

Name (to be shown on DefiLlama):
Twitter Link:
List of audit links if any:
Website Link:
Logo (High resolution, will be shown with rounded borders):
Current TVL:
Treasury Addresses (if the protocol has treasury)
Chain:
Coingecko ID (so your TVL can appear on Coingecko, leave empty if not listed): (https://api.coingecko.com/api/v3/coins/list)
Coinmarketcap ID (so your TVL can appear on Coinmarketcap, leave empty if not listed): (https://api.coinmarketcap.com/data-api/v3/map/all?listing_status=active,inactive,untracked&start=1&limit=10000)
Short Description (to be shown on DefiLlama):
Token address and ticker if any:
Category (full list at https://defillama.com/categories) *Please choose only one:
Oracle Provider(s): Specify the oracle(s) used (e.g., Chainlink, Band, API3, TWAP, etc.):
Implementation Details: Briefly describe how the oracle is integrated into your project:
Documentation/Proof: Provide links to documentation or any other resources that verify the oracle's usage:
forkedFrom (Does your project originate from another project):
methodology (what is being counted as tvl, how is tvl being calculated):
Github org/user (Optional, if your code is open source, we can track activity):

@Jovonni Jovonni requested review from ivanlei and LuqiPan June 10, 2024 17:56
@Jovonni Jovonni self-assigned this Jun 10, 2024
@Jovonni
Copy link
Collaborator Author

Jovonni commented Jun 13, 2024

Updated calculation:

IST Data: { 'inter-stable-token': 1334769.86126 }
Reserve Data: 155816555
PSM Data: 30010011
Vault Data: 31960.239999999998
--- ist ---
IST                       185.86 M
Total: 185.86 M 

--- tvl ---
IST                       185.86 M
Total: 185.86 M 

------ TVL ------
ist                       185.86 M

total                    185.86 M 

@Jovonni
Copy link
Collaborator Author

Jovonni commented Jun 13, 2024

TLDR; (added to readme too) - Deprecated approach, see recent comments

for fetchISTData, we are pulling the total IST supply from the cosmos directory. the endpoint we’re hitting is https://rest.cosmos.directory/agoric/cosmos/bank/v1beta1/supply/by_denom?denom=uist. we process this to get the amount of IST in circulation. (May not need this as it might be redundant..?)

for fetchReserveData, this one’s for grabbing reserve metrics. we hit the path /custom/vstorage/data/published.reserve.metrics. the data we get back includes some marshaled bigints. we parse this to get the fee allocation value.

for fetchPSMData, we first get the different asset types from /custom/vstorage/children/published.psm.IST. then, for each asset type, we fetch its metrics from /custom/vstorage/data/published.psm.IST.${assetType}.metrics. we parse these to sum up the anchor pool balances.

for fetchVaultData, we start by fetching the vault managers from /custom/vstorage/children/published.vaultFactory.managers. then, for each manager, we get the vault data from /custom/vstorage/children/published.vaultFactory.managers.${managerId}.vaults. finally, we fetch individual vault details from /custom/vstorage/data/published.vaultFactory.managers.${managerId}.vaults.${vaultId}. we parse these to collect unique collateral types, fetch their prices from coingecko, and calculate the total locked USD value.

for fetchTotalTVL, we sum up the data from all the above fetches to get the total TVL: IST supply, reserves, PSMs, and vaults.

@Jovonni
Copy link
Collaborator Author

Jovonni commented Jun 14, 2024

Rethought the approach, and instead decided to go with graphql, much better approach.

Thanks @toliaqat @ivanlei for the resources on the indexer, got this working. Here is an updated calculation, which is wrong, but its using the new approach. Now we can just iterate on the calc to get it finalized to become more accurate, bulk of work is done 🚀

IST Data: { 'inter-stable-token': 1324886.823845 }
Reserve Data: 88840.683426
PSM Data: 5475.886243
Vault Data: 1981257.5781
Total Collat:  57157332.45712694
--- ist ---
IST                       56.99 M
Total: 56.99 M 

--- tvl ---
IST                       56.99 M
Total: 56.99 M 

------ TVL ------
ist                       56.99 M

total                    56.99 M 

@Jovonni
Copy link
Collaborator Author

Jovonni commented Jun 14, 2024

For example here is the get total collateral:

const fetchTotalCollateral = async () => {
  const query = `
  query {
    vaultManagerMetrics {
      nodes {
        totalCollateral
        liquidatingCollateralBrand
      }
    }
    oraclePrices {
      nodes {
        priceFeedName
        typeOutAmount
        typeInAmount
      }
    }
    boardAuxes {
      nodes {
        allegedName
        decimalPlaces
      }
    }
  }`;

  const data = await fetchSubqueryData(query);
  let totalCollateral = 0;
  const collateralPrices = {};
  const collateralDecimals = {};
  const collateralSet = new Set();

  data.vaultManagerMetrics.nodes.forEach(vault => {
    totalCollateral += parseFloat(vault.totalCollateral);
    collateralSet.add(vault.liquidatingCollateralBrand);
  });

  data.oraclePrices.nodes.forEach(price => {
    collateralPrices[price.priceFeedName] = parseFloat(price.typeOutAmount) / parseFloat(price.typeInAmount);
  });

  data.boardAuxes.nodes.forEach(aux => {
    collateralDecimals[aux.allegedName.toLowerCase()] = Math.pow(10, aux.decimalPlaces);
  });

  let totalCollateralUSD = 0;
  collateralSet.forEach(collateral => {
    const collatKey = `${collateral}-USD`;
    const price = collateralPrices[collatKey];
    const decimals = collateralDecimals[collateral.toLowerCase()] || 1;
    if (price) {
      totalCollateralUSD += (totalCollateral / decimals) * price;
    } else {
      console.error(`Price not found for collateral: ${collateral}`);
    }
  });

  return totalCollateralUSD / getCoinDecimals('uist');
};

Also saw in our notes, we are no longer using coingecko, so decided to do the same, and just use our price oracle data from the indexer. Finalizing the decimals part now, to ensure decimals are being used right:

https://github.com/Agoric/DefiLlama-Adapters/blob/c4780223339c640c84de25938724815d0844e0d5/projects/inter-protocol/index.js#L223C11-L223C19

@Jovonni Jovonni requested a review from toliaqat June 14, 2024 04:13
@Jovonni
Copy link
Collaborator Author

Jovonni commented Jun 14, 2024

Updated calculation, seems right-er:

IST Data: { 'inter-stable-token': 1324886.823845 }
Reserve Data: 88840.683426
PSM Data: 5475.886243
Vault Data: 1981257.5781
Total Collat:  4158362.531680237
--- ist ---
IST                       4.15 M
Total: 4.15 M 

--- tvl ---
IST                       4.15 M
Total: 4.15 M 

------ TVL ------
ist                       4.15 M

total                    4.15 M 

its not exactly what I'm seeing on dashboard though:

Total Collateral Value Locked
$4.18M

@toliaqat
Copy link

The dashboard showing Total Collateral Value Locked that only uses Vaults metrics. Here is the query that gives ~$4.19M

curl -s 'https://api.subquery.network/sq/agoric-labs/agoric-mainnet-v2' \
-H 'Content-Type: application/json' \
--data-binary '{
  "query": "{ vaultManagerMetrics { nodes { liquidatingCollateralBrand totalCollateral } } oraclePrices { nodes { typeInName typeOutAmount } } }"
}' | jq '(
  .data.oraclePrices.nodes | map({(.typeInName): (.typeOutAmount | tonumber / 1000000)}) | add
) as $prices |
(.data.vaultManagerMetrics.nodes | map({
    collateral: (.totalCollateral | tonumber / 1000000),
    brand: .liquidatingCollateralBrand
})) | map({
  collateral: .collateral,
  brand: .brand,
  price: ($prices[.brand] // 0),
  value: (.collateral * ($prices[.brand] // 0))
}) | [.[] | .value] | add / 1000000'

In code above, you are adding up all collateral values which might be the cause of the problem. I think we need to multiple vault.totalCollateral to collateralPrices[collatKey] before adding final dollar amounts.

totalCollateral += parseFloat(vault.totalCollateral);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants