From 306ea0b583741098786a18dffcc5b0070a69571b Mon Sep 17 00:00:00 2001 From: Touseef Liaqat Date: Sun, 5 May 2024 01:52:47 -0700 Subject: [PATCH] Make naming consistent --- schema.graphql | 47 ++++++++------------------------ src/mappings/events/boardAux.ts | 2 +- src/mappings/events/priceFeed.ts | 18 ++++++------ src/mappings/events/psm.ts | 19 ++++--------- src/mappings/events/reserves.ts | 23 ++++++++-------- src/mappings/events/vaults.ts | 29 +------------------- src/mappings/mappingHandlers.ts | 19 ------------- 7 files changed, 40 insertions(+), 117 deletions(-) diff --git a/schema.graphql b/schema.graphql index d06eac052..a563d0482 100644 --- a/schema.graphql +++ b/schema.graphql @@ -36,7 +36,7 @@ type StateChangeEvent @entity { blockHeight: BigInt! blockTime: Date! module: String! @index - path: String! + path: String! @index idx: Int! slots: String! body: String! @@ -55,11 +55,9 @@ type OraclePrice @entity { type OraclePriceDaily @entity { id: ID! - path: String! @index dateKey: Int! @index # YYYYMMDD format blockHeightLast: BigInt! blockTimeLast: Date! - priceFeedName: String typeInAmountLast: BigInt typeInAmountSum: BigInt typeOutAmountLast: BigInt @@ -69,12 +67,12 @@ type OraclePriceDaily @entity { metricsCount: BigInt } -type PsmMetric @entity { +type PsmMetrics @entity { id: ID! blockHeight: BigInt! blockTime: Date! - coin: String! - token: String! + coin: String! # use denom instead + denom: String! anchorPoolBalance: BigInt! feePoolBalance: BigInt! mintedPoolBalance: BigInt! @@ -82,24 +80,19 @@ type PsmMetric @entity { totalMintedProvided: BigInt! } -type PsmMetricDaily @entity { +type PsmMetricsDaily @entity { id: ID! path: String! @index dateKey: Int! @index # YYYYMMDD format blockHeightLast: BigInt! blockTimeLast: Date! - token: String @index + denom: String @index anchorPoolBalanceLast: BigInt - anchorPoolBalanceSum: BigInt feePoolBalanceLast: BigInt - feePoolBalanceSum: BigInt mintedPoolBalanceLast: BigInt - mintedPoolBalanceSum: BigInt totalAnchorProvidedLast: BigInt - totalAnchorProvidedSum: BigInt totalMintedProvidedLast: BigInt - totalMintedProvidedSum: BigInt metricsCount: BigInt } @@ -107,8 +100,8 @@ type PsmGovernance @entity { id: ID! blockHeight: BigInt! blockTime: Date! - coin: String! - token: String! + coin: String! # use denom instead + denom: String! mintLimit: BigInt! giveMintedFeeDenominator: BigInt! giveMintedFeeNumerator: BigInt! @@ -139,8 +132,8 @@ type Vault @entity { blockTime: Date! balance: BigInt lockedValue: BigInt - coin: String @index - token: String @index + coin: String @index # use denom instead + denom: String @index debt: BigInt state: String wallet: Wallet! @@ -182,35 +175,20 @@ type VaultManagerMetricsDaily @entity { liquidatingDebtBrand: String liquidatingCollateralValueLast: BigInt - liquidatingCollateralValueSum: BigInt liquidatingDebtValueLast: BigInt - liquidatingDebtValueSum: BigInt lockedQuoteDenominatorLast: BigInt - lockedQuoteDenominatorSum: BigInt lockedQuoteNumeratorLast: BigInt - lockedQuoteNumeratorSum: BigInt numActiveVaultsLast: BigInt - numActiveVaultsSum: BigInt numLiquidatingVaultsLast: BigInt - numLiquidatingVaultsSum: BigInt numLiquidationsAbortedLast: BigInt - numLiquidationsAbortedSum: BigInt numLiquidationsCompletedLast: BigInt - numLiquidationsCompletedSum: BigInt retainedCollateralLast: BigInt - retainedCollateralSum: BigInt totalCollateralLast: BigInt - totalCollateralSum: BigInt totalCollateralSoldLast: BigInt - totalCollateralSoldSum: BigInt totalDebtLast: BigInt - totalDebtSum: BigInt totalOverageReceivedLast: BigInt - totalOverageReceivedSum: BigInt totalProceedsReceivedLast: BigInt - totalProceedsReceivedSum: BigInt totalShortfallReceivedLast: BigInt - totalShortfallReceivedSum: BigInt metricsCount: BigInt } @@ -236,7 +214,7 @@ type ReserveAllocationMetrics @entity { id: ID! blockHeight: BigInt! blockTime: Date! - token: String! @index + denom: String! @index key: String! @index value: BigInt! reserveMetrics: ReserveMetrics! @@ -248,10 +226,9 @@ type ReserveAllocationMetricsDaily @entity { dateKey: Int! @index # YYYYMMDD format blockHeightLast: BigInt! blockTimeLast: Date! - token: String @index + denom: String @index key: String valueLast: BigInt - valueSum: BigInt metricsCount: BigInt } diff --git a/src/mappings/events/boardAux.ts b/src/mappings/events/boardAux.ts index 61834f35c..de573c22f 100644 --- a/src/mappings/events/boardAux.ts +++ b/src/mappings/events/boardAux.ts @@ -10,7 +10,7 @@ export const boardAuxEventKit = (block: any, data: any, module: string, path: st payload.displayInfo.assetKind, payload.displayInfo.decimalPlaces ?? 0 ).save(); - // logger.info("boardAux: " + JSON.stringify(boardAux)); + return [boardAux]; } diff --git a/src/mappings/events/priceFeed.ts b/src/mappings/events/priceFeed.ts index 3b1a5cd4c..2e0f838f7 100644 --- a/src/mappings/events/priceFeed.ts +++ b/src/mappings/events/priceFeed.ts @@ -9,15 +9,16 @@ export const priceFeedEventKit = (block: any, data: any, module: string, path: s const typeOutName = matchTypeOutName ? matchTypeOutName[1] : undefined; if (typeInName !== undefined && typeOutName !== undefined) { + const id = `${typeInName}-${typeOutName}`; // First save daily Oracle Prices - const oraclePriceDaily = saveOraclePriceDaily(payload, typeInName, typeOutName); + const oraclePriceDaily = saveOraclePriceDaily(id, payload, typeInName, typeOutName); // Save the Oracle Price const oraclePrice = new OraclePrice( - path, + id, BigInt(data.blockHeight), block.block.header.time as any, - path.split("published.priceFeed.")[1], + id, BigInt(payload.amountIn.__value), BigInt(payload.amountOut.__value), typeInName, @@ -29,12 +30,10 @@ export const priceFeedEventKit = (block: any, data: any, module: string, path: s return []; } - async function saveOraclePriceDaily(payload: any, typeInName: string, typeOutName: string): Promise { + async function saveOraclePriceDaily(id: string, payload: any, typeInName: string, typeOutName: string): Promise { const dateKey = dateToDayKey(block.block.header.time); - let state = await getOraclePriceDaily(path, dateKey); - - state.priceFeedName = path.split("published.priceFeed.")[1]; + let state = await getOraclePriceDaily(id, dateKey); state.typeInAmountLast = BigInt(BigInt(payload.amountIn.__value)); state.typeInAmountSum = (state.typeInAmountSum ?? BigInt(0)) + BigInt(payload.amountIn.__value); @@ -49,13 +48,12 @@ export const priceFeedEventKit = (block: any, data: any, module: string, path: s return state.save(); } - async function getOraclePriceDaily(path: string, dateKey: number): Promise { - const id = path + ":" + dateKey.toString(); + async function getOraclePriceDaily(feedName: string, dateKey: number): Promise { + const id = feedName + ":" + dateKey.toString(); let state = await OraclePriceDaily.get(id); if (!state) { state = new OraclePriceDaily( id, - path, dateKey, BigInt(data.blockHeight), new Date(block.block.header.time as any) diff --git a/src/mappings/events/psm.ts b/src/mappings/events/psm.ts index a3ded320e..c50033366 100644 --- a/src/mappings/events/psm.ts +++ b/src/mappings/events/psm.ts @@ -1,10 +1,10 @@ -import { PsmGovernance, PsmMetric, PsmMetricDaily } from "../../types"; +import { PsmGovernance, PsmMetrics, PsmMetricsDaily } from "../../types"; import { dateToDayKey } from "../utils"; export const psmEventKit = (block: any, data: any, module: string, path: string) => { async function savePsmMetrics(payload: any): Promise[]> { const psmMetricDaily = savePsmMetricDaily(payload); - const psmMetric = new PsmMetric( + const psmMetric = new PsmMetrics( path, BigInt(data.blockHeight), block.block.header.time as any, @@ -25,7 +25,7 @@ export const psmEventKit = (block: any, data: any, module: string, path: string) let state = await getPsmMetricDaily(dateKey); - state.token = path.split(".")[3]; + state.denom = path.split(".")[3]; state.anchorPoolBalanceLast = BigInt(payload.anchorPoolBalance.__value); state.feePoolBalanceLast = BigInt(payload.feePoolBalance.__value); @@ -33,23 +33,16 @@ export const psmEventKit = (block: any, data: any, module: string, path: string) state.totalAnchorProvidedLast = BigInt(payload.totalAnchorProvided.__value); state.totalMintedProvidedLast = BigInt(payload.totalMintedProvided.__value); - state.anchorPoolBalanceSum = (state.anchorPoolBalanceSum ?? BigInt(0)) + BigInt(payload.anchorPoolBalance.__value); - state.feePoolBalanceSum = (state.feePoolBalanceSum ?? BigInt(0)) + BigInt(payload.feePoolBalance.__value); - state.mintedPoolBalanceSum = (state.mintedPoolBalanceSum ?? BigInt(0)) + BigInt(payload.mintedPoolBalance.__value); - state.totalAnchorProvidedSum = - (state.totalAnchorProvidedSum ?? BigInt(0)) + BigInt(payload.totalAnchorProvided.__value); - state.totalMintedProvidedSum = - (state.totalMintedProvidedSum ?? BigInt(0)) + BigInt(payload.totalMintedProvided.__value); state.metricsCount = (state.metricsCount ?? BigInt(0)) + BigInt(1); return state.save(); } - async function getPsmMetricDaily(dateKey: number): Promise { + async function getPsmMetricDaily(dateKey: number): Promise { const id = path + ":" + dateKey.toString(); - let state = await PsmMetricDaily.get(id); + let state = await PsmMetricsDaily.get(id); if (!state) { - state = new PsmMetricDaily(id, path, dateKey, BigInt(data.blockHeight), new Date(block.block.header.time as any)); + state = new PsmMetricsDaily(id, path, dateKey, BigInt(data.blockHeight), new Date(block.block.header.time as any)); } return state; } diff --git a/src/mappings/events/reserves.ts b/src/mappings/events/reserves.ts index b244f5d02..4c93ad702 100644 --- a/src/mappings/events/reserves.ts +++ b/src/mappings/events/reserves.ts @@ -19,13 +19,15 @@ export const reservesEventKit = (block: any, data: any, module: string, path: st if (payload.allocations.hasOwnProperty(key)) { const allocation = payload.allocations[key]; // Save daily metrics - saveReserveAllocationMetricDaily(payload, allocation, key); + const brand = extractBrand(allocation.__brand); + const reserveAllocationMetricDaily = saveReserveAllocationMetricDaily(brand, payload, allocation, key); + promises.push(reserveAllocationMetricDaily); const reserveAllocationMetric = new ReserveAllocationMetrics( - `${path}:${key}`, + `${brand}`, BigInt(data.blockHeight), block.block.header.time as any, - extractBrand(allocation.__brand), + brand, key, BigInt(allocation.__value), reserveMetric.id @@ -38,29 +40,28 @@ export const reservesEventKit = (block: any, data: any, module: string, path: st return promises; } - async function saveReserveAllocationMetricDaily(payload: any, allocation: any, key: string): Promise[]> { + async function saveReserveAllocationMetricDaily(brand: string, payload: any, allocation: any, key: string): Promise { const dateKey = dateToDayKey(block.block.header.time); - let state = await getReserveAllocationMetricDaily(path, dateKey); + let state = await getReserveAllocationMetricDaily(brand, dateKey); - state.token = allocation.__brand; + state.denom = allocation.__brand; state.key = key; state.valueLast = BigInt(allocation.__value); - state.valueSum = (state.valueSum ?? BigInt(0)) + BigInt(allocation.__value); state.metricsCount = (state.metricsCount ?? BigInt(0)) + BigInt(1); - return [state.save()]; + return state.save(); } async function getReserveAllocationMetricDaily( - path: string, + brand: string, dateKey: number ): Promise { - const id = path + ":" + dateKey.toString(); + const id = brand + ":" + dateKey.toString(); let state = await ReserveAllocationMetricsDaily.get(id); if (!state) { state = new ReserveAllocationMetricsDaily( id, - path, + brand, dateKey, BigInt(data.blockHeight), new Date(block.block.header.time as any) diff --git a/src/mappings/events/vaults.ts b/src/mappings/events/vaults.ts index fc9204cf4..b825b412c 100644 --- a/src/mappings/events/vaults.ts +++ b/src/mappings/events/vaults.ts @@ -52,7 +52,7 @@ export const vaultsEventKit = (block: any, data: any, module: string, path: stri } vault.coin = payload?.locked?.__brand; - vault.token = payload?.locked?.__brand; + vault.denom = payload?.locked?.__brand; vault.debt = payload?.debtSnapshot?.debt?.__value; vault.balance = payload?.locked?.__value; vault.lockedValue = payload?.locked?.__value; @@ -121,33 +121,6 @@ export const vaultsEventKit = (block: any, data: any, module: string, path: stri state.totalProceedsReceivedLast = BigInt(payload.totalProceedsReceived.__value); state.totalShortfallReceivedLast = BigInt(payload.totalShortfallReceived.__value); - state.liquidatingCollateralValueSum = - (state.liquidatingCollateralValueSum ?? BigInt(0)) + BigInt(payload.liquidatingCollateral.__value); - state.liquidatingDebtValueSum = - (state.liquidatingDebtValueSum ?? BigInt(0)) + BigInt(payload.liquidatingDebt.__value); - state.lockedQuoteDenominatorSum = - (state.lockedQuoteDenominatorSum ?? BigInt(0)) + BigInt(payload.lockedQuote?.denominator.__value ?? 0); - state.lockedQuoteNumeratorSum = - (state.lockedQuoteNumeratorSum ?? BigInt(0)) + BigInt(payload.lockedQuote?.numerator.__value ?? 0); - state.numActiveVaultsSum = (state.numActiveVaultsSum ?? BigInt(0)) + BigInt(payload.numActiveVaults); - state.numLiquidatingVaultsSum = (state.numLiquidatingVaultsSum ?? BigInt(0)) + BigInt(payload.numLiquidatingVaults); - state.numLiquidationsAbortedSum = - (state.numLiquidationsAbortedSum ?? BigInt(0)) + BigInt(payload.numLiquidationsAborted); - state.numLiquidationsCompletedSum = - (state.numLiquidationsCompletedSum ?? BigInt(0)) + BigInt(payload.numLiquidationsCompleted); - state.retainedCollateralSum = - (state.retainedCollateralSum ?? BigInt(0)) + BigInt(payload.retainedCollateral.__value); - state.totalCollateralSum = (state.totalCollateralSum ?? BigInt(0)) + BigInt(payload.totalCollateral.__value); - state.totalCollateralSoldSum = - (state.totalCollateralSoldSum ?? BigInt(0)) + BigInt(payload.totalCollateralSold.__value); - state.totalDebtSum = (state.totalDebtSum ?? BigInt(0)) + BigInt(payload.totalDebt.__value); - state.totalOverageReceivedSum = - (state.totalOverageReceivedSum ?? BigInt(0)) + BigInt(payload.totalOverageReceived.__value); - state.totalProceedsReceivedSum = - (state.totalProceedsReceivedSum ?? BigInt(0)) + BigInt(payload.totalProceedsReceived.__value); - state.totalShortfallReceivedSum = - (state.totalShortfallReceivedSum ?? BigInt(0)) + BigInt(payload.totalShortfallReceived.__value); - state.metricsCount = (state.metricsCount ?? BigInt(0)) + BigInt(1); return [state.save()]; } diff --git a/src/mappings/mappingHandlers.ts b/src/mappings/mappingHandlers.ts index 891acd389..0c1efba6a 100644 --- a/src/mappings/mappingHandlers.ts +++ b/src/mappings/mappingHandlers.ts @@ -1,31 +1,12 @@ import { - Message, - TransferEvent, StateChangeEvent, - OraclePrice, - OraclePriceDaily, - PsmMetric, - PsmGovernance, - Wallet, - Vault, - VaultManagerMetrics, - VaultManagerGovernance, - ReserveMetrics, - ReserveAllocationMetrics, - BoardAux, - VaultManagerMetricsDaily, - PsmMetricDaily, - ReserveAllocationMetricsDaily, } from "../types"; import { CosmosEvent } from "@subql/types-cosmos"; import { - b64encode, b64decode, extractStoragePath, getStateChangeModule, - extractBrand, resolveBrandNamesAndValues, - dateToDayKey, } from "./utils"; import {