Skip to content

Commit

Permalink
implemented Account.tokensDelegatedFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
EdNoepel committed Dec 19, 2023
1 parent 042f40a commit 9d00f9f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
7 changes: 5 additions & 2 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,10 @@ type Account @entity {
rewardsClaimed: BigDecimal!
# voting state for each distribution period
distributionPeriodVotes: [DistributionPeriodVote!]!
# amount of tokens delegated to this account
tokensDelegated: BigDecimal!
# amount of tokens delegated by this user to a single delegated voter
tokensDelegatedFrom: BigDecimal!
# cumulative amount of tokens delegated from multiple users to this voter
tokensDelegatedTo: BigDecimal!

# positions associated with the account
positions: [Position!]!
Expand Down Expand Up @@ -961,6 +963,7 @@ type DelegateChanged @entity(immutable: true) {

type DelegateVotesChanged @entity(immutable: true) {
id: Bytes!
delegator: Bytes! # address
delegate: Bytes! # address
previousBalance: BigDecimal! # uint256
newBalance: BigDecimal! # uint256
Expand Down
7 changes: 6 additions & 1 deletion src/mappings/ajna-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export function handleDelegateVotesChanged(
let entity = new DelegateVotesChanged(
event.transaction.hash.concatI32(event.logIndex.toI32())
)
entity.delegator = event.transaction.from
entity.delegate = event.params.delegate
entity.previousBalance = wadToDecimal(event.params.previousBalance)
entity.newBalance = wadToDecimal(event.params.newBalance)
Expand All @@ -62,10 +63,14 @@ export function handleDelegateVotesChanged(
entity.blockTimestamp = event.block.timestamp
entity.transactionHash = event.transaction.hash

const delegator = loadOrCreateAccount(entity.delegator)
delegator.tokensDelegatedFrom = entity.newBalance

const delegateId = addressToBytes(event.params.delegate)
const delegate = loadOrCreateAccount(delegateId)
delegate.tokensDelegated = delegate.tokensDelegated.plus(changeInBalance)
delegate.tokensDelegatedTo = delegate.tokensDelegatedTo.plus(changeInBalance)

delegator.save()
delegate.save()
entity.save()
}
3 changes: 2 additions & 1 deletion src/utils/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export function loadOrCreateAccount(accountId: Bytes): Account {
account.delegatedFrom = []
account.rewardsClaimed = ZERO_BD
account.distributionPeriodVotes = []
account.tokensDelegated = ZERO_BD
account.tokensDelegatedFrom = ZERO_BD
account.tokensDelegatedTo = ZERO_BD

account.txCount = ZERO_BI
}
Expand Down

0 comments on commit 9d00f9f

Please sign in to comment.