Skip to content

Commit

Permalink
Merge pull request #595 from balancer/fix-preminted-total-shares
Browse files Browse the repository at this point in the history
fix negative total shares and holders count
  • Loading branch information
mendesfabio authored Jun 18, 2024
2 parents 048944e + daf7c03 commit fd884e6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 23 deletions.
16 changes: 13 additions & 3 deletions src/mappings/poolController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import {
hexToBigInt,
getBalancerSnapshot,
} from './helpers/misc';
import { ONE_BD, ProtocolFeeType, ZERO_ADDRESS, ZERO_BD } from './helpers/constants';
import { ONE_BD, ProtocolFeeType, VAULT_ADDRESS, ZERO_ADDRESS, ZERO_BD } from './helpers/constants';
import { updateAmpFactor } from './helpers/stable';
import { getPoolTokenManager, getPoolTokens } from './helpers/pools';
import {
Expand Down Expand Up @@ -678,11 +678,21 @@ export function handleTransfer(event: Transfer): void {
poolShareFrom.save();
}

if (poolShareTo !== null && poolShareTo.balance.notEqual(ZERO_BD) && poolShareToBalance.equals(ZERO_BD)) {
if (
poolShareTo !== null &&
poolShareTo.balance.notEqual(ZERO_BD) &&
poolShareToBalance.equals(ZERO_BD) &&
poolShareTo.userAddress != VAULT_ADDRESS.toHex()
) {
pool.holdersCount = pool.holdersCount.plus(BigInt.fromI32(1));
}

if (poolShareFrom !== null && poolShareFrom.balance.equals(ZERO_BD) && poolShareFromBalance.notEqual(ZERO_BD)) {
if (
poolShareFrom !== null &&
poolShareFrom.balance.equals(ZERO_BD) &&
poolShareFromBalance.notEqual(ZERO_BD) &&
poolShareFrom.userAddress != VAULT_ADDRESS.toHex()
) {
pool.holdersCount = pool.holdersCount.minus(BigInt.fromI32(1));
}

Expand Down
2 changes: 1 addition & 1 deletion src/mappings/poolFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
} from './helpers/misc';
import { updatePoolWeights } from './helpers/weighted';

import { BigInt, Address, Bytes, ethereum, log } from '@graphprotocol/graph-ts';
import { BigInt, Address, Bytes, ethereum } from '@graphprotocol/graph-ts';

import { PoolCreated } from '../types/WeightedPoolFactory/WeightedPoolFactory';
import { AaveLinearPoolCreated } from '../types/AaveLinearPoolV3Factory/AaveLinearPoolV3Factory';
Expand Down
1 change: 1 addition & 0 deletions src/mappings/pricing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ export function handleAnswerUpdated(event: AnswerUpdated): void {
} else if (oracle && oracle.divisor !== null && oracle.decimals) {
const updatedAnswer = answer
.times(BigInt.fromString('10').pow(oracle.decimals as u8))
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
.div(BigInt.fromString(oracle.divisor!));
token.latestFXPrice = scaleDown(updatedAnswer, 8);
} else {
Expand Down
24 changes: 5 additions & 19 deletions src/mappings/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
getTradePairSnapshot,
getTradePair,
getBalancerSnapshot,
bytesToAddress,
getPoolShare,
} from './helpers/misc';
import { updatePoolWeights } from './helpers/weighted';
Expand Down Expand Up @@ -273,24 +272,11 @@ function handlePoolJoined(event: PoolBalanceChanged): void {
pool.save();

// This amount will also be transferred to the vault,
// causing the vault's 'user shares' to incorrectly increase,
// so we need to negate it. We do so by processing a mock transfer event
// from the vault to the zero address
const mockEvent = new Transfer(
bytesToAddress(pool.address),
event.logIndex,
event.transactionLogIndex,
event.logType,
event.block,
event.transaction,
[
new ethereum.EventParam('from', ethereum.Value.fromAddress(VAULT_ADDRESS)),
new ethereum.EventParam('to', ethereum.Value.fromAddress(ZERO_ADDRESS)),
new ethereum.EventParam('value', ethereum.Value.fromUnsignedBigInt(preMintedBpt)),
],
event.receipt
);
handleTransfer(mockEvent);
// causing the vault's 'user shares' to incorrectly
// increase, so we need to subtract it.
const vaultPoolShare = getPoolShare(poolId, VAULT_ADDRESS);
vaultPoolShare.balance = vaultPoolShare.balance.minus(tokenToDecimal(preMintedBpt, 18));
vaultPoolShare.save();
}

updatePoolLiquidity(poolId, event.block.number, event.block.timestamp);
Expand Down

0 comments on commit fd884e6

Please sign in to comment.