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: added vaultStateDaily entity to save data for daily stats #21

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,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,
OraclePrice,
} from '../../types';
import { VAULT_STATES } from '../constants';
Expand Down Expand Up @@ -54,8 +54,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();
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is yesterday's date? Is it the one we have on line 66? or does it become yesterday's date on line 67?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it becomes yesterdays date after line 67. unfortunately i dont have a better way of conveying this info

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',
};

Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you think we should add propertyMap and closedPropertyMap in constants.js?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

its very specific to this function and i dont think it will be reused anywhere

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

Choose a reason for hiding this comment

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

Why as assertion? Why not convert oldProperty to big int?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the issue here is that typescript warns me about indexing a class object with a string. which is physically possible but the type wont allow it.
therefore i have to convert the objects type to any before indexing it

Copy link
Collaborator

Choose a reason for hiding this comment

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

Goal is to compare big int with big int? this doesn't work? if (BigInt(oldProperty) === BigInt(0)) ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

oldProperty is the name of the propert e.g "active", "liquidated" etc

we index it from "vaultState" to get the actual BigInt value

}
(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 @@ -72,7 +148,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()];
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not sure but will it wait for .save() calls to complete? or should we use await?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the returned array is awaited in the Calling function. so we dont have to handle it here


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