From d603d8f9c943a7c9fdda4ca319aec8642325f0b4 Mon Sep 17 00:00:00 2001 From: Touseef Liaqat Date: Sat, 4 May 2024 12:19:24 -0700 Subject: [PATCH] Add vault state daily metrics --- schema.graphql | 12 +++++++++ src/mappings/constants.ts | 2 ++ src/mappings/events/vaults.ts | 46 +++++++++++++++++++++++++++++++++-- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/schema.graphql b/schema.graphql index d06eac05..21b47f65 100644 --- a/schema.graphql +++ b/schema.graphql @@ -146,7 +146,19 @@ type Vault @entity { wallet: Wallet! liquidatingAt: Date liquidatedAt: Date + liquidating: Boolean liquidated: Boolean + active: Boolean + closed: Boolean +} + +type VaultsMetricsDaily @entity { + id: ID! + dateKey: Int! @index # YYYYMMDD format + numActiveVaultsLast: BigInt + numClosedVaultsLast: BigInt + numLiquidatingVaultsLast: BigInt + numLiquidatedVaultsLast: BigInt } type VaultManagerMetrics @entity { diff --git a/src/mappings/constants.ts b/src/mappings/constants.ts index 465b9302..35ea41a9 100644 --- a/src/mappings/constants.ts +++ b/src/mappings/constants.ts @@ -20,6 +20,8 @@ export const EVENT_TYPES = { }; export const VAULT_STATES = { + ACTIVE: "active", + CLOSED: "closed", LIQUIDATING: "liquidating", LIQUIDATED: "liquidated" } diff --git a/src/mappings/events/vaults.ts b/src/mappings/events/vaults.ts index fc9204cf..c9f65f34 100644 --- a/src/mappings/events/vaults.ts +++ b/src/mappings/events/vaults.ts @@ -1,4 +1,4 @@ -import { VaultManagerMetrics, VaultManagerMetricsDaily, VaultManagerGovernance, Wallet, Vault } from "../../types"; +import { VaultsMetricsDaily, VaultManagerMetrics, VaultManagerMetricsDaily, VaultManagerGovernance, Wallet, Vault } from "../../types"; import { VAULT_STATES } from "../constants"; import { dateToDayKey, extractBrand } from "../utils"; @@ -47,6 +47,12 @@ export const vaultsEventKit = (block: any, data: any, module: string, path: stri async function saveVaults(payload: any): Promise[]> { let vault = await Vault.get(path); + let active = 0; + let closed = 0; + let liquidating = 0; + let liquidated = 0; + + if (!vault) { vault = new Vault(path, BigInt(data.blockHeight), block.block.header.time as any, ""); } @@ -60,13 +66,49 @@ export const vaultsEventKit = (block: any, data: any, module: string, path: stri if (vault.state === VAULT_STATES.LIQUIDATING && !vault.liquidatingAt) { vault.liquidatingAt = block.block.header.time; + vault.liquidating = true; + liquidating = 1; } if (vault.state === VAULT_STATES.LIQUIDATED && !vault.liquidatedAt) { vault.liquidatedAt = block.block.header.time; vault.liquidated = true; + liquidated = 1; } - return [vault.save()]; + + if (vault.state === VAULT_STATES.ACTIVE && !vault.active) { + vault.active = true; + active = 1; + } + + if (vault.state === VAULT_STATES.CLOSED && !vault.closed) { + vault.closed = true; + closed = 1; + } + + const metrics = saveVaultsMetricsDaily(active, closed, liquidated, liquidating); + + return [metrics, vault.save()]; + } + + async function saveVaultsMetricsDaily(active: number, closed: number, liquidated: number, liquidating: number): Promise { + const dateKey = dateToDayKey(block.block.header.time); + const metrics = await getVaultsMetricsDaily(dateKey); + metrics.numActiveVaultsLast = BigInt(active) + BigInt(metrics.numActiveVaultsLast ?? 0); + metrics.numClosedVaultsLast = BigInt(closed) + BigInt(metrics.numClosedVaultsLast ?? 0); + metrics.numLiquidatedVaultsLast = BigInt(liquidated) + BigInt(metrics.numLiquidatedVaultsLast ?? 0); + metrics.numLiquidatingVaultsLast = BigInt(liquidating) + BigInt(metrics.numLiquidatingVaultsLast ?? 0); + return metrics.save(); + } + + async function getVaultsMetricsDaily(dateKey: number): Promise { + const id = `vaults:${dateKey}`; + let state = await VaultsMetricsDaily.get(id); + if (!state) { + state = new VaultsMetricsDaily(id, dateKey); + return state; + } + return state; } async function saveVaultManagerMetrics(payload: any): Promise[]> {