Skip to content

Commit

Permalink
Add rp interaction reward token amounts
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Jul 15, 2024
1 parent bf3c294 commit 646c91a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 27 deletions.
12 changes: 7 additions & 5 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ type RewardPool @entity {
# ----- PRICES & STATS -----

"The total supply of the reward pool shares token in circulation. Express with `sharesToken.decimals` decimals."
rewardPoolSharesTotalSupply: BigInt!
sharesTotalSupply: BigInt!

"Latest LP token price in native we have seen. Expressed with 18 decimals."
underlyingToNativePrice: BigInt!
Expand Down Expand Up @@ -803,7 +803,7 @@ type RewardPoolSnapshot @entity {
timestamp: BigInt!

"The total supply of the reward pool shares token in circulation. Express with `sharesToken.decimals` decimals."
rewardPoolSharesTotalSupply: BigInt!
sharesTotalSupply: BigInt!

"Latest LP token price in native of this snapshot. Expressed with 18 decimals."
underlyingToNativePrice: BigInt!
Expand Down Expand Up @@ -831,7 +831,7 @@ type RewardPoolPosition @entity {
createdWith: Transaction!

"The amount of reward pool shares the investor holds"
rewardPoolBalance: BigInt!
sharesBalance: BigInt!
"Total amount of reward pool shares the investor holds. This is mostly used for filtering."
totalBalance: BigInt!

Expand Down Expand Up @@ -871,14 +871,16 @@ type RewardPoolPositionInteraction @entity(immutable: true) {
type: RewardPoolPositionInteractionType!

"The amount of reward pool shares the investor holds at the time of the interaction"
rewardPoolBalance: BigInt!
sharesBalance: BigInt!
"Total amount of reward pool shares the investor holds. This is mostly used for filtering."
totalBalance: BigInt!
"Amount of underlying token the investor is entitled to at the time of the interaction"
underlyingBalance: BigInt!

"Amount of reward pool shares change in the interaction"
rewardPoolBalanceDelta: BigInt!
sharesBalanceDelta: BigInt!
"Amount of reward tokens change in the interaction. Ordered by rewardPool.rewardTokensOrder."
rewardBalancesDelta: [BigInt!]!
"Amount of underlying token change in the interaction"
underlyingBalanceDelta: BigInt!

Expand Down
2 changes: 1 addition & 1 deletion src/reward-pool/entity/position.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function getRewardPoolPosition(rewardPool: RewardPool, investor: Investor
position.rewardPool = rewardPool.id
position.investor = investor.id
position.createdWith = ADDRESS_ZERO
position.rewardPoolBalance = ZERO_BI
position.sharesBalance = ZERO_BI
position.totalBalance = ZERO_BI
}
return position
Expand Down
6 changes: 3 additions & 3 deletions src/reward-pool/entity/reward-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function getRewardPool(rewardPoolAddress: Bytes): RewardPool {
rewardPool.rewardTokens = []
rewardPool.rewardTokensOrder = []
rewardPool.underlyingToken = ADDRESS_ZERO
rewardPool.rewardPoolSharesTotalSupply = ZERO_BI
rewardPool.sharesTotalSupply = ZERO_BI
rewardPool.underlyingToNativePrice = ZERO_BI
rewardPool.rewardToNativePrices = []
rewardPool.nativeToUSDPrice = ZERO_BI
Expand All @@ -48,7 +48,7 @@ export function getRewardPoolSnapshot(rewardPool: RewardPool, timestamp: BigInt,
snapshot.timestamp = timestamp
snapshot.roundedTimestamp = interval

snapshot.rewardPoolSharesTotalSupply = ZERO_BI
snapshot.sharesTotalSupply = ZERO_BI

snapshot.underlyingToNativePrice = ZERO_BI
snapshot.rewardToNativePrices = []
Expand All @@ -60,7 +60,7 @@ export function getRewardPoolSnapshot(rewardPool: RewardPool, timestamp: BigInt,
const previousSnapshotId = rewardPool.id.concat(getPreviousSnapshotIdSuffix(period, interval))
const previousSnapshot = RewardPoolSnapshot.load(previousSnapshotId)
if (previousSnapshot) {
snapshot.rewardPoolSharesTotalSupply = previousSnapshot.rewardPoolSharesTotalSupply
snapshot.sharesTotalSupply = previousSnapshot.sharesTotalSupply
snapshot.underlyingToNativePrice = previousSnapshot.underlyingToNativePrice
snapshot.rewardToNativePrices = previousSnapshot.rewardToNativePrices
snapshot.nativeToUSDPrice = previousSnapshot.nativeToUSDPrice
Expand Down
23 changes: 12 additions & 11 deletions src/reward-pool/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function updateUserPosition(
rewardPool: RewardPool,
event: ethereum.Event,
investorAddress: Address,
rewardPoolBalanceDelta: BigInt,
sharesBalanceDelta: BigInt,
rewardBalancesDelta: Array<BigInt>,
): void {
if (!isRewardPoolInitialized(rewardPool.id)) {
Expand All @@ -80,17 +80,17 @@ function updateUserPosition(

///////
// investor position
if (position.rewardPoolBalance.equals(ZERO_BI)) {
if (position.sharesBalance.equals(ZERO_BI)) {
position.createdWith = event.transaction.hash
}

position.rewardPoolBalance = position.rewardPoolBalance.plus(rewardPoolBalanceDelta)
position.totalBalance = position.rewardPoolBalance
position.sharesBalance = position.sharesBalance.plus(sharesBalanceDelta)
position.totalBalance = position.sharesBalance
position.save()

///////
// interaction
const isSharesTransfer = !rewardPoolBalanceDelta.equals(ZERO_BI)
const isSharesTransfer = !sharesBalanceDelta.equals(ZERO_BI)
const isRewardClaim = rewardBalancesDelta.some((delta) => !delta.equals(ZERO_BI))

// if both shares and reward pool are transferred, we will create two interactions
Expand All @@ -108,27 +108,28 @@ function updateUserPosition(
interaction.blockNumber = event.block.number
interaction.timestamp = event.block.timestamp
if (isSharesTransfer) {
interaction.type = rewardPoolBalanceDelta.gt(ZERO_BI) ? "REWARD_POOL_DEPOSIT" : "REWARD_POOL_WITHDRAW"
interaction.type = sharesBalanceDelta.gt(ZERO_BI) ? "REWARD_POOL_DEPOSIT" : "REWARD_POOL_WITHDRAW"
} else if (isRewardClaim) {
interaction.type = "REWARD_CLAIM"
}

interaction.rewardPoolBalance = position.rewardPoolBalance
interaction.sharesBalance = position.sharesBalance
interaction.totalBalance = position.totalBalance
interaction.rewardPoolBalanceDelta = rewardPoolBalanceDelta
interaction.sharesBalanceDelta = sharesBalanceDelta

// set the underlying balances at the time of the transaction
interaction.underlyingBalance = ZERO_BI
interaction.underlyingBalanceDelta = ZERO_BI
if (!rewardPoolData.rewardPoolTotalSupply.equals(ZERO_BI)) {
if (!rewardPoolData.sharesTotalSupply.equals(ZERO_BI)) {
interaction.underlyingBalance = interaction.underlyingBalance.plus(
rewardPoolData.underlyingTokenBalance.times(position.totalBalance).div(rewardPoolData.rewardPoolTotalSupply),
rewardPoolData.underlyingTokenBalance.times(position.totalBalance).div(rewardPoolData.sharesTotalSupply),
)

interaction.underlyingBalanceDelta = interaction.underlyingBalanceDelta.plus(
rewardPoolData.underlyingTokenBalance.times(rewardPoolBalanceDelta).div(rewardPoolData.rewardPoolTotalSupply),
rewardPoolData.underlyingTokenBalance.times(sharesBalanceDelta).div(rewardPoolData.sharesTotalSupply),
)
}
interaction.rewardBalancesDelta = rewardBalancesDelta
interaction.underlyingToNativePrice = rewardPoolData.underlyingToNativePrice
interaction.rewardToNativePrices = rewardPoolData.rewardToNativePrices
interaction.nativeToUSDPrice = rewardPoolData.nativeToUSDPrice
Expand Down
14 changes: 7 additions & 7 deletions src/reward-pool/util/reward-pool-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ export function fetchRewardPoolData(rewardPool: RewardPool): RewardPoolData {
}

// extract the data
let rewardPoolTotalSupply = ZERO_BI
let sharesTotalSupply = ZERO_BI
if (!totalSupplyRes.reverted) {
rewardPoolTotalSupply = totalSupplyRes.value.toBigInt()
sharesTotalSupply = totalSupplyRes.value.toBigInt()
} else {
log.error("Failed to fetch totalSupply for Reward Pool {}", [rewardPool.id.toHexString()])
}
Expand Down Expand Up @@ -162,7 +162,7 @@ export function fetchRewardPoolData(rewardPool: RewardPool): RewardPoolData {
}

return new RewardPoolData(
rewardPoolTotalSupply,
sharesTotalSupply,
underlyingTokenBalance,
underlyingToNativePrice,
rewardToNativePrices,
Expand All @@ -172,7 +172,7 @@ export function fetchRewardPoolData(rewardPool: RewardPool): RewardPoolData {

class RewardPoolData {
constructor(
public rewardPoolTotalSupply: BigInt,
public sharesTotalSupply: BigInt,
public underlyingTokenBalance: BigInt,
public underlyingToNativePrice: BigInt,
public rewardToNativePrices: Array<BigInt>,
Expand All @@ -186,7 +186,7 @@ export function updateRewardPoolDataAndSnapshots(
nowTimestamp: BigInt,
): RewardPool {
// update reward pool data
rewardPool.rewardPoolSharesTotalSupply = rewardPoolData.rewardPoolTotalSupply
rewardPool.sharesTotalSupply = rewardPoolData.sharesTotalSupply
rewardPool.underlyingAmount = rewardPoolData.underlyingTokenBalance
rewardPool.underlyingToNativePrice = rewardPoolData.underlyingToNativePrice
rewardPool.rewardToNativePrices = rewardPoolData.rewardToNativePrices
Expand All @@ -195,14 +195,14 @@ export function updateRewardPoolDataAndSnapshots(

// don't save a snapshot if we don't have a deposit yet
// or if the vault becomes empty
if (rewardPool.rewardPoolSharesTotalSupply.equals(ZERO_BI)) {
if (rewardPool.sharesTotalSupply.equals(ZERO_BI)) {
return rewardPool
}

for (let i = 0; i < REWARD_POOL_SNAPSHOT_PERIODS.length; i++) {
const period = REWARD_POOL_SNAPSHOT_PERIODS[i]
const snapshot = getRewardPoolSnapshot(rewardPool, nowTimestamp, period)
snapshot.rewardPoolSharesTotalSupply = rewardPoolData.rewardPoolTotalSupply
snapshot.sharesTotalSupply = rewardPoolData.sharesTotalSupply
snapshot.underlyingAmount = rewardPoolData.underlyingTokenBalance
snapshot.underlyingToNativePrice = rewardPoolData.underlyingToNativePrice
snapshot.rewardToNativePrices = rewardPoolData.rewardToNativePrices
Expand Down

0 comments on commit 646c91a

Please sign in to comment.