Skip to content

Commit

Permalink
Merge branch 'main' into feat/vault-state-entity
Browse files Browse the repository at this point in the history
  • Loading branch information
frazarshad authored May 15, 2024
2 parents 880ff9f + f2232e6 commit 4b50374
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
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
31 changes: 29 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 {
Vault,
VaultLiquidation,
VaultStatesDaily,
OraclePrice,
} from '../../types';
import { VAULT_STATES } from '../constants';
import { dateToDayKey, extractBrand } from '../utils';
Expand Down Expand Up @@ -153,6 +154,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 @@ -165,8 +169,31 @@ export const vaultsEventKit = (block: any, data: any, module: string, path: stri
);
}

vault.coin = payload?.locked?.__brand;
vault.denom = payload?.locked?.__brand;
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`;
const oraclePrice = await OraclePrice.get(oraclPriceId);

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

0 comments on commit 4b50374

Please sign in to comment.