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

[Release] Dashboard: BLV and Metric Improvements (#2909) #2915

Merged
merged 1 commit into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading