From 8dcbfba6dc846bfc9a5b2f9ac169e0de161d0309 Mon Sep 17 00:00:00 2001 From: Fraz Arshad Date: Tue, 14 May 2024 11:15:17 +0500 Subject: [PATCH 1/4] feat: added vaultManagerGovernance added to liquidated vaults --- schema.graphql | 6 ++++++ src/mappings/events/vaults.ts | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/schema.graphql b/schema.graphql index ee51c0eb..d47d34fc 100644 --- a/schema.graphql +++ b/schema.graphql @@ -139,6 +139,11 @@ type Vault @entity { wallet: Wallet! } +type VaultManagerGovernanceJson @jsonField { + liquidationMarginDenominator: BigInt + liquidationMarginNumerator: BigInt +} + type VaultLiquidation @entity { id: ID! blockHeight: BigInt! @@ -152,6 +157,7 @@ type VaultLiquidation @entity { wallet: Wallet! currentState: Vault! liquidatingState: VaultLiquidation! + vaultManagerGovernance: VaultManagerGovernanceJson } type VaultManagerMetrics @entity { diff --git a/src/mappings/events/vaults.ts b/src/mappings/events/vaults.ts index 00535679..922f69a8 100644 --- a/src/mappings/events/vaults.ts +++ b/src/mappings/events/vaults.ts @@ -89,6 +89,16 @@ export const vaultsEventKit = (block: any, data: any, module: string, path: stri ); } + const vaultGovernanceId = id.split('.').slice(0, 4).join('.') + '.governance'; + const vaultManagerGovernance = await VaultManagerGovernance.get(vaultGovernanceId); + + if (vaultManagerGovernance && payload?.vaultState === 'liquidated' && vault.vaultManagerGovernance === undefined) { + vault.vaultManagerGovernance = { + liquidationMarginNumerator: vaultManagerGovernance?.liquidationMarginNumerator, + liquidationMarginDenominator: vaultManagerGovernance?.liquidationMarginDenominator, + }; + } + vault.coin = payload?.locked?.__brand; vault.denom = payload?.locked?.__brand; vault.debt = payload?.debtSnapshot?.debt?.__value; From 1cb5e760d92060e2b3076b5161c0147c8d1ea424 Mon Sep 17 00:00:00 2001 From: Fraz Arshad Date: Tue, 14 May 2024 12:35:24 +0500 Subject: [PATCH 2/4] feat: oraclePrice added to liquidated vaults --- schema.graphql | 6 ++++++ src/mappings/events/vaults.ts | 28 +++++++++++++++++++++------- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/schema.graphql b/schema.graphql index d47d34fc..8dfd20aa 100644 --- a/schema.graphql +++ b/schema.graphql @@ -144,6 +144,11 @@ type VaultManagerGovernanceJson @jsonField { liquidationMarginNumerator: BigInt } +type OraclePriceJson @jsonField { + typeInAmount: BigInt + typeOutAmount: BigInt +} + type VaultLiquidation @entity { id: ID! blockHeight: BigInt! @@ -158,6 +163,7 @@ type VaultLiquidation @entity { currentState: Vault! liquidatingState: VaultLiquidation! vaultManagerGovernance: VaultManagerGovernanceJson + oraclePrice: OraclePriceJson } type VaultManagerMetrics @entity { diff --git a/src/mappings/events/vaults.ts b/src/mappings/events/vaults.ts index 922f69a8..7899e059 100644 --- a/src/mappings/events/vaults.ts +++ b/src/mappings/events/vaults.ts @@ -6,6 +6,7 @@ import { Wallet, Vault, VaultLiquidation, + OraclePrice, } from '../../types'; import { VAULT_STATES } from '../constants'; import { dateToDayKey, extractBrand } from '../utils'; @@ -77,6 +78,9 @@ export const vaultsEventKit = (block: any, data: any, module: string, path: stri async function saveVaultsLiquidation(payload: any): Promise { const id = `${path}-${payload?.vaultState}`; const liquidatingId = `${path}-${VAULT_STATES.LIQUIDATING}`; + + const denom = payload?.locked?.__brand; + let vault = await VaultLiquidation.get(id); if (!vault) { vault = new VaultLiquidation( @@ -92,15 +96,25 @@ export const vaultsEventKit = (block: any, data: any, module: string, path: stri const vaultGovernanceId = id.split('.').slice(0, 4).join('.') + '.governance'; const vaultManagerGovernance = await VaultManagerGovernance.get(vaultGovernanceId); - if (vaultManagerGovernance && payload?.vaultState === 'liquidated' && vault.vaultManagerGovernance === undefined) { - vault.vaultManagerGovernance = { - liquidationMarginNumerator: vaultManagerGovernance?.liquidationMarginNumerator, - liquidationMarginDenominator: vaultManagerGovernance?.liquidationMarginDenominator, - }; + const oraclPriceId = `${denom}-USD`; + const oraclePrice = await OraclePrice.get(oraclPriceId); + + if (payload?.vaultState === 'liquidated') { + if (vaultManagerGovernance && vault.vaultManagerGovernance === undefined) + vault.vaultManagerGovernance = { + liquidationMarginNumerator: vaultManagerGovernance.liquidationMarginNumerator, + liquidationMarginDenominator: vaultManagerGovernance.liquidationMarginDenominator, + }; + + if (oraclePrice && vault.oraclePrice === undefined) + vault.oraclePrice = { + typeInAmount: oraclePrice.typeInAmount, + typeOutAmount: oraclePrice.typeOutAmount, + }; } - vault.coin = payload?.locked?.__brand; - vault.denom = payload?.locked?.__brand; + vault.coin = denom; + vault.denom = denom; vault.debt = payload?.debtSnapshot?.debt?.__value; vault.balance = payload?.locked?.__value; vault.lockedValue = payload?.locked?.__value; From a72a70add288aff28bf7b5c6bedaac13dbbf36a6 Mon Sep 17 00:00:00 2001 From: Fraz Arshad Date: Tue, 14 May 2024 17:24:08 +0500 Subject: [PATCH 3/4] fix: save data for liquidated and liquidating state --- src/mappings/events/vaults.ts | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/mappings/events/vaults.ts b/src/mappings/events/vaults.ts index 7899e059..489cfcc1 100644 --- a/src/mappings/events/vaults.ts +++ b/src/mappings/events/vaults.ts @@ -99,19 +99,17 @@ export const vaultsEventKit = (block: any, data: any, module: string, path: stri const oraclPriceId = `${denom}-USD`; const oraclePrice = await OraclePrice.get(oraclPriceId); - if (payload?.vaultState === 'liquidated') { - if (vaultManagerGovernance && vault.vaultManagerGovernance === undefined) - vault.vaultManagerGovernance = { - liquidationMarginNumerator: vaultManagerGovernance.liquidationMarginNumerator, - liquidationMarginDenominator: vaultManagerGovernance.liquidationMarginDenominator, - }; - - if (oraclePrice && vault.oraclePrice === undefined) - vault.oraclePrice = { - typeInAmount: oraclePrice.typeInAmount, - typeOutAmount: oraclePrice.typeOutAmount, - }; - } + if (vaultManagerGovernance && vault.vaultManagerGovernance === undefined) + vault.vaultManagerGovernance = { + liquidationMarginNumerator: vaultManagerGovernance.liquidationMarginNumerator, + liquidationMarginDenominator: vaultManagerGovernance.liquidationMarginDenominator, + }; + + if (oraclePrice && vault.oraclePrice === undefined) + vault.oraclePrice = { + typeInAmount: oraclePrice.typeInAmount, + typeOutAmount: oraclePrice.typeOutAmount, + }; vault.coin = denom; vault.denom = denom; From 004753d2469a8dd6dd0e3c5a69ac7cb2f5ede3e8 Mon Sep 17 00:00:00 2001 From: Fraz Arshad Date: Wed, 15 May 2024 14:35:36 +0500 Subject: [PATCH 4/4] chore: updated id parse method --- src/mappings/events/vaults.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/mappings/events/vaults.ts b/src/mappings/events/vaults.ts index 489cfcc1..9049643a 100644 --- a/src/mappings/events/vaults.ts +++ b/src/mappings/events/vaults.ts @@ -93,7 +93,12 @@ export const vaultsEventKit = (block: any, data: any, module: string, path: stri ); } - const vaultGovernanceId = id.split('.').slice(0, 4).join('.') + '.governance'; + const pathRegex = /^(published\.vaultFactory\.managers\.manager[0-9]+)\.vaults\.vault[0-9]+$/ + const pathRegexMatch = path.match(pathRegex); + if (!pathRegexMatch) { + throw new Error('path format is invalid'); + } + const vaultGovernanceId = pathRegexMatch[1] + '.governance'; const vaultManagerGovernance = await VaultManagerGovernance.get(vaultGovernanceId); const oraclPriceId = `${denom}-USD`;