Skip to content

Commit

Permalink
track AJNA token balances
Browse files Browse the repository at this point in the history
  • Loading branch information
EdNoepel committed Dec 20, 2023
1 parent 042f40a commit bd3b3c0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
2 changes: 2 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ type Account @entity {
proposalsCreated: [Proposal!]!
# ecosystem coordination proposals passed and funded
proposalsExecuted: [Proposal!]!
# balance of AJNA tokens this account holds
tokens: BigDecimal!
# account to which this account is delegating votes
delegatedTo: Account
# accounts from which votes are delegated
Expand Down
16 changes: 16 additions & 0 deletions src/mappings/ajna-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Bytes } from "@graphprotocol/graph-ts"
import {
DelegateChanged as DelegateChangedEvent,
DelegateVotesChanged as DelegateVotesChangedEvent,
AjnaTokenTransfer as AjnaTokenTransferEvent,
} from "../../generated/AjnaToken/AjnaToken"
import {
Account,
Expand All @@ -11,6 +12,7 @@ import {
import { loadOrCreateAccount } from "../utils/account"
import { addressToBytes, wadToDecimal } from "../utils/convert"
import { addDelegator, removeDelegator } from "../utils/grants/voter"
import { getTokenBalance } from "../utils/token-erc20"

export function handleDelegateChanged(event: DelegateChangedEvent): void {
let entity = new DelegateChanged(
Expand Down Expand Up @@ -69,3 +71,17 @@ export function handleDelegateVotesChanged(
delegate.save()
entity.save()
}

export function handleAjnaTokenTransfer(
event: AjnaTokenTransferEvent
): void {
const ajnaToken = event.transaction.from

const fromAccount = loadOrCreateAccount(addressToBytes(event.params.from))
fromAccount.tokens = wadToDecimal(getTokenBalance(ajnaToken, event.params.from))
fromAccount.save()

const toAccount = loadOrCreateAccount(addressToBytes(event.params.to))
toAccount.tokens = wadToDecimal(getTokenBalance(ajnaToken, event.params.to))
toAccount.save()
}
1 change: 1 addition & 0 deletions src/utils/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function loadOrCreateAccount(accountId: Bytes): Account {
account.delegatedFrom = []
account.rewardsClaimed = ZERO_BD
account.distributionPeriodVotes = []
account.tokens = ZERO_BD
account.tokensDelegated = ZERO_BD

account.txCount = ZERO_BI
Expand Down
2 changes: 2 additions & 0 deletions subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ dataSources:
handler: handleDelegateChanged
- event: DelegateVotesChanged(indexed address,uint256,uint256)
handler: handleDelegateVotesChanged
- event: AjnaTokenTransfer(indexed address,indexed address,uint256)
handler: handleAjnaTokenTransfer
file: ./src/mappings/ajna-token.ts
- kind: ethereum
name: BurnWrappedAjna
Expand Down

0 comments on commit bd3b3c0

Please sign in to comment.