Skip to content

Commit

Permalink
Make naming consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
toliaqat committed May 5, 2024
1 parent 98f02f4 commit 306ea0b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 117 deletions.
47 changes: 12 additions & 35 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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
Expand All @@ -69,46 +67,41 @@ 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!
totalAnchorProvided: BigInt!
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
}

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!
Expand Down Expand Up @@ -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!
Expand Down Expand Up @@ -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
}
Expand All @@ -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!
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion src/mappings/events/boardAux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down
18 changes: 8 additions & 10 deletions src/mappings/events/priceFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<any> {
async function saveOraclePriceDaily(id: string, payload: any, typeInName: string, typeOutName: string): Promise<any> {
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);
Expand All @@ -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<OraclePriceDaily> {
const id = path + ":" + dateKey.toString();
async function getOraclePriceDaily(feedName: string, dateKey: number): Promise<OraclePriceDaily> {
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)
Expand Down
19 changes: 6 additions & 13 deletions src/mappings/events/psm.ts
Original file line number Diff line number Diff line change
@@ -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<Promise<any>[]> {
const psmMetricDaily = savePsmMetricDaily(payload);
const psmMetric = new PsmMetric(
const psmMetric = new PsmMetrics(
path,
BigInt(data.blockHeight),
block.block.header.time as any,
Expand All @@ -25,31 +25,24 @@ 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);
state.mintedPoolBalanceLast = BigInt(payload.mintedPoolBalance.__value);
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<PsmMetricDaily> {
async function getPsmMetricDaily(dateKey: number): Promise<PsmMetricsDaily> {
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;
}
Expand Down
23 changes: 12 additions & 11 deletions src/mappings/events/reserves.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Promise<any>[]> {
async function saveReserveAllocationMetricDaily(brand: string, payload: any, allocation: any, key: string): Promise<any> {
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<ReserveAllocationMetricsDaily> {
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)
Expand Down
29 changes: 1 addition & 28 deletions src/mappings/events/vaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()];
}
Expand Down
19 changes: 0 additions & 19 deletions src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit 306ea0b

Please sign in to comment.