-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
|
@@ -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(); | ||
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', | ||
}; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think we should add There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, ''); | ||
} | ||
|
@@ -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()]; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure but will it wait for There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> { | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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