Skip to content

Commit

Permalink
fix: updated vault yesterday vault state logic
Browse files Browse the repository at this point in the history
  • Loading branch information
frazarshad committed May 29, 2024
1 parent 49d5bd3 commit 14a322d
Showing 1 changed file with 20 additions and 35 deletions.
55 changes: 20 additions & 35 deletions src/mappings/events/vaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,39 +59,20 @@ export const vaultsEventKit = (block: any, data: any, module: string, path: stri
newState: string,
blockTime: Date,
blockHeight: number,
): Promise<VaultStatesDaily> {
const dateKey = dateToDayKey(blockTime).toString();
let vaultState: VaultStatesDaily | undefined = await VaultStatesDaily.get(dateKey);
): Promise<[VaultStatesDaily, VaultStatesDaily]> {
let vaultState: VaultStatesDaily | undefined = await VaultStatesDaily.get('latest');

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),
);
}
vaultState = new VaultStatesDaily(
'latest',
BigInt(blockHeight),
blockTime,
BigInt(0),
BigInt(0),
BigInt(0),
BigInt(0),
BigInt(0),
);
}

const propertyMap = {
Expand All @@ -113,17 +94,21 @@ export const vaultsEventKit = (block: any, data: any, module: string, path: stri
(vaultState as any)[oldProperty] -= BigInt(1);
}

if ((newState && propertyMap[newState])) {
if (newState && propertyMap[newState]) {
const newProperty = propertyMap[newState];
(vaultState as any)[newProperty] += BigInt(1);
}

return vaultState;
const dateKey = dateToDayKey(blockTime).toString();
const vaultStateToday = VaultStatesDaily.create(vaultState);
vaultStateToday.id = dateKey;

return [vaultState, vaultStateToday];
}

async function saveVaults(payload: any): Promise<Promise<any>[]> {
let vault = await Vault.get(path);
const dailyVaultState = await updateVaultStatesDaily(
const [latestVaultState, dailyVaultState] = await updateVaultStatesDaily(
vault?.state,
payload?.vaultState,
block.block.header.time,
Expand All @@ -145,7 +130,7 @@ export const vaultsEventKit = (block: any, data: any, module: string, path: stri
liquidation = await saveVaultsLiquidation(payload);
}

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

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

0 comments on commit 14a322d

Please sign in to comment.