diff --git a/packages/subgraph/schema.graphql b/packages/subgraph/schema.graphql index da309b0a93..f8e8c990c6 100644 --- a/packages/subgraph/schema.graphql +++ b/packages/subgraph/schema.graphql @@ -1809,6 +1809,8 @@ type PoolMember @entity { units: BigInt! isConnected: Boolean! totalAmountClaimed: BigInt! + poolTotalAmountDistributedUntilUpdatedAt: BigInt! + totalAmountReceivedUntilUpdatedAt: BigInt! account: Account! pool: Pool! @@ -2259,7 +2261,6 @@ type AccountTokenSnapshot @entity { """ totalCFANumberOfActiveStreams: Int! - # delete this property """ The count of currently open streams for an account, both incoming and outgoing for the GDA. """ @@ -2290,7 +2291,6 @@ type AccountTokenSnapshot @entity { """ activeCFAIncomingStreamCount: Int! - # delete this property """ The count of active incoming streams to this account for the GDA. """ diff --git a/packages/subgraph/src/mappingHelpers.ts b/packages/subgraph/src/mappingHelpers.ts index 65632f86b6..8b01207d90 100644 --- a/packages/subgraph/src/mappingHelpers.ts +++ b/packages/subgraph/src/mappingHelpers.ts @@ -558,6 +558,8 @@ export function getOrInitPoolMember( poolMember.units = BIG_INT_ZERO; poolMember.isConnected = false; poolMember.totalAmountClaimed = BIG_INT_ZERO; + poolMember.poolTotalAmountDistributedUntilUpdatedAt = BIG_INT_ZERO; + poolMember.totalAmountReceivedUntilUpdatedAt = BIG_INT_ZERO; poolMember.account = poolMemberAddress.toHex(); poolMember.pool = poolAddress.toHex(); @@ -1608,3 +1610,25 @@ export function updateAggregateEntitiesTransferData( tokenStatistic.totalAmountTransferredUntilUpdatedAt.plus(value); tokenStatistic.save(); } + +/** + * Updates `totalAmountReceivedUntilUpdatedAt` and `poolTotalAmountDistributedUntilUpdatedAt` fields + * Requires an explicit save on the PoolMember entity. + * Requires `pool.totalAmountDistributedUntilUpdatedAt` to be updated prior to calling this function. + * @param pool the pool entity + * @param poolMember the pool member entity + * @returns the updated pool member entity to be saved + */ +export function updatePoolMemberTotalAmountUntilUpdatedAtFields(pool: Pool, poolMember: PoolMember): PoolMember { + const amountReceivedDelta = pool.totalUnits.equals(BIG_INT_ZERO) + ? BIG_INT_ZERO + : pool.totalAmountDistributedUntilUpdatedAt + .minus(poolMember.poolTotalAmountDistributedUntilUpdatedAt) + .div(pool.totalUnits) + .times(poolMember.units); + poolMember.totalAmountReceivedUntilUpdatedAt = + poolMember.totalAmountReceivedUntilUpdatedAt.plus(amountReceivedDelta); + poolMember.poolTotalAmountDistributedUntilUpdatedAt = pool.totalAmountDistributedUntilUpdatedAt; + + return poolMember; +} \ No newline at end of file diff --git a/packages/subgraph/src/mappings/gdav1.ts b/packages/subgraph/src/mappings/gdav1.ts index 58f57be3ae..69577afe1d 100644 --- a/packages/subgraph/src/mappings/gdav1.ts +++ b/packages/subgraph/src/mappings/gdav1.ts @@ -24,6 +24,7 @@ import { updateATSStreamedAndBalanceUntilUpdatedAt, updateAggregateDistributionAgreementData, updatePoolDistributorTotalAmountFlowedAndDistributed, + updatePoolMemberTotalAmountUntilUpdatedAtFields, updatePoolTotalAmountFlowedAndDistributed, updateSenderATSStreamData, updateTokenStatisticStreamData, @@ -83,7 +84,7 @@ export function handlePoolConnectionUpdated( event: PoolConnectionUpdated ): void { // Update Pool Member Entity - const poolMember = getOrInitPoolMember( + let poolMember = getOrInitPoolMember( event, event.params.pool, event.params.account @@ -128,6 +129,10 @@ export function handlePoolConnectionUpdated( } pool.save(); + // Update totalAmountDistributedUntilUpdatedAt + poolMember = updatePoolMemberTotalAmountUntilUpdatedAtFields(pool, poolMember); + poolMember.save(); + // Update Token Stats Streamed Until Updated At updateTokenStatsStreamedUntilUpdatedAt(event.params.token, event.block); // Update ATS Balance and Streamed Until Updated At diff --git a/packages/subgraph/src/mappings/superfluidPool.ts b/packages/subgraph/src/mappings/superfluidPool.ts index cdc3e5eb6d..a90b5f5c9f 100644 --- a/packages/subgraph/src/mappings/superfluidPool.ts +++ b/packages/subgraph/src/mappings/superfluidPool.ts @@ -11,6 +11,7 @@ import { getOrInitPoolMember, updateATSStreamedAndBalanceUntilUpdatedAt, updateAggregateDistributionAgreementData, + updatePoolMemberTotalAmountUntilUpdatedAtFields, updatePoolTotalAmountFlowedAndDistributed, updateTokenStatsStreamedUntilUpdatedAt, } from "../mappingHelpers"; @@ -27,8 +28,10 @@ export function handleDistributionClaimed(event: DistributionClaimed): void { pool.save(); // Update PoolMember - const poolMember = getOrInitPoolMember(event, event.address, event.params.member); + let poolMember = getOrInitPoolMember(event, event.address, event.params.member); poolMember.totalAmountClaimed = event.params.totalClaimed; + + poolMember = updatePoolMemberTotalAmountUntilUpdatedAtFields(pool, poolMember); poolMember.save(); // Update Token Statistics @@ -47,7 +50,7 @@ export function handleDistributionClaimed(event: DistributionClaimed): void { export function handleMemberUnitsUpdated(event: MemberUnitsUpdated): void { // - PoolMember // - units - const poolMember = getOrInitPoolMember(event, event.address, event.params.member); + let poolMember = getOrInitPoolMember(event, event.address, event.params.member); const hasMembershipWithUnits = membershipWithUnitsExists(poolMember.id); const previousUnits = poolMember.units; @@ -73,6 +76,9 @@ export function handleMemberUnitsUpdated(event: MemberUnitsUpdated): void { pool.totalUnits = pool.totalUnits.plus(unitsDelta); pool.save(); + poolMember = updatePoolMemberTotalAmountUntilUpdatedAtFields(pool, poolMember); + poolMember.save(); + // 0 units to > 0 units if (previousUnits.equals(BIG_INT_ZERO) && event.params.newUnits.gt(BIG_INT_ZERO)) { pool.totalMembers = pool.totalMembers + 1;