From 21be4222f232e213af48edeb5f566043778ce8b1 Mon Sep 17 00:00:00 2001 From: Mehul Soni Date: Mon, 16 May 2022 15:09:36 +0300 Subject: [PATCH 1/9] added logs entitt --- packages/subgraph/schema.graphql | 76 +++++++++++++++++++++++++ packages/subgraph/src/mappingHelpers.ts | 50 +++++++++++++--- packages/subgraph/src/utils.ts | 19 +++++++ 3 files changed, 137 insertions(+), 8 deletions(-) diff --git a/packages/subgraph/schema.graphql b/packages/subgraph/schema.graphql index 47d35802bb..bf4f9db4f1 100644 --- a/packages/subgraph/schema.graphql +++ b/packages/subgraph/schema.graphql @@ -1255,6 +1255,82 @@ type AccountTokenSnapshot @entity { account: Account! token: Token! flowOperators: [FlowOperator!]! @derivedFrom(field: "accountTokenSnapshot") + accountTokenSnapshotLogs: [AccountTokenSnapshotLog!]! @derivedFrom(field: "accountTokenSnapshot") +} + +""" +AccountTokenSnapshotLog: An aggregate entity which aggregates data on an account's +interaction with a `token`. +""" +type AccountTokenSnapshotLog @entity { + id: ID! + timestamp: BigInt! + blockNumber: BigInt! + + transactionHash: Bytes! + logIndex: BigInt! + order: BigInt! + + # ---------------------------------- state ---------------------------------- + """ + The number of currently open streams. + """ + totalNumberOfActiveStreamsSoFar: Int! + + """ + The number of all-time closed streams. + """ + totalNumberOfClosedStreamsSoFar: Int! + + """ + The current (as of updatedAt) number of subscriptions with units allocated to them tied to this `account`. + """ + totalSubscriptionsWithUnitsSoFar: Int! + + """ + Counts all currently (as of updatedAt) approved subscriptions whether or not they have units. + """ + totalApprovedSubscriptionsSoFar: Int! + + """ + Balance of `account` as of `updatedAtTimestamp`/`updatedAtBlock`. + """ + balanceSoFar: BigInt! + + """ + The total deposit this account has held by the CFA agreement for `account` active streams. + """ + totalDepositSoFar: BigInt! + + """ + The total net flow rate of the `account` as of `updatedAtTimestamp`/`updatedAtBlock`. + """ + totalNetFlowRateSoFar: BigInt! + + """ + The total inflow rate (receive flowRate per second) of the `account`. + """ + totalInflowRateSoFar: BigInt! + + """ + The total outflow rate (send flowrate per second) of the `account`. + """ + totalOutflowRateSoFar: BigInt! + + """ + The total amount of `token` streamed to this `account` until + the `updatedAtTimestamp`/`updatedAtBlock`. + """ + totalAmountStreamedSoFar: BigInt! + + """ + The total amount of `token` this `account` has transferred. + """ + totalAmountTransferredSoFar: BigInt! + # ---------------------------------- links ---------------------------------- + account: Account! + token: Token! + accountTokenSnapshot: AccountTokenSnapshot! } """ diff --git a/packages/subgraph/src/mappingHelpers.ts b/packages/subgraph/src/mappingHelpers.ts index 769a3402c9..994bea758d 100644 --- a/packages/subgraph/src/mappingHelpers.ts +++ b/packages/subgraph/src/mappingHelpers.ts @@ -1,8 +1,9 @@ -import { Address, BigInt, Bytes, ethereum } from "@graphprotocol/graph-ts"; -import { ISuperfluid as Superfluid } from "../generated/Host/ISuperfluid"; +import {Address, BigInt, ethereum} from "@graphprotocol/graph-ts"; +import {ISuperfluid as Superfluid} from "../generated/Host/ISuperfluid"; import { Account, AccountTokenSnapshot, + AccountTokenSnapshotLog, FlowOperator, Index, IndexSubscription, @@ -13,23 +14,25 @@ import { } from "../generated/schema"; import { BIG_INT_ZERO, + createLogID, getAccountTokenSnapshotID, getAmountStreamedSinceLastUpdatedAt, + getFlowOperatorID, getIndexID, getIsListedToken, + getOrder, getStreamID, getStreamRevisionID, getSubscriptionID, getTokenInfoAndReturn, - updateTotalSupplyForNativeSuperToken, streamRevisionExists, + updateTotalSupplyForNativeSuperToken, ZERO_ADDRESS, - getFlowOperatorID, } from "./utils"; -import { SuperToken as SuperTokenTemplate } from "../generated/templates"; -import { ISuperToken as SuperToken } from "../generated/templates/SuperToken/ISuperToken"; -import { getHostAddress, getResolverAddress } from "./addresses"; -import { FlowUpdated } from "../generated/ConstantFlowAgreementV1/IConstantFlowAgreementV1"; +import {SuperToken as SuperTokenTemplate} from "../generated/templates"; +import {ISuperToken as SuperToken} from "../generated/templates/SuperToken/ISuperToken"; +import {getHostAddress, getResolverAddress} from "./addresses"; +import {FlowUpdated} from "../generated/ConstantFlowAgreementV1/IConstantFlowAgreementV1"; /************************************************************************** * HOL initializer functions @@ -717,3 +720,34 @@ export function updateAggregateEntitiesTransferData( tokenStatistic.totalAmountTransferredUntilUpdatedAt.plus(value); tokenStatistic.save(); } + +function createAccountTokenSnapshotLogEntity( + event: FlowUpdated, + accountTokenSnapshot: AccountTokenSnapshot, +): void { + if (accountTokenSnapshot == null) return; + // Transaction + let ev = new AccountTokenSnapshotLog(createLogID("AccountTokenSnapshotLog", accountTokenSnapshot.id, event)); + ev.transactionHash = event.transaction.hash; + ev.timestamp = event.block.timestamp; + ev.order = getOrder(event.block.number, event.logIndex); + ev.blockNumber = event.block.number; + ev.logIndex = event.logIndex; + + // Account token snapshot state + ev.totalNumberOfActiveStreamsSoFar = accountTokenSnapshot.totalNumberOfActiveStreams; + ev.totalNumberOfClosedStreamsSoFar = accountTokenSnapshot.totalNumberOfClosedStreams; + ev.totalSubscriptionsWithUnitsSoFar = accountTokenSnapshot.totalSubscriptionsWithUnits; + ev.totalApprovedSubscriptionsSoFar = accountTokenSnapshot.totalApprovedSubscriptions; + ev.balanceSoFar = accountTokenSnapshot.balanceUntilUpdatedAt; + ev.totalNetFlowRateSoFar = accountTokenSnapshot.totalNetFlowRate; + ev.totalInflowRateSoFar = accountTokenSnapshot.totalInflowRate; + ev.totalOutflowRateSoFar = accountTokenSnapshot.totalOutflowRate; + ev.totalAmountStreamedSoFar = accountTokenSnapshot.totalAmountStreamedUntilUpdatedAt; + ev.totalAmountTransferredSoFar = accountTokenSnapshot.totalAmountTransferredUntilUpdatedAt; + ev.totalDepositSoFar = accountTokenSnapshot.totalDeposit; + ev.account = accountTokenSnapshot.account; + ev.token = accountTokenSnapshot.token; + ev.accountTokenSnapshot = accountTokenSnapshot; + ev.save(); +} diff --git a/packages/subgraph/src/utils.ts b/packages/subgraph/src/utils.ts index 7109916f91..c1fe7a00a8 100644 --- a/packages/subgraph/src/utils.ts +++ b/packages/subgraph/src/utils.ts @@ -253,3 +253,22 @@ export function getOrder( return blockNumber.times(ORDER_MULTIPLIER).plus(logIndex); } +/************************************************************************** + * Log entities util functions + *************************************************************************/ + +export function createLogID( + logPrefix: string, + aggregateId: string, + event: ethereum.Event, +): string { + return ( + logPrefix + + "-" + + aggregateId + + "-" + + event.transaction.hash.toHexString() + + "-" + + event.logIndex.toString() + ); +} From 6df583cde655f7137fb8d5e2d92b9ea0bf8f73a3 Mon Sep 17 00:00:00 2001 From: Mehul Soni Date: Tue, 17 May 2022 15:34:24 +0300 Subject: [PATCH 2/9] added code for account balance log --- packages/subgraph/src/mappingHelpers.ts | 14 ++++++++++---- packages/subgraph/src/mappings/cfav1.ts | 3 +++ packages/subgraph/src/mappings/idav1.ts | 12 +++++++++++- packages/subgraph/src/mappings/superToken.ts | 13 ++++++++++++- 4 files changed, 36 insertions(+), 6 deletions(-) diff --git a/packages/subgraph/src/mappingHelpers.ts b/packages/subgraph/src/mappingHelpers.ts index 994bea758d..1f41a02978 100644 --- a/packages/subgraph/src/mappingHelpers.ts +++ b/packages/subgraph/src/mappingHelpers.ts @@ -721,10 +721,16 @@ export function updateAggregateEntitiesTransferData( tokenStatistic.save(); } -function createAccountTokenSnapshotLogEntity( - event: FlowUpdated, - accountTokenSnapshot: AccountTokenSnapshot, +export function createAccountTokenSnapshotLogEntity( + event: ethereum.Event, + accountAddress: Address, + tokenAddress: Address, ): void { + if (accountAddress.equals(ZERO_ADDRESS)) { + return; + } + let atsId = getAccountTokenSnapshotID(accountAddress, tokenAddress); + let accountTokenSnapshot = AccountTokenSnapshot.load(atsId); if (accountTokenSnapshot == null) return; // Transaction let ev = new AccountTokenSnapshotLog(createLogID("AccountTokenSnapshotLog", accountTokenSnapshot.id, event)); @@ -748,6 +754,6 @@ function createAccountTokenSnapshotLogEntity( ev.totalDepositSoFar = accountTokenSnapshot.totalDeposit; ev.account = accountTokenSnapshot.account; ev.token = accountTokenSnapshot.token; - ev.accountTokenSnapshot = accountTokenSnapshot; + ev.accountTokenSnapshot = accountTokenSnapshot.id; ev.save(); } diff --git a/packages/subgraph/src/mappings/cfav1.ts b/packages/subgraph/src/mappings/cfav1.ts index deeb89df19..c14d73c350 100644 --- a/packages/subgraph/src/mappings/cfav1.ts +++ b/packages/subgraph/src/mappings/cfav1.ts @@ -19,6 +19,7 @@ import { ZERO_ADDRESS, } from "../utils"; import { + createAccountTokenSnapshotLogEntity, getOrInitFlowOperator, getOrInitStream, getOrInitStreamRevision, @@ -327,6 +328,8 @@ export function handleStreamUpdated(event: FlowUpdated): void { isDelete, event.block ); + createAccountTokenSnapshotLogEntity(event,senderAddress, tokenAddress); + createAccountTokenSnapshotLogEntity(event,receiverAddress, tokenAddress); } // NOTE: This handler is run right after handleStreamUpdated as the FlowUpdatedExtension diff --git a/packages/subgraph/src/mappings/idav1.ts b/packages/subgraph/src/mappings/idav1.ts index c5066fdd5a..0c6f13dd17 100644 --- a/packages/subgraph/src/mappings/idav1.ts +++ b/packages/subgraph/src/mappings/idav1.ts @@ -31,6 +31,7 @@ import { tokenHasValidHost, } from "../utils"; import { + createAccountTokenSnapshotLogEntity, getOrInitIndex, getOrInitSubscription, getOrInitTokenStatistic, @@ -77,6 +78,7 @@ export function handleIndexCreated(event: IndexCreated): void { event.block ); + createAccountTokenSnapshotLogEntity(event, event.params.publisher, event.params.token); createIndexCreatedEntity(event, index.id); } @@ -149,7 +151,7 @@ export function handleIndexUpdated(event: IndexUpdated): void { event.params.token, event.block ); - + createAccountTokenSnapshotLogEntity(event, event.params.publisher, event.params.token); createIndexUpdatedEntity(event, index.id); } @@ -242,6 +244,7 @@ export function handleSubscriptionApproved(event: SubscriptionApproved): void { event.params.token, event.block ); + createAccountTokenSnapshotLogEntity(event, event.params.publisher, event.params.token); } subscription.save(); @@ -263,6 +266,7 @@ export function handleSubscriptionApproved(event: SubscriptionApproved): void { index.save(); createSubscriptionApprovedEntity(event, subscription.id); + createAccountTokenSnapshotLogEntity(event,event.params.subscriber, event.params.token) } export function handleSubscriptionDistributionClaimed( @@ -307,6 +311,8 @@ export function handleSubscriptionDistributionClaimed( event.params.token, event.block ); + createAccountTokenSnapshotLogEntity(event, event.params.publisher, event.params.token); + createAccountTokenSnapshotLogEntity(event, event.params.subscriber, event.params.token); } /** @@ -396,6 +402,8 @@ export function handleSubscriptionRevoked(event: SubscriptionRevoked): void { subscription.save(); createSubscriptionRevokedEntity(event, subscription.id); + createAccountTokenSnapshotLogEntity(event,event.params.subscriber, event.params.token) + createAccountTokenSnapshotLogEntity(event, event.params.publisher, event.params.token); } /** @@ -520,6 +528,8 @@ export function handleSubscriptionUnitsUpdated( subscription.save(); createSubscriptionUnitsUpdatedEntity(event, subscription.id, oldUnits); + createAccountTokenSnapshotLogEntity(event,event.params.subscriber, event.params.token); + createAccountTokenSnapshotLogEntity(event,event.params.subscriber, event.params.token); } /************************************************************************** diff --git a/packages/subgraph/src/mappings/superToken.ts b/packages/subgraph/src/mappings/superToken.ts index 6f354a6cfe..875defd0c0 100644 --- a/packages/subgraph/src/mappings/superToken.ts +++ b/packages/subgraph/src/mappings/superToken.ts @@ -18,8 +18,9 @@ import { SentEvent, AgreementLiquidatedV2Event, } from "../../generated/schema"; -import {createEventID, getOrder, tokenHasValidHost} from "../utils"; +import {createEventID, getOrder, tokenHasValidHost, ZERO_ADDRESS} from "../utils"; import { + createAccountTokenSnapshotLogEntity, getOrInitAccount, getOrInitSuperToken, getOrInitTokenStatistic, @@ -53,6 +54,9 @@ function updateHOLEntitiesForLiquidation( event.address, event.block ); + createAccountTokenSnapshotLogEntity(event, targetAccount, event.address); + createAccountTokenSnapshotLogEntity(event, liquidatorAccount, event.address); + createAccountTokenSnapshotLogEntity(event, bondAccount, event.address); } export function handleAgreementLiquidatedBy( @@ -111,6 +115,7 @@ export function handleTokenUpgraded(event: TokenUpgraded): void { event.address, event.block ); + createAccountTokenSnapshotLogEntity(event, event.params.account, event.address); } export function handleTokenDowngraded(event: TokenDowngraded): void { @@ -131,6 +136,7 @@ export function handleTokenDowngraded(event: TokenDowngraded): void { event.address, event.block ); + createAccountTokenSnapshotLogEntity(event, event.params.account, event.address); } export function handleTransfer(event: Transfer): void { @@ -164,6 +170,11 @@ export function handleTransfer(event: Transfer): void { event.params.value, event.block ); + + if(event.params.to.equals(ZERO_ADDRESS)) return; + if(event.params.from.equals(ZERO_ADDRESS)) return; // Ignoring downgrade and upgrade transfer event logs. + createAccountTokenSnapshotLogEntity(event, event.params.to, event.address); + createAccountTokenSnapshotLogEntity(event, event.params.from, event.address); } export function handleSent(event: Sent): void { From 9fe604196c3c2378447b4e25c94f4b5f4d8f0034 Mon Sep 17 00:00:00 2001 From: Mehul Soni Date: Wed, 18 May 2022 07:56:47 +0300 Subject: [PATCH 3/9] fixed -- added test cases --- .../subgraph/test/helpers/initializers.ts | 20 ++--- packages/subgraph/test/interfaces.ts | 23 +++++- .../subgraph/test/queries/aggregateQueries.ts | 56 ++++++++++++++ .../test/validation/aggregateValidators.ts | 73 +++++++++++++++++-- 4 files changed, 151 insertions(+), 21 deletions(-) diff --git a/packages/subgraph/test/helpers/initializers.ts b/packages/subgraph/test/helpers/initializers.ts index 62b090ef4a..66be3e74fe 100644 --- a/packages/subgraph/test/helpers/initializers.ts +++ b/packages/subgraph/test/helpers/initializers.ts @@ -9,23 +9,16 @@ import { IAccountTokenSnapshot, + IFlowOperator, + IFlowOperatorUpdatedInitTestData, IFlowUpdatedInitTestData, IIndex, + IIndexSubscription, IInstantDistributionTestData, IStreamData, - IIndexSubscription, ITokenStatistic, - IFlowOperatorUpdatedInitTestData, - IFlowOperator, } from "../interfaces"; -import { - getATSId, - getFlowOperatorId, - getIndexId, - getRevisionIndexId, - getStreamId, - getSubscriptionId, -} from "./helpers"; +import {getATSId, getFlowOperatorId, getIndexId, getRevisionIndexId, getStreamId, getSubscriptionId,} from "./helpers"; export const getOrInitRevisionIndex = ( revisionIndexes: { [id: string]: number | undefined }, @@ -181,9 +174,10 @@ export const getOrInitAccountTokenSnapshot = ( totalNetFlowRate: "0", totalInflowRate: "0", totalOutflowRate: "0", - account: { id: accountId }, - token: { id: tokenId }, + account: {id: accountId}, + token: {id: tokenId}, flowOperators: [], + accountTokenSnapshotLogs: [], }; } return existingATS; diff --git a/packages/subgraph/test/interfaces.ts b/packages/subgraph/test/interfaces.ts index 4dda75d0d7..ce1d318227 100644 --- a/packages/subgraph/test/interfaces.ts +++ b/packages/subgraph/test/interfaces.ts @@ -301,7 +301,6 @@ export interface IIndex extends IBaseEntity { /** * Aggregate Entities */ - export interface IBaseAggregateEntity { readonly id: string; readonly updatedAtTimestamp: string; @@ -323,6 +322,28 @@ export interface IAccountTokenSnapshot extends IBaseAggregateEntity { readonly account: ILightEntity; readonly token: ILightEntity; readonly flowOperators: ILightEntity[]; + readonly accountTokenSnapshotLogs: IAccountTokenSnapshotLog[]; +} + +export interface IAccountTokenSnapshotLog { + readonly transactionHash: string; + readonly blockNumber: string; + readonly logIndex: string; + readonly timestamp: string; + readonly order: string; + readonly balanceSoFar: string; + readonly totalApprovedSubscriptionsSoFar: number; + readonly totalNumberOfActiveStreamsSoFar: number; + readonly totalNumberOfClosedStreamsSoFar: number; + readonly totalSubscriptionsWithUnitsSoFar: number; + readonly totalAmountStreamedSoFar: string; + readonly totalAmountTransferredSoFar: string; + readonly totalDepositSoFar: string; + readonly totalInflowRateSoFar: string; + readonly totalNetFlowRateSoFar: string; + readonly totalOutflowRateSoFar: string; + readonly account: ILightEntity; + readonly token: ILightEntity; } export interface ITokenStatistic extends IBaseAggregateEntity { diff --git a/packages/subgraph/test/queries/aggregateQueries.ts b/packages/subgraph/test/queries/aggregateQueries.ts index 00e4a204ea..af7aafa2c5 100644 --- a/packages/subgraph/test/queries/aggregateQueries.ts +++ b/packages/subgraph/test/queries/aggregateQueries.ts @@ -25,6 +25,28 @@ export const getAccountTokenSnapshot = gql` token { id } + accountTokenSnapshotLogs( + first: 1 + orderBy: order + orderDirection: desc + ) { + blockNumber + transactionHash + balanceSoFar + logIndex + order + timestamp + totalAmountStreamedSoFar + totalAmountTransferredSoFar + totalApprovedSubscriptionsSoFar + totalDepositSoFar + totalInflowRateSoFar + totalNetFlowRateSoFar + totalNumberOfActiveStreamsSoFar + totalNumberOfClosedStreamsSoFar + totalOutflowRateSoFar + totalSubscriptionsWithUnitsSoFar + } } } `; @@ -53,3 +75,37 @@ export const getTokenStatistic = gql` } } `; + +export const getAccountTokenSnapshotLogs = gql` + query getAccountTokenSnapshotLogs($id: ID!) { + response: accountTokenSnapshots( + where: { account: $id } + first: 1 + orderBy: order + orderDirection: desc + ) { + blockNumber + transactionHash + balanceSoFar + logIndex + order + timestamp + totalAmountStreamedSoFar + totalAmountTransferredSoFar + totalApprovedSubscriptionsSoFar + totalDepositSoFar + totalInflowRateSoFar + totalNetFlowRateSoFar + totalNumberOfActiveStreamsSoFar + totalNumberOfClosedStreamsSoFar + totalOutflowRateSoFar + totalSubscriptionsWithUnitsSoFar + account { + id + } + token { + id + } + } + } +`; diff --git a/packages/subgraph/test/validation/aggregateValidators.ts b/packages/subgraph/test/validation/aggregateValidators.ts index 730b846d36..df672ccf9a 100644 --- a/packages/subgraph/test/validation/aggregateValidators.ts +++ b/packages/subgraph/test/validation/aggregateValidators.ts @@ -1,10 +1,7 @@ -import { expect } from "chai"; -import { fetchEntityAndEnsureExistence } from "../helpers/helpers"; -import { IAccountTokenSnapshot, ITokenStatistic } from "../interfaces"; -import { - getAccountTokenSnapshot, - getTokenStatistic, -} from "../queries/aggregateQueries"; +import {expect} from "chai"; +import {fetchEntityAndEnsureExistence} from "../helpers/helpers"; +import {IAccountTokenSnapshot, IAccountTokenSnapshotLog, ITokenStatistic} from "../interfaces"; +import {getAccountTokenSnapshot, getTokenStatistic,} from "../queries/aggregateQueries"; export const fetchATSAndValidate = async ( expectedATSData: IAccountTokenSnapshot @@ -15,6 +12,7 @@ export const fetchATSAndValidate = async ( "AccountTokenSnapshot" ); validateATSEntity(graphATS, expectedATSData); + validateASTEntityAndItsLogEntries(graphATS); }; export const fetchTokenStatsAndValidate = async ( @@ -98,6 +96,67 @@ export const validateATSEntity = ( ).to.equal(expectedTotalAmountTransferredUntilUpdatedAt); }; +export const validateASTEntityAndItsLogEntries = ( + graphATSData: IAccountTokenSnapshot +) => { + const accountTokenSnapshotLogs: IAccountTokenSnapshotLog[] = graphATSData.accountTokenSnapshotLogs; + const accountTokenSnapshotLog: IAccountTokenSnapshotLog = accountTokenSnapshotLogs[0]; + console.log(accountTokenSnapshotLog, graphATSData) + console.log(); + expect( + graphATSData.totalNumberOfActiveStreams, + "ATS: totalNumberOfActiveStreams error" + ).to.equal(accountTokenSnapshotLog.totalNumberOfActiveStreamsSoFar); + expect( + graphATSData.totalNumberOfClosedStreams, + "ATS: totalNumberOfClosedStreams error" + ).to.equal(accountTokenSnapshotLog.totalNumberOfClosedStreamsSoFar); + expect( + graphATSData.totalSubscriptionWithUnits, + "ATS: totalSubscriptionWithUnits error" + ).to.equal(accountTokenSnapshotLog.totalSubscriptionsWithUnitsSoFar); + expect( + graphATSData.totalApprovedSubscriptions, + "ATS: totalApprovedSubscriptions error" + ).to.equal(accountTokenSnapshotLog.totalApprovedSubscriptionsSoFar); + expect( + graphATSData.balanceUntilUpdatedAt, + "ATS: balanceUntilUpdatedAt error" + ).to.equal(accountTokenSnapshotLog.balanceSoFar); + expect( + graphATSData.totalNetFlowRate, + "ATS: totalNetFlowRate error" + ).to.equal(accountTokenSnapshotLog.totalNetFlowRateSoFar); + expect(graphATSData.totalInflowRate, "ATS: totalInflowRate error").to.equal( + accountTokenSnapshotLog.totalInflowRateSoFar + ); + expect(graphATSData.totalDeposit, "ATS: totalDeposit error").to.equal( + accountTokenSnapshotLog.totalDepositSoFar + ); + expect( + graphATSData.totalOutflowRate, + "ATS: totalOutflowRate error" + ).to.equal(accountTokenSnapshotLog.totalOutflowRateSoFar); + expect( + graphATSData.totalAmountStreamedUntilUpdatedAt, + "ATS: totalAmountStreamedUntilUpdatedAt error" + ).to.equal(accountTokenSnapshotLog.totalAmountStreamedSoFar); + expect( + graphATSData.totalAmountTransferredUntilUpdatedAt, + "ATS: totalAmountTransferredUntilUpdatedAt error" + ).to.equal(accountTokenSnapshotLog.totalAmountStreamedSoFar); + + expect( + graphATSData.updatedAtTimestamp, + "ATS: updatedAtTimestamp error" + ).to.equal(accountTokenSnapshotLog.timestamp); + + expect( + graphATSData.updatedAtBlockNumber, + "ATS: updatedAtBlockNumber error" + ).to.equal(accountTokenSnapshotLog.blockNumber); +} + /** * Validates the TokenStatistic entity. * @param graphTokenStats From b26cffd9d288325a0fbcd4f708094aebb42ba311 Mon Sep 17 00:00:00 2001 From: Mehul Soni Date: Wed, 18 May 2022 11:55:30 +0300 Subject: [PATCH 4/9] fixed ida broken test cases --- .../test/validation/aggregateValidators.ts | 15 ++++--- .../subgraph/test/validation/validators.ts | 42 +++++++++---------- 2 files changed, 26 insertions(+), 31 deletions(-) diff --git a/packages/subgraph/test/validation/aggregateValidators.ts b/packages/subgraph/test/validation/aggregateValidators.ts index df672ccf9a..dca5081efc 100644 --- a/packages/subgraph/test/validation/aggregateValidators.ts +++ b/packages/subgraph/test/validation/aggregateValidators.ts @@ -4,7 +4,8 @@ import {IAccountTokenSnapshot, IAccountTokenSnapshotLog, ITokenStatistic} from " import {getAccountTokenSnapshot, getTokenStatistic,} from "../queries/aggregateQueries"; export const fetchATSAndValidate = async ( - expectedATSData: IAccountTokenSnapshot + expectedATSData: IAccountTokenSnapshot, + skipLogEntryValidation: Boolean ) => { const graphATS = await fetchEntityAndEnsureExistence( getAccountTokenSnapshot, @@ -12,7 +13,7 @@ export const fetchATSAndValidate = async ( "AccountTokenSnapshot" ); validateATSEntity(graphATS, expectedATSData); - validateASTEntityAndItsLogEntries(graphATS); + if(!skipLogEntryValidation) validateASTEntityAndItsLogEntry(graphATS); }; export const fetchTokenStatsAndValidate = async ( @@ -96,13 +97,11 @@ export const validateATSEntity = ( ).to.equal(expectedTotalAmountTransferredUntilUpdatedAt); }; -export const validateASTEntityAndItsLogEntries = ( +export const validateASTEntityAndItsLogEntry = ( graphATSData: IAccountTokenSnapshot ) => { const accountTokenSnapshotLogs: IAccountTokenSnapshotLog[] = graphATSData.accountTokenSnapshotLogs; const accountTokenSnapshotLog: IAccountTokenSnapshotLog = accountTokenSnapshotLogs[0]; - console.log(accountTokenSnapshotLog, graphATSData) - console.log(); expect( graphATSData.totalNumberOfActiveStreams, "ATS: totalNumberOfActiveStreams error" @@ -112,7 +111,7 @@ export const validateASTEntityAndItsLogEntries = ( "ATS: totalNumberOfClosedStreams error" ).to.equal(accountTokenSnapshotLog.totalNumberOfClosedStreamsSoFar); expect( - graphATSData.totalSubscriptionWithUnits, + graphATSData.totalSubscriptionsWithUnits, "ATS: totalSubscriptionWithUnits error" ).to.equal(accountTokenSnapshotLog.totalSubscriptionsWithUnitsSoFar); expect( @@ -127,6 +126,7 @@ export const validateASTEntityAndItsLogEntries = ( graphATSData.totalNetFlowRate, "ATS: totalNetFlowRate error" ).to.equal(accountTokenSnapshotLog.totalNetFlowRateSoFar); + expect(graphATSData.totalInflowRate, "ATS: totalInflowRate error").to.equal( accountTokenSnapshotLog.totalInflowRateSoFar ); @@ -144,8 +144,7 @@ export const validateASTEntityAndItsLogEntries = ( expect( graphATSData.totalAmountTransferredUntilUpdatedAt, "ATS: totalAmountTransferredUntilUpdatedAt error" - ).to.equal(accountTokenSnapshotLog.totalAmountStreamedSoFar); - + ).to.equal(accountTokenSnapshotLog.totalAmountTransferredSoFar); expect( graphATSData.updatedAtTimestamp, "ATS: updatedAtTimestamp error" diff --git a/packages/subgraph/test/validation/validators.ts b/packages/subgraph/test/validation/validators.ts index cf77d36d5d..5972a0b2e9 100644 --- a/packages/subgraph/test/validation/validators.ts +++ b/packages/subgraph/test/validation/validators.ts @@ -1,28 +1,24 @@ import { IAccountTokenSnapshot, + IEvent, + IExpectedFlowOperatorData, + IIDAEvents, IIndex, - IStreamData, IIndexSubscription, - ITokenStatistic, - IEvent, ILightEntity, - IIDAEvents, - IFlowOperator, - IExpectedFlowOperatorData, + IStreamData, + ITokenStatistic, } from "../interfaces"; -import { fetchStreamPeriodAndValidate } from "./hol/streamPeriodValidator"; -import { fetchIndexAndValidate } from "./hol/indexValidator"; -import { fetchStreamAndValidate } from "./hol/streamValidator"; -import { fetchSubscriptionAndValidate } from "./hol/subscriptionValidator"; -import { - fetchATSAndValidate, - fetchTokenStatsAndValidate, -} from "./aggregateValidators"; -import { Framework } from "@superfluid-finance/sdk-core"; -import { BigNumber } from "@ethersproject/bignumber"; -import { expect } from "chai"; -import { FlowActionType, IDAEventType } from "../helpers/constants"; -import { fetchFlowOperatorAndValidate } from "./hol/flowOperatorValidator"; +import {fetchStreamPeriodAndValidate} from "./hol/streamPeriodValidator"; +import {fetchIndexAndValidate} from "./hol/indexValidator"; +import {fetchStreamAndValidate} from "./hol/streamValidator"; +import {fetchSubscriptionAndValidate} from "./hol/subscriptionValidator"; +import {fetchATSAndValidate, fetchTokenStatsAndValidate,} from "./aggregateValidators"; +import {Framework} from "@superfluid-finance/sdk-core"; +import {BigNumber} from "@ethersproject/bignumber"; +import {expect} from "chai"; +import {FlowActionType, IDAEventType} from "../helpers/constants"; +import {fetchFlowOperatorAndValidate} from "./hol/flowOperatorValidator"; export async function validateFlowUpdated( pastStreamData: IStreamData, @@ -54,10 +50,10 @@ export async function validateFlowUpdated( ); // validate sender ATS - await fetchATSAndValidate(updatedSenderATS); + await fetchATSAndValidate(updatedSenderATS, false); // validate receiver ATS - await fetchATSAndValidate(updatedReceiverATS); + await fetchATSAndValidate(updatedReceiverATS, false); // validate token stats await fetchTokenStatsAndValidate(updatedTokenStats); @@ -103,7 +99,7 @@ export async function validateModifyIDA( events, subscriptionExists ); - await fetchATSAndValidate(updatedSubscriberATS); + await fetchATSAndValidate(updatedSubscriberATS, true); } await fetchIndexAndValidate( framework, @@ -113,7 +109,7 @@ export async function validateModifyIDA( updatedSubscription.id, subscriptionExists ); - await fetchATSAndValidate(updatedPublisherATS); + await fetchATSAndValidate(updatedPublisherATS, true); await fetchTokenStatsAndValidate(updatedTokenStats); } From 52ca199df2404eaf52de1202909d9ddb7b64d1d3 Mon Sep 17 00:00:00 2001 From: Mehul Soni Date: Wed, 18 May 2022 12:03:28 +0300 Subject: [PATCH 5/9] comment added --- packages/subgraph/test/validation/aggregateValidators.ts | 2 +- packages/subgraph/test/validation/validators.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/subgraph/test/validation/aggregateValidators.ts b/packages/subgraph/test/validation/aggregateValidators.ts index dca5081efc..7ea83ec36c 100644 --- a/packages/subgraph/test/validation/aggregateValidators.ts +++ b/packages/subgraph/test/validation/aggregateValidators.ts @@ -5,7 +5,7 @@ import {getAccountTokenSnapshot, getTokenStatistic,} from "../queries/aggregateQ export const fetchATSAndValidate = async ( expectedATSData: IAccountTokenSnapshot, - skipLogEntryValidation: Boolean + skipLogEntryValidation: Boolean // Boolean flag to decide, whether to check log entries or not. Ignore IDA claim/distribute case ) => { const graphATS = await fetchEntityAndEnsureExistence( getAccountTokenSnapshot, diff --git a/packages/subgraph/test/validation/validators.ts b/packages/subgraph/test/validation/validators.ts index 5972a0b2e9..58b9b179fa 100644 --- a/packages/subgraph/test/validation/validators.ts +++ b/packages/subgraph/test/validation/validators.ts @@ -50,10 +50,10 @@ export async function validateFlowUpdated( ); // validate sender ATS - await fetchATSAndValidate(updatedSenderATS, false); + await fetchATSAndValidate(updatedSenderATS, false); // Boolean flag to decide, whether to check log entries or not. // validate receiver ATS - await fetchATSAndValidate(updatedReceiverATS, false); + await fetchATSAndValidate(updatedReceiverATS, false); // Boolean flag to decide, whether to check log entries or not. // validate token stats await fetchTokenStatsAndValidate(updatedTokenStats); @@ -99,7 +99,7 @@ export async function validateModifyIDA( events, subscriptionExists ); - await fetchATSAndValidate(updatedSubscriberATS, true); + await fetchATSAndValidate(updatedSubscriberATS, true); // Boolean flag to decide, whether to check log entries or not. } await fetchIndexAndValidate( framework, @@ -109,7 +109,7 @@ export async function validateModifyIDA( updatedSubscription.id, subscriptionExists ); - await fetchATSAndValidate(updatedPublisherATS, true); + await fetchATSAndValidate(updatedPublisherATS, true); // Boolean flag to decide, whether to check log entries or not. await fetchTokenStatsAndValidate(updatedTokenStats); } From 43e8eb9ba197fd7ea9e09ccc86c5a55391bba418 Mon Sep 17 00:00:00 2001 From: Mehul Soni Date: Wed, 18 May 2022 12:17:41 +0300 Subject: [PATCH 6/9] comment added --- packages/subgraph/schema.graphql | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/subgraph/schema.graphql b/packages/subgraph/schema.graphql index bf4f9db4f1..a92f8dc5a1 100644 --- a/packages/subgraph/schema.graphql +++ b/packages/subgraph/schema.graphql @@ -1259,8 +1259,7 @@ type AccountTokenSnapshot @entity { } """ -AccountTokenSnapshotLog: An aggregate entity which aggregates data on an account's -interaction with a `token`. +AccountTokenSnapshotLog: Historical entries of AccountTokenSnapshot updates. """ type AccountTokenSnapshotLog @entity { id: ID! From 2b970a02ca038e03caaebf770335449343641044 Mon Sep 17 00:00:00 2001 From: Mehul Soni Date: Wed, 18 May 2022 12:59:48 +0300 Subject: [PATCH 7/9] merged critical at timestamp changes into branch --- packages/subgraph/schema.graphql | 5 ++ packages/subgraph/src/mappingHelpers.ts | 50 +++++++++---------- packages/subgraph/src/mappings/cfav1.ts | 4 +- packages/subgraph/src/mappings/idav1.ts | 23 +++++---- packages/subgraph/src/mappings/superToken.ts | 45 +++++++++-------- packages/subgraph/test/interfaces.ts | 2 + .../subgraph/test/queries/aggregateQueries.ts | 37 +------------- .../test/validation/aggregateValidators.ts | 3 ++ 8 files changed, 75 insertions(+), 94 deletions(-) diff --git a/packages/subgraph/schema.graphql b/packages/subgraph/schema.graphql index 9719dd8a35..62c3f8f2c9 100644 --- a/packages/subgraph/schema.graphql +++ b/packages/subgraph/schema.graphql @@ -1274,9 +1274,14 @@ type AccountTokenSnapshotLog @entity { transactionHash: Bytes! logIndex: BigInt! order: BigInt! + triggeredByEventName: String! # ---------------------------------- state ---------------------------------- """ + Optimistic liquidation estimation property. + """ + maybeCriticalAtTimestampSoFar: BigInt! + """ The number of currently open streams. """ totalNumberOfActiveStreamsSoFar: Int! diff --git a/packages/subgraph/src/mappingHelpers.ts b/packages/subgraph/src/mappingHelpers.ts index 4ff876d1f4..4e193aaf3a 100644 --- a/packages/subgraph/src/mappingHelpers.ts +++ b/packages/subgraph/src/mappingHelpers.ts @@ -748,35 +748,35 @@ export function createAccountTokenSnapshotLogEntity( event: ethereum.Event, accountAddress: Address, tokenAddress: Address, + eventName: string, ): void { if (accountAddress.equals(ZERO_ADDRESS)) { return; } - let atsId = getAccountTokenSnapshotID(accountAddress, tokenAddress); - let accountTokenSnapshot = AccountTokenSnapshot.load(atsId); - if (accountTokenSnapshot == null) return; + let accountTokenSnapshot = getOrInitAccountTokenSnapshot(accountAddress, tokenAddress, event.block); // Transaction - let ev = new AccountTokenSnapshotLog(createLogID("AccountTokenSnapshotLog", accountTokenSnapshot.id, event)); - ev.transactionHash = event.transaction.hash; - ev.timestamp = event.block.timestamp; - ev.order = getOrder(event.block.number, event.logIndex); - ev.blockNumber = event.block.number; - ev.logIndex = event.logIndex; - + let atsLog = new AccountTokenSnapshotLog(createLogID("ATSLog", accountTokenSnapshot.id, event)); + atsLog.transactionHash = event.transaction.hash; + atsLog.timestamp = event.block.timestamp; + atsLog.order = getOrder(event.block.number, event.logIndex); + atsLog.blockNumber = event.block.number; + atsLog.logIndex = event.logIndex; + atsLog.triggeredByEventName = eventName; // Account token snapshot state - ev.totalNumberOfActiveStreamsSoFar = accountTokenSnapshot.totalNumberOfActiveStreams; - ev.totalNumberOfClosedStreamsSoFar = accountTokenSnapshot.totalNumberOfClosedStreams; - ev.totalSubscriptionsWithUnitsSoFar = accountTokenSnapshot.totalSubscriptionsWithUnits; - ev.totalApprovedSubscriptionsSoFar = accountTokenSnapshot.totalApprovedSubscriptions; - ev.balanceSoFar = accountTokenSnapshot.balanceUntilUpdatedAt; - ev.totalNetFlowRateSoFar = accountTokenSnapshot.totalNetFlowRate; - ev.totalInflowRateSoFar = accountTokenSnapshot.totalInflowRate; - ev.totalOutflowRateSoFar = accountTokenSnapshot.totalOutflowRate; - ev.totalAmountStreamedSoFar = accountTokenSnapshot.totalAmountStreamedUntilUpdatedAt; - ev.totalAmountTransferredSoFar = accountTokenSnapshot.totalAmountTransferredUntilUpdatedAt; - ev.totalDepositSoFar = accountTokenSnapshot.totalDeposit; - ev.account = accountTokenSnapshot.account; - ev.token = accountTokenSnapshot.token; - ev.accountTokenSnapshot = accountTokenSnapshot.id; - ev.save(); + atsLog.totalNumberOfActiveStreamsSoFar = accountTokenSnapshot.totalNumberOfActiveStreams; + atsLog.totalNumberOfClosedStreamsSoFar = accountTokenSnapshot.totalNumberOfClosedStreams; + atsLog.totalSubscriptionsWithUnitsSoFar = accountTokenSnapshot.totalSubscriptionsWithUnits; + atsLog.totalApprovedSubscriptionsSoFar = accountTokenSnapshot.totalApprovedSubscriptions; + atsLog.balanceSoFar = accountTokenSnapshot.balanceUntilUpdatedAt; + atsLog.totalNetFlowRateSoFar = accountTokenSnapshot.totalNetFlowRate; + atsLog.totalInflowRateSoFar = accountTokenSnapshot.totalInflowRate; + atsLog.totalOutflowRateSoFar = accountTokenSnapshot.totalOutflowRate; + atsLog.totalAmountStreamedSoFar = accountTokenSnapshot.totalAmountStreamedUntilUpdatedAt; + atsLog.totalAmountTransferredSoFar = accountTokenSnapshot.totalAmountTransferredUntilUpdatedAt; + atsLog.totalDepositSoFar = accountTokenSnapshot.totalDeposit; + atsLog.maybeCriticalAtTimestampSoFar = accountTokenSnapshot.maybeCriticalAtTimestamp; + atsLog.account = accountTokenSnapshot.account; + atsLog.token = accountTokenSnapshot.token; + atsLog.accountTokenSnapshot = accountTokenSnapshot.id; + atsLog.save(); } diff --git a/packages/subgraph/src/mappings/cfav1.ts b/packages/subgraph/src/mappings/cfav1.ts index c14d73c350..e35b297f0c 100644 --- a/packages/subgraph/src/mappings/cfav1.ts +++ b/packages/subgraph/src/mappings/cfav1.ts @@ -328,8 +328,8 @@ export function handleStreamUpdated(event: FlowUpdated): void { isDelete, event.block ); - createAccountTokenSnapshotLogEntity(event,senderAddress, tokenAddress); - createAccountTokenSnapshotLogEntity(event,receiverAddress, tokenAddress); + createAccountTokenSnapshotLogEntity(event, senderAddress, tokenAddress, "FlowUpdated"); + createAccountTokenSnapshotLogEntity(event, receiverAddress, tokenAddress, "FlowUpdated"); } // NOTE: This handler is run right after handleStreamUpdated as the FlowUpdatedExtension diff --git a/packages/subgraph/src/mappings/idav1.ts b/packages/subgraph/src/mappings/idav1.ts index 0c6f13dd17..e6fee6aca4 100644 --- a/packages/subgraph/src/mappings/idav1.ts +++ b/packages/subgraph/src/mappings/idav1.ts @@ -26,7 +26,8 @@ import { import { BIG_INT_ZERO, createEventID, - getIndexID, getOrder, + getIndexID, + getOrder, subscriptionExists as subscriptionWithUnitsExists, tokenHasValidHost, } from "../utils"; @@ -78,7 +79,7 @@ export function handleIndexCreated(event: IndexCreated): void { event.block ); - createAccountTokenSnapshotLogEntity(event, event.params.publisher, event.params.token); + createAccountTokenSnapshotLogEntity(event, event.params.publisher, event.params.token, "IndexCreated"); createIndexCreatedEntity(event, index.id); } @@ -151,7 +152,7 @@ export function handleIndexUpdated(event: IndexUpdated): void { event.params.token, event.block ); - createAccountTokenSnapshotLogEntity(event, event.params.publisher, event.params.token); + createAccountTokenSnapshotLogEntity(event, event.params.publisher, event.params.token, "IndexUpdated"); createIndexUpdatedEntity(event, index.id); } @@ -244,7 +245,7 @@ export function handleSubscriptionApproved(event: SubscriptionApproved): void { event.params.token, event.block ); - createAccountTokenSnapshotLogEntity(event, event.params.publisher, event.params.token); + createAccountTokenSnapshotLogEntity(event, event.params.publisher, event.params.token, "SubscriptionApproved"); } subscription.save(); @@ -266,7 +267,7 @@ export function handleSubscriptionApproved(event: SubscriptionApproved): void { index.save(); createSubscriptionApprovedEntity(event, subscription.id); - createAccountTokenSnapshotLogEntity(event,event.params.subscriber, event.params.token) + createAccountTokenSnapshotLogEntity(event, event.params.subscriber, event.params.token, "SubscriptionApproved") } export function handleSubscriptionDistributionClaimed( @@ -311,8 +312,8 @@ export function handleSubscriptionDistributionClaimed( event.params.token, event.block ); - createAccountTokenSnapshotLogEntity(event, event.params.publisher, event.params.token); - createAccountTokenSnapshotLogEntity(event, event.params.subscriber, event.params.token); + createAccountTokenSnapshotLogEntity(event, event.params.publisher, event.params.token, "SubscriptionDistributionClaimed"); + createAccountTokenSnapshotLogEntity(event, event.params.subscriber, event.params.token, "SubscriptionDistributionClaimed"); } /** @@ -402,8 +403,8 @@ export function handleSubscriptionRevoked(event: SubscriptionRevoked): void { subscription.save(); createSubscriptionRevokedEntity(event, subscription.id); - createAccountTokenSnapshotLogEntity(event,event.params.subscriber, event.params.token) - createAccountTokenSnapshotLogEntity(event, event.params.publisher, event.params.token); + createAccountTokenSnapshotLogEntity(event, event.params.subscriber, event.params.token, "SubscriptionRevoked") + createAccountTokenSnapshotLogEntity(event, event.params.publisher, event.params.token, "SubscriptionRevoked"); } /** @@ -528,8 +529,8 @@ export function handleSubscriptionUnitsUpdated( subscription.save(); createSubscriptionUnitsUpdatedEntity(event, subscription.id, oldUnits); - createAccountTokenSnapshotLogEntity(event,event.params.subscriber, event.params.token); - createAccountTokenSnapshotLogEntity(event,event.params.subscriber, event.params.token); + createAccountTokenSnapshotLogEntity(event, event.params.subscriber, event.params.token, "SubscriptionUnitsUpdated"); + createAccountTokenSnapshotLogEntity(event, event.params.subscriber, event.params.token, "SubscriptionUnitsUpdated"); } /************************************************************************** diff --git a/packages/subgraph/src/mappings/superToken.ts b/packages/subgraph/src/mappings/superToken.ts index 875defd0c0..fe5e233c6d 100644 --- a/packages/subgraph/src/mappings/superToken.ts +++ b/packages/subgraph/src/mappings/superToken.ts @@ -1,22 +1,22 @@ import { - TokenUpgraded, - TokenDowngraded, - Transfer, AgreementLiquidatedBy, + AgreementLiquidatedV2, Burned, Minted, Sent, - AgreementLiquidatedV2, + TokenDowngraded, + TokenUpgraded, + Transfer, } from "../../generated/templates/SuperToken/ISuperToken"; import { AgreementLiquidatedByEvent, + AgreementLiquidatedV2Event, BurnedEvent, MintedEvent, - TokenUpgradedEvent, + SentEvent, TokenDowngradedEvent, + TokenUpgradedEvent, TransferEvent, - SentEvent, - AgreementLiquidatedV2Event, } from "../../generated/schema"; import {createEventID, getOrder, tokenHasValidHost, ZERO_ADDRESS} from "../utils"; import { @@ -28,14 +28,15 @@ import { updateATSStreamedAndBalanceUntilUpdatedAt, updateTokenStatsStreamedUntilUpdatedAt, } from "../mappingHelpers"; -import { getHostAddress } from "../addresses"; -import { Address, BigInt, ethereum, log } from "@graphprotocol/graph-ts"; +import {getHostAddress} from "../addresses"; +import {Address, BigInt, ethereum, log} from "@graphprotocol/graph-ts"; function updateHOLEntitiesForLiquidation( event: ethereum.Event, liquidatorAccount: Address, targetAccount: Address, - bondAccount: Address + bondAccount: Address, + eventName: String, ): void { getOrInitSuperToken(event.address, event.block); @@ -54,9 +55,9 @@ function updateHOLEntitiesForLiquidation( event.address, event.block ); - createAccountTokenSnapshotLogEntity(event, targetAccount, event.address); - createAccountTokenSnapshotLogEntity(event, liquidatorAccount, event.address); - createAccountTokenSnapshotLogEntity(event, bondAccount, event.address); + createAccountTokenSnapshotLogEntity(event, targetAccount, event.address, eventName); + createAccountTokenSnapshotLogEntity(event, liquidatorAccount, event.address, eventName); + createAccountTokenSnapshotLogEntity(event, bondAccount, event.address, eventName); } export function handleAgreementLiquidatedBy( @@ -74,7 +75,8 @@ export function handleAgreementLiquidatedBy( event, event.params.liquidatorAccount, event.params.penaltyAccount, - event.params.bondAccount + event.params.bondAccount, + "AgreementLiquidatedBy" ); } @@ -93,7 +95,8 @@ export function handleAgreementLiquidatedV2( event, event.params.liquidatorAccount, event.params.targetAccount, - event.params.rewardAccount + event.params.rewardAccount, + "AgreementLiquidatedV2" ); } @@ -115,7 +118,7 @@ export function handleTokenUpgraded(event: TokenUpgraded): void { event.address, event.block ); - createAccountTokenSnapshotLogEntity(event, event.params.account, event.address); + createAccountTokenSnapshotLogEntity(event, event.params.account, event.address, "TokenUpgraded"); } export function handleTokenDowngraded(event: TokenDowngraded): void { @@ -136,7 +139,7 @@ export function handleTokenDowngraded(event: TokenDowngraded): void { event.address, event.block ); - createAccountTokenSnapshotLogEntity(event, event.params.account, event.address); + createAccountTokenSnapshotLogEntity(event, event.params.account, event.address, "TokenDowngraded"); } export function handleTransfer(event: Transfer): void { @@ -171,10 +174,10 @@ export function handleTransfer(event: Transfer): void { event.block ); - if(event.params.to.equals(ZERO_ADDRESS)) return; - if(event.params.from.equals(ZERO_ADDRESS)) return; // Ignoring downgrade and upgrade transfer event logs. - createAccountTokenSnapshotLogEntity(event, event.params.to, event.address); - createAccountTokenSnapshotLogEntity(event, event.params.from, event.address); + if (event.params.to.equals(ZERO_ADDRESS)) return; + if (event.params.from.equals(ZERO_ADDRESS)) return; // Ignoring downgrade and upgrade transfer event logs. + createAccountTokenSnapshotLogEntity(event, event.params.to, event.address, "Transfer"); + createAccountTokenSnapshotLogEntity(event, event.params.from, event.address, "Transfer"); } export function handleSent(event: Sent): void { diff --git a/packages/subgraph/test/interfaces.ts b/packages/subgraph/test/interfaces.ts index 6d6e646609..f11cdd311e 100644 --- a/packages/subgraph/test/interfaces.ts +++ b/packages/subgraph/test/interfaces.ts @@ -332,6 +332,7 @@ export interface IAccountTokenSnapshotLog { readonly logIndex: string; readonly timestamp: string; readonly order: string; + readonly triggeredByEventName: string; readonly balanceSoFar: string; readonly totalApprovedSubscriptionsSoFar: number; readonly totalNumberOfActiveStreamsSoFar: number; @@ -343,6 +344,7 @@ export interface IAccountTokenSnapshotLog { readonly totalInflowRateSoFar: string; readonly totalNetFlowRateSoFar: string; readonly totalOutflowRateSoFar: string; + readonly maybeCriticalAtTimestampSoFar: string; readonly account: ILightEntity; readonly token: ILightEntity; } diff --git a/packages/subgraph/test/queries/aggregateQueries.ts b/packages/subgraph/test/queries/aggregateQueries.ts index 58db1dbe48..fe2bd69aa6 100644 --- a/packages/subgraph/test/queries/aggregateQueries.ts +++ b/packages/subgraph/test/queries/aggregateQueries.ts @@ -6,7 +6,6 @@ export const getAccountTokenSnapshot = gql` updatedAtTimestamp updatedAtBlockNumber totalNumberOfActiveStreams - maybeCriticalAtTimestamp totalNumberOfClosedStreams totalSubscriptionsWithUnits totalApprovedSubscriptions @@ -40,6 +39,7 @@ export const getAccountTokenSnapshot = gql` timestamp totalAmountStreamedSoFar totalAmountTransferredSoFar + maybeCriticalAtTimestampSoFar totalApprovedSubscriptionsSoFar totalDepositSoFar totalInflowRateSoFar @@ -48,6 +48,7 @@ export const getAccountTokenSnapshot = gql` totalNumberOfClosedStreamsSoFar totalOutflowRateSoFar totalSubscriptionsWithUnitsSoFar + triggeredByEventName } } } @@ -77,37 +78,3 @@ export const getTokenStatistic = gql` } } `; - -export const getAccountTokenSnapshotLogs = gql` - query getAccountTokenSnapshotLogs($id: ID!) { - response: accountTokenSnapshots( - where: { account: $id } - first: 1 - orderBy: order - orderDirection: desc - ) { - blockNumber - transactionHash - balanceSoFar - logIndex - order - timestamp - totalAmountStreamedSoFar - totalAmountTransferredSoFar - totalApprovedSubscriptionsSoFar - totalDepositSoFar - totalInflowRateSoFar - totalNetFlowRateSoFar - totalNumberOfActiveStreamsSoFar - totalNumberOfClosedStreamsSoFar - totalOutflowRateSoFar - totalSubscriptionsWithUnitsSoFar - account { - id - } - token { - id - } - } - } -`; diff --git a/packages/subgraph/test/validation/aggregateValidators.ts b/packages/subgraph/test/validation/aggregateValidators.ts index 21ffc07539..8bd84609f9 100644 --- a/packages/subgraph/test/validation/aggregateValidators.ts +++ b/packages/subgraph/test/validation/aggregateValidators.ts @@ -103,6 +103,9 @@ export const validateASTEntityAndItsLogEntry = ( ) => { const accountTokenSnapshotLogs: IAccountTokenSnapshotLog[] = graphATSData.accountTokenSnapshotLogs; const accountTokenSnapshotLog: IAccountTokenSnapshotLog = accountTokenSnapshotLogs[0]; + + expect(graphATSData.maybeCriticalAtTimestamp, "ATS: maybeCriticalAtTimestamp error") + .to.equal(accountTokenSnapshotLog.maybeCriticalAtTimestampSoFar) expect( graphATSData.totalNumberOfActiveStreams, "ATS: totalNumberOfActiveStreams error" From 4bf0e537b79587bcd17d3aea12d554fce7bc76a9 Mon Sep 17 00:00:00 2001 From: Mehul Soni Date: Wed, 18 May 2022 14:26:52 +0300 Subject: [PATCH 8/9] added isLiquidationEstimateOptimistic flag --- packages/subgraph/schema.graphql | 6 ++++++ packages/subgraph/src/mappingHelpers.ts | 3 +++ 2 files changed, 9 insertions(+) diff --git a/packages/subgraph/schema.graphql b/packages/subgraph/schema.graphql index 62c3f8f2c9..50cae903d1 100644 --- a/packages/subgraph/schema.graphql +++ b/packages/subgraph/schema.graphql @@ -1196,6 +1196,12 @@ type AccountTokenSnapshot @entity { updatedAtTimestamp: BigInt! updatedAtBlockNumber: BigInt! # ---------------------------------- state ---------------------------------- + + """ + isLiquidationEstimateOptimistic, If they have `0` subcription with no units it return true or else false. + """ + isLiquidationEstimateOptimistic: Boolean! + """ Optimistic liquidation estimation property. """ diff --git a/packages/subgraph/src/mappingHelpers.ts b/packages/subgraph/src/mappingHelpers.ts index 4e193aaf3a..e9aeb44fad 100644 --- a/packages/subgraph/src/mappingHelpers.ts +++ b/packages/subgraph/src/mappingHelpers.ts @@ -389,6 +389,7 @@ export function getOrInitAccountTokenSnapshot( accountTokenSnapshot.totalNumberOfActiveStreams = 0; accountTokenSnapshot.totalNumberOfClosedStreams = 0; accountTokenSnapshot.totalSubscriptionsWithUnits = 0; + accountTokenSnapshot.isLiquidationEstimateOptimistic = false; accountTokenSnapshot.totalApprovedSubscriptions = 0; accountTokenSnapshot.balanceUntilUpdatedAt = BIG_INT_ZERO; accountTokenSnapshot.totalNetFlowRate = BIG_INT_ZERO; @@ -492,6 +493,7 @@ export function updateAggregateIDASubscriptionsData( accountTokenSnapshot.totalSubscriptionsWithUnits = accountTokenSnapshot.totalSubscriptionsWithUnits + totalSubscriptionWithUnitsDelta; + accountTokenSnapshot.isLiquidationEstimateOptimistic = accountTokenSnapshot.totalSubscriptionsWithUnits > 0; accountTokenSnapshot.totalApprovedSubscriptions = accountTokenSnapshot.totalApprovedSubscriptions + totalApprovedSubscriptionsDelta; @@ -504,6 +506,7 @@ export function updateAggregateIDASubscriptionsData( tokenStatistic.totalSubscriptionsWithUnits = tokenStatistic.totalSubscriptionsWithUnits + totalSubscriptionWithUnitsDelta; + accountTokenSnapshot.isLiquidationEstimateOptimistic = accountTokenSnapshot.totalSubscriptionsWithUnits > 0; tokenStatistic.totalApprovedSubscriptions = tokenStatistic.totalApprovedSubscriptions + totalApprovedSubscriptionsDelta; From c5663db5a534039d2dafb239b033f3720bc8d648 Mon Sep 17 00:00:00 2001 From: Mehul Soni Date: Wed, 18 May 2022 15:54:03 +0300 Subject: [PATCH 9/9] updated:: as per review comment --- packages/subgraph/schema.graphql | 52 +++++++++---------- packages/subgraph/src/mappingHelpers.ts | 24 ++++----- packages/subgraph/src/mappings/idav1.ts | 2 +- packages/subgraph/src/utils.ts | 4 +- packages/subgraph/test/interfaces.ts | 28 +++++----- .../subgraph/test/queries/aggregateQueries.ts | 24 ++++----- .../test/validation/aggregateValidators.ts | 28 +++++----- 7 files changed, 80 insertions(+), 82 deletions(-) diff --git a/packages/subgraph/schema.graphql b/packages/subgraph/schema.graphql index 50cae903d1..616d9a51b4 100644 --- a/packages/subgraph/schema.graphql +++ b/packages/subgraph/schema.graphql @@ -1196,9 +1196,9 @@ type AccountTokenSnapshot @entity { updatedAtTimestamp: BigInt! updatedAtBlockNumber: BigInt! # ---------------------------------- state ---------------------------------- - """ - isLiquidationEstimateOptimistic, If they have `0` subcription with no units it return true or else false. + isLiquidationEstimateOptimistic, If `totalSubscriptionsWithUnits > 0`, it is true. + Optimistic means that it is the earliest time the user will be liquidated as they may receive ongoing distributions which aren't tracked by the subgraph """ isLiquidationEstimateOptimistic: Boolean! @@ -1286,62 +1286,62 @@ type AccountTokenSnapshotLog @entity { """ Optimistic liquidation estimation property. """ - maybeCriticalAtTimestampSoFar: BigInt! + maybeCriticalAtTimestamp: BigInt! """ - The number of currently open streams. + The current (as of timestamp) number of currently open streams. """ - totalNumberOfActiveStreamsSoFar: Int! + totalNumberOfActiveStreams: Int! """ - The number of all-time closed streams. + The current (as of timestamp) number of all-time closed streams. """ - totalNumberOfClosedStreamsSoFar: Int! + totalNumberOfClosedStreams: Int! """ - The current (as of updatedAt) number of subscriptions with units allocated to them tied to this `account`. + The current (as of timestamp) number of subscriptions with units allocated to them tied to this `account`. """ - totalSubscriptionsWithUnitsSoFar: Int! + totalSubscriptionsWithUnits: Int! """ - Counts all currently (as of updatedAt) approved subscriptions whether or not they have units. + Counts all currently (as of timestamp) approved subscriptions whether or not they have units. """ - totalApprovedSubscriptionsSoFar: Int! + totalApprovedSubscriptions: Int! """ - Balance of `account` as of `updatedAtTimestamp`/`updatedAtBlock`. + Balance of `account` as of `timestamp`/`block`. """ - balanceSoFar: BigInt! + balance: BigInt! """ - The total deposit this account has held by the CFA agreement for `account` active streams. + The total (as of timestamp) deposit this account has held by the CFA agreement for `account` active streams. """ - totalDepositSoFar: BigInt! + totalDeposit: BigInt! """ - The total net flow rate of the `account` as of `updatedAtTimestamp`/`updatedAtBlock`. + The total (as of timestamp) net flow rate of the `account` as of `timestamp`/`block`. """ - totalNetFlowRateSoFar: BigInt! + totalNetFlowRate: BigInt! """ - The total inflow rate (receive flowRate per second) of the `account`. + The total (as of timestamp) inflow rate (receive flowRate per second) of the `account`. """ - totalInflowRateSoFar: BigInt! + totalInflowRate: BigInt! """ - The total outflow rate (send flowrate per second) of the `account`. + The total (as of timestamp) outflow rate (send flowrate per second) of the `account`. """ - totalOutflowRateSoFar: BigInt! + totalOutflowRate: BigInt! """ - The total amount of `token` streamed to this `account` until - the `updatedAtTimestamp`/`updatedAtBlock`. + The total (as of timestamp) amount of `token` streamed to this `account` until + the `timestamp`/`block`. """ - totalAmountStreamedSoFar: BigInt! + totalAmountStreamed: BigInt! """ - The total amount of `token` this `account` has transferred. + The total (as of timestamp) amount of `token` this `account` has transferred. """ - totalAmountTransferredSoFar: BigInt! + totalAmountTransferred: BigInt! # ---------------------------------- links ---------------------------------- account: Account! token: Token! diff --git a/packages/subgraph/src/mappingHelpers.ts b/packages/subgraph/src/mappingHelpers.ts index e9aeb44fad..a343c85253 100644 --- a/packages/subgraph/src/mappingHelpers.ts +++ b/packages/subgraph/src/mappingHelpers.ts @@ -766,18 +766,18 @@ export function createAccountTokenSnapshotLogEntity( atsLog.logIndex = event.logIndex; atsLog.triggeredByEventName = eventName; // Account token snapshot state - atsLog.totalNumberOfActiveStreamsSoFar = accountTokenSnapshot.totalNumberOfActiveStreams; - atsLog.totalNumberOfClosedStreamsSoFar = accountTokenSnapshot.totalNumberOfClosedStreams; - atsLog.totalSubscriptionsWithUnitsSoFar = accountTokenSnapshot.totalSubscriptionsWithUnits; - atsLog.totalApprovedSubscriptionsSoFar = accountTokenSnapshot.totalApprovedSubscriptions; - atsLog.balanceSoFar = accountTokenSnapshot.balanceUntilUpdatedAt; - atsLog.totalNetFlowRateSoFar = accountTokenSnapshot.totalNetFlowRate; - atsLog.totalInflowRateSoFar = accountTokenSnapshot.totalInflowRate; - atsLog.totalOutflowRateSoFar = accountTokenSnapshot.totalOutflowRate; - atsLog.totalAmountStreamedSoFar = accountTokenSnapshot.totalAmountStreamedUntilUpdatedAt; - atsLog.totalAmountTransferredSoFar = accountTokenSnapshot.totalAmountTransferredUntilUpdatedAt; - atsLog.totalDepositSoFar = accountTokenSnapshot.totalDeposit; - atsLog.maybeCriticalAtTimestampSoFar = accountTokenSnapshot.maybeCriticalAtTimestamp; + atsLog.totalNumberOfActiveStreams = accountTokenSnapshot.totalNumberOfActiveStreams; + atsLog.totalNumberOfClosedStreams = accountTokenSnapshot.totalNumberOfClosedStreams; + atsLog.totalSubscriptionsWithUnits = accountTokenSnapshot.totalSubscriptionsWithUnits; + atsLog.totalApprovedSubscriptions = accountTokenSnapshot.totalApprovedSubscriptions; + atsLog.balance = accountTokenSnapshot.balanceUntilUpdatedAt; + atsLog.totalNetFlowRate = accountTokenSnapshot.totalNetFlowRate; + atsLog.totalInflowRate = accountTokenSnapshot.totalInflowRate; + atsLog.totalOutflowRate = accountTokenSnapshot.totalOutflowRate; + atsLog.totalAmountStreamed = accountTokenSnapshot.totalAmountStreamedUntilUpdatedAt; + atsLog.totalAmountTransferred = accountTokenSnapshot.totalAmountTransferredUntilUpdatedAt; + atsLog.totalDeposit = accountTokenSnapshot.totalDeposit; + atsLog.maybeCriticalAtTimestamp = accountTokenSnapshot.maybeCriticalAtTimestamp; atsLog.account = accountTokenSnapshot.account; atsLog.token = accountTokenSnapshot.token; atsLog.accountTokenSnapshot = accountTokenSnapshot.id; diff --git a/packages/subgraph/src/mappings/idav1.ts b/packages/subgraph/src/mappings/idav1.ts index e6fee6aca4..4437a392be 100644 --- a/packages/subgraph/src/mappings/idav1.ts +++ b/packages/subgraph/src/mappings/idav1.ts @@ -529,7 +529,7 @@ export function handleSubscriptionUnitsUpdated( subscription.save(); createSubscriptionUnitsUpdatedEntity(event, subscription.id, oldUnits); - createAccountTokenSnapshotLogEntity(event, event.params.subscriber, event.params.token, "SubscriptionUnitsUpdated"); + createAccountTokenSnapshotLogEntity(event, event.params.publisher, event.params.token, "SubscriptionUnitsUpdated"); createAccountTokenSnapshotLogEntity(event, event.params.subscriber, event.params.token, "SubscriptionUnitsUpdated"); } diff --git a/packages/subgraph/src/utils.ts b/packages/subgraph/src/utils.ts index cf85193f6a..b042dfc419 100644 --- a/packages/subgraph/src/utils.ts +++ b/packages/subgraph/src/utils.ts @@ -283,13 +283,13 @@ export function getOrder( export function createLogID( logPrefix: string, - aggregateId: string, + accountTokenSnapshotId: string, event: ethereum.Event, ): string { return ( logPrefix + "-" + - aggregateId + accountTokenSnapshotId + "-" + event.transaction.hash.toHexString() + "-" + diff --git a/packages/subgraph/test/interfaces.ts b/packages/subgraph/test/interfaces.ts index f11cdd311e..1da14fc458 100644 --- a/packages/subgraph/test/interfaces.ts +++ b/packages/subgraph/test/interfaces.ts @@ -333,20 +333,20 @@ export interface IAccountTokenSnapshotLog { readonly timestamp: string; readonly order: string; readonly triggeredByEventName: string; - readonly balanceSoFar: string; - readonly totalApprovedSubscriptionsSoFar: number; - readonly totalNumberOfActiveStreamsSoFar: number; - readonly totalNumberOfClosedStreamsSoFar: number; - readonly totalSubscriptionsWithUnitsSoFar: number; - readonly totalAmountStreamedSoFar: string; - readonly totalAmountTransferredSoFar: string; - readonly totalDepositSoFar: string; - readonly totalInflowRateSoFar: string; - readonly totalNetFlowRateSoFar: string; - readonly totalOutflowRateSoFar: string; - readonly maybeCriticalAtTimestampSoFar: string; - readonly account: ILightEntity; - readonly token: ILightEntity; + readonly balance: string; + readonly totalApprovedSubscriptions: number; + readonly totalNumberOfActiveStreams: number; + readonly totalNumberOfClosedStreams: number; + readonly totalSubscriptionsWithUnits: number; + readonly totalAmountStreamed: string; + readonly totalAmountTransferred: string; + readonly totalDeposit: string; + readonly totalInflowRate: string; + readonly totalNetFlowRate: string; + readonly totalOutflowRate: string; + readonly maybeCriticalAtTimestamp: string; + readonly account: ILightEntity; + readonly token: ILightEntity; } export interface ITokenStatistic extends IBaseAggregateEntity { diff --git a/packages/subgraph/test/queries/aggregateQueries.ts b/packages/subgraph/test/queries/aggregateQueries.ts index fe2bd69aa6..7cde9da9aa 100644 --- a/packages/subgraph/test/queries/aggregateQueries.ts +++ b/packages/subgraph/test/queries/aggregateQueries.ts @@ -33,21 +33,21 @@ export const getAccountTokenSnapshot = gql` ) { blockNumber transactionHash - balanceSoFar + balance logIndex order timestamp - totalAmountStreamedSoFar - totalAmountTransferredSoFar - maybeCriticalAtTimestampSoFar - totalApprovedSubscriptionsSoFar - totalDepositSoFar - totalInflowRateSoFar - totalNetFlowRateSoFar - totalNumberOfActiveStreamsSoFar - totalNumberOfClosedStreamsSoFar - totalOutflowRateSoFar - totalSubscriptionsWithUnitsSoFar + totalAmountStreamed + totalAmountTransferred + maybeCriticalAtTimestamp + totalApprovedSubscriptions + totalDeposit + totalInflowRate + totalNetFlowRate + totalNumberOfActiveStreams + totalNumberOfClosedStreams + totalOutflowRate + totalSubscriptionsWithUnits triggeredByEventName } } diff --git a/packages/subgraph/test/validation/aggregateValidators.ts b/packages/subgraph/test/validation/aggregateValidators.ts index 8bd84609f9..be2a00cfb5 100644 --- a/packages/subgraph/test/validation/aggregateValidators.ts +++ b/packages/subgraph/test/validation/aggregateValidators.ts @@ -101,54 +101,52 @@ export const validateATSEntity = ( export const validateASTEntityAndItsLogEntry = ( graphATSData: IAccountTokenSnapshot ) => { - const accountTokenSnapshotLogs: IAccountTokenSnapshotLog[] = graphATSData.accountTokenSnapshotLogs; - const accountTokenSnapshotLog: IAccountTokenSnapshotLog = accountTokenSnapshotLogs[0]; - + const accountTokenSnapshotLog: IAccountTokenSnapshotLog = graphATSData.accountTokenSnapshotLogs[0]; expect(graphATSData.maybeCriticalAtTimestamp, "ATS: maybeCriticalAtTimestamp error") - .to.equal(accountTokenSnapshotLog.maybeCriticalAtTimestampSoFar) + .to.equal(accountTokenSnapshotLog.maybeCriticalAtTimestamp) expect( graphATSData.totalNumberOfActiveStreams, "ATS: totalNumberOfActiveStreams error" - ).to.equal(accountTokenSnapshotLog.totalNumberOfActiveStreamsSoFar); + ).to.equal(accountTokenSnapshotLog.totalNumberOfActiveStreams); expect( graphATSData.totalNumberOfClosedStreams, "ATS: totalNumberOfClosedStreams error" - ).to.equal(accountTokenSnapshotLog.totalNumberOfClosedStreamsSoFar); + ).to.equal(accountTokenSnapshotLog.totalNumberOfClosedStreams); expect( graphATSData.totalSubscriptionsWithUnits, "ATS: totalSubscriptionWithUnits error" - ).to.equal(accountTokenSnapshotLog.totalSubscriptionsWithUnitsSoFar); + ).to.equal(accountTokenSnapshotLog.totalSubscriptionsWithUnits); expect( graphATSData.totalApprovedSubscriptions, "ATS: totalApprovedSubscriptions error" - ).to.equal(accountTokenSnapshotLog.totalApprovedSubscriptionsSoFar); + ).to.equal(accountTokenSnapshotLog.totalApprovedSubscriptions); expect( graphATSData.balanceUntilUpdatedAt, "ATS: balanceUntilUpdatedAt error" - ).to.equal(accountTokenSnapshotLog.balanceSoFar); + ).to.equal(accountTokenSnapshotLog.balance); expect( graphATSData.totalNetFlowRate, "ATS: totalNetFlowRate error" - ).to.equal(accountTokenSnapshotLog.totalNetFlowRateSoFar); + ).to.equal(accountTokenSnapshotLog.totalNetFlowRate); expect(graphATSData.totalInflowRate, "ATS: totalInflowRate error").to.equal( - accountTokenSnapshotLog.totalInflowRateSoFar + accountTokenSnapshotLog.totalInflowRate ); expect(graphATSData.totalDeposit, "ATS: totalDeposit error").to.equal( - accountTokenSnapshotLog.totalDepositSoFar + accountTokenSnapshotLog.totalDeposit ); expect( graphATSData.totalOutflowRate, "ATS: totalOutflowRate error" - ).to.equal(accountTokenSnapshotLog.totalOutflowRateSoFar); + ).to.equal(accountTokenSnapshotLog.totalOutflowRate); expect( graphATSData.totalAmountStreamedUntilUpdatedAt, "ATS: totalAmountStreamedUntilUpdatedAt error" - ).to.equal(accountTokenSnapshotLog.totalAmountStreamedSoFar); + ).to.equal(accountTokenSnapshotLog.totalAmountStreamed); expect( graphATSData.totalAmountTransferredUntilUpdatedAt, "ATS: totalAmountTransferredUntilUpdatedAt error" - ).to.equal(accountTokenSnapshotLog.totalAmountTransferredSoFar); + ).to.equal(accountTokenSnapshotLog.totalAmountTransferred); expect( graphATSData.updatedAtTimestamp, "ATS: updatedAtTimestamp error"