Skip to content

Commit

Permalink
Dashboard: BLV and Metric Improvements (#2909)
Browse files Browse the repository at this point in the history
* Keep wundergraph SDK version consistent with treasury-subgraph-client

* Sync circulating and floating supply definitions with treasury-subgraph

OlympusDAO/treasury-subgraph#38

* Remove console log

* Re-generate lockfile

* Remove duplicate dependency (imported through treasury-subgraph-client)

* Bump wundergraph and treasury-subgraph-client dependencies

* Shift from use of protocolMetrics query (now internal) to metrics query

* WIP shifting to metrics from treasury-subgraph

* TODOs

* Incorrect query parameter

* Shift OHM supply graph to use the API metrics

* Updated enum values for supply categories

* Shift OHM supply table to use API metrics

* Shift remaining functions to use metrics/simplified hooks

* Cleanup

* Add ts-prune to find dead code

* Remove unused code

* Correct definition for liquid backing per gOHM. Remove dead code.

* Correct description

* Bump treasury-subgraph-client version

* Fix date displays

* Fix missing key error

* Fix error with Skeleton component

* remove ts-prune

---------

Co-authored-by: appleseed-iii <[email protected]>
  • Loading branch information
0xJem and appleseed-iii authored Jul 27, 2023
1 parent 036ac7c commit 3296451
Show file tree
Hide file tree
Showing 18 changed files with 505 additions and 1,495 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"@types/recharts": "^1.8.24",
"@vitejs/plugin-react-swc": "^3.3.2",
"@wundergraph/react-query": "^0.8.46",
"@wundergraph/sdk": "^0.166.1",
"axios": "^1.4.0",
"class-validator": "^0.14.0",
"date-fns": "^2.30.0",
Expand Down
16 changes: 0 additions & 16 deletions src/helpers/react-query/createDependentQuery.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/helpers/subgraph/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,4 @@ export const CATEGORY_STABLE = "Stable";
export const CATEGORY_VOLATILE = "Volatile";
export const CATEGORY_POL = "Protocol-Owned Liquidity";

export const TOKEN_SUPPLY_TYPE_BONDS_DEPOSITS = "OHM Bonds (Burnable Deposits)";
export const TOKEN_SUPPLY_TYPE_BONDS_PREMINTED = "OHM Bonds (Pre-minted)";
export const TOKEN_SUPPLY_TYPE_BONDS_VESTING_DEPOSITS = "OHM Bonds (Vesting Deposits)";
export const TOKEN_SUPPLY_TYPE_BONDS_VESTING_TOKENS = "OHM Bonds (Vesting Tokens)";
export const TOKEN_SUPPLY_TYPE_LIQUIDITY = "Liquidity";
export const TOKEN_SUPPLY_TYPE_BOOSTED_LIQUIDITY_VAULT = "Boosted Liquidity Vault";
export const TOKEN_SUPPLY_TYPE_OFFSET = "Manual Offset";
export const TOKEN_SUPPLY_TYPE_TOTAL_SUPPLY = "Total Supply";
export const TOKEN_SUPPLY_TYPE_TREASURY = "Treasury";
export const TOKEN_SUPPLY_TYPE_LENDING = "Lending";

export const CHAIN_ETHEREUM = "Ethereum";
300 changes: 3 additions & 297 deletions src/helpers/subgraph/TreasuryQueryHelper.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,7 @@
import { GOHM_ADDRESSES, OHM_ADDRESSES } from "src/constants/addresses";
import {
CATEGORY_POL,
CATEGORY_STABLE,
CATEGORY_VOLATILE,
TOKEN_SUPPLY_TYPE_BONDS_DEPOSITS,
TOKEN_SUPPLY_TYPE_BONDS_PREMINTED,
TOKEN_SUPPLY_TYPE_BONDS_VESTING_DEPOSITS,
TOKEN_SUPPLY_TYPE_BONDS_VESTING_TOKENS,
TOKEN_SUPPLY_TYPE_BOOSTED_LIQUIDITY_VAULT,
TOKEN_SUPPLY_TYPE_LENDING,
TOKEN_SUPPLY_TYPE_LIQUIDITY,
TOKEN_SUPPLY_TYPE_OFFSET,
TOKEN_SUPPLY_TYPE_TOTAL_SUPPLY,
TOKEN_SUPPLY_TYPE_TREASURY,
} from "src/helpers/subgraph/Constants";
import { TokenRecord, TokenSupply } from "src/hooks/useFederatedSubgraphQuery";
import { CATEGORY_POL, CATEGORY_STABLE, CATEGORY_VOLATILE } from "src/helpers/subgraph/Constants";
import { TokenRecord } from "src/hooks/useFederatedSubgraphQuery";

export const getLiquidBackingPerOhmBacked = (liquidBacking: number, tokenSupplies: TokenSupply[], ohmIndex: number) =>
liquidBacking / getOhmBackedSupply(tokenSupplies, ohmIndex)[0];

export const getLiquidBackingPerOhmFloating = (liquidBacking: number, tokenSupplies: TokenSupply[], ohmIndex: number) =>
liquidBacking / getOhmFloatingSupply(tokenSupplies, ohmIndex)[0];

export const getLiquidBackingPerGOhmSynthetic = (
liquidBacking: number,
currentIndex: number,
tokenSupplies: TokenSupply[],
) => liquidBacking / getGOhmSyntheticSupply(currentIndex, getOhmBackedSupply(tokenSupplies, currentIndex)[0]);

export const filterReduce = (
const filterReduce = (
records: TokenRecord[],
filterPredicate: (value: TokenRecord) => unknown,
valueExcludingOhm = false,
Expand All @@ -49,270 +22,3 @@ export const getTreasuryAssetValue = (

return filterReduce(records, record => categories.includes(record.category), false);
};

let supportedTokens: string[];

const getOhmAddresses = (): string[] => {
return Object.values(OHM_ADDRESSES).map(address => address.toLowerCase());
};

const getGOhmAddresses = (): string[] => {
return Object.values(GOHM_ADDRESSES).map(address => address.toLowerCase());
};

const getSupportedTokens = (): string[] => {
if (!supportedTokens) {
const tokens: string[] = [];
tokens.push(...getOhmAddresses());
tokens.push(...getGOhmAddresses());

supportedTokens = tokens;
}

return supportedTokens;
};

const isSupportedToken = (record: TokenSupply) => {
if (!getSupportedTokens().includes(record.tokenAddress.toLowerCase())) {
return false;
}

return true;
};

const getBalanceMultiplier = (record: TokenSupply, ohmIndex: number): number => {
if (getOhmAddresses().includes(record.tokenAddress.toLowerCase())) {
return 1;
}

if (getGOhmAddresses().includes(record.tokenAddress.toLowerCase())) {
return ohmIndex;
}

throw new Error(`Unsupported token address: ${record.tokenAddress}`);
};

/**
* Returns the sum of balances for different supply types.
*
* Note that this will return positive balances for each type.
*
* For example, passing [TOKEN_SUPPLY_TYPE_LIQUIDITY, TOKEN_SUPPLY_TYPE_TREASURY]
* in the {includedTypes} parameter will return a number that is
* the sum of the balance property all records with matching types.
*
* @param records TokenSupply records for the given day
* @param ohmIndex The index of OHM for the given day
* @param includedTypes
* @returns [balance, included records]
*/
const getBalanceForTypes = (
records: TokenSupply[],
includedTypes: string[],
ohmIndex: number,
): [number, TokenSupply[]] => {
const filteredRecords = records.filter(record => isSupportedToken(record) && includedTypes.includes(record.type));
const balance = filteredRecords.reduce(
(previousValue, record) => previousValue + +record.balance * getBalanceMultiplier(record, ohmIndex),
0,
);

return [balance, filteredRecords];
};

/**
* Returns the sum of balances for different supply types.
*
* Note that this will return positive or negative balances, depending on the type.
*
* For example, passing [TOKEN_SUPPLY_TYPE_LIQUIDITY, TOKEN_SUPPLY_TYPE_TREASURY]
* in the {includedTypes} parameter will return a number that is
* the sum of the supplyBalance property all records with matching types.
*
* @param records TokenSupply records for the given day
* @param ohmIndex The index of OHM for the given day
* @param includedTypes
* @returns [balance, included records]
*/
const getSupplyBalanceForTypes = (
records: TokenSupply[],
includedTypes: string[],
ohmIndex: number,
): [number, TokenSupply[]] => {
const filteredRecords = records.filter(record => isSupportedToken(record) && includedTypes.includes(record.type));

const supplyBalance = filteredRecords.reduce(
(previousValue, record) => previousValue + +record.supplyBalance * getBalanceMultiplier(record, ohmIndex),
0,
);

return [supplyBalance, filteredRecords];
};

export const getProtocolOwnedLiquiditySupply = (records: TokenSupply[], ohmIndex: number): [number, TokenSupply[]] => {
return getBalanceForTypes(records, [TOKEN_SUPPLY_TYPE_LIQUIDITY], ohmIndex);
};

export const getTreasurySupply = (records: TokenSupply[], ohmIndex: number): [number, TokenSupply[]] => {
return getBalanceForTypes(records, [TOKEN_SUPPLY_TYPE_TREASURY], ohmIndex);
};

export const getMigrationOffsetSupply = (records: TokenSupply[], ohmIndex: number): [number, TokenSupply[]] => {
return getBalanceForTypes(records, [TOKEN_SUPPLY_TYPE_OFFSET], ohmIndex);
};

export const getBondDepositsSupply = (records: TokenSupply[], ohmIndex: number): [number, TokenSupply[]] => {
return getBalanceForTypes(
records,
[TOKEN_SUPPLY_TYPE_BONDS_VESTING_DEPOSITS, TOKEN_SUPPLY_TYPE_BONDS_DEPOSITS],
ohmIndex,
);
};

export const getBondVestingTokensSupply = (records: TokenSupply[], ohmIndex: number): [number, TokenSupply[]] => {
return getBalanceForTypes(records, [TOKEN_SUPPLY_TYPE_BONDS_VESTING_TOKENS], ohmIndex);
};

export const getLendingSupply = (records: TokenSupply[], ohmIndex: number): [number, TokenSupply[]] => {
return getBalanceForTypes(records, [TOKEN_SUPPLY_TYPE_LENDING], ohmIndex);
};

export const getBondPremintedSupply = (records: TokenSupply[], ohmIndex: number): [number, TokenSupply[]] => {
return getBalanceForTypes(records, [TOKEN_SUPPLY_TYPE_BONDS_PREMINTED], ohmIndex);
};

export const getBoostedLiquidityVaultSupply = (records: TokenSupply[], ohmIndex: number): [number, TokenSupply[]] => {
return getBalanceForTypes(records, [TOKEN_SUPPLY_TYPE_BOOSTED_LIQUIDITY_VAULT], ohmIndex);
};

export const getExternalSupply = (records: TokenSupply[], ohmIndex: number): number => {
return (
getOhmTotalSupply(records, ohmIndex)[0] -
getProtocolOwnedLiquiditySupply(records, ohmIndex)[0] -
getTreasurySupply(records, ohmIndex)[0] -
getMigrationOffsetSupply(records, ohmIndex)[0] -
getBondDepositsSupply(records, ohmIndex)[0] -
getBondVestingTokensSupply(records, ohmIndex)[0] -
getBondPremintedSupply(records, ohmIndex)[0] -
getLendingSupply(records, ohmIndex)[0] -
getBoostedLiquidityVaultSupply(records, ohmIndex)[0]
);
};

/**
* For a given array of TokenSupply records (assumed to be at the same point in time),
* this function returns the OHM circulating supply.
*
* Circulating supply is defined as:
* - OHM total supply
* - minus: OHM in circulating supply wallets
* - minus: migration offset
* - minus: pre-minted OHM for bonds
* - minus: OHM user deposits for bonds
*
* @param records TokenSupply records for the given day
* @param ohmIndex The index of OHM for the given day
* @returns
*/
export const getOhmCirculatingSupply = (records: TokenSupply[], ohmIndex: number): [number, TokenSupply[]] => {
const includedTypes = [
TOKEN_SUPPLY_TYPE_TOTAL_SUPPLY,
TOKEN_SUPPLY_TYPE_TREASURY,
TOKEN_SUPPLY_TYPE_OFFSET,
TOKEN_SUPPLY_TYPE_BONDS_PREMINTED,
TOKEN_SUPPLY_TYPE_BONDS_VESTING_DEPOSITS,
TOKEN_SUPPLY_TYPE_BONDS_DEPOSITS,
TOKEN_SUPPLY_TYPE_BOOSTED_LIQUIDITY_VAULT,
];

return getSupplyBalanceForTypes(records, includedTypes, ohmIndex);
};

/**
* For a given array of TokenSupply records (assumed to be at the same point in time),
* this function returns the OHM floating supply.
*
* Floating supply is defined as:
* - OHM total supply
* - minus: OHM in circulating supply wallets
* - minus: migration offset
* - minus: pre-minted OHM for bonds
* - minus: OHM user deposits for bonds
* - minus: protocol-owned OHM in liquidity pools
*
* @param records TokenSupply records for the given day
* @param ohmIndex The index of OHM for the given day
* @returns
*/
export const getOhmFloatingSupply = (records: TokenSupply[], ohmIndex: number): [number, TokenSupply[]] => {
const includedTypes = [
TOKEN_SUPPLY_TYPE_TOTAL_SUPPLY,
TOKEN_SUPPLY_TYPE_TREASURY,
TOKEN_SUPPLY_TYPE_OFFSET,
TOKEN_SUPPLY_TYPE_BONDS_PREMINTED,
TOKEN_SUPPLY_TYPE_BONDS_VESTING_DEPOSITS,
TOKEN_SUPPLY_TYPE_BONDS_DEPOSITS,
TOKEN_SUPPLY_TYPE_BOOSTED_LIQUIDITY_VAULT,
TOKEN_SUPPLY_TYPE_LIQUIDITY,
];

return getSupplyBalanceForTypes(records, includedTypes, ohmIndex);
};

/**
* For a given array of TokenSupply records (assumed to be at the same point in time),
* this function returns the OHM backed supply.
*
* Backed supply is the quantity of OHM backed by treasury assets.
*
* Backed supply is calculated as:
* - OHM total supply
* - minus: OHM in circulating supply wallets
* - minus: migration offset
* - minus: pre-minted OHM for bonds
* - minus: OHM user deposits for bonds
* - minus: protocol-owned OHM in liquidity pools
* - minus: OHM minted and deployed into lending markets
*
* @param records TokenSupply records for the given day
* @param ohmIndex The index of OHM for the given day
*/
export const getOhmBackedSupply = (records: TokenSupply[], ohmIndex: number): [number, TokenSupply[]] => {
const includedTypes = [
TOKEN_SUPPLY_TYPE_TOTAL_SUPPLY,
TOKEN_SUPPLY_TYPE_TREASURY,
TOKEN_SUPPLY_TYPE_OFFSET,
TOKEN_SUPPLY_TYPE_BONDS_PREMINTED,
TOKEN_SUPPLY_TYPE_BONDS_VESTING_DEPOSITS,
TOKEN_SUPPLY_TYPE_BONDS_DEPOSITS,
TOKEN_SUPPLY_TYPE_BOOSTED_LIQUIDITY_VAULT,
TOKEN_SUPPLY_TYPE_LIQUIDITY,
TOKEN_SUPPLY_TYPE_LENDING,
];

return getSupplyBalanceForTypes(records, includedTypes, ohmIndex);
};

/**
* For a given array of TokenSupply records (assumed to be at the same point in time),
* this function returns the OHM total supply.
*
* @param records TokenSupply records for the given day
* @param ohmIndex The index of OHM for the given day
* @returns
*/
export const getOhmTotalSupply = (records: TokenSupply[], ohmIndex: number): [number, TokenSupply[]] => {
return getSupplyBalanceForTypes(records, [TOKEN_SUPPLY_TYPE_TOTAL_SUPPLY], ohmIndex);
};

/**
* gOHM circulating supply is synthetically calculated as:
*
* OHM floating supply / current index
*
* @param currentIndex
* @param ohmFloatingSupply
* @returns
*/
export const getGOhmSyntheticSupply = (currentIndex: number, ohmFloatingSupply: number) =>
ohmFloatingSupply / currentIndex;
Loading

0 comments on commit 3296451

Please sign in to comment.