Skip to content

Commit

Permalink
Add initial position tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Mar 23, 2024
1 parent 3f76f52 commit 98ddb2d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
11 changes: 11 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,17 @@ type InvestorPosition @entity {
"The amount of shares the investor holds"
sharesBalance: BigDecimal!

"Initial amount of token0 the investor deposited. This includes for new deposits and withdrawals."
initialUnderlyingBalance0: BigDecimal!
"Initial amount of token1 the investor deposited. This includes for new deposits and withdrawals."
initialUnderlyingBalance1: BigDecimal!
"Initial amount of token0 the investor deposited in USD. This includes for new deposits and withdrawals."
initialUnderlyingBalance0USD: BigDecimal!
"Initial amount of token1 the investor deposited in USD. This includes for new deposits and withdrawals."
initialUnderlyingBalance1USD: BigDecimal!
"Initial position value in USD. This includes for new deposits and withdrawals."
initialPositionValueUSD: BigDecimal!

"The amount of first underlying tokens the investor is entitled to"
underlyingBalance0: BigDecimal!
"The amount of second underlying tokens the investor is entitled to"
Expand Down
20 changes: 19 additions & 1 deletion src/vault-transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getBeefyCLVault, getBeefyCLVaultSnapshot, isVaultRunning } from "./enti
import { getTransaction } from "./entity/transaction"
import { getBeefyCLProtocol, getBeefyCLProtocolSnapshot } from "./entity/protocol"
import { getInvestor, getInvestorSnapshot, isNewInvestor } from "./entity/investor"
import { ZERO_BD, ZERO_BI, tokenAmountToDecimal } from "./utils/decimal"
import { ONE_BD, ZERO_BD, ZERO_BI, tokenAmountToDecimal } from "./utils/decimal"
import { BeefyVaultConcLiq as BeefyCLVaultContract } from "./../generated/templates/BeefyCLVault/BeefyVaultConcLiq"
import { SNAPSHOT_PERIODS } from "./utils/time"
import { getToken } from "./entity/token"
Expand Down Expand Up @@ -179,11 +179,29 @@ function updateUserPosition(
position.underlyingBalance1USD = position.underlyingBalance1.times(token1PriceInUSD)
position.positionValueUSD = position.underlyingBalance0USD.plus(position.underlyingBalance1USD)
const sharesBalanceDelta = position.sharesBalance.minus(previousSharesBalance)
const shareBalancePercentChange = previousSharesBalance.gt(ZERO_BD)
? sharesBalanceDelta.div(previousSharesBalance)
: ZERO_BD
const underlyingBalance0Delta = position.underlyingBalance0.minus(previousUnderlyingBalance0)
const underlyingBalance1Delta = position.underlyingBalance1.minus(previousUnderlyingBalance1)
const underlyingBalance0DeltaUSD = position.underlyingBalance0USD.minus(previousUnderlyingBalance0USD)
const underlyingBalance1DeltaUSD = position.underlyingBalance1USD.minus(previousUnderlyingBalance1USD)
const positionValueUSDDelta = position.positionValueUSD.minus(previousPositionValueUSD)
if (isNewPosition) {
position.initialUnderlyingBalance0 = position.underlyingBalance0
position.initialUnderlyingBalance1 = position.underlyingBalance0
position.initialUnderlyingBalance0USD = position.underlyingBalance0
position.initialUnderlyingBalance1USD = position.underlyingBalance0
position.initialPositionValueUSD = position.underlyingBalance0
} else {
// apply the share balance percent change to the initial position value
const mult = shareBalancePercentChange.plus(ONE_BD)
position.initialUnderlyingBalance0 = position.initialUnderlyingBalance0.times(mult)
position.initialUnderlyingBalance1 = position.initialUnderlyingBalance1.times(mult)
position.initialUnderlyingBalance0USD = position.initialUnderlyingBalance0USD.times(mult)
position.initialUnderlyingBalance1USD = position.initialUnderlyingBalance1USD.times(mult)
position.initialPositionValueUSD = position.initialPositionValueUSD.times(mult)
}
position.save()
for (let i = 0; i < periods.length; i++) {
log.debug("updateUserPosition: updating investor position snapshot of investor {} for vault {} and period {}", [
Expand Down

0 comments on commit 98ddb2d

Please sign in to comment.