From 40e7b55beb1525e12a2f467d3253f8794546fc04 Mon Sep 17 00:00:00 2001 From: 0xdavinchee <0xdavinchee@gmail.com> Date: Tue, 30 Jan 2024 19:54:03 +0200 Subject: [PATCH] [GDA][SUBGRAPH] GDA Subgraph Fixes (#1816) * fix mapping logic * removed unused fields (mostly related to GDA inflows), fix possible overflow in updatePoolMemberTotalAmountUntilUpdatedAtFields * fix build --- packages/subgraph/schema.graphql | 137 +------------ packages/subgraph/src/mappingHelpers.ts | 180 +++--------------- packages/subgraph/src/mappings/cfav1.ts | 1 - packages/subgraph/src/mappings/gdav1.ts | 4 +- .../subgraph/src/mappings/superfluidPool.ts | 20 +- packages/subgraph/src/utils.ts | 2 +- packages/subgraph/tests/assertionHelpers.ts | 12 -- .../superToken/event/superToken.event.test.ts | 4 - 8 files changed, 50 insertions(+), 310 deletions(-) diff --git a/packages/subgraph/schema.graphql b/packages/subgraph/schema.graphql index f8e8c990c6..d8ac452824 100644 --- a/packages/subgraph/schema.graphql +++ b/packages/subgraph/schema.graphql @@ -2281,20 +2281,11 @@ type AccountTokenSnapshot @entity { """ activeGDAOutgoingStreamCount: Int! - """ - The count of active incoming streams to this account for all agreements. - """ - activeIncomingStreamCount: Int! - """ The count of active incoming streams to this account for the CFA. + GDA incoming streams are *NOT* counted here. """ - activeCFAIncomingStreamCount: Int! - - """ - The count of active incoming streams to this account for the GDA. - """ - activeGDAIncomingStreamCount: Int! + activeIncomingStreamCount: Int! """ The count of closed streams by `account`, both incoming and outgoing for all agreements. @@ -2326,20 +2317,11 @@ type AccountTokenSnapshot @entity { """ inactiveGDAOutgoingStreamCount: Int! - """ - The count of closed incoming streams by `account` for all agreements. - """ - inactiveIncomingStreamCount: Int! - """ The count of closed incoming streams by `account` for the CFA. + Close incoming GDA streams are *NOT* counted here. """ - inactiveCFAIncomingStreamCount: Int! - - """ - The count of closed incoming streams by `account` for the GDA. - """ - inactiveGDAIncomingStreamCount: Int! + inactiveIncomingStreamCount: Int! """ The current (as of updatedAt) number of subscriptions with units allocated to them tied to this `account`. @@ -2384,6 +2366,7 @@ type AccountTokenSnapshot @entity { """ The total net flow rate of the `account` as of `updatedAtTimestamp`/`updatedAtBlock` for all flow agreements. This can be obtained by: `totalInflowRate - totalOutflowRate`. + NOTE: this property will NOT be 100% accurate all the time for receivers of GDA flows. """ totalNetFlowRate: BigInt! @@ -2392,25 +2375,11 @@ type AccountTokenSnapshot @entity { """ totalCFANetFlowRate: BigInt! - """ - The total net flow rate of the `account` as of `updatedAtTimestamp`/`updatedAtBlock` for the GDA. - """ - totalGDANetFlowRate: BigInt! - - """ - The total inflow rate (receive flowRate per second) of the `account` for all flow agreements. - """ - totalInflowRate: BigInt! - """ The total inflow rate (receive flowRate per second) of the `account` for the CFA. + GDA inflow rate is *NOT* included here. """ - totalCFAInflowRate: BigInt! - - """ - The total inflow rate (receive flowRate per second) of the `account` for the GDA. - """ - totalGDAInflowRate: BigInt! + totalInflowRate: BigInt! """ The total outflow rate (send flowrate per second) of the `account` for all flow agreements. @@ -2427,20 +2396,10 @@ type AccountTokenSnapshot @entity { """ totalGDAOutflowRate: BigInt! - """ - The total amount of `token` streamed into this `account` until the `updatedAtTimestamp`/`updatedAtBlock` for all flow agreements. - """ - totalAmountStreamedInUntilUpdatedAt: BigInt! - """ The total amount of `token` streamed into this `account` until the `updatedAtTimestamp`/`updatedAtBlock` for the CFA. """ - totalCFAAmountStreamedInUntilUpdatedAt: BigInt! - - """ - The total amount of `token` streamed into this `account` until the `updatedAtTimestamp`/`updatedAtBlock` for the GDA. - """ - totalGDAAmountStreamedInUntilUpdatedAt: BigInt! + totalAmountStreamedInUntilUpdatedAt: BigInt! """ The total amount of `token` streamed from this `account` until the `updatedAtTimestamp`/`updatedAtBlock` for all flow agreements. @@ -2452,11 +2411,6 @@ type AccountTokenSnapshot @entity { """ totalCFAAmountStreamedOutUntilUpdatedAt: BigInt! - """ - The total amount of `token` streamed from this `account` until the `updatedAtTimestamp`/`updatedAtBlock` for the GDA. - """ - totalGDAAmountStreamedOutUntilUpdatedAt: BigInt! - """ The total amount of `token` streamed through this `account` until the `updatedAtTimestamp`/`updatedAtBlock` for all flow agreements. """ @@ -2467,11 +2421,6 @@ type AccountTokenSnapshot @entity { """ totalCFAAmountStreamedUntilUpdatedAt: BigInt! - """ - The total amount of `token` streamed through this `account` until the `updatedAtTimestamp`/`updatedAtBlock` for the GDA. - """ - totalGDAAmountStreamedUntilUpdatedAt: BigInt! - """ The total amount of `token` this `account` has transferred. """ @@ -2538,16 +2487,6 @@ type AccountTokenSnapshotLog @entity { """ activeIncomingStreamCount: Int! - """ - The count of active incoming streams to this account for the CFA. - """ - activeCFAIncomingStreamCount: Int! - - """ - The count of active incoming streams to this account for the GDA. - """ - activeGDAIncomingStreamCount: Int! - """ The current (as of timestamp) count of closed streams for all agreements. """ @@ -2578,20 +2517,11 @@ type AccountTokenSnapshotLog @entity { """ inactiveGDAOutgoingStreamCount: Int! - """ - The count of closed incoming streams by `account` for all agreements. - """ - inactiveIncomingStreamCount: Int! - """ The count of closed incoming streams by `account` for the CFA. + Close incoming GDA streams are *NOT* counted here. """ - inactiveCFAIncomingStreamCount: Int! - - """ - The count of closed incoming streams by `account` for the GDA. - """ - inactiveGDAIncomingStreamCount: Int! + inactiveIncomingStreamCount: Int! """ The current (as of timestamp) number of subscriptions with units allocated to them tied to this `account`. @@ -2641,31 +2571,14 @@ type AccountTokenSnapshotLog @entity { """ The total (as of timestamp) net flow rate of the `account` as of `timestamp`/`block` for the CFA. - This can be obtained by: `totalCFAInflowRate - totalCFAOutflowRate` """ totalCFANetFlowRate: BigInt! - """ - The total (as of timestamp) net flow rate of the `account` as of `timestamp`/`block` for the GDA. - This can be obtained by: `totalGDAInflowRate - totalGDAOutflowRate` - """ - totalGDANetFlowRate: BigInt! - """ The total (as of timestamp) inflow rate (receive flowRate per second) of the `account`. """ totalInflowRate: BigInt! - """ - The total (as of timestamp) inflow rate (receive flowRate per second) of the `account` for the CFA. - """ - totalCFAInflowRate: BigInt! - - """ - The total (as of timestamp) inflow rate (receive flowRate per second) of the `account` for the GDA. - """ - totalGDAInflowRate: BigInt! - """ The total (as of timestamp) outflow rate (send flowrate per second) of the `account`. """ @@ -2686,16 +2599,6 @@ type AccountTokenSnapshotLog @entity { """ totalAmountStreamedIn: BigInt! - """ - The total (as of timestamp) amount of `token` streamed into this `account` until the `timestamp`/`block` for the CFA. - """ - totalCFAAmountStreamedIn: BigInt! - - """ - The total (as of timestamp) amount of `token` streamed into this `account` until the `timestamp`/`block` for the GDA. - """ - totalGDAAmountStreamedIn: BigInt! - """ The total (as of timestamp) amount of `token` streamed from this `account` until the `timestamp`/`block`. """ @@ -2706,11 +2609,6 @@ type AccountTokenSnapshotLog @entity { """ totalCFAAmountStreamedOut: BigInt! - """ - The total (as of timestamp) amount of `token` streamed from this `account` until the `timestamp`/`block` for the GDA. - """ - totalGDAAmountStreamedOut: BigInt! - """ The total (as of timestamp) net amount of `token` streamed through this `account` until the `timestamp`/`block`. """ @@ -2721,11 +2619,6 @@ type AccountTokenSnapshotLog @entity { """ totalCFAAmountStreamed: BigInt! - """ - The total (as of timestamp) net amount of `token` streamed through this `account` until the `timestamp`/`block` for the GDA. - """ - totalGDAAmountStreamed: BigInt! - """ The total (as of timestamp) amount of `token` this `account` has transferred out until the `timestamp`/`block`. """ @@ -2857,11 +2750,6 @@ type TokenStatistic @entity { """ totalCFAAmountStreamedUntilUpdatedAt: BigInt! - """ - The all-time total amount streamed (outflows) until the `updatedAtTimestamp`/`updatedAtBlock` for the GDA. - """ - totalGDAAmountStreamedUntilUpdatedAt: BigInt! - """ The all-time total amount transferred until the `updatedAtTimestamp`/`updatedAtBlock`. """ @@ -3016,11 +2904,6 @@ type TokenStatisticLog @entity { """ totalCFAAmountStreamed: BigInt! - """ - The all-time total amount of `token` streamed (outflows) until the `timestamp`/`block` for the GDA. - """ - totalGDAAmountStreamed: BigInt! - """ The all-time total amount of `token` transferred until the `timestamp`/`block`. """ diff --git a/packages/subgraph/src/mappingHelpers.ts b/packages/subgraph/src/mappingHelpers.ts index 8b01207d90..69e997e75a 100644 --- a/packages/subgraph/src/mappingHelpers.ts +++ b/packages/subgraph/src/mappingHelpers.ts @@ -37,6 +37,7 @@ import { getPoolDistributorID, getActiveStreamsDelta, getClosedStreamsDelta, + MAX_UINT256, } from "./utils"; import { SuperToken as SuperTokenTemplate } from "../generated/templates"; import { ISuperToken as SuperToken } from "../generated/templates/SuperToken/ISuperToken"; @@ -646,14 +647,10 @@ if (accountTokenSnapshot == null) { accountTokenSnapshot.totalCFANumberOfActiveStreams = 0; accountTokenSnapshot.totalGDANumberOfActiveStreams = 0; accountTokenSnapshot.activeIncomingStreamCount = 0; - accountTokenSnapshot.activeCFAIncomingStreamCount = 0; - accountTokenSnapshot.activeGDAIncomingStreamCount = 0; accountTokenSnapshot.activeOutgoingStreamCount = 0; accountTokenSnapshot.activeCFAOutgoingStreamCount = 0; accountTokenSnapshot.activeGDAOutgoingStreamCount = 0; accountTokenSnapshot.inactiveIncomingStreamCount = 0; - accountTokenSnapshot.inactiveCFAIncomingStreamCount = 0; - accountTokenSnapshot.inactiveGDAIncomingStreamCount = 0; accountTokenSnapshot.inactiveOutgoingStreamCount = 0; accountTokenSnapshot.inactiveCFAOutgoingStreamCount = 0; accountTokenSnapshot.inactiveGDAOutgoingStreamCount = 0; @@ -668,29 +665,18 @@ if (accountTokenSnapshot == null) { accountTokenSnapshot.balanceUntilUpdatedAt = BIG_INT_ZERO; accountTokenSnapshot.totalNetFlowRate = BIG_INT_ZERO; accountTokenSnapshot.totalCFANetFlowRate = BIG_INT_ZERO; - accountTokenSnapshot.totalGDANetFlowRate = BIG_INT_ZERO; accountTokenSnapshot.totalInflowRate = BIG_INT_ZERO; - accountTokenSnapshot.totalCFAInflowRate = BIG_INT_ZERO; - accountTokenSnapshot.totalGDAInflowRate = BIG_INT_ZERO; accountTokenSnapshot.totalOutflowRate = BIG_INT_ZERO; accountTokenSnapshot.totalCFAOutflowRate = BIG_INT_ZERO; accountTokenSnapshot.totalGDAOutflowRate = BIG_INT_ZERO; accountTokenSnapshot.totalAmountStreamedInUntilUpdatedAt = BIG_INT_ZERO; - accountTokenSnapshot.totalCFAAmountStreamedInUntilUpdatedAt = - BIG_INT_ZERO; - accountTokenSnapshot.totalGDAAmountStreamedInUntilUpdatedAt = - BIG_INT_ZERO; accountTokenSnapshot.totalAmountStreamedOutUntilUpdatedAt = BIG_INT_ZERO; accountTokenSnapshot.totalCFAAmountStreamedOutUntilUpdatedAt = BIG_INT_ZERO; - accountTokenSnapshot.totalGDAAmountStreamedOutUntilUpdatedAt = - BIG_INT_ZERO; accountTokenSnapshot.totalAmountStreamedUntilUpdatedAt = BIG_INT_ZERO; accountTokenSnapshot.totalCFAAmountStreamedUntilUpdatedAt = BIG_INT_ZERO; - accountTokenSnapshot.totalGDAAmountStreamedUntilUpdatedAt = - BIG_INT_ZERO; accountTokenSnapshot.totalAmountTransferredUntilUpdatedAt = BIG_INT_ZERO; accountTokenSnapshot.totalDeposit = BIG_INT_ZERO; @@ -739,8 +725,6 @@ export function _createAccountTokenSnapshotLogEntity( atsLog.totalCFANumberOfActiveStreams = ats.totalCFANumberOfActiveStreams; atsLog.totalGDANumberOfActiveStreams = ats.totalGDANumberOfActiveStreams; atsLog.activeIncomingStreamCount = ats.activeIncomingStreamCount; - atsLog.activeCFAIncomingStreamCount = ats.activeCFAIncomingStreamCount; - atsLog.activeGDAIncomingStreamCount = ats.activeGDAIncomingStreamCount; atsLog.activeOutgoingStreamCount = ats.activeOutgoingStreamCount; atsLog.activeCFAOutgoingStreamCount = ats.activeCFAOutgoingStreamCount; atsLog.activeGDAOutgoingStreamCount = ats.activeGDAOutgoingStreamCount; @@ -748,8 +732,6 @@ export function _createAccountTokenSnapshotLogEntity( atsLog.totalCFANumberOfClosedStreams = ats.totalCFANumberOfClosedStreams; atsLog.totalGDANumberOfClosedStreams = ats.totalGDANumberOfClosedStreams; atsLog.inactiveIncomingStreamCount = ats.inactiveIncomingStreamCount; - atsLog.inactiveCFAIncomingStreamCount = ats.inactiveCFAIncomingStreamCount; - atsLog.inactiveGDAIncomingStreamCount = ats.inactiveGDAIncomingStreamCount; atsLog.inactiveOutgoingStreamCount = ats.inactiveOutgoingStreamCount; atsLog.inactiveCFAOutgoingStreamCount = ats.inactiveCFAOutgoingStreamCount; atsLog.inactiveGDAOutgoingStreamCount = ats.inactiveGDAOutgoingStreamCount; @@ -760,26 +742,16 @@ export function _createAccountTokenSnapshotLogEntity( atsLog.balance = ats.balanceUntilUpdatedAt; atsLog.totalNetFlowRate = ats.totalNetFlowRate; atsLog.totalCFANetFlowRate = ats.totalCFANetFlowRate; - atsLog.totalGDANetFlowRate = ats.totalGDANetFlowRate; atsLog.totalInflowRate = ats.totalInflowRate; - atsLog.totalCFAInflowRate = ats.totalCFAInflowRate; - atsLog.totalGDAInflowRate = ats.totalGDAInflowRate; atsLog.totalOutflowRate = ats.totalOutflowRate; atsLog.totalCFAOutflowRate = ats.totalCFAOutflowRate; atsLog.totalGDAOutflowRate = ats.totalGDAOutflowRate; atsLog.totalAmountStreamed = ats.totalAmountStreamedUntilUpdatedAt; atsLog.totalCFAAmountStreamed = ats.totalCFAAmountStreamedUntilUpdatedAt; - atsLog.totalGDAAmountStreamed = ats.totalGDAAmountStreamedUntilUpdatedAt; atsLog.totalAmountStreamedIn = ats.totalAmountStreamedInUntilUpdatedAt; - atsLog.totalCFAAmountStreamedIn = - ats.totalCFAAmountStreamedInUntilUpdatedAt; atsLog.totalAmountStreamedOut = ats.totalAmountStreamedOutUntilUpdatedAt; - atsLog.totalGDAAmountStreamedIn = - ats.totalGDAAmountStreamedInUntilUpdatedAt; atsLog.totalCFAAmountStreamedOut = ats.totalCFAAmountStreamedOutUntilUpdatedAt; - atsLog.totalGDAAmountStreamedOut = - ats.totalGDAAmountStreamedOutUntilUpdatedAt; atsLog.totalAmountTransferred = ats.totalAmountTransferredUntilUpdatedAt; atsLog.totalDeposit = ats.totalDeposit; atsLog.totalCFADeposit = ats.totalCFADeposit; @@ -821,7 +793,6 @@ export function getOrInitTokenStatistic( tokenStatistic.totalNumberOfAccounts = 0; tokenStatistic.totalAmountStreamedUntilUpdatedAt = BIG_INT_ZERO; tokenStatistic.totalCFAAmountStreamedUntilUpdatedAt = BIG_INT_ZERO; - tokenStatistic.totalGDAAmountStreamedUntilUpdatedAt = BIG_INT_ZERO; tokenStatistic.totalAmountTransferredUntilUpdatedAt = BIG_INT_ZERO; tokenStatistic.totalAmountDistributedUntilUpdatedAt = BIG_INT_ZERO; tokenStatistic.totalSupply = BIG_INT_ZERO; @@ -891,8 +862,6 @@ export function _createTokenStatisticLogEntity( tokenStatistic.totalAmountStreamedUntilUpdatedAt; tokenStatisticLog.totalCFAAmountStreamed = tokenStatistic.totalCFAAmountStreamedUntilUpdatedAt; - tokenStatisticLog.totalGDAAmountStreamed = - tokenStatistic.totalGDAAmountStreamedUntilUpdatedAt; tokenStatisticLog.totalAmountTransferred = tokenStatistic.totalAmountTransferredUntilUpdatedAt; tokenStatisticLog.totalAmountDistributed = @@ -1139,12 +1108,6 @@ export function updateATSStreamedAndBalanceUntilUpdatedAt( accountTokenSnapshot.updatedAtTimestamp, accountTokenSnapshot.totalCFANetFlowRate ); - const totalCFAAmountStreamedInSinceLastUpdatedAt = - getAmountStreamedSinceLastUpdatedAt( - block.timestamp, - accountTokenSnapshot.updatedAtTimestamp, - accountTokenSnapshot.totalCFAInflowRate - ); const totalCFAAmountStreamedOutSinceLastUpdatedAt = getAmountStreamedSinceLastUpdatedAt( block.timestamp, @@ -1158,56 +1121,12 @@ export function updateATSStreamedAndBalanceUntilUpdatedAt( totalCFAAmountStreamedSinceLastUpdatedAt ); - // update the totalCFAStreamedUntilUpdatedAt (in) - accountTokenSnapshot.totalCFAAmountStreamedInUntilUpdatedAt = - accountTokenSnapshot.totalCFAAmountStreamedInUntilUpdatedAt.plus( - totalCFAAmountStreamedInSinceLastUpdatedAt - ); - // update the totalCFAStreamedUntilUpdatedAt (out) accountTokenSnapshot.totalCFAAmountStreamedOutUntilUpdatedAt = accountTokenSnapshot.totalCFAAmountStreamedOutUntilUpdatedAt.plus( totalCFAAmountStreamedOutSinceLastUpdatedAt ); - //////////////// GDA streamed amounts //////////////// - const totalGDAAmountStreamedSinceLastUpdatedAt = - getAmountStreamedSinceLastUpdatedAt( - block.timestamp, - accountTokenSnapshot.updatedAtTimestamp, - accountTokenSnapshot.totalGDANetFlowRate - ); - const totalGDAAmountStreamedInSinceLastUpdatedAt = - getAmountStreamedSinceLastUpdatedAt( - block.timestamp, - accountTokenSnapshot.updatedAtTimestamp, - accountTokenSnapshot.totalGDAInflowRate - ); - const totalGDAAmountStreamedOutSinceLastUpdatedAt = - getAmountStreamedSinceLastUpdatedAt( - block.timestamp, - accountTokenSnapshot.updatedAtTimestamp, - accountTokenSnapshot.totalGDAOutflowRate - ); - - // update the totalGDAStreamedUntilUpdatedAt (net) - accountTokenSnapshot.totalGDAAmountStreamedUntilUpdatedAt = - accountTokenSnapshot.totalGDAAmountStreamedUntilUpdatedAt.plus( - totalGDAAmountStreamedSinceLastUpdatedAt - ); - - // update the totalGDAStreamedUntilUpdatedAt (in) - accountTokenSnapshot.totalGDAAmountStreamedInUntilUpdatedAt = - accountTokenSnapshot.totalGDAAmountStreamedInUntilUpdatedAt.plus( - totalGDAAmountStreamedInSinceLastUpdatedAt - ); - - // update the totalGDAStreamedUntilUpdatedAt (out) - accountTokenSnapshot.totalGDAAmountStreamedOutUntilUpdatedAt = - accountTokenSnapshot.totalGDAAmountStreamedOutUntilUpdatedAt.plus( - totalGDAAmountStreamedOutSinceLastUpdatedAt - ); - accountTokenSnapshot.save(); const balanceUntilUpdatedAtAfterUpdate = accountTokenSnapshot.balanceUntilUpdatedAt; @@ -1276,18 +1195,6 @@ export function updateTokenStatsStreamedUntilUpdatedAt( cfaAmountStreamedSinceLastUpdatedAt ); - //// GDA streamed amounts //// - const gdaAmountStreamedSinceLastUpdatedAt = - getAmountStreamedSinceLastUpdatedAt( - block.timestamp, - tokenStats.updatedAtTimestamp, - tokenStats.totalGDAOutflowRate - ); - tokenStats.totalGDAAmountStreamedUntilUpdatedAt = - tokenStats.totalGDAAmountStreamedUntilUpdatedAt.plus( - gdaAmountStreamedSinceLastUpdatedAt - ); - tokenStats.updatedAtTimestamp = block.timestamp; tokenStats.updatedAtBlockNumber = block.number; tokenStats.save(); @@ -1446,9 +1353,6 @@ export function updateSenderATSStreamData( senderATS.totalCFADeposit = senderATS.totalCFADeposit.plus(depositDelta); } else { - senderATS.totalGDANetFlowRate = - senderATS.totalGDANetFlowRate.minus(flowRateDelta); - // the outflow rate should never go below 0. senderATS.totalGDAOutflowRate = senderATS.totalGDAOutflowRate .plus(flowRateDelta) @@ -1488,7 +1392,6 @@ export function updateReceiverATSStreamData( flowRateDelta: BigInt, isCreate: boolean, isDelete: boolean, - isCFA: boolean, block: ethereum.Block ): void { const totalNumberOfActiveStreamsDelta = getActiveStreamsDelta( @@ -1531,55 +1434,17 @@ export function updateReceiverATSStreamData( receiverATS.maybeCriticalAtTimestamp ); - if (isCFA) { - receiverATS.totalCFANetFlowRate = - receiverATS.totalCFANetFlowRate.plus(flowRateDelta); - - // the inflow rate should never go below 0. - receiverATS.totalCFAInflowRate = receiverATS.totalCFAInflowRate - .plus(flowRateDelta) - .lt(BIG_INT_ZERO) - ? newFlowRate - : receiverATS.totalCFAInflowRate.plus(flowRateDelta); - - receiverATS.totalCFANumberOfActiveStreams = - receiverATS.totalCFANumberOfActiveStreams + - totalNumberOfActiveStreamsDelta; - receiverATS.activeCFAIncomingStreamCount = - receiverATS.activeCFAIncomingStreamCount + - totalNumberOfActiveStreamsDelta; - receiverATS.inactiveCFAIncomingStreamCount = - receiverATS.inactiveCFAIncomingStreamCount + - totalNumberOfClosedStreamsDelta; - - receiverATS.totalCFANumberOfClosedStreams = - receiverATS.totalCFANumberOfClosedStreams + - totalNumberOfClosedStreamsDelta; - } else { - receiverATS.totalGDANetFlowRate = - receiverATS.totalGDANetFlowRate.plus(flowRateDelta); - - // the inflow rate should never go below 0. - receiverATS.totalGDAInflowRate = receiverATS.totalGDAInflowRate - .plus(flowRateDelta) - .lt(BIG_INT_ZERO) - ? newFlowRate - : receiverATS.totalGDAInflowRate.plus(flowRateDelta); + receiverATS.totalCFANetFlowRate = + receiverATS.totalCFANetFlowRate.plus(flowRateDelta); - receiverATS.totalGDANumberOfActiveStreams = - receiverATS.totalGDANumberOfActiveStreams + - totalNumberOfActiveStreamsDelta; - receiverATS.activeGDAIncomingStreamCount = - receiverATS.activeGDAIncomingStreamCount + - totalNumberOfActiveStreamsDelta; - receiverATS.inactiveGDAIncomingStreamCount = - receiverATS.inactiveGDAIncomingStreamCount + - totalNumberOfClosedStreamsDelta; + receiverATS.totalCFANumberOfActiveStreams = + receiverATS.totalCFANumberOfActiveStreams + + totalNumberOfActiveStreamsDelta; + totalNumberOfClosedStreamsDelta; - receiverATS.totalGDANumberOfClosedStreams = - receiverATS.totalGDANumberOfClosedStreams + - totalNumberOfClosedStreamsDelta; - } + receiverATS.totalCFANumberOfClosedStreams = + receiverATS.totalCFANumberOfClosedStreams + + totalNumberOfClosedStreamsDelta; receiverATS.save(); } @@ -1614,21 +1479,30 @@ export function updateAggregateEntitiesTransferData( /** * Updates `totalAmountReceivedUntilUpdatedAt` and `poolTotalAmountDistributedUntilUpdatedAt` fields * Requires an explicit save on the PoolMember entity. - * Requires `pool.totalAmountDistributedUntilUpdatedAt` to be updated prior to calling this function. + * Requires `pool.totalAmountDistributedUntilUpdatedAt` is updated *BEFORE* this function is called. + * Requires that pool.totalUnits and poolMember.units are updated *AFTER* this function is called. * @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); + let amountReceivedDelta = BIG_INT_ZERO; + // if the pool has any units, we calculate the delta + // otherwise the delta is going to be 0 + if (!pool.totalUnits.equals(BIG_INT_ZERO)) { + const distributedAmountDelta = pool.totalAmountDistributedUntilUpdatedAt + .minus(poolMember.poolTotalAmountDistributedUntilUpdatedAt); + + const isSafeToMultiplyWithoutOverflow = MAX_UINT256.div(poolMember.units).gt(distributedAmountDelta); + if (isSafeToMultiplyWithoutOverflow) { + amountReceivedDelta = distributedAmountDelta.times(poolMember.units).div(pool.totalUnits); + } else { + amountReceivedDelta = distributedAmountDelta.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/cfav1.ts b/packages/subgraph/src/mappings/cfav1.ts index fa548d6091..6fd69406bd 100644 --- a/packages/subgraph/src/mappings/cfav1.ts +++ b/packages/subgraph/src/mappings/cfav1.ts @@ -163,7 +163,6 @@ export function handleFlowUpdated(event: FlowUpdated): void { flowRateDelta, isCreate, isDelete, - true, event.block ); diff --git a/packages/subgraph/src/mappings/gdav1.ts b/packages/subgraph/src/mappings/gdav1.ts index 69577afe1d..81381e7464 100644 --- a/packages/subgraph/src/mappings/gdav1.ts +++ b/packages/subgraph/src/mappings/gdav1.ts @@ -93,7 +93,6 @@ export function handlePoolConnectionUpdated( const memberConnectedStatusUpdated = previousIsConnected !== event.params.connected; poolMember.isConnected = event.params.connected; - poolMember.save(); const hasMembershipWithUnits = membershipWithUnitsExists(poolMember.id); @@ -127,10 +126,11 @@ export function handlePoolConnectionUpdated( } } } - pool.save(); // Update totalAmountDistributedUntilUpdatedAt poolMember = updatePoolMemberTotalAmountUntilUpdatedAtFields(pool, poolMember); + + pool.save(); poolMember.save(); // Update Token Stats Streamed Until Updated At diff --git a/packages/subgraph/src/mappings/superfluidPool.ts b/packages/subgraph/src/mappings/superfluidPool.ts index a90b5f5c9f..b018d2377a 100644 --- a/packages/subgraph/src/mappings/superfluidPool.ts +++ b/packages/subgraph/src/mappings/superfluidPool.ts @@ -53,11 +53,16 @@ export function handleMemberUnitsUpdated(event: MemberUnitsUpdated): void { let poolMember = getOrInitPoolMember(event, event.address, event.params.member); const hasMembershipWithUnits = membershipWithUnitsExists(poolMember.id); + let pool = getOrInitPool(event, event.address.toHex()); + const previousUnits = poolMember.units; const unitsDelta = event.params.newUnits.minus(previousUnits); - poolMember.units = event.params.newUnits; - poolMember.save(); + pool = updatePoolTotalAmountFlowedAndDistributed(event, pool); + + poolMember = updatePoolMemberTotalAmountUntilUpdatedAtFields(pool, poolMember); + + poolMember.units = event.params.newUnits; const eventName = "MemberUnitsUpdated"; updateTokenStatsStreamedUntilUpdatedAt(event.params.token, event.block); @@ -66,18 +71,12 @@ export function handleMemberUnitsUpdated(event: MemberUnitsUpdated): void { updateATSStreamedAndBalanceUntilUpdatedAt(event.params.member, event.params.token, event.block, null); _createAccountTokenSnapshotLogEntity(event, event.params.member, event.params.token, eventName); - let pool = getOrInitPool(event, event.address.toHex()); - pool = updatePoolTotalAmountFlowedAndDistributed(event, pool); if (poolMember.isConnected) { pool.totalConnectedUnits = pool.totalConnectedUnits.plus(unitsDelta); } else { pool.totalDisconnectedUnits = pool.totalDisconnectedUnits.plus(unitsDelta); } 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)) { @@ -89,7 +88,6 @@ export function handleMemberUnitsUpdated(event: MemberUnitsUpdated): void { // if the member is disconnected with units now, we add one to disconnected pool.totalDisconnectedMembers = pool.totalDisconnectedMembers + 1; } - pool.save(); updateAggregateDistributionAgreementData( event.params.member, @@ -114,7 +112,6 @@ export function handleMemberUnitsUpdated(event: MemberUnitsUpdated): void { // if the member is disconnected with no units now, we subtract one from disconnected pool.totalDisconnectedMembers = pool.totalDisconnectedMembers - 1; } - pool.save(); updateAggregateDistributionAgreementData( event.params.member, @@ -130,6 +127,9 @@ export function handleMemberUnitsUpdated(event: MemberUnitsUpdated): void { ); } + poolMember.save(); + pool.save(); + // Create Event Entity _createMemberUnitsUpdatedEntity(event, poolMember.id, pool.totalUnits); } diff --git a/packages/subgraph/src/utils.ts b/packages/subgraph/src/utils.ts index 1d11509bc2..859f213a1a 100644 --- a/packages/subgraph/src/utils.ts +++ b/packages/subgraph/src/utils.ts @@ -25,7 +25,7 @@ export const ZERO_ADDRESS = Address.zero(); export const MAX_FLOW_RATE = BigInt.fromI32(2).pow(95).minus(BigInt.fromI32(1)); export const ORDER_MULTIPLIER = BigInt.fromI32(10000); export const MAX_SAFE_SECONDS = BigInt.fromI64(8640000000000); //In seconds, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#the_ecmascript_epoch_and_timestamps - +export const MAX_UINT256 = BigInt.fromString("115792089237316195423570985008687907853269984665640564039457584007913129639935"); /************************************************************************** * Convenience Conversions *************************************************************************/ diff --git a/packages/subgraph/tests/assertionHelpers.ts b/packages/subgraph/tests/assertionHelpers.ts index d844ead10d..faf09c8bdb 100644 --- a/packages/subgraph/tests/assertionHelpers.ts +++ b/packages/subgraph/tests/assertionHelpers.ts @@ -160,7 +160,6 @@ export function assertIDAEventBaseProperties( * @param totalGDAOutflowRate expected total outflow rate for the GDA * @param totalAmountStreamedUntilUpdatedAt expected total amount streamed until updated at timestamp for all flow agreements * @param totalCFAAmountStreamedUntilUpdatedAt expected total amount streamed until updated at timestamp for the CFA - * @param totalGDAAmountStreamedUntilUpdatedAt expected total amount streamed until updated at timestamp for the GDA * @param totalAmountTransferredUntilUpdatedAt expected total amount transferred until updated at timestamp * @param totalAmountDistributedUntilUpdatedAt expected total amount distributed (with IDA) until updated at timestamp * @param totalSupply expected total supply @@ -191,7 +190,6 @@ export function assertTokenStatisticProperties( totalGDAOutflowRate: BigInt, totalAmountStreamedUntilUpdatedAt: BigInt, totalCFAAmountStreamedUntilUpdatedAt: BigInt, - totalGDAAmountStreamedUntilUpdatedAt: BigInt, totalAmountTransferredUntilUpdatedAt: BigInt, totalAmountDistributedUntilUpdatedAt: BigInt, totalSupply: BigInt, @@ -222,7 +220,6 @@ export function assertTokenStatisticProperties( assert.fieldEquals(entityName, id, "totalGDAOutflowRate", totalGDAOutflowRate.toString()); assert.fieldEquals(entityName, id, "totalAmountStreamedUntilUpdatedAt", totalAmountStreamedUntilUpdatedAt.toString()); assert.fieldEquals(entityName, id, "totalCFAAmountStreamedUntilUpdatedAt", totalCFAAmountStreamedUntilUpdatedAt.toString()); - assert.fieldEquals(entityName, id, "totalGDAAmountStreamedUntilUpdatedAt", totalGDAAmountStreamedUntilUpdatedAt.toString()); assert.fieldEquals(entityName, id, "totalAmountTransferredUntilUpdatedAt", totalAmountTransferredUntilUpdatedAt.toString()); assert.fieldEquals(entityName, id, "totalAmountDistributedUntilUpdatedAt", totalAmountDistributedUntilUpdatedAt.toString()); assert.fieldEquals(entityName, id, "totalSupply", totalSupply.toString()); @@ -250,10 +247,8 @@ export function assertTokenStatisticProperties( totalGDADeposit, totalOutflowRate, totalCFAOutflowRate, - totalGDAOutflowRate, totalAmountStreamedUntilUpdatedAt, totalCFAAmountStreamedUntilUpdatedAt, - totalGDAAmountStreamedUntilUpdatedAt, totalAmountTransferredUntilUpdatedAt, totalAmountDistributedUntilUpdatedAt, totalSupply, @@ -302,10 +297,8 @@ export function assertTokenStatisticProperties( * @param totalGDADeposit expected total deposit amount for the GDA * @param totalOutflowRate expected total outflow rate for all flow agreements * @param totalCFAOutflowRate expected total outflow rate for the CFA - * @param totalGDAOutflowRate expected total outflow rate for the GDA * @param totalAmountStreamed expected total amount streamed until timestamp for all flow agreements * @param totalCFAAmountStreamed expected total amount streamed until timestamp for the CFA - * @param totalGDAAmountStreamed expected total amount streamed until timestamp for the GDA * @param totalAmountTransferred expected total amount transferred until timestamp * @param totalAmountDistributed expected total amount distributed (with IDA) until timestamp * @param totalSupply expected total supply @@ -331,10 +324,8 @@ export function assertTokenStatisticLogProperties( totalGDADeposit: BigInt, totalOutflowRate: BigInt, totalCFAOutflowRate: BigInt, - totalGDAOutflowRate: BigInt, totalAmountStreamed: BigInt, totalCFAAmountStreamed: BigInt, - totalGDAAmountStreamed: BigInt, totalAmountTransferred: BigInt, totalAmountDistributed: BigInt, totalSupply: BigInt, @@ -370,10 +361,8 @@ export function assertTokenStatisticLogProperties( assert.fieldEquals(entityName, id, "totalGDADeposit", totalGDADeposit.toString()); assert.fieldEquals(entityName, id, "totalOutflowRate", totalOutflowRate.toString()); assert.fieldEquals(entityName, id, "totalCFAOutflowRate", totalCFAOutflowRate.toString()); - assert.fieldEquals(entityName, id, "totalGDAOutflowRate", totalGDAOutflowRate.toString()); assert.fieldEquals(entityName, id, "totalAmountStreamed", totalAmountStreamed.toString()); assert.fieldEquals(entityName, id, "totalCFAAmountStreamed", totalCFAAmountStreamed.toString()); - assert.fieldEquals(entityName, id, "totalGDAAmountStreamed", totalGDAAmountStreamed.toString()); assert.fieldEquals(entityName, id, "totalAmountTransferred", totalAmountTransferred.toString()); assert.fieldEquals(entityName, id, "totalAmountDistributed", totalAmountDistributed.toString()); assert.fieldEquals(entityName, id, "totalSupply", totalSupply.toString()); @@ -424,7 +413,6 @@ export function assertEmptyTokenStatisticProperties( BIG_INT_ZERO, // totalGDAOutflowRate BIG_INT_ZERO, // totalAmountStreamedUntilUpdatedAt BIG_INT_ZERO, // totalCFAAmountStreamedUntilUpdatedAt - BIG_INT_ZERO, // totalGDAAmountStreamedUntilUpdatedAt BIG_INT_ZERO, // totalAmountTransferredUntilUpdatedAt BIG_INT_ZERO, // totalAmountDistributedUntilUpdatedAt totalSupply, // totalSupply diff --git a/packages/subgraph/tests/superToken/event/superToken.event.test.ts b/packages/subgraph/tests/superToken/event/superToken.event.test.ts index 4a335bd4c0..09d46197f7 100644 --- a/packages/subgraph/tests/superToken/event/superToken.event.test.ts +++ b/packages/subgraph/tests/superToken/event/superToken.event.test.ts @@ -287,7 +287,6 @@ describe("SuperToken Mapper Unit Tests", () => { BIG_INT_ZERO, // totalGDAOutflowRate BIG_INT_ZERO, // totalAmountStreamedUntilUpdatedAt BIG_INT_ZERO, // totalCFAAmountStreamedUntilUpdatedAt - BIG_INT_ZERO, // totalGDAAmountStreamedUntilUpdatedAt BIG_INT_ZERO, // totalAmountTransferredUntilUpdatedAt BIG_INT_ZERO, // totalAmountDistributedUntilUpdatedAt BigInt.fromI32(1000000), // totalSupply = 100 @@ -382,7 +381,6 @@ describe("SuperToken Mapper Unit Tests", () => { BIG_INT_ZERO, // totalGDAOutflowRate BIG_INT_ZERO, // totalAmountStreamedUntilUpdatedAt BIG_INT_ZERO, // totalCFAAmountStreamedUntilUpdatedAt - BIG_INT_ZERO, // totalGDAAmountStreamedUntilUpdatedAt value, // totalAmountTransferredUntilUpdatedAt BIG_INT_ZERO, // totalAmountDistributedUntilUpdatedAt BigInt.fromI32(1000000), // totalSupply = 100 @@ -529,7 +527,6 @@ describe("SuperToken Mapper Unit Tests", () => { BIG_INT_ZERO, // totalGDAOutflowRate BIG_INT_ZERO, // totalAmountStreamedUntilUpdatedAt BIG_INT_ZERO, // totalCFAAmountStreamedUntilUpdatedAt - BIG_INT_ZERO, // totalGDAAmountStreamedUntilUpdatedAt value, // totalAmountTransferredUntilUpdatedAt BIG_INT_ZERO, // totalAmountDistributedUntilUpdatedAt BigInt.fromI32(1000000), // totalSupply = 100 @@ -579,7 +576,6 @@ describe("SuperToken Mapper Unit Tests", () => { BIG_INT_ZERO, // totalGDAOutflowRate BIG_INT_ZERO, // totalAmountStreamedUntilUpdatedAt BIG_INT_ZERO, // totalCFAAmountStreamedUntilUpdatedAt - BIG_INT_ZERO, // totalGDAAmountStreamedUntilUpdatedAt value.times(BigInt.fromI32(2)), // totalAmountTransferredUntilUpdatedAt BIG_INT_ZERO, // totalAmountDistributedUntilUpdatedAt BigInt.fromI32(1000000), // totalSupply = 100