diff --git a/packages/sdk-core/src/subgraph/entities/accountTokenSnapshot/accountTokenSnapshot.ts b/packages/sdk-core/src/subgraph/entities/accountTokenSnapshot/accountTokenSnapshot.ts index 41ea869968..ba2ae5dcf4 100644 --- a/packages/sdk-core/src/subgraph/entities/accountTokenSnapshot/accountTokenSnapshot.ts +++ b/packages/sdk-core/src/subgraph/entities/accountTokenSnapshot/accountTokenSnapshot.ts @@ -31,7 +31,7 @@ export interface AccountTokenSnapshot { totalNetFlowRate: BigNumber; totalNumberOfActiveStreams: number; totalOutflowRate: BigNumber; - maybeCriticalAtTimestamp: Timestamp; + maybeCriticalAtTimestamp: Timestamp | null; isLiquidationEstimateOptimistic: boolean; totalNumberOfClosedStreams: number; totalSubscriptionsWithUnits: number; @@ -76,7 +76,10 @@ export class AccountTokenSnapshotQueryHandler extends SubgraphQueryHandler< account: x.account.id, token: x.token.id, tokenSymbol: x.token.symbol, - maybeCriticalAtTimestamp: Number(x.maybeCriticalAtTimestamp), + maybeCriticalAtTimestamp: + x.maybeCriticalAtTimestamp != null + ? Number(x.maybeCriticalAtTimestamp) + : null, updatedAtBlockNumber: Number(x.updatedAtBlockNumber), updatedAtTimestamp: Number(x.updatedAtTimestamp), })); diff --git a/packages/sdk-core/src/subgraph/entities/accountTokenSnapshotLog/accountTokenSnapshotLog.ts b/packages/sdk-core/src/subgraph/entities/accountTokenSnapshotLog/accountTokenSnapshotLog.ts index f0585c1783..a7c476324c 100644 --- a/packages/sdk-core/src/subgraph/entities/accountTokenSnapshotLog/accountTokenSnapshotLog.ts +++ b/packages/sdk-core/src/subgraph/entities/accountTokenSnapshotLog/accountTokenSnapshotLog.ts @@ -29,7 +29,7 @@ export interface AccountTokenSnapshotLog { timestamp: Timestamp; blockNumber: BlockNumber; balance: BigNumber; - maybeCriticalAtTimestamp: Timestamp; + maybeCriticalAtTimestamp: Timestamp | null; totalAmountStreamed: BigNumber; totalAmountTransferred: BigNumber; totalApprovedSubscriptions: number; @@ -80,7 +80,10 @@ export class AccountTokenSnapshotLogQueryHandler extends SubgraphQueryHandler< account: x.account.id, token: x.token.id, tokenSymbol: x.token.symbol, - maybeCriticalAtTimestamp: Number(x.maybeCriticalAtTimestamp), + maybeCriticalAtTimestamp: + x.maybeCriticalAtTimestamp != null + ? Number(x.maybeCriticalAtTimestamp) + : null, blockNumber: Number(x.blockNumber), timestamp: Number(x.timestamp), order: Number(x.order), diff --git a/packages/sdk-core/src/subgraph/schema.graphql b/packages/sdk-core/src/subgraph/schema.graphql index 325eb3f3d8..96ebb02867 100644 --- a/packages/sdk-core/src/subgraph/schema.graphql +++ b/packages/sdk-core/src/subgraph/schema.graphql @@ -121,9 +121,6 @@ input Account_filter { isSuperApp_not: Boolean isSuperApp_in: [Boolean!] isSuperApp_not_in: [Boolean!] - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum Account_orderBy { @@ -170,7 +167,7 @@ type AccountTokenSnapshot { Optimistic liquidation estimation property. """ - maybeCriticalAtTimestamp: BigInt! + maybeCriticalAtTimestamp: BigInt """ The number of currently open streams. @@ -409,9 +406,6 @@ input AccountTokenSnapshot_filter { token_ends_with_nocase: String token_not_ends_with: String token_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum AccountTokenSnapshot_orderBy { @@ -454,7 +448,7 @@ type AccountTokenSnapshotLog { Optimistic liquidation estimation property. """ - maybeCriticalAtTimestamp: BigInt! + maybeCriticalAtTimestamp: BigInt """ The current (as of timestamp) number of currently open streams. @@ -750,9 +744,6 @@ input AccountTokenSnapshotLog_filter { accountTokenSnapshot_ends_with_nocase: String accountTokenSnapshot_not_ends_with: String accountTokenSnapshot_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum AccountTokenSnapshotLog_orderBy { @@ -883,9 +874,6 @@ input AgreementClassRegisteredEvent_filter { code_not_in: [Bytes!] code_contains: Bytes code_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum AgreementClassRegisteredEvent_orderBy { @@ -1004,9 +992,6 @@ input AgreementClassUpdatedEvent_filter { code_not_in: [Bytes!] code_contains: Bytes code_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum AgreementClassUpdatedEvent_orderBy { @@ -1171,9 +1156,6 @@ input AgreementLiquidatedByEvent_filter { bailoutAmount_lte: BigInt bailoutAmount_in: [BigInt!] bailoutAmount_not_in: [BigInt!] - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum AgreementLiquidatedByEvent_orderBy { @@ -1374,9 +1356,6 @@ input AgreementLiquidatedV2Event_filter { rewardAccount_not_in: [Bytes!] rewardAccount_contains: Bytes rewardAccount_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum AgreementLiquidatedV2Event_orderBy { @@ -1497,9 +1476,6 @@ input AppRegisteredEvent_filter { app_not_in: [Bytes!] app_contains: Bytes app_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum AppRegisteredEvent_orderBy { @@ -1518,14 +1494,22 @@ scalar BigDecimal scalar BigInt +"""The block at which the query should be executed.""" input Block_height { + """Value containing a block hash""" hash: Bytes + + """Value containing a block number""" number: Int - number_gte: Int -} -input BlockChangedFilter { - number_gte: Int! + """ + Value containing the minimum block number. + In the case of `number_gte`, the query will be executed on the latest block only if + the subgraph has progressed to or past the minimum block number. + Defaults to the latest block when omitted. + + """ + number_gte: Int } type BurnedEvent implements Event { @@ -1654,9 +1638,6 @@ input BurnedEvent_filter { operatorData_not_in: [Bytes!] operatorData_contains: Bytes operatorData_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum BurnedEvent_orderBy { @@ -1794,9 +1775,6 @@ input CFAv1LiquidationPeriodChangedEvent_filter { liquidationPeriod_lte: BigInt liquidationPeriod_in: [BigInt!] liquidationPeriod_not_in: [BigInt!] - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum CFAv1LiquidationPeriodChangedEvent_orderBy { @@ -1938,9 +1916,6 @@ input ConfigChangedEvent_filter { value_lte: BigInt value_in: [BigInt!] value_not_in: [BigInt!] - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum ConfigChangedEvent_orderBy { @@ -2055,9 +2030,6 @@ input CustomSuperTokenCreatedEvent_filter { token_not_in: [Bytes!] token_contains: Bytes token_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum CustomSuperTokenCreatedEvent_orderBy { @@ -2167,9 +2139,6 @@ input Event_filter { transactionHash_not_in: [Bytes!] transactionHash_contains: Bytes transactionHash_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum Event_orderBy { @@ -2360,9 +2329,6 @@ input FlowOperator_filter { accountTokenSnapshot_ends_with_nocase: String accountTokenSnapshot_not_ends_with: String accountTokenSnapshot_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum FlowOperator_orderBy { @@ -2534,9 +2500,6 @@ input FlowOperatorUpdatedEvent_filter { flowOperator_ends_with_nocase: String flowOperator_not_ends_with: String flowOperator_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum FlowOperatorUpdatedEvent_orderBy { @@ -2809,9 +2772,6 @@ input FlowUpdatedEvent_filter { stream_ends_with_nocase: String stream_not_ends_with: String stream_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum FlowUpdatedEvent_orderBy { @@ -2941,9 +2901,6 @@ input GovernanceReplacedEvent_filter { newGovernance_not_in: [Bytes!] newGovernance_contains: Bytes newGovernance_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum GovernanceReplacedEvent_orderBy { @@ -3193,9 +3150,6 @@ input Index_filter { indexCreatedEvent_ends_with_nocase: String indexCreatedEvent_not_ends_with: String indexCreatedEvent_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum Index_orderBy { @@ -3362,9 +3316,6 @@ input IndexCreatedEvent_filter { index_ends_with_nocase: String index_not_ends_with: String index_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum IndexCreatedEvent_orderBy { @@ -3532,9 +3483,6 @@ input IndexDistributionClaimedEvent_filter { index_ends_with_nocase: String index_not_ends_with: String index_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum IndexDistributionClaimedEvent_orderBy { @@ -3701,9 +3649,6 @@ input IndexSubscribedEvent_filter { index_ends_with_nocase: String index_not_ends_with: String index_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum IndexSubscribedEvent_orderBy { @@ -3890,9 +3835,6 @@ input IndexSubscription_filter { index_ends_with_nocase: String index_not_ends_with: String index_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum IndexSubscription_orderBy { @@ -4078,9 +4020,6 @@ input IndexUnitsUpdatedEvent_filter { index_ends_with_nocase: String index_not_ends_with: String index_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum IndexUnitsUpdatedEvent_orderBy { @@ -4249,9 +4188,6 @@ input IndexUnsubscribedEvent_filter { index_ends_with_nocase: String index_not_ends_with: String index_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum IndexUnsubscribedEvent_orderBy { @@ -4447,9 +4383,6 @@ input IndexUpdatedEvent_filter { index_ends_with_nocase: String index_not_ends_with: String index_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum IndexUpdatedEvent_orderBy { @@ -4577,9 +4510,6 @@ input JailEvent_filter { reason_lte: BigInt reason_in: [BigInt!] reason_not_in: [BigInt!] - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum JailEvent_orderBy { @@ -4721,9 +4651,6 @@ input MintedEvent_filter { operatorData_not_in: [Bytes!] operatorData_contains: Bytes operatorData_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum MintedEvent_orderBy { @@ -4874,9 +4801,6 @@ input PPPConfigurationChangedEvent_filter { patricianPeriod_lte: BigInt patricianPeriod_in: [BigInt!] patricianPeriod_not_in: [BigInt!] - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum PPPConfigurationChangedEvent_orderBy { @@ -7056,9 +6980,6 @@ input RewardAddressChangedEvent_filter { rewardAddress_not_in: [Bytes!] rewardAddress_contains: Bytes rewardAddress_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum RewardAddressChangedEvent_orderBy { @@ -7186,9 +7107,6 @@ input RoleAdminChangedEvent_filter { newAdminRole_not_in: [Bytes!] newAdminRole_contains: Bytes newAdminRole_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum RoleAdminChangedEvent_orderBy { @@ -7315,9 +7233,6 @@ input RoleGrantedEvent_filter { sender_not_in: [Bytes!] sender_contains: Bytes sender_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum RoleGrantedEvent_orderBy { @@ -7444,9 +7359,6 @@ input RoleRevokedEvent_filter { sender_not_in: [Bytes!] sender_contains: Bytes sender_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum RoleRevokedEvent_orderBy { @@ -7596,9 +7508,6 @@ input SentEvent_filter { operatorData_not_in: [Bytes!] operatorData_contains: Bytes operatorData_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum SentEvent_orderBy { @@ -7705,9 +7614,6 @@ input SFMeta_filter { branch_ends_with_nocase: String branch_not_ends_with: String branch_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum SFMeta_orderBy { @@ -7878,9 +7784,6 @@ input Stream_filter { receiver_ends_with_nocase: String receiver_not_ends_with: String receiver_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum Stream_orderBy { @@ -8114,9 +8017,6 @@ input StreamPeriod_filter { totalAmountStreamed_lte: BigInt totalAmountStreamed_in: [BigInt!] totalAmountStreamed_not_in: [BigInt!] - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum StreamPeriod_orderBy { @@ -8167,9 +8067,6 @@ input StreamRevision_filter { periodRevisionIndex_lte: Int periodRevisionIndex_in: [Int!] periodRevisionIndex_not_in: [Int!] - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum StreamRevision_orderBy { @@ -10371,9 +10268,6 @@ input SubscriptionApprovedEvent_filter { subscription_ends_with_nocase: String subscription_not_ends_with: String subscription_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum SubscriptionApprovedEvent_orderBy { @@ -10542,9 +10436,6 @@ input SubscriptionDistributionClaimedEvent_filter { subscription_ends_with_nocase: String subscription_not_ends_with: String subscription_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum SubscriptionDistributionClaimedEvent_orderBy { @@ -10711,9 +10602,6 @@ input SubscriptionRevokedEvent_filter { subscription_ends_with_nocase: String subscription_not_ends_with: String subscription_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum SubscriptionRevokedEvent_orderBy { @@ -10898,9 +10786,6 @@ input SubscriptionUnitsUpdatedEvent_filter { subscription_ends_with_nocase: String subscription_not_ends_with: String subscription_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum SubscriptionUnitsUpdatedEvent_orderBy { @@ -11018,9 +10903,6 @@ input SuperTokenCreatedEvent_filter { token_not_in: [Bytes!] token_contains: Bytes token_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum SuperTokenCreatedEvent_orderBy { @@ -11131,9 +11013,6 @@ input SuperTokenFactoryUpdatedEvent_filter { newFactory_not_in: [Bytes!] newFactory_contains: Bytes newFactory_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum SuperTokenFactoryUpdatedEvent_orderBy { @@ -11244,9 +11123,6 @@ input SuperTokenLogicCreatedEvent_filter { tokenLogic_not_in: [Bytes!] tokenLogic_contains: Bytes tokenLogic_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum SuperTokenLogicCreatedEvent_orderBy { @@ -11364,9 +11240,6 @@ input SuperTokenLogicUpdatedEvent_filter { code_not_in: [Bytes!] code_contains: Bytes code_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum SuperTokenLogicUpdatedEvent_orderBy { @@ -11527,9 +11400,6 @@ input Token_filter { underlyingToken_ends_with_nocase: String underlyingToken_not_ends_with: String underlyingToken_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum Token_orderBy { @@ -11671,9 +11541,6 @@ input TokenDowngradedEvent_filter { amount_lte: BigInt amount_in: [BigInt!] amount_not_in: [BigInt!] - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum TokenDowngradedEvent_orderBy { @@ -11921,9 +11788,6 @@ input TokenStatistic_filter { token_ends_with_nocase: String token_not_ends_with: String token_not_ends_with_nocase: String - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum TokenStatistic_orderBy { @@ -12071,9 +11935,6 @@ input TokenUpgradedEvent_filter { amount_lte: BigInt amount_in: [BigInt!] amount_not_in: [BigInt!] - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum TokenUpgradedEvent_orderBy { @@ -12237,9 +12098,6 @@ input TransferEvent_filter { token_not_in: [Bytes!] token_contains: Bytes token_not_contains: Bytes - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum TransferEvent_orderBy { @@ -12377,9 +12235,6 @@ input TrustedForwarderChangedEvent_filter { enabled_not: Boolean enabled_in: [Boolean!] enabled_not_in: [Boolean!] - - """Filter for the block changed event.""" - _change_block: BlockChangedFilter } enum TrustedForwarderChangedEvent_orderBy { diff --git a/packages/subgraph/schema.graphql b/packages/subgraph/schema.graphql index 9edebb1caa..13abe8da94 100644 --- a/packages/subgraph/schema.graphql +++ b/packages/subgraph/schema.graphql @@ -1208,7 +1208,7 @@ type AccountTokenSnapshot @entity { """ Optimistic liquidation estimation property. """ - maybeCriticalAtTimestamp: BigInt! + maybeCriticalAtTimestamp: BigInt """ The number of currently open streams. @@ -1289,7 +1289,7 @@ type AccountTokenSnapshotLog @entity { """ Optimistic liquidation estimation property. """ - maybeCriticalAtTimestamp: BigInt! + maybeCriticalAtTimestamp: BigInt """ The current (as of timestamp) number of currently open streams. """ diff --git a/packages/subgraph/src/mappingHelpers.ts b/packages/subgraph/src/mappingHelpers.ts index 63c700672b..bf98a2d5ff 100644 --- a/packages/subgraph/src/mappingHelpers.ts +++ b/packages/subgraph/src/mappingHelpers.ts @@ -399,7 +399,7 @@ export function getOrInitAccountTokenSnapshot( accountTokenSnapshot.totalAmountTransferredUntilUpdatedAt = BIG_INT_ZERO; accountTokenSnapshot.totalDeposit = BIG_INT_ZERO; - accountTokenSnapshot.maybeCriticalAtTimestamp = BIG_INT_ZERO; + accountTokenSnapshot.maybeCriticalAtTimestamp = null; accountTokenSnapshot.account = accountAddress.toHex(); accountTokenSnapshot.token = tokenAddress.toHex(); accountTokenSnapshot.save(); @@ -542,8 +542,8 @@ function updateATSBalanceAndUpdatedAt( accountTokenSnapshot.maybeCriticalAtTimestamp = calculateMaybeCriticalAtTimestamp( accountTokenSnapshot.updatedAtTimestamp, accountTokenSnapshot.balanceUntilUpdatedAt, - accountTokenSnapshot.totalDeposit, - accountTokenSnapshot.totalNetFlowRate + accountTokenSnapshot.totalNetFlowRate, + accountTokenSnapshot.maybeCriticalAtTimestamp ); accountTokenSnapshot.save(); @@ -592,8 +592,8 @@ export function updateATSStreamedAndBalanceUntilUpdatedAt( /** * This function should always be called with updateATSStreamedAndBalanceUntilUpdatedAt - * @param tokenAddress - * @param block + * @param tokenAddress + * @param block */ export function updateTokenStatsStreamedUntilUpdatedAt( tokenAddress: Address, @@ -689,8 +689,8 @@ export function updateAggregateEntitiesStreamData( senderATS.maybeCriticalAtTimestamp = calculateMaybeCriticalAtTimestamp( senderATS.updatedAtTimestamp, senderATS.balanceUntilUpdatedAt, - senderATS.totalDeposit, - senderATS.totalNetFlowRate + senderATS.totalNetFlowRate, + senderATS.maybeCriticalAtTimestamp ); let receiverATS = getOrInitAccountTokenSnapshot( @@ -719,8 +719,8 @@ export function updateAggregateEntitiesStreamData( receiverATS.maybeCriticalAtTimestamp = calculateMaybeCriticalAtTimestamp( receiverATS.updatedAtTimestamp, receiverATS.balanceUntilUpdatedAt, - receiverATS.totalDeposit, - receiverATS.totalNetFlowRate + receiverATS.totalNetFlowRate, + receiverATS.maybeCriticalAtTimestamp ); receiverATS.save(); diff --git a/packages/subgraph/src/utils.ts b/packages/subgraph/src/utils.ts index b042dfc419..4b744c37a3 100644 --- a/packages/subgraph/src/utils.ts +++ b/packages/subgraph/src/utils.ts @@ -242,23 +242,29 @@ export function getAmountStreamedSinceLastUpdatedAt( } /** - * calculateMaybeCriticalAtTimestamp will return optimistic date based on updatedAtTimestamp, totalDeposit, balanceUntilUpdatedAt and totalNetFlowRate. + * calculateMaybeCriticalAtTimestamp will return optimistic date based on updatedAtTimestamp, balanceUntilUpdatedAt and totalNetFlowRate. * @param updatedAtTimestamp * @param balanceUntilUpdatedAt - * @param totalDeposit * @param totalNetFlowRate + * @param previousMaybeCriticalAtTimestamp */ export function calculateMaybeCriticalAtTimestamp( updatedAtTimestamp: BigInt, balanceUntilUpdatedAt: BigInt, - totalDeposit: BigInt, - totalNetFlowRate: BigInt -): BigInt { - if (balanceUntilUpdatedAt.le(BIG_INT_ZERO)) return BIG_INT_ZERO; - if (totalNetFlowRate.ge(BIG_INT_ZERO)) return BIG_INT_ZERO; - const criticalTimestamp = balanceUntilUpdatedAt.plus(totalDeposit).div(totalNetFlowRate.abs()); - const calculatedCriticalTimestamp = criticalTimestamp.plus(updatedAtTimestamp); + totalNetFlowRate: BigInt, + previousMaybeCriticalAtTimestamp: BigInt | null, +): BigInt | null { + // When the flow rate is not negative then there's no way to have a critical balance timestamp anymore. + if (totalNetFlowRate.ge(BIG_INT_ZERO)) return null; + + // When there's no balance then that either means: + // 1. account is already critical, and we keep the existing timestamp when the liquidations supposedly started + // 2. it's a new account without a critical balance timestamp to begin with + if (balanceUntilUpdatedAt.le(BIG_INT_ZERO)) return previousMaybeCriticalAtTimestamp; + + const secondsUntilCritical = balanceUntilUpdatedAt.div(totalNetFlowRate.abs()); + const calculatedCriticalTimestamp = updatedAtTimestamp.plus(secondsUntilCritical); if (calculatedCriticalTimestamp.gt(MAX_SAFE_SECONDS)) { return MAX_SAFE_SECONDS; } diff --git a/packages/subgraph/test/helpers/helpers.ts b/packages/subgraph/test/helpers/helpers.ts index 8f61a8b471..6396b30c73 100644 --- a/packages/subgraph/test/helpers/helpers.ts +++ b/packages/subgraph/test/helpers/helpers.ts @@ -14,6 +14,7 @@ import TestTokenABI from "../../abis/TestToken.json"; import {TestToken} from "../../typechain"; import {SignerWithAddress} from "@nomiclabs/hardhat-ethers/signers"; import {BigNumber} from "ethers"; +import {BigInt} from "@graphprotocol/graph-ts"; // the resolver address should be consistent as long as you use the // first account retrieved by hardhat's ethers.getSigners(): @@ -468,13 +469,16 @@ export const getOrder = (blockNumber?: number, logIndex?: number) => { export function calculateMaybeCriticalAtTimestamp( updatedAtTimestamp: string, balanceUntilUpdatedAt: string, - totalDeposit: string, - totalNetFlowRate: string + totalNetFlowRate: string, ) { - if (BigNumber.from(balanceUntilUpdatedAt).lte(BigNumber.from("0"))) return "0"; - if (BigNumber.from(totalNetFlowRate).gte(BigNumber.from("0"))) return "0"; - const criticalTimestamp = BigNumber.from(balanceUntilUpdatedAt).add(BigNumber.from(totalDeposit)).div(BigNumber.from(totalNetFlowRate).abs()); - const calculatedCriticalTimestamp = criticalTimestamp.add(BigNumber.from(updatedAtTimestamp)); + // When the flow rate is not negative then there's no way to have a critical balance timestamp anymore. + if (toBN(totalNetFlowRate).gte(toBN("0"))) return null; + // When there's no balance then that either means: + // 1. account is already critical, and we keep the existing timestamp when the liquidations supposedly started + // 2. it's a new account without a critical balance timestamp to begin with + if (toBN(balanceUntilUpdatedAt).lte(toBN("0"))) throw new Error("This will never gonna hit `Already critical` case because can't simulate realistic liquidation" ); //https://github.com/superfluid-finance/protocol-monorepo/pull/885 + const secondsUntilCritical = toBN(balanceUntilUpdatedAt).div(toBN(totalNetFlowRate).abs()); + const calculatedCriticalTimestamp = secondsUntilCritical.add(toBN(updatedAtTimestamp)); if (calculatedCriticalTimestamp.gt(MAX_SAFE_SECONDS)) { return MAX_SAFE_SECONDS.toString(); } diff --git a/packages/subgraph/test/validation/aggregateValidators.ts b/packages/subgraph/test/validation/aggregateValidators.ts index be2a00cfb5..17a3fc47b0 100644 --- a/packages/subgraph/test/validation/aggregateValidators.ts +++ b/packages/subgraph/test/validation/aggregateValidators.ts @@ -53,7 +53,7 @@ export const validateATSEntity = ( totalDeposit: expectedTotalDeposit, } = expectedATSData; - const calculatedCriticalTimestamp = calculateMaybeCriticalAtTimestamp(expectedATSData.updatedAtTimestamp, expectedBalanceUntilUpdatedAt, expectedTotalDeposit, expectedTotalNetFlowRate); + const calculatedCriticalTimestamp = calculateMaybeCriticalAtTimestamp(expectedATSData.updatedAtTimestamp, expectedBalanceUntilUpdatedAt, expectedTotalNetFlowRate); expect(graphATSData.maybeCriticalAtTimestamp, "ATS: maybeCriticalAtTimestamp error").to.equal(calculatedCriticalTimestamp) expect(graphATSData.totalNumberOfActiveStreams, "ATS: totalNumberOfActiveStreams error"