Skip to content

Commit

Permalink
logging cash amounts into vault
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffywu committed Aug 22, 2022
1 parent 5973fbb commit 88a27bf
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
3 changes: 3 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,9 @@ type LeveragedVaultTrade @entity {
secondaryDebtSharesBefore: [BigInt!]
secondaryDebtSharesAfter: [BigInt!]
netSecondaryDebtSharesChange: [BigInt!]

"Positive cash into vault, negative amount of cash when exiting, liquidation cash amounts not logged"
netUnderlyingCash: BigInt
}

type LeveragedVaultMaturityEvent @entity {
Expand Down
41 changes: 20 additions & 21 deletions src/vaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@ import {
ProtocolInsolvency,
VaultBorrowCapacityChange,
VaultDeleverageAccount,
VaultEnterPosition,
VaultExitPostMaturity,
VaultExitPreMaturity,
VaultFeeAccrued,
VaultLiquidatorProfit,
VaultMintStrategyToken,
VaultRedeemStrategyToken,
VaultRepaySecondaryBorrow,
VaultRollPosition,
VaultSecondaryBorrow,
VaultSecondaryBorrowSnapshot,
VaultShortfall,
VaultStateUpdate,
VaultUpdateSecondaryBorrowCapacity,
VaultEnterMaturity,
} from "../generated/NotionalVaults/Notional"
import { IStrategyVault } from "../generated/NotionalVaults/IStrategyVault"
import {
Expand Down Expand Up @@ -148,7 +147,8 @@ function setVaultTrade(
accountBefore: LeveragedVaultAccount,
accountAfter: LeveragedVaultAccount,
vaultTradeType: string,
event: ethereum.Event
event: ethereum.Event,
netUnderlyingCash: BigInt | null
): void {
let id =
vault +
Expand Down Expand Up @@ -210,6 +210,8 @@ function setVaultTrade(
entity.netSecondaryDebtSharesChange = netSecondaryDebtSharesChange
}

entity.netUnderlyingCash = netUnderlyingCash;

entity.save()
}

Expand Down Expand Up @@ -462,14 +464,22 @@ function updateVaultState(vault: LeveragedVault, maturity: BigInt, event: ethere
vaultMaturity.save()
}

export function handleVaultEnterPosition(event: VaultEnterPosition): void {
export function handleVaultEnterMaturity(event: VaultEnterMaturity): void {
let vault = getVault(event.params.vault.toHexString())
let accountBefore = getVaultAccount(vault.id, event.params.account.toHexString())
updateVaultMarkets(vault, event)
updateNTokenPortfolio(getNToken(vault.primaryBorrowCurrency), event, null)
let accountAfter = updateVaultAccount(vault, event.params.account, event)
// TODO: VaultEnterMaturity has more metadata not available here...
setVaultTrade(vault.id, accountBefore, accountAfter, "EnterPosition", event)

let tradeType: string;
if (accountBefore.maturity == accountAfter.maturity) {
tradeType = "EnterPosition"
} else {
tradeType = "RollPosition"
}


setVaultTrade(vault.id, accountBefore, accountAfter, tradeType, event, event.params.underlyingTokensTransferred)
}

export function handleVaultExitPreMaturity(event: VaultExitPreMaturity): void {
Expand All @@ -478,25 +488,14 @@ export function handleVaultExitPreMaturity(event: VaultExitPreMaturity): void {
// No nToken Fee to update
updateVaultMarkets(vault, event)
let accountAfter = updateVaultAccount(vault, event.params.account, event)
setVaultTrade(vault.id, accountBefore, accountAfter, "ExitPreMaturity", event)
}

export function handleVaultRollPosition(event: VaultRollPosition): void {
let vault = getVault(event.params.vault.toHexString())
let accountBefore = getVaultAccount(vault.id, event.params.account.toHexString())
updateVaultMarkets(vault, event)
updateNTokenPortfolio(getNToken(vault.primaryBorrowCurrency), event, null)
let accountAfter = updateVaultAccount(vault, event.params.account, event)
// TODO: VaultEnterMaturity has more metadata not available here, we should attempt to detect
// if this is a roll position or enter position based on the previous maturity
setVaultTrade(vault.id, accountBefore, accountAfter, "RollPosition", event)
setVaultTrade(vault.id, accountBefore, accountAfter, "ExitPreMaturity", event, null)
}

export function handleVaultExitPostMaturity(event: VaultExitPostMaturity): void {
let vault = getVault(event.params.vault.toHexString())
let accountBefore = getVaultAccount(vault.id, event.params.account.toHexString())
let accountAfter = updateVaultAccount(vault, event.params.account, event)
setVaultTrade(vault.id, accountBefore, accountAfter, "ExitPostMaturity", event)
setVaultTrade(vault.id, accountBefore, accountAfter, "ExitPostMaturity", event, null)
}

export function handleVaultStateUpdate(event: VaultStateUpdate): void {
Expand Down Expand Up @@ -539,15 +538,15 @@ export function handleDeleverageAccount(event: VaultDeleverageAccount): void {
let vault = getVault(event.params.vault.toHexString())
let accountBefore = getVaultAccount(vault.id, event.params.account.toHexString())
let accountAfter = updateVaultAccount(vault, event.params.account, event)
setVaultTrade(vault.id, accountBefore, accountAfter, "DeleverageAccount", event)
setVaultTrade(vault.id, accountBefore, accountAfter, "DeleverageAccount", event, null)
}

export function handleUpdateLiquidator(event: VaultLiquidatorProfit): void {
if (event.params.transferSharesToLiquidator) {
let vault = getVault(event.params.vault.toHexString())
let accountBefore = getVaultAccount(vault.id, event.params.liquidator.toHexString())
let accountAfter = updateVaultAccount(vault, event.params.liquidator, event)
setVaultTrade(vault.id, accountBefore, accountAfter, "TransferFromDeleverage", event)
setVaultTrade(vault.id, accountBefore, accountAfter, "TransferFromDeleverage", event, null)
}
}

Expand Down
4 changes: 2 additions & 2 deletions subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ dataSources:
- event: VaultUpdateSecondaryBorrowCapacity(indexed address,indexed uint16,uint80)
handler: handleVaultUpdateSecondaryBorrowCapacity
# Account Entry Events
- event: VaultEnterPosition(indexed address,indexed address,indexed uint256,uint256)
handler: handleVaultEnterPosition
- event: VaultEnterMaturity(indexed address,indexed uint256,indexed address,uint256,uint256,uint256)
handler: handleVaultEnterMaturity
- event: VaultSecondaryBorrow(indexed address,indexed address,indexed uint16,uint256,uint256,uint256)
handler: handleVaultSecondaryBorrow
- event: VaultFeeAccrued(indexed address,indexed uint16,indexed uint256,int256,int256)
Expand Down

0 comments on commit 88a27bf

Please sign in to comment.