diff --git a/schema.graphql b/schema.graphql index d45cec4..133da6b 100644 --- a/schema.graphql +++ b/schema.graphql @@ -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 diff --git a/src/mappings/ajna-token.ts b/src/mappings/ajna-token.ts index 67f9b4e..ef2e5b1 100644 --- a/src/mappings/ajna-token.ts +++ b/src/mappings/ajna-token.ts @@ -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, @@ -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( @@ -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() +} \ No newline at end of file diff --git a/src/utils/account.ts b/src/utils/account.ts index 90e88aa..7791333 100644 --- a/src/utils/account.ts +++ b/src/utils/account.ts @@ -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 diff --git a/subgraph.yaml b/subgraph.yaml index ed57de3..079ad20 100644 --- a/subgraph.yaml +++ b/subgraph.yaml @@ -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