Skip to content

Commit

Permalink
[subgraph] fix pool total amount distributed being off (#2019)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasparkallas authored Sep 13, 2024
1 parent 2b2fe84 commit ff65ead
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 30 deletions.
3 changes: 2 additions & 1 deletion packages/subgraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"generate-sf-meta": "./tasks/getSFMeta.sh",
"generate-sf-meta-local": "COMMIT_HASH=local CONFIGURATION=local BRANCH=local TAG=local ./tasks/getSFMeta.sh",
"check-updates": "ncu --target minor --dep prod,dev",
"cloc": "cloc src"
"cloc": "cloc src",
"example:deploy-to-goldsky": "GOLDSKY_API_KEY=${YOUR_API_KEY} bash ./tasks/deploy.sh -o goldsky -n ${NETWORK} -r ${TAG} -v ${VERSION}"
}
}
5 changes: 3 additions & 2 deletions packages/subgraph/src/mappingHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ export function getOrInitPool(event: ethereum.Event, poolId: string): Pool {
return pool;
}

export function updatePoolTotalAmountFlowedAndDistributed(
export function updatePoolParticleAndTotalAmountFlowedAndDistributed(
event: ethereum.Event,
pool: Pool
): Pool {
Expand All @@ -513,6 +513,8 @@ export function updatePoolTotalAmountFlowedAndDistributed(
pool.totalAmountDistributedUntilUpdatedAt.plus(
amountFlowedSinceLastUpdate
);

settlePoolParticle(pool, event.block);

pool.save();

Expand Down Expand Up @@ -1535,7 +1537,6 @@ export function syncPoolMemberParticle(pool: Pool, poolMember: PoolMember): Pool
}

export function settlePDPoolMemberMU(pool: Pool, poolMember: PoolMember, block: ethereum.Block): void {
pool = settlePoolParticle(pool, block);
poolMember.totalAmountReceivedUntilUpdatedAt = monetaryUnitPoolMemberRTB(pool, poolMember, block.timestamp);
poolMember = syncPoolMemberParticle(pool, poolMember);
}
13 changes: 5 additions & 8 deletions packages/subgraph/src/mappings/gdav1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ import {
getOrInitOrUpdatePoolMember,
getOrInitTokenStatistic,
settlePDPoolMemberMU,
settlePoolParticle,
updateATSStreamedAndBalanceUntilUpdatedAt,
updateAggregateDistributionAgreementData,
updatePoolDistributorTotalAmountFlowedAndDistributed,
updatePoolTotalAmountFlowedAndDistributed,
updatePoolParticleAndTotalAmountFlowedAndDistributed,
updateSenderATSStreamData,
updateTokenStatisticStreamData,
updateTokenStatsStreamedUntilUpdatedAt,
Expand Down Expand Up @@ -101,7 +100,7 @@ export function handlePoolConnectionUpdated(
// Update Pool Entity
let pool = getOrInitPool(event, event.params.pool.toHex());
// @note we modify pool and poolMember here in memory, but do not save
pool = updatePoolTotalAmountFlowedAndDistributed(event, pool);
pool = updatePoolParticleAndTotalAmountFlowedAndDistributed(event, pool);
settlePDPoolMemberMU(pool, poolMember, event.block);
if (poolMember.units.gt(BIG_INT_ZERO)) {
if (memberConnectedStatusUpdated) {
Expand Down Expand Up @@ -194,7 +193,7 @@ export function handleBufferAdjusted(event: BufferAdjusted): void {

// Update Pool
let pool = getOrInitPool(event, event.params.pool.toHex());
pool = updatePoolTotalAmountFlowedAndDistributed(event, pool);
pool = updatePoolParticleAndTotalAmountFlowedAndDistributed(event, pool);
pool.totalBuffer = pool.totalBuffer.plus(event.params.bufferDelta);
pool.save();

Expand Down Expand Up @@ -233,8 +232,7 @@ export function handleFlowDistributionUpdated(

// @note that we are duplicating update of updatedAtTimestamp/BlockNumber here
// in the two functions
pool = updatePoolTotalAmountFlowedAndDistributed(event, pool);
pool = settlePoolParticle(pool, event.block);
pool = updatePoolParticleAndTotalAmountFlowedAndDistributed(event, pool);
pool.perUnitFlowRate = divideOrZero(event.params.newDistributorToPoolFlowRate, pool.totalUnits);
pool.flowRate = event.params.newTotalDistributionFlowRate;
pool.adjustmentFlowRate = event.params.adjustmentFlowRate;
Expand Down Expand Up @@ -321,8 +319,7 @@ export function handleInstantDistributionUpdated(

// @note that we are duplicating update of updatedAtTimestamp/BlockNumber here
// in the two functions
pool = updatePoolTotalAmountFlowedAndDistributed(event, pool);
pool = settlePoolParticle(pool, event.block);
pool = updatePoolParticleAndTotalAmountFlowedAndDistributed(event, pool);
// @note a speculations on what needs to be done
pool.perUnitSettledValue = pool.perUnitSettledValue.plus(divideOrZero(event.params.actualAmount, pool.totalUnits));
const previousTotalAmountDistributed =
Expand Down
6 changes: 3 additions & 3 deletions packages/subgraph/src/mappings/superfluidPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
settlePDPoolMemberMU,
updateATSStreamedAndBalanceUntilUpdatedAt,
updateAggregateDistributionAgreementData,
updatePoolTotalAmountFlowedAndDistributed,
updatePoolParticleAndTotalAmountFlowedAndDistributed,
updateTokenStatsStreamedUntilUpdatedAt,
} from "../mappingHelpers";
import { BIG_INT_ZERO, createEventID, initializeEventEntity, membershipWithUnitsExists } from "../utils";
Expand All @@ -28,7 +28,7 @@ export function handleDistributionClaimed(event: DistributionClaimed): void {
poolMember.totalAmountClaimed = event.params.totalClaimed;

// settle pool and pool member
pool = updatePoolTotalAmountFlowedAndDistributed(event, pool);
pool = updatePoolParticleAndTotalAmountFlowedAndDistributed(event, pool);
settlePDPoolMemberMU(pool, poolMember, event.block);

// Update PoolMember
Expand Down Expand Up @@ -58,7 +58,7 @@ export function handleMemberUnitsUpdated(event: MemberUnitsUpdated): void {
const unitsDelta = event.params.newUnits.minus(previousUnits);
const newTotalUnits = pool.totalUnits.plus(unitsDelta);

pool = updatePoolTotalAmountFlowedAndDistributed(event, pool);
pool = updatePoolParticleAndTotalAmountFlowedAndDistributed(event, pool);
settlePDPoolMemberMU(pool, poolMember, event.block);

const existingPoolFlowRate = pool.perUnitFlowRate.times(pool.totalUnits);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,6 @@ describe("PoolMember ending up with wrong `totalAmountReceivedUntilUpdatedAt`",

const pool = Pool.load(poolAddress.toHexString());

if (pool) {
pool.updatedAtTimestamp = poolCreatedEvent.block.timestamp;
}

// ## Arrange PoolMember 1
const aliceAddress = Address.fromString(alice_);
const aliceId = getPoolMemberID(poolAddress, aliceAddress);
Expand All @@ -262,10 +258,6 @@ describe("PoolMember ending up with wrong `totalAmountReceivedUntilUpdatedAt`",
handleMemberUnitsUpdated(aliceCreatedEvent);

// # First flow rate
if (pool) {
pool.updatedAtTimestamp = aliceCreatedEvent.block.timestamp;
}

const firstFlowRateEvent = createFlowDistributionUpdatedEvent(
superTokenAddress,
poolAddress.toHexString(),
Expand Down Expand Up @@ -337,10 +329,6 @@ describe("PoolMember ending up with wrong `totalAmountReceivedUntilUpdatedAt`",

mockedAppManifestAndRealtimeBalanceOf(superTokenAddress, bobAddress.toHexString(), createBobEvent.block.timestamp);
handleMemberUnitsUpdated(createBobEvent);

if (pool) {
pool.updatedAtTimestamp = createBobEvent.block.timestamp;
}

assert.fieldEquals(
"Pool",
Expand Down Expand Up @@ -374,10 +362,6 @@ describe("PoolMember ending up with wrong `totalAmountReceivedUntilUpdatedAt`",
mockedAppManifestAndRealtimeBalanceOf(superTokenAddress, poolAdminAndDistributorAddress.toHexString(), secondFlowRateEvent.block.timestamp);
handleFlowDistributionUpdated(secondFlowRateEvent);

if (pool) {
pool.updatedAtTimestamp = secondFlowRateEvent.block.timestamp;
}

assert.fieldEquals(
"Pool",
poolAddress.toHexString(),
Expand Down

0 comments on commit ff65ead

Please sign in to comment.