Skip to content

Commit

Permalink
fix: update total delegate count, set balance to zero instead of dele…
Browse files Browse the repository at this point in the history
…ting
  • Loading branch information
coreyar committed Dec 3, 2024
1 parent a9b5dce commit ec6e56b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
21 changes: 15 additions & 6 deletions subgraphs/venus-governance/src/mappings/xvsVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

/* eslint-disable @typescript-eslint/no-empty-function */
import { BigInt } from '@graphprotocol/graph-ts';
import { store } from '@graphprotocol/graph-ts';

import {
DelegateChangedV2,
Expand All @@ -13,6 +12,8 @@ import {
import { xvsVaultPid } from '../constants/config';
import { getOrCreateDelegate } from '../operations/getOrCreate';
import { updateDelegateChanged, updateDelegateVoteChanged } from '../operations/update';
import { BIGINT_ONE, BIGINT_ZERO } from '../constants';
import { getGovernanceEntity } from '../operations/get';

// - event: DelegateChanged(indexed address,indexed address,indexed address)
// handler: handleDelegateChanged
Expand All @@ -35,8 +36,15 @@ export function handleDeposit(event: Deposit): void {
const amount = event.params.amount;
// Update user's staked XVS
const result = getOrCreateDelegate(user);
const previousStake = result.entity.stakedXvsMantissa;
result.entity.stakedXvsMantissa = result.entity.stakedXvsMantissa.plus(amount);
result.entity.save();

if (previousStake.equals(BIGINT_ZERO) && amount.gt(BIGINT_ZERO)) {
const governance = getGovernanceEntity();
governance.totalDelegates = governance.totalDelegates.plus(BIGINT_ONE);
governance.save();
}
}
}

Expand All @@ -47,12 +55,13 @@ export function handleRequestedWithdrawal(event: RequestedWithdrawal): void {
const amount = event.params.amount;
const result = getOrCreateDelegate(user);
const newAmount = result.entity.stakedXvsMantissa.minus(amount);
result.entity.stakedXvsMantissa = newAmount;
result.entity.save();
// Update their delegate
if (newAmount.equals(new BigInt(0))) {
store.remove('Delegate', user.toHex());
} else {
result.entity.stakedXvsMantissa = newAmount;
result.entity.save();
if (newAmount.equals(BIGINT_ZERO)) {
const governance = getGovernanceEntity();
governance.totalDelegates = governance.totalDelegates.minus(BIGINT_ONE);
governance.save();
}
}
}
10 changes: 1 addition & 9 deletions subgraphs/venus-governance/src/operations/getOrCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import {
Transaction,
TrustedRemote,
} from '../../generated/schema';
import { BIGINT_ONE, BIGINT_ZERO } from '../constants';
import { nullAddress } from '../constants/addresses';
import { BIGINT_ZERO } from '../constants';
import { getRemoteProposalId } from '../utilities/ids';
import {
getDelegateId,
getMaxDailyLimitId,
getRemoteProposalStateTransactionId,
getTrustedRemoteId,
} from '../utilities/ids';
import { getGovernanceEntity } from './get';

export class GetOrCreateDelegateReturn {
entity: Delegate;
Expand All @@ -33,12 +31,6 @@ export const getOrCreateDelegate = (address: Address): GetOrCreateDelegateReturn
delegate.totalVotesMantissa = BIGINT_ZERO;
delegate.delegateCount = 0;

if (id != nullAddress) {
const governance = getGovernanceEntity();
governance.totalDelegates = governance.totalDelegates.plus(BIGINT_ONE);
governance.save();
}

delegate.save();
created = true;
}
Expand Down
6 changes: 3 additions & 3 deletions subgraphs/venus-governance/tests/unit/XVSVault/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('XVS Vault', () => {
assert.fieldEquals('Delegate', user.toHex(), 'stakedXvsMantissa', '800000000000000000');
});

test('removes delegate after withdrawing everything', () => {
test('updates delegate after withdrawing everything', () => {
const user = Address.fromString('0x0000000000000000000000000000000000000404');
const amount = '800000000000000000';
/** run handler */
Expand All @@ -89,8 +89,8 @@ describe('XVS Vault', () => {

handleRequestedWithdrawal(withdrawRequestedEvent);

// Expect delegate to have been removed
assert.entityCount('Delegate', 1);
assert.entityCount('Delegate', 2);
assert.fieldEquals('Delegate', user.toHex(), 'stakedXvsMantissa', '0');
});

test('delegate changed', () => {
Expand Down

0 comments on commit ec6e56b

Please sign in to comment.