Skip to content

Commit

Permalink
Update unclaimedAmounts in handleRewardsClaimed (#93)
Browse files Browse the repository at this point in the history
* Update unclaimedAmounts in handleRewardsClaimed

* Del eslint.config.js

* Review fix
  • Loading branch information
evgeny-stakewise authored Dec 20, 2024
1 parent bf2f9d2 commit f859a95
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/entities/merkleDistributor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ export function createOrLoadDistributorClaim(user: Address): DistributorClaim {
return claim
}

export function loadDistributorClaim(user: Address): DistributorClaim | null {
return DistributorClaim.load(user.toHex())
}

export function getDistributionType(distData: Bytes): DistributionType {
// only passed addresses are currently supported
const distAddress = Address.fromBytes(distData)
Expand Down
25 changes: 12 additions & 13 deletions src/mappings/merkleDistributor.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
import {
Address,
BigInt,
Bytes,
ethereum,
ipfs,
json,
JSONValue,
JSONValueKind,
log,
store,
} from '@graphprotocol/graph-ts'
import { Address, BigInt, Bytes, ethereum, ipfs, json, JSONValue, JSONValueKind, log } from '@graphprotocol/graph-ts'
import { DistributorClaimedAmount, PeriodicDistribution } from '../../generated/schema'
import {
OneTimeDistributionAdded,
Expand All @@ -27,6 +16,7 @@ import {
DistributionType,
getDistributionType,
loadDistributor,
loadDistributorClaim,
} from '../entities/merkleDistributor'
import { createTransaction } from '../entities/transaction'
import { loadUniswapPool } from '../entities/uniswap'
Expand Down Expand Up @@ -280,6 +270,12 @@ export function handleRewardsClaimed(event: RewardsClaimed): void {
const user = event.params.account
const tokens = event.params.tokens
const cumulativeAmounts = event.params.cumulativeAmounts

// The DistributorClaim object is guaranteed to exist at this point because
// the user has claimed rewards using the DistributorClaim object.
const claim = loadDistributorClaim(user)!
const unclaimedAmounts = claim.unclaimedAmounts

for (let i = 0; i < tokens.length; i++) {
const claimedAmountId = `${tokens[i].toHex()}-${user.toHex()}`
let claimedAmount = DistributorClaimedAmount.load(claimedAmountId)
Expand All @@ -289,8 +285,11 @@ export function handleRewardsClaimed(event: RewardsClaimed): void {
}
claimedAmount.cumulativeClaimedAmount = cumulativeAmounts[i]
claimedAmount.save()
unclaimedAmounts[i] = BigInt.zero()
}
store.remove('DistributorClaim', user.toHex())

claim.unclaimedAmounts = unclaimedAmounts
claim.save()

createTransaction(event.transaction.hash.toHex())
log.info('[MerkleDistributor] RewardsClaimed user={}', [user.toHex()])
Expand Down

0 comments on commit f859a95

Please sign in to comment.