Skip to content

Commit

Permalink
Fix vault transfer bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed May 11, 2024
1 parent 6d20f53 commit c664e1a
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 35 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,6 @@ yarn test:lint # run prettier linter
- vault.strategy()
- vault.wants()
- vault: Initialized(uint8)
- vault: Deposit(indexed address,uint256,uint256,uint256)
- vault: Withdraw(indexed address,uint256,uint256,uint256)
- vault: Transfer(indexed address,indexed address,uint256)
- strategyFactory: GlobalPause(bool)
Expand Down
2 changes: 0 additions & 2 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,6 @@ enum InvestorPositionInteractionType {
DEPOSIT
"The investor withdrew funds from the vault"
WITHDRAW
"The investor transferred funds from one vault to another vault"
TRANSFER
"Boosts are given to investors in the form of a dedicated incentive token"
BOOST_START
"The investor claimed a boost"
Expand Down
4 changes: 2 additions & 2 deletions src/clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getClockTick } from "./entity/clock"
import { getBeefyCLProtocol } from "./entity/protocol"
import { getToken } from "./entity/token"
import { fetchVaultLatestData } from "./utils/price"
import { getBeefyCLStrategy, getBeefyCLVaultSnapshot, isVaultRunning } from "./entity/vault"
import { getBeefyCLStrategy, getBeefyCLVaultSnapshot, isVaultInitialized } from "./entity/vault"

export function handleClockTick(block: ethereum.Block): void {
const timestamp = block.timestamp
Expand All @@ -26,7 +26,7 @@ function updateDataOnClockTick(tick: ClockTick): void {

for (let i = 0; i < vaults.length; i++) {
const vault = vaults[i]
if (!isVaultRunning(vault)) {
if (!isVaultInitialized(vault)) {
continue
}

Expand Down
4 changes: 4 additions & 0 deletions src/entity/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export function isVaultRunning(vault: BeefyCLVault): boolean {
return vault.lifecycle == BEEFY_CL_VAULT_LIFECYCLE_RUNNING
}

export function isVaultInitialized(vault: BeefyCLVault): boolean {
return vault.lifecycle != BEEFY_CL_VAULT_LIFECYCLE_INITIALIZING
}

export function isNewVault(vault: BeefyCLVault): boolean {
return vault.sharesToken.equals(ADDRESS_ZERO)
}
Expand Down
2 changes: 0 additions & 2 deletions src/mapping/vault.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export { handleVaultInitialized as handleInitialized } from "../vault-lifecycle"
export { handleVaultDeposit as handleDeposit } from "../vault-interaction"
export { handleVaultWithdraw as handleWithdraw } from "../vault-interaction"
export { handleVaultTransfer as handleTransfer } from "../vault-interaction"
6 changes: 3 additions & 3 deletions src/vault-compound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
Harvest as HarvestEvent,
ClaimedOutput as ClaimedOutputEvent,
} from "../generated/templates/BeefyCLStrategy/BeefyStrategy"
import { getBeefyCLStrategy, getBeefyCLVault, getBeefyCLVaultSnapshot, isVaultRunning } from "./entity/vault"
import { getBeefyCLStrategy, getBeefyCLVault, getBeefyCLVaultSnapshot, isVaultInitialized } from "./entity/vault"
import { getTransaction } from "./entity/transaction"
import { BeefyCLVaultHarvestEvent, BeefyCLVaultUnderlyingFeesCollectedEvent } from "../generated/schema"
import { getEventIdentifier } from "./utils/event"
Expand All @@ -16,7 +16,7 @@ import { fetchVaultLatestData } from "./utils/price"
export function handleStrategyHarvest(event: HarvestEvent): void {
let strategy = getBeefyCLStrategy(event.address)
let vault = getBeefyCLVault(strategy.vault)
if (!isVaultRunning(vault)) {
if (!isVaultInitialized(vault)) {
return
}

Expand Down Expand Up @@ -69,7 +69,7 @@ function handleStrategyFees(
): void {
let strategy = getBeefyCLStrategy(event.address)
let vault = getBeefyCLVault(strategy.vault)
if (!isVaultRunning(vault)) {
if (!isVaultInitialized(vault)) {
return
}

Expand Down
29 changes: 10 additions & 19 deletions src/vault-interaction.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { Address, BigInt, ethereum, log } from "@graphprotocol/graph-ts"
import { Address, BigInt, ethereum } from "@graphprotocol/graph-ts"
import {
Deposit as DepositEvent,
Withdraw as WithdrawEvent,
Transfer as TransferEvent,
} from "../generated/templates/BeefyCLVault/BeefyVaultConcLiq"
import { getBeefyCLStrategy, getBeefyCLVault, getBeefyCLVaultSnapshot, isVaultRunning } from "./entity/vault"
import { getBeefyCLStrategy, getBeefyCLVault, getBeefyCLVaultSnapshot, isVaultInitialized } from "./entity/vault"
import { getTransaction } from "./entity/transaction"
import { getInvestor } from "./entity/investor"
import { ZERO_BI } from "./utils/decimal"
Expand All @@ -16,12 +14,6 @@ import { InvestorPositionInteraction } from "../generated/schema"
import { getEventIdentifier } from "./utils/event"
import { SHARE_TOKEN_MINT_ADDRESS } from "./config"

export function handleVaultDeposit(event: DepositEvent): void {
updateUserPosition(event, event.params.user, event.params.shares, true, false)
}
export function handleVaultWithdraw(event: WithdrawEvent): void {
updateUserPosition(event, event.params.user, event.params.shares.neg(), false, false)
}

export function handleVaultTransfer(event: TransferEvent): void {
// sending to self
Expand All @@ -35,23 +27,22 @@ export function handleVaultTransfer(event: TransferEvent): void {
}

// don't duplicate processing between Transfer and Deposit/Withdraw
if (event.params.from.equals(SHARE_TOKEN_MINT_ADDRESS) || event.params.to.equals(SHARE_TOKEN_MINT_ADDRESS)) {
return
if (!event.params.from.equals(SHARE_TOKEN_MINT_ADDRESS)) {
updateUserPosition(event, event.params.from, event.params.value.neg())
}

if (!event.params.to.equals(SHARE_TOKEN_MINT_ADDRESS)) {
updateUserPosition(event, event.params.to, event.params.value)
}

updateUserPosition(event, event.params.to, event.params.value, true, true)
updateUserPosition(event, event.params.from, event.params.value.neg(), false, true)
}

function updateUserPosition(
event: ethereum.Event,
investorAddress: Address,
sharesDelta: BigInt,
isDeposit: boolean,
isTransfer: boolean,
): void {
let vault = getBeefyCLVault(event.address)
if (!isVaultRunning(vault)) {
if (!isVaultInitialized(vault)) {
return
}

Expand Down Expand Up @@ -121,7 +112,7 @@ function updateUserPosition(
interaction.createdWith = event.transaction.hash
interaction.blockNumber = event.block.number
interaction.timestamp = event.block.timestamp
interaction.type = isTransfer ? "TRANSFER" : isDeposit ? "DEPOSIT" : "WITHDRAW"
interaction.type = sharesDelta.gt(ZERO_BI) ? "DEPOSIT" : "WITHDRAW"
interaction.sharesBalance = position.sharesBalance
interaction.underlyingBalance0 = vaultData.token0Balance
.times(position.sharesBalance)
Expand Down
6 changes: 1 addition & 5 deletions subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ templates:
eventHandlers:
- event: Initialized(uint8)
handler: handleInitialized
- event: Deposit(indexed address,uint256,uint256,uint256)
handler: handleDeposit
- event: Withdraw(indexed address,uint256,uint256,uint256)
handler: handleWithdraw
- event: Transfer(indexed address,indexed address,uint256)
handler: handleTransfer

Expand Down Expand Up @@ -178,4 +174,4 @@ templates:
abis: *abis
eventHandlers:
- event: Initialized(uint8)
handler: handleInitialized
handler: handleInitialized

0 comments on commit c664e1a

Please sign in to comment.