Skip to content

Commit

Permalink
✨ rewards: track claims
Browse files Browse the repository at this point in the history
  • Loading branch information
itofarina authored and cruzdanilo committed Jul 24, 2023
1 parent 1d3696b commit 397e979
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/afraid-panthers-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@exactly/subgraph": patch
---

✨ rewards: track claims
15 changes: 15 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -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!
}
19 changes: 18 additions & 1 deletion src/RewardsController.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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();
}
14 changes: 14 additions & 0 deletions src/utils/loadClaimedRewards.ts
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 3 additions & 0 deletions subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit 397e979

Please sign in to comment.