Skip to content

Commit

Permalink
feat: added vaultStateDaily entity
Browse files Browse the repository at this point in the history
  • Loading branch information
frazarshad committed May 15, 2024
1 parent c09906b commit 880ff9f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
11 changes: 11 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,14 @@ type IBCTransfer @entity {
amount: String!
transferType: TransferType!
}

type VaultStatesDaily @entity {
id: ID!
blockHeightLast: BigInt!
blockTimeLast: Date!
active: BigInt!
closed: BigInt!
liquidating: BigInt!
liquidated: BigInt!
liquidatedClosed: BigInt!
}
2 changes: 2 additions & 0 deletions src/mappings/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export const EVENT_TYPES = {
};

export const VAULT_STATES = {
ACTIVE: 'active',
CLOSED: 'closed',
LIQUIDATING: 'liquidating',
LIQUIDATED: 'liquidated',
};
Expand Down
80 changes: 78 additions & 2 deletions src/mappings/events/vaults.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { P } from 'pino';
import {
VaultManagerMetrics,
VaultManagerMetricsDaily,
VaultManagerGovernance,
Wallet,
Vault,
VaultLiquidation,
VaultStatesDaily,
} from '../../types';
import { VAULT_STATES } from '../constants';
import { dateToDayKey, extractBrand } from '../utils';
Expand Down Expand Up @@ -53,8 +53,84 @@ export const vaultsEventKit = (block: any, data: any, module: string, path: stri
return promises;
}

async function updateDailyVaultState(
oldState: string | undefined,
newState: string,
blockTime: Date,
blockHeight: number,
): Promise<VaultStatesDaily> {
const dateKey = dateToDayKey(blockTime).toString();
let vaultState: VaultStatesDaily | undefined = await VaultStatesDaily.get(dateKey);

if (!vaultState) {
const yesterdayDate = new Date(blockTime);
yesterdayDate.setDate(yesterdayDate.getDate() - 1);
const yesterdayDateKey = dateToDayKey(yesterdayDate).toString();
const yesterdayVaultState: VaultStatesDaily | undefined = await VaultStatesDaily.get(yesterdayDateKey);

if (yesterdayVaultState) {
vaultState = new VaultStatesDaily(
dateKey,
BigInt(blockHeight),
blockTime,
yesterdayVaultState.active,
yesterdayVaultState.closed,
yesterdayVaultState.liquidating,
yesterdayVaultState.liquidated,
yesterdayVaultState.liquidatedClosed,
);
} else {
vaultState = new VaultStatesDaily(
dateKey,
BigInt(blockHeight),
blockTime,
BigInt(0),
BigInt(0),
BigInt(0),
BigInt(0),
BigInt(0),
);
}
}

const propertyMap = {
[VAULT_STATES.ACTIVE]: 'active',
[VAULT_STATES.LIQUIDATED]: 'liquidated',
[VAULT_STATES.LIQUIDATING]: 'liquidating',
[VAULT_STATES.CLOSED]: 'closed',
};

const closedPropertyMap = {
[VAULT_STATES.ACTIVE]: 'closed',
[VAULT_STATES.LIQUIDATED]: 'liquidatedClosed',
};

if (oldState) {
const oldProperty = propertyMap[oldState];
if ((vaultState as any)[oldProperty] === BigInt(0)) {
throw Error(oldState + ' vaults are 0. cannot subtract more');
}
(vaultState as any)[oldProperty] -= BigInt(1);
}

const newProperty =
newState === VAULT_STATES.CLOSED && oldState ? closedPropertyMap[oldState] : propertyMap[newState];
(vaultState as any)[newProperty] += BigInt(1);

vaultState.blockHeightLast = BigInt(blockHeight);
vaultState.blockTimeLast = blockTime;

return vaultState;
}

async function saveVaults(payload: any): Promise<Promise<any>[]> {
let vault = await Vault.get(path);
const dailyVaultState = await updateDailyVaultState(
vault?.state,
payload?.vaultState,
block.block.header.time,
data.blockHeight,
);
if (!vault) {
vault = new Vault(path, BigInt(data.blockHeight), block.block.header.time as any, '');
}
Expand All @@ -71,7 +147,7 @@ export const vaultsEventKit = (block: any, data: any, module: string, path: stri
liquidation = await saveVaultsLiquidation(payload);
}

return [liquidation, vault.save()];
return [liquidation, vault.save(), dailyVaultState.save()];
}

async function saveVaultsLiquidation(payload: any): Promise<any> {
Expand Down

0 comments on commit 880ff9f

Please sign in to comment.