From 9d00f9f6a4a295f09e31fcf5710d2542aa51c481 Mon Sep 17 00:00:00 2001 From: Ed Noepel Date: Tue, 19 Dec 2023 13:13:55 -0500 Subject: [PATCH] implemented Account.tokensDelegatedFrom --- schema.graphql | 7 +++++-- src/mappings/ajna-token.ts | 7 ++++++- src/utils/account.ts | 3 ++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/schema.graphql b/schema.graphql index d45cec4..bd1325c 100644 --- a/schema.graphql +++ b/schema.graphql @@ -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!]! @@ -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 diff --git a/src/mappings/ajna-token.ts b/src/mappings/ajna-token.ts index 67f9b4e..ebdca83 100644 --- a/src/mappings/ajna-token.ts +++ b/src/mappings/ajna-token.ts @@ -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) @@ -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() } diff --git a/src/utils/account.ts b/src/utils/account.ts index 90e88aa..dae26c2 100644 --- a/src/utils/account.ts +++ b/src/utils/account.ts @@ -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 }