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

fix: update total delegate count, set balance to zero instead of deleting #200

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
5 changes: 3 additions & 2 deletions subgraphs/venus-governance/tests/integration/xvsVault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ describe('XVS Vault and Delegation', function () {
expect(delegate2.totalVotesMantissa).to.equal('0');
});

it('should remove delegate when xvs is withdrawn', async function () {
it('should update delegate when xvs is withdrawn', async function () {
const [_, user1, user2] = signers;

await xvsVault.connect(user1).requestWithdrawal(xvs.address, 0, amount.toFixed());
Expand All @@ -201,7 +201,8 @@ describe('XVS Vault and Delegation', function () {
data: { delegate: delegate1 },
} = await subgraphClient.getDelegateById(user1.address.toLowerCase());

expect(delegate1).to.equal(null);
expect(delegate1.stakedXvsMantissa).to.equal('0');
expect(delegate1.totalVotesMantissa).to.equal('0');

const {
data: { delegate: delegate2 },
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
Loading