diff --git a/.changeset/afraid-panthers-impress.md b/.changeset/afraid-panthers-impress.md new file mode 100644 index 0000000..88f349d --- /dev/null +++ b/.changeset/afraid-panthers-impress.md @@ -0,0 +1,5 @@ +--- +"@exactly/subgraph": patch +--- + +✨ rewards: track claims diff --git a/schema.graphql b/schema.graphql index 301c8cc..e0b8d16 100644 --- a/schema.graphql +++ b/schema.graphql @@ -411,3 +411,18 @@ type IndexUpdate @entity { newUndistributed: BigInt! timestamp: Int! } + +type ClaimRewards @entity { + id: ID! + account: Bytes! + reward: Bytes! + to: Bytes! + amount: BigInt! +} + +type ClaimedRewards @entity { + id: ID! # account-reward + account: Bytes! + reward: Bytes! + amount: BigInt! +} diff --git a/src/RewardsController.ts b/src/RewardsController.ts index d39ff45..540993e 100644 --- a/src/RewardsController.ts +++ b/src/RewardsController.ts @@ -1,8 +1,12 @@ import { DistributionSet as DistributionSetEvent, IndexUpdate as IndexUpdateEvent, + Claim as ClaimEvent, } from '../generated/RewardsController/RewardsController'; -import { Config, DistributionSet, IndexUpdate } from '../generated/schema'; +import { + Config, DistributionSet, IndexUpdate, ClaimRewards, +} from '../generated/schema'; +import loadClaimedRewards from './utils/loadClaimedRewards'; import toId from './utils/toId'; export function handleIndexUpdate(event: IndexUpdateEvent): void { @@ -43,3 +47,16 @@ export function handleDistributionSet(event: DistributionSetEvent): void { distributionSet.save(); } + +export function handleRewardsClaim(event: ClaimEvent): void { + const claim = new ClaimRewards(toId(event)); + claim.account = event.params.account; + claim.amount = event.params.amount; + claim.to = event.params.to; + claim.reward = event.params.reward; + claim.save(); + + const claimedRewards = loadClaimedRewards(event.params.account, event.params.reward); + claimedRewards.amount = claimedRewards.amount.plus(event.params.amount); + claimedRewards.save(); +} diff --git a/src/utils/loadClaimedRewards.ts b/src/utils/loadClaimedRewards.ts new file mode 100644 index 0000000..c412040 --- /dev/null +++ b/src/utils/loadClaimedRewards.ts @@ -0,0 +1,14 @@ +import { BigInt, Bytes } from '@graphprotocol/graph-ts'; +import { ClaimedRewards } from '../../generated/schema'; + +export default function loadClaimedRewards(account: Bytes, reward: Bytes): ClaimedRewards { + const id = `${account.toHexString()}-${reward.toHexString()}`; + let claimedRewards = ClaimedRewards.load(id); + if (claimedRewards) return claimedRewards; + + claimedRewards = new ClaimedRewards(id); + claimedRewards.account = account; + claimedRewards.reward = reward; + claimedRewards.amount = BigInt.zero(); + return claimedRewards; +} diff --git a/subgraph.template.yaml b/subgraph.template.yaml index d4508d1..fc63b11 100644 --- a/subgraph.template.yaml +++ b/subgraph.template.yaml @@ -157,12 +157,15 @@ dataSources: apiVersion: 0.0.7 language: wasm/assemblyscript entities: + - Claim - IndexUpdate - DistributionSet abis: - name: RewardsController file: node_modules/@exactly/protocol/deployments/{{ network }}/RewardsController.json eventHandlers: + - event: Claim(indexed address,indexed address,indexed address,uint256) + handler: handleRewardsClaim - event: IndexUpdate(indexed address,indexed address,uint256,uint256,uint256,uint256) handler: handleIndexUpdate - event: DistributionSet(indexed address,indexed address,(address,address,address,uint32,uint256,uint256,uint256,uint256,int128,uint64,uint64,uint64,uint64,uint64))