Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show how many tokens a user has delegated #82

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I got this line wrong; delegator should be event.transaction.to.

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be new - prev? Current approach would reflect the total amount including others

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be no "others"; a user can only delegate their current balance.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was under the impression that DelegateVotesChanged shows the new total delegated amount a delegate has across all delegations. If so, setting delegator.tokensDelegatedFrom would then show the total delegated amount available to the delegatee inclusive of the delegator and others.


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this value just be equivalent to newBalance?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I just renamed tokensDelegated to tokensDelegatedTo; no change in logic.

Copy link
Contributor

@MikeHathaway MikeHathaway Dec 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, but was just suggesting that we could simplify the logic by setting to just newBalance, but not a big deal


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
Loading