Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ type Subgraph @entity {
signalledTokens: BigInt!
"CUMULATIVE unsignalled tokens on this subgraph all time"
unsignalledTokens: BigInt!
"CURRENT amount of tokens signalled on this subgraph. Only accounts for signalling done through GNS (name signalling)"
"CURRENT amount of tokens signalled on this subgraph latest version. Mirrors the total amount signalled towards the current deployment."
currentSignalledTokens: BigInt!
"The CURRENT name signal amount for this subgraph"
nameSignalAmount: BigInt!
Expand Down Expand Up @@ -356,8 +356,24 @@ type Subgraph @entity {
displayName: String
"Categories that the subgraph belongs to. Modelled with a relation to allow for many-to-many relationship querying"
categories: [SubgraphCategoryRelation!]! @derivedFrom(field: "subgraph")

# Auxiliary fields
currentVersionRelationEntity: CurrentSubgraphDeploymentRelation
}

type CurrentSubgraphDeploymentRelation @entity {
"Auxiliary entity used to batch update Subgraph entities when signalling on the deployment changes. ID replicates the deployment ID and adds a counter, to make it easy to reproduce."
id: ID!

subgraph: Subgraph!

deployment: SubgraphDeployment!

"Indicates whether this relation is active. This means that the deployment is still the current deployment for the named Subgraph"
active: Boolean!
}


type Network @entity {
id: ID!

Expand Down Expand Up @@ -472,6 +488,14 @@ type SubgraphDeployment @entity {
schema: String
"IPFS hash of the schema file"
schemaIpfsHash: String

# Counters for currentSignalledTokens tracking on Subgraph
"Total amount of Subgraph entities that used this deployment at some point. subgraphCount >= activeSubgraphCount + deprecatedSubgraphCount"
subgraphCount: Int!
"Amount of active Subgraph entities that are currently using this deployment. Deprecated subgraph entities are not counted"
activeSubgraphCount: Int!
"Amount of Subgraph entities that were currently using this deployment when they got deprecated"
deprecatedSubgraphCount: Int!
}

# TODO - add when we have the ability to parse data sources
Expand Down
6 changes: 5 additions & 1 deletion src/mappings/curation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
createOrLoadEpoch,
joinID,
calculatePricePerShare,
batchUpdateSubgraphSignalledTokens,
} from './helpers'
import { zeroBD } from './utils'

Expand Down Expand Up @@ -97,9 +98,10 @@ export function handleSignalled(event: Signalled): void {
)
deployment.signalAmount = deployment.signalAmount.plus(event.params.signal)
deployment.pricePerShare = calculatePricePerShare(deployment as SubgraphDeployment)

deployment.save()

batchUpdateSubgraphSignalledTokens(deployment as SubgraphDeployment)

// Update epoch
let epoch = createOrLoadEpoch(event.block.number)
epoch.signalledTokens = epoch.signalledTokens.plus(
Expand Down Expand Up @@ -195,6 +197,8 @@ export function handleBurned(event: Burned): void {
deployment.pricePerShare = calculatePricePerShare(deployment as SubgraphDeployment)
deployment.save()

batchUpdateSubgraphSignalledTokens(deployment as SubgraphDeployment)

// Update epoch - none

// Update graph network
Expand Down
39 changes: 30 additions & 9 deletions src/mappings/gns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
createOrLoadSubgraph,
joinID,
createOrLoadNameSignal,
updateCurrentDeploymentLinks,
} from './helpers'
import { fetchSubgraphMetadata, fetchSubgraphVersionMetadata } from './metadataHelpers'

Expand Down Expand Up @@ -199,6 +200,7 @@ export function handleSubgraphPublished(event: SubgraphPublished): void {

// Update subgraph
let subgraph = createOrLoadSubgraph(subgraphID, event.params.graphAccount, event.block.timestamp)
let oldVersionID = subgraph.currentVersion

versionID = joinID([subgraphID, subgraph.versionCount.toString()])
subgraph.currentVersion = versionID

@nenadjaja nenadjaja Jul 27, 2021 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't let oldVersionID = subgraph.currentVersion line of code be after this line? (After we set subgraph.currentVersion) ?

@juanmardefago juanmardefago Jul 28, 2021 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually want to maintain the previous version id (before we set the new one, even if it's null) that's why I do it before.
This is done to be able to properly update the old version helper entity and also the active amount of subgraphs that currently are linked to this deployment.

Expand All @@ -211,7 +213,7 @@ export function handleSubgraphPublished(event: SubgraphPublished): void {

// Create subgraph deployment, if needed. Can happen if the deployment has never been staked on
let subgraphDeploymentID = event.params.subgraphDeploymentID.toHexString()
createOrLoadSubgraphDeployment(subgraphDeploymentID, event.block.timestamp)
let deployment = createOrLoadSubgraphDeployment(subgraphDeploymentID, event.block.timestamp)

// Create subgraph version
let subgraphVersion = new SubgraphVersion(versionID)
Expand All @@ -224,6 +226,18 @@ export function handleSubgraphPublished(event: SubgraphPublished): void {
subgraphVersion.metadataHash = event.params.versionMetadata
subgraphVersion = fetchSubgraphVersionMetadata(subgraphVersion, base58Hash)
subgraphVersion.save()

let oldDeployment: SubgraphDeployment | null = null
if (oldVersionID != null) {
let oldVersion = SubgraphVersion.load(oldVersionID)
oldDeployment = SubgraphDeployment.load(oldVersion.subgraphDeployment)
}
// create deployment - named subgraph relationship, and update the old one
updateCurrentDeploymentLinks(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this function, or one similar to it, will have to run on handleSubgraphDeprecated. it might be that we just need to lower the active count, and set active to false. wdyt?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔
maybe, yeah, although technically the subgraph still has the deployment as its' current version, so it might be weird when querying that data 🤔

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be best to track that as a separate count, like having subgraphCount, activeSubgraphCount and deprecatedSubgraphCount, since an active subgraph for the deployment can stop being active because it was upgraded or because it was deprecated

oldDeployment,
deployment,
subgraph as Subgraph,
)
}
/**
* @dev handleSubgraphDeprecated
Expand All @@ -243,6 +257,19 @@ export function handleSubgraphDeprecated(event: SubgraphDeprecated): void {
let graphNetwork = GraphNetwork.load('1')
graphNetwork.activeSubgraphCount = graphNetwork.activeSubgraphCount - 1
graphNetwork.save()


let version = SubgraphVersion.load(subgraph.currentVersion)
if (version != null) {
let deployment = SubgraphDeployment.load(version.subgraphDeployment)

updateCurrentDeploymentLinks(
deployment,
null,
subgraph as Subgraph,
true
)
}
}

export function handleNameSignalEnabled(event: NameSignalEnabled): void {
Expand All @@ -266,9 +293,6 @@ export function handleNSignalMinted(event: NSignalMinted): void {
subgraph.nameSignalAmount = subgraph.nameSignalAmount.plus(event.params.nSignalCreated)
subgraph.signalAmount = subgraph.signalAmount.plus(event.params.vSignalCreated)
subgraph.signalledTokens = subgraph.signalledTokens.plus(event.params.tokensDeposited)
subgraph.currentSignalledTokens = subgraph.currentSignalledTokens.plus(
event.params.tokensDeposited,
)
subgraph.save()

// Update the curator
Expand Down Expand Up @@ -347,7 +371,7 @@ export function handleNSignalMinted(event: NSignalMinted): void {
curator.activeNameSignalCount = curator.activeNameSignalCount + 1
curator.activeCombinedSignalCount = curator.activeCombinedSignalCount + 1

if(curator.activeCombinedSignalCount == 1) {
if (curator.activeCombinedSignalCount == 1) {
let graphNetwork = GraphNetwork.load('1')
graphNetwork.activeCuratorCount = graphNetwork.activeCuratorCount + 1
graphNetwork.save()
Expand Down Expand Up @@ -379,9 +403,6 @@ export function handleNSignalBurned(event: NSignalBurned): void {
subgraph.nameSignalAmount = subgraph.nameSignalAmount.minus(event.params.nSignalBurnt)
subgraph.signalAmount = subgraph.signalAmount.minus(event.params.vSignalBurnt)
subgraph.unsignalledTokens = subgraph.unsignalledTokens.plus(event.params.tokensReceived)
subgraph.currentSignalledTokens = subgraph.currentSignalledTokens.minus(
event.params.tokensReceived,
)
subgraph.save()

// update name signal
Expand Down Expand Up @@ -458,7 +479,7 @@ export function handleNSignalBurned(event: NSignalBurned): void {
curator.activeNameSignalCount = curator.activeNameSignalCount - 1
curator.activeCombinedSignalCount = curator.activeCombinedSignalCount - 1

if(curator.activeCombinedSignalCount == 0) {
if (curator.activeCombinedSignalCount == 0) {
let graphNetwork = GraphNetwork.load('1')
graphNetwork.activeCuratorCount = graphNetwork.activeCuratorCount - 1
graphNetwork.save()
Expand Down
79 changes: 71 additions & 8 deletions src/mappings/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
SubgraphCategory,
SubgraphCategoryRelation,
NameSignalSubgraphRelation,
CurrentSubgraphDeploymentRelation,
} from '../types/schema'
import { ENS } from '../types/GNS/ENS'
import { Controller } from '../types/Controller/Controller'
Expand Down Expand Up @@ -70,7 +71,10 @@ export function createOrLoadSubgraphDeployment(
let prefix = '1220'
deployment = new SubgraphDeployment(subgraphID)
deployment.ipfsHash = Bytes.fromHexString(prefix.concat(subgraphID.slice(2))).toBase58()
deployment = fetchSubgraphDeploymentManifest(deployment as SubgraphDeployment, deployment.ipfsHash)
deployment = fetchSubgraphDeploymentManifest(
deployment as SubgraphDeployment,
deployment.ipfsHash,
)
deployment.createdAt = timestamp.toI32()
deployment.stakedTokens = BigInt.fromI32(0)
deployment.indexingRewardAmount = BigInt.fromI32(0)
Expand All @@ -86,6 +90,10 @@ export function createOrLoadSubgraphDeployment(
deployment.pricePerShare = BigDecimal.fromString('0')
deployment.reserveRatio = graphNetwork.defaultReserveRatio
deployment.deniedAt = 0

deployment.subgraphCount = 0
deployment.activeSubgraphCount = 0
deployment.deprecatedSubgraphCount = 0
deployment.save()

let graphNetwork = GraphNetwork.load('1')
Expand Down Expand Up @@ -201,8 +209,8 @@ export function createOrLoadDelegatedStake(
delegatedStake.save()

let delegatorEntity = Delegator.load(delegator)
delegatorEntity.stakesCount = delegatorEntity.stakesCount + 1;
delegatorEntity.save();
delegatorEntity.stakesCount = delegatorEntity.stakesCount + 1
delegatorEntity.save()

let graphNetwork = GraphNetwork.load('1')
graphNetwork.delegationCount = graphNetwork.delegationCount + 1
Expand Down Expand Up @@ -319,12 +327,14 @@ export function createOrLoadNameSignal(
curatorEntity.save()

let subgraphEntity = Subgraph.load(subgraphID)
let relation = new NameSignalSubgraphRelation(joinID([subgraphID, BigInt.fromI32(subgraphEntity.nameSignalCount).toString()]))
subgraphEntity.nameSignalCount = subgraphEntity.nameSignalCount + 1;
let relation = new NameSignalSubgraphRelation(
joinID([subgraphID, BigInt.fromI32(subgraphEntity.nameSignalCount).toString()]),
)
subgraphEntity.nameSignalCount = subgraphEntity.nameSignalCount + 1
subgraphEntity.save()

relation.subgraph = subgraphEntity.id;
relation.nameSignal = nameSignal.id;
relation.subgraph = subgraphEntity.id
relation.nameSignal = nameSignal.id
relation.save()
}
return nameSignal as NameSignal
Expand Down Expand Up @@ -751,7 +761,7 @@ export function calculatePricePerShare(deployment: SubgraphDeployment): BigDecim

// reserve ratio multiplier = MAX_WEIGHT / reserveRatio = 1M (ppm) / reserveRatio
// HOTFIX for now, if deployment.reserveRatio -> 0, use a known previous default
let reserveRatioMultiplier = deployment.reserveRatio == 0 ? 2 : 1000000 / deployment.reserveRatio
let reserveRatioMultiplier = deployment.reserveRatio == 0 ? 2 : 1000000 / deployment.reserveRatio
let pricePerShare =
deployment.signalAmount == BigInt.fromI32(0)
? BigDecimal.fromString('0')
Expand Down Expand Up @@ -798,3 +808,56 @@ export function createOrLoadSubgraphCategoryRelation(
}
return relation as SubgraphCategoryRelation
}

export function updateCurrentDeploymentLinks(
oldDeployment: SubgraphDeployment | null,
newDeployment: SubgraphDeployment | null,
subgraph: Subgraph,
deprecated: boolean = false,
): void {
if (oldDeployment != null) {
if (!deprecated) {
let oldRelationEntity = CurrentSubgraphDeploymentRelation.load(
subgraph.currentVersionRelationEntity,
)
oldRelationEntity.active = false
oldRelationEntity.save()
}

oldDeployment.activeSubgraphCount = oldDeployment.activeSubgraphCount - 1
if (deprecated) {
oldDeployment.deprecatedSubgraphCount = oldDeployment.deprecatedSubgraphCount + 1
}
oldDeployment.save()
}

if (newDeployment != null) {
let newRelationID = newDeployment.id
.concat('-')
.concat(BigInt.fromI32(newDeployment.subgraphCount).toString())
let newRelationEntity = new CurrentSubgraphDeploymentRelation(newRelationID)
newRelationEntity.deployment = newDeployment.id
newRelationEntity.subgraph = subgraph.id
newRelationEntity.active = true
newRelationEntity.save()

newDeployment.subgraphCount = newDeployment.subgraphCount + 1
newDeployment.activeSubgraphCount = newDeployment.activeSubgraphCount + 1
newDeployment.save()

subgraph.currentVersionRelationEntity = newRelationEntity.id
subgraph.save()
}
}

export function batchUpdateSubgraphSignalledTokens(deployment: SubgraphDeployment): void {
for (let i = 0; i < deployment.subgraphCount; i++) {
let id = deployment.id.concat('-').concat(BigInt.fromI32(i).toString())
let relationEntity = CurrentSubgraphDeploymentRelation.load(id)
if (relationEntity.active) {
let subgraphEntity = Subgraph.load(relationEntity.subgraph)
subgraphEntity.currentSignalledTokens = deployment.signalledTokens
subgraphEntity.save()
}
}
}
68 changes: 0 additions & 68 deletions src/mappings/metadataHelpers.ts

This file was deleted.

3 changes: 3 additions & 0 deletions src/mappings/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import {
updateAdvancedIndexerMetrics,
updateDelegationExchangeRate,
calculatePricePerShare,
batchUpdateSubgraphSignalledTokens,
} from './helpers'

export function handleDelegationParametersUpdated(event: DelegationParametersUpdated): void {
Expand Down Expand Up @@ -418,6 +419,8 @@ export function handleAllocationCollected(event: AllocationCollected): void {
deployment.pricePerShare = calculatePricePerShare(deployment as SubgraphDeployment)
deployment.save()

batchUpdateSubgraphSignalledTokens(deployment as SubgraphDeployment)

// since we don't get the protocol tax explicitly, we will use tokens - (curation + rebate) to calculate it
// This could also be calculated by doing: protocolPercentage * event.params.tokens
let taxedFees = event.params.tokens.minus(event.params.rebateFees.plus(event.params.curationFees))
Expand Down