Docs
Поиск⌘ K
  • Главная страница
  • О The Graph
  • Поддерживаемые сети
  • Protocol Contracts
  • Субграфы
    • Субпотоки
      • Token API
        • AI Suite
          • Индексирование
            • Ресурсы
              Субграфы > Разработка > Создание

              15 минуты

              Advanced Subgraph Features

              Обзор

              Add and implement advanced Subgraph features to enhanced your Subgraph’s built.

              Starting from specVersion 0.0.4, Subgraph features must be explicitly declared in the features section at the top level of the manifest file, using their camelCase name, as listed in the table below:

              FeatureName
              Non-fatal errorsnonFatalErrors
              Full-text SearchfullTextSearch
              Graftinggrafting

              For instance, if a Subgraph uses the Full-Text Search and the Non-fatal Errors features, the features field in the manifest should be:

              1specVersion: 1.3.02description: Gravatar for Ethereum3features:4  - fullTextSearch5  - nonFatalErrors6dataSources: ...

              Note that using a feature without declaring it will incur a validation error during Subgraph deployment, but no errors will occur if a feature is declared but not used.

              Тайм-серии и агрегации

              Prerequisites:

              • Subgraph specVersion must be ≥1.1.0.

              Timeseries and aggregations enable your Subgraph to track statistics like daily average price, hourly total transfers, and more.

              This feature introduces two new types of Subgraph entity. Timeseries entities record data points with timestamps. Aggregation entities perform pre-declared calculations on the timeseries data points on an hourly or daily basis, then store the results for easy access via GraphQL.

              Пример схемы

              1type Data @entity(timeseries: true) {2  id: Int8!3  timestamp: Timestamp!4  price: BigDecimal!5}67type Stats @aggregation(intervals: ["hour", "day"], source: "Data") {8  id: Int8!9  timestamp: Timestamp!10  sum: BigDecimal! @aggregate(fn: "sum", arg: "price")11}

              How to Define Timeseries and Aggregations

              Timeseries entities are defined with @entity(timeseries: true) in the GraphQL schema. Every timeseries entity must:

              • have a unique ID of the int8 type
              • have a timestamp of the Timestamp type
              • include data that will be used for calculation by aggregation entities.

              These Timeseries entities can be saved in regular trigger handlers, and act as the “raw data” for the aggregation entities.

              Aggregation entities are defined with @aggregation in the GraphQL schema. Every aggregation entity defines the source from which it will gather data (which must be a timeseries entity), sets the intervals (e.g., hour, day), and specifies the aggregation function it will use (e.g., sum, count, min, max, first, last).

              Aggregation entities are automatically calculated on the basis of the specified source at the end of the required interval.

              Доступные интервалы агрегации

              • hour: sets the timeseries period every hour, on the hour.
              • day: sets the timeseries period every day, starting and ending at 00:00.

              Доступные функции агрегации

              • sum: Total of all values.
              • count: Number of values.
              • min: Minimum value.
              • max: Maximum value.
              • first: First value in the period.
              • last: Last value in the period.

              Пример запроса агрегации

              1{2  stats(interval: "hour", where: { timestamp_gt: 1704085200 }) {3    id4    timestamp5    sum6  }7}

              Read more⁠ about Timeseries and Aggregations.

              Нефатальные ошибки

              Indexing errors on already synced Subgraphs will, by default, cause the Subgraph to fail and stop syncing. Subgraphs can alternatively be configured to continue syncing in the presence of errors, by ignoring the changes made by the handler which provoked the error. This gives Subgraph authors time to correct their Subgraphs while queries continue to be served against the latest block, though the results might be inconsistent due to the bug that caused the error. Note that some errors are still always fatal. To be non-fatal, the error must be known to be deterministic.

              Note: The Graph Network does not yet support non-fatal errors, and developers should not deploy Subgraphs using that functionality to the network via the Studio.

              Enabling non-fatal errors requires setting the following feature flag on the Subgraph manifest:

              1specVersion: 1.3.02description: Gravatar for Ethereum3features:4    - nonFatalErrors5    ...

              The query must also opt-in to querying data with potential inconsistencies through the subgraphError argument. It is also recommended to query _meta to check if the Subgraph has skipped over errors, as in the example:

              1foos(first: 100, subgraphError: allow) {2  id3}45_meta {6  hasIndexingErrors7}

              If the Subgraph encounters an error, that query will return both the data and a graphql error with the message "indexing_error", as in this example response:

              1"data": {2    "foos": [3        {4          "id": "0xdead"5        }6    ],7    "_meta": {8        "hasIndexingErrors": true9    }10},11"errors": [12    {13        "message": "indexing_error"14    }15]

              Источники файловых данных IPFS/Arweave

              File data sources are a new Subgraph functionality for accessing off-chain data during indexing in a robust, extendable way. File data sources support fetching files from IPFS and from Arweave.

              Это также закладывает основу для детерминированного индексирования данных вне сети, а также потенциального введения произвольных данных из HTTP-источников.

              Обзор

              Вместо “встроенного” получения файлов во время выполнения обработчика, это решение вводит шаблоны, которые могут создаваться в качестве новых источников данных для заданного идентификатора файла. Эти новые источники данных загружают файлы, повторяя попытки в случае неудачи, и запускают специальный обработчик при нахождении файла.

              This is similar to the existing data source templates, which are used to dynamically create new chain-based data sources.

              This replaces the existing ipfs.cat API

              Руководство по обновлению

              Update graph-ts and graph-cli

              File data sources requires graph-ts >=0.29.0 and graph-cli >=0.33.1

              Добавьте новый тип объекта, который будет обновляться при обнаружении файлов

              Источники файловых данных не могут получать доступ к объектам на чейн-основе или обновлять их, но должны обновлять объекты, специфичные для файлов.

              Это может означать разделение полей существующих объектов на отдельные объекты, связанные между собой.

              Исходный объединенный объект:

              1type Token @entity {2  id: ID!3  tokenID: BigInt!4  tokenURI: String!5  externalURL: String!6  ipfsURI: String!7  image: String!8  name: String!9  description: String!10  type: String!11  updatedAtTimestamp: BigInt12  owner: User!13}

              Новый разделенный объект:

              1type Token @entity {2  id: ID!3  tokenID: BigInt!4  tokenURI: String!5  ipfsURI: TokenMetadata6  updatedAtTimestamp: BigInt7  owner: String!8}910type TokenMetadata @entity {11  id: ID!12  image: String!13  externalURL: String!14  name: String!15  description: String!16}

              Если между родительским объектом и результирующим объектом-источником данных существует связь1:1, то наиболее простым вариантом будет связать родительский объект с результирующим файловым объектом, используя в качестве поиска IPFS CID. Свяжитесь с нами в Discord, если у Вас возникли трудности с моделированием новых объектов на основе файлов!

              You can use nested filters to filter parent entities on the basis of these nested entities.

              Add a new templated data source with kind: file/ipfs or kind: file/arweave

              Это источник данных, который будет создан при обнаружении интересующего файла.

              1templates:2  - name: TokenMetadata3    kind: file/ipfs4    mapping:5      apiVersion: 0.0.96      language: wasm/assemblyscript7      file: ./src/mapping.ts8      handler: handleMetadata9      entities:10        - TokenMetadata11      abis:12        - name: Token13          file: ./abis/Token.json

              Currently abis are required, though it is not possible to call contracts from within file data sources

              The file data source must specifically mention all the entity types which it will interact with under entities. See limitations for more details.

              Создание нового обработчика для обработки файлов

              This handler should accept one Bytes parameter, which will be the contents of the file, when it is found, which can then be processed. This will often be a JSON file, which can be processed with graph-ts helpers (documentation).

              The CID of the file as a readable string can be accessed via the dataSource as follows:

              1const cid = dataSource.stringParam()

              Пример обработчика:

              1import { json, Bytes, dataSource } from '@graphprotocol/graph-ts'2import { TokenMetadata } from '../generated/schema'34export function handleMetadata(content: Bytes): void {5  let tokenMetadata = new TokenMetadata(dataSource.stringParam())6  const value = json.fromBytes(content).toObject()7  if (value) {8    const image = value.get('image')9    const name = value.get('name')10    const description = value.get('description')11    const externalURL = value.get('external_url')1213    if (name && image && description && externalURL) {14      tokenMetadata.name = name.toString()15      tokenMetadata.image = image.toString()16      tokenMetadata.externalURL = externalURL.toString()17      tokenMetadata.description = description.toString()18    }1920    tokenMetadata.save()21  }22}

              Создание файловых источников данных при необходимости

              Теперь вы можете создавать файловые источники данных во время выполнения обработчиков на чейн-основе:

              • Import the template from the auto-generated templates
              • call TemplateName.create(cid: string) from within a mapping, where the cid is a valid content identifier for IPFS or Arweave

              For IPFS, Graph Node supports v0 and v1 content identifiers⁠, and content identifiers with directories (e.g. bafyreighykzv2we26wfrbzkcdw37sbrby4upq7ae3aqobbq7i4er3tnxci/metadata.json).

              For Arweave, as of version 0.33.0 Graph Node can fetch files stored on Arweave based on their transaction ID⁠ from an Arweave gateway (example file⁠). Arweave supports transactions uploaded via Irys (previously Bundlr), and Graph Node can also fetch files based on Irys manifests⁠.

              Пример:

              1import { TokenMetadata as TokenMetadataTemplate } from '../generated/templates'23const ipfshash = 'QmaXzZhcYnsisuue5WRdQDH6FDvqkLQX1NckLqBYeYYEfm'4//This example code is for a Crypto coven Subgraph. The above ipfs hash is a directory with token metadata for all crypto coven NFTs.56export function handleTransfer(event: TransferEvent): void {7  let token = Token.load(event.params.tokenId.toString())8  if (!token) {9    token = new Token(event.params.tokenId.toString())10    token.tokenID = event.params.tokenId1112    token.tokenURI = '/' + event.params.tokenId.toString() + '.json'13    const tokenIpfsHash = ipfshash + token.tokenURI14    //This creates a path to the metadata for a single Crypto coven NFT. It concats the directory with "/" + filename + ".json"1516    token.ipfsURI = tokenIpfsHash1718    TokenMetadataTemplate.create(tokenIpfsHash)19  }2021  token.updatedAtTimestamp = event.block.timestamp22  token.owner = event.params.to.toHexString()23  token.save()24}

              Это создаст новый источник данных файла, который будет опрашивать настроенную конечную точку IPFS или Arweave Graph Node, повторяя попытку, если она не найдена. Когда файл будет найден, будет выполнен обработчик источника данных файла.

              This example is using the CID as the lookup between the parent Token entity and the resulting TokenMetadata entity.

              Previously, this is the point at which a Subgraph developer would have called ipfs.cat(CID) to fetch the file

              Поздравляем, Вы используете файловые источники данных!

              Deploying your Subgraphs

              You can now build and deploy your Subgraph to any Graph Node >=v0.30.0-rc.0.

              Ограничения

              File data source handlers and entities are isolated from other Subgraph entities, ensuring that they are deterministic when executed, and ensuring no contamination of chain-based data sources. To be specific:

              • Объекты, созданные с помощью файловых источников данных, неизменяемы и не могут быть обновлены
              • Обработчики файловых источников данных не могут получить доступ к объектам из других файловых источников данных
              • Объекты, связанные с источниками данных файлов, не могут быть доступны обработчикам на чейн-основе

              While this constraint should not be problematic for most use-cases, it may introduce complexity for some. Please get in touch via Discord if you are having issues modelling your file-based data in a Subgraph!

              Кроме того, невозможно создать источники данных из файлового источника данных, будь то источник данных onchain или другой файловый источник данных. Это ограничение может быть снято в будущем.

              Лучшие практики

              Если Вы связываете метаданные NFT с соответствующими токенами, используйте хэш IPFS метаданных для ссылки на объект Metadata из объекта Token. Сохраните объект Metadata, используя хэш IPFS в качестве идентификатора.

              You can use DataSource context when creating File Data Sources to pass extra information which will be available to the File Data Source handler.

              If you have entities which are refreshed multiple times, create unique file-based entities using the IPFS hash & the entity ID, and reference them using a derived field in the chain-based entity.

              Мы работаем над улучшением приведенной выше рекомендации, поэтому запросы возвращают только “самую последнюю” версию

              Известные проблемы

              File data sources currently require ABIs, even though ABIs are not used (issue⁠). Workaround is to add any ABI.

              Handlers for File Data Sources cannot be in files which import eth_call contract bindings, failing with “unknown import: ethereum::ethereum.call has not been defined” (issue⁠). Workaround is to create file data source handlers in a dedicated file.

              Примеры

              Crypto Coven Subgraph migration⁠

              Ссылки

              GIP File Data Sources⁠

              Фильтры индексированных аргументов/фильтры тем

              Requires: SpecVersion >= 1.2.0

              Topic filters, also known as indexed argument filters, are a powerful feature in Subgraphs that allow users to precisely filter blockchain events based on the values of their indexed arguments.

              • These filters help isolate specific events of interest from the vast stream of events on the blockchain, allowing Subgraphs to operate more efficiently by focusing only on relevant data.

              • This is useful for creating personal Subgraphs that track specific addresses and their interactions with various smart contracts on the blockchain.

              Как работают фильтры тем

              When a smart contract emits an event, any arguments that are marked as indexed can be used as filters in a Subgraph’s manifest. This allows the Subgraph to listen selectively for events that match these indexed arguments.

              • The event’s first indexed argument corresponds to topic1, the second to topic2, and so on, up to topic3, since the Ethereum Virtual Machine (EVM) allows up to three indexed arguments per event.
              1// Идентификатор лицензии SPDX: MIT2pragma solidity ^0.8.0;34contract Token {5    // Объявление события с индексируемыми параметрами для адресов6    event Transfer(address indexed from, address indexed to, uint256 value);78    // Функция для имитации передачи токенов9    function transfer(address to, uint256 value) public {10        // Генерация события Transfer с указанием from, to и value11        emit Transfer(msg.sender, to, value);12    }13}

              В этом примере:

              • The Transfer event is used to log transactions of tokens between addresses.
              • The from and to parameters are indexed, allowing event listeners to filter and monitor transfers involving specific addresses.
              • The transfer function is a simple representation of a token transfer action, emitting the Transfer event whenever it is called.

              Конфигурация в субграфах

              Topic filters are defined directly within the event handler configuration in the Subgraph manifest. Here is how they are configured:

              1eventHandlers:2  - event: SomeEvent(indexed uint256, indexed address, indexed uint256)3    handler: handleSomeEvent4    topic1: ['0xValue1', '0xValue2']5    topic2: ['0xAddress1', '0xAddress2']6    topic3: ['0xValue3']

              В этой настройке:

              • topic1 corresponds to the first indexed argument of the event, topic2 to the second, and topic3 to the third.
              • Каждая тема может иметь одно или несколько значений, и событие обрабатывается только в том случае, если оно соответствует одному из значений в каждой указанной теме.

              Логика фильтра

              • В рамках одной темы: логика действует как условие OR. Событие будет обработано, если оно соответствует любому из перечисленных значений в данной теме.
              • Между разными темами: логика функционирует как условие AND. Событие должно удовлетворять всем указанным условиям в разных темах, чтобы вызвать соответствующий обработчик.

              Пример 1. Отслеживание прямых переводов с адреса A на адрес B

              1eventHandlers:2  - event: Transfer(indexed address,indexed address,uint256)3    handler: handleDirectedTransfer4    topic1: ['0xAddressA'] # Sender Address5    topic2: ['0xAddressB'] # Receiver Address

              В данной конфигурации:

              • topic1 is configured to filter Transfer events where 0xAddressA is the sender.
              • topic2 is configured to filter Transfer events where 0xAddressB is the receiver.
              • The Subgraph will only index transactions that occur directly from 0xAddressA to 0xAddressB.

              Пример 2. Отслеживание транзакций в любом направлении между двумя и более адресами

              1eventHandlers:2  - event: Transfer(indexed address,indexed address,uint256)3    handler: handleTransferToOrFrom4    topic1: ['0xAddressA', '0xAddressB', '0xAddressC'] # Адрес отправителя5    topic2: ['0xAddressB', '0xAddressC'] # Адрес получателя

              В данной конфигурации:

              • topic1 is configured to filter Transfer events where 0xAddressA, 0xAddressB, 0xAddressC is the sender.
              • topic2 is configured to filter Transfer events where 0xAddressB and 0xAddressC is the receiver.
              • The Subgraph will index transactions that occur in either direction between multiple addresses allowing for comprehensive monitoring of interactions involving all addresses.

              Декларированный eth_call

              Примечание: Это экспериментальная функция, которая пока недоступна в стабильной версии Graph Node. Вы можете использовать её только в Subgraph Studio или на своей локальной ноде.

              Declarative eth_calls are a valuable Subgraph feature that allows eth_calls to be executed ahead of time, enabling graph-node to execute them in parallel.

              Эта функция выполняет следующие действия:

              • Significantly improves the performance of fetching data from the Ethereum blockchain by reducing the total time for multiple calls and optimizing the Subgraph’s overall efficiency.
              • Обеспечивает ускоренное получение данных, что приводит к более быстрому реагированию на запросы и улучшению пользовательского опыта.
              • Сокращает время ожидания для приложений, которым необходимо агрегировать данные из нескольких вызовов Ethereum, что делает процесс получения данных более эффективным.

              Ключевые понятия

              • Declarative eth_calls: Ethereum calls that are defined to be executed in parallel rather than sequentially.
              • Параллельное выполнение: Вместо того, чтобы ждать завершения одного вызова перед началом следующего, можно инициировать несколько вызовов одновременно.
              • Эффективность использования времени: Общее время, затраченное на все вызовы, изменяется от суммы времени отдельных вызовов (последовательные) до времени, затраченного на самый продолжительный вызов (параллельные).

              Scenario without Declarative eth_calls

              Imagine you have a Subgraph that needs to make three Ethereum calls to fetch data about a user’s transactions, balance, and token holdings.

              Традиционно эти вызовы могут выполняться последовательно:

              1. Вызов 1 (Транзакции): Занимает 3 секунды
              2. Вызов 2 (Баланс): Занимает 2 секунды
              3. Вызов 3 (Холдинг токенов): Занимает 4 секунды

              Общее затраченное время = 3 + 2 + 4 = 9 секунд

              Scenario with Declarative eth_calls

              С помощью этой функции Вы можете объявить, что эти вызовы будут выполняться параллельно:

              1. Вызов 1 (Транзакции): Занимает 3 секунды
              2. Вызов 2 (Баланс): Занимает 2 секунды
              3. Вызов 3 (Холдинг токенов): Занимает 4 секунды

              Поскольку эти вызовы выполняются параллельно, общее затраченное время равно времени, затраченному на самый длительный вызов.

              Общее затраченное время = макс. (3, 2, 4) = 4 секунды

              Как это работает

              1. Declarative Definition: In the Subgraph manifest, you declare the Ethereum calls in a way that indicates they can be executed in parallel.
              2. Механизм параллельного выполнения: Механизм выполнения The Graph Node распознает эти объявления и выполняет вызовы одновременно.
              3. Result Aggregation: Once all calls are complete, the results are aggregated and used by the Subgraph for further processing.

              Пример конфигурации в манифесте субграфа

              Declared eth_calls can access the event.address of the underlying event as well as all the event.params.

              subgraph.yaml using event.address:

              1eventHandlers:2event: Swap(indexed address,indexed address,int256,int256,uint160,uint128,int24)3handler: handleSwap4calls:5  global0X128: Pool[event.address].feeGrowthGlobal0X128()6  global1X128: Pool[event.address].feeGrowthGlobal1X128()

              Подробности для приведенного выше примера:

              • global0X128 is the declared eth_call.
              • The text (global0X128) is the label for this eth_call which is used when logging errors.
              • The text (Pool[event.address].feeGrowthGlobal0X128()) is the actual eth_call that will be executed, which is in the form of Contract[address].function(arguments)
              • The address and arguments can be replaced with variables that will be available when the handler is executed.

              subgraph.yaml using event.params

              1calls:2  - ERC20DecimalsToken0: ERC20[event.params.token0].decimals()

              Графтинг (перенос) на существующие субграфы

              Note: it is not recommended to use grafting when initially upgrading to The Graph Network. Learn more here.

              When a Subgraph is first deployed, it starts indexing events at the genesis block of the corresponding chain (or at the startBlock defined with each data source) In some circumstances; it is beneficial to reuse the data from an existing Subgraph and start indexing at a much later block. This mode of indexing is called Grafting. Grafting is, for example, useful during development to get past simple errors in the mappings quickly or to temporarily get an existing Subgraph working again after it has failed.

              A Subgraph is grafted onto a base Subgraph when the Subgraph manifest in subgraph.yaml contains a graft block at the top-level:

              1description: ...2graft:3  base: Qm... # Subgraph ID of base Subgraph4  block: 7345624 # Block number

              When a Subgraph whose manifest contains a graft block is deployed, Graph Node will copy the data of the base Subgraph up to and including the given block and then continue indexing the new Subgraph from that block on. The base Subgraph must exist on the target Graph Node instance and must have indexed up to at least the given block. Because of this restriction, grafting should only be used during development or during an emergency to speed up producing an equivalent non-grafted Subgraph.

              Because grafting copies rather than indexes base data, it is much quicker to get the Subgraph to the desired block than indexing from scratch, though the initial data copy can still take several hours for very large Subgraphs. While the grafted Subgraph is being initialized, the Graph Node will log information about the entity types that have already been copied.

              Перенесённый субграф может использовать схему GraphQL, которая не идентична схеме базового субграфа, а просто совместима с ней. Это должна быть автономно действующая схема субграфа, но она может отличаться от схемы базового субграфа следующим образом:

              • Она добавляет или удаляет типы объектов
              • Она удаляет атрибуты из типов объектов
              • Она добавляет обнуляемые атрибуты к типам объектов
              • Она превращает необнуляемые атрибуты в обнуляемые
              • Она добавляет значения в перечисления
              • Она добавляет или удаляет интерфейсы
              • Она изменяется в зависимости от того, под какой тип объектов реализован интерфейс

              Feature Management: grafting must be declared under features in the Subgraph manifest.

              ⁠Редактировать на GitHub⁠

              Writing AssemblyScript MappingsВведение
              На этой странице
              • Обзор
              • Тайм-серии и агрегации
              • Пример схемы
              • How to Define Timeseries and Aggregations
              • Нефатальные ошибки
              • Источники файловых данных IPFS/Arweave
              • Обзор
              • Руководство по обновлению
              • Фильтры индексированных аргументов/фильтры тем
              • Как работают фильтры тем
              • Декларированный eth_call
              • Ключевые понятия
              • Графтинг (перенос) на существующие субграфы
              The GraphСтатусТестовая сетьБрундовые ресурсыФорумБезопасностьПолитика конфиденциальностиУсловия обслуживания