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

feat: add oraclePrice and goverance data to liquidated vaults #20

Merged
merged 4 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 12 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ type Vault @entity {
wallet: Wallet!
}

type VaultManagerGovernanceJson @jsonField {
liquidationMarginDenominator: BigInt
liquidationMarginNumerator: BigInt
}

type OraclePriceJson @jsonField {
typeInAmount: BigInt
typeOutAmount: BigInt
}

type VaultLiquidation @entity {
id: ID!
blockHeight: BigInt!
Expand All @@ -152,6 +162,8 @@ type VaultLiquidation @entity {
wallet: Wallet!
currentState: Vault!
liquidatingState: VaultLiquidation!
vaultManagerGovernance: VaultManagerGovernanceJson
oraclePrice: OraclePriceJson
}

type VaultManagerMetrics @entity {
Expand Down
26 changes: 24 additions & 2 deletions src/mappings/events/vaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Wallet,
Vault,
VaultLiquidation,
OraclePrice,
} from '../../types';
import { VAULT_STATES } from '../constants';
import { dateToDayKey, extractBrand } from '../utils';
Expand Down Expand Up @@ -77,6 +78,9 @@ export const vaultsEventKit = (block: any, data: any, module: string, path: stri
async function saveVaultsLiquidation(payload: any): Promise<any> {
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(
Expand All @@ -89,8 +93,26 @@ export const vaultsEventKit = (block: any, data: any, module: string, path: stri
);
}

vault.coin = payload?.locked?.__brand;
vault.denom = payload?.locked?.__brand;
const vaultGovernanceId = id.split('.').slice(0, 4).join('.') + '.governance';
const vaultManagerGovernance = await VaultManagerGovernance.get(vaultGovernanceId);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should add optional chaining on line 96:

const vaultGovernanceId = id?.split('.')?.slice(0, 4).join('.') + '.governance';

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

id here should exist in all cases. if we put optional chaining here, we will get an invalid id. crashing in case the id format is invalid would be the preferred option here

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh, I see. I reckon it's the we're using the id declared on line 79?
Do you think we should have some sort of validation for id over there?
It's possible that id can have the value undefined-null.governance when path and payload?.vaultState have falsy values.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function is only triggered from line: src/mappings/mappingHandlers.ts:214 where we perform a regex check on the path. so there is no way it is falsy.
in any case i've added a regex check just to be safe


const oraclPriceId = `${denom}-USD`;
const oraclePrice = await OraclePrice.get(oraclPriceId);

if (vaultManagerGovernance && vault.vaultManagerGovernance === undefined)
vault.vaultManagerGovernance = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also handle the case when vaultManagerGovernance is an empty object. Perhaps add some fallback values?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vaultManagerGovernace is not a json object object, but rather a class object. there is no empty state for it

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;
vault.debt = payload?.debtSnapshot?.debt?.__value;
vault.balance = payload?.locked?.__value;
vault.lockedValue = payload?.locked?.__value;
Expand Down