Recetario > Construcción de subgrafos en Base

Construcción de subgrafos en Base

Reading time: 3 min

Esta guía te conducirá rápidamente a través de cómo iniciar, crear e deployar tu subgrafo en Base testnet.

Lo que necesitarás:

  • A Base Sepolia testnet contract address
  • Una wallet cripto (por ejemplo, MetaMask o Coinbase Wallet)

Subgraph Studio

Enlace a esta sección

1. Instala The Graph CLI

Enlace a esta sección

La CLI de The Graph (>=v0.41.0) está escrita en JavaScript y necesitarás tener instalado npm o yarn para usarlo.

# NPM
npm install -g @graphprotocol/graph-cli
# Yarn
yarn global add @graphprotocol/graph-cli

2. Create your subgraph in Subgraph Studio

Enlace a esta sección

Go to Subgraph Studio and connect your crypto wallet.

Once connected, click "Create a Subgraph", enter a name for your subgraph and click Create a Subgraph.

3. Inicia tu subgrafo

Enlace a esta sección

You can find specific commands for your subgraph in Subgraph Studio.

Asegúrate de que el graph-cli esté actualizado a la última versión (superior a 0.41.0)

graph --versión

Inicia tu subgrafo a partir de un contrato existente.

graph init --studio <SUBGRAPH_SLUG>

Tu slug de subgrafo es un identificador para tu subgrafo. La herramienta CLI te guiará a través de los pasos para crear un subgrafo, que incluyen:

  • Protocolo: ethereum
  • Slug de subgrafo: <SUBGRAPH_SLUG>
  • Directorio para crear el subgrafo en: <SUBGRAPH_SLUG>
  • Ethereum network: base-sepolia
  • Contract address: <CONTRACT_ADDRESS>
  • Start block (optional)
  • Contract name: <CONTRACT_NAME>
  • Yes/no to indexing events (yes means your subgraph will be bootstrapped with entities in the schema and simple mappings for emitted events)

3. Write your Subgraph

Enlace a esta sección

If emitted events are the only thing you want to index, then no additional work is required, and you can skip to the next step.

The previous command creates a scaffold subgraph that you can use as a starting point for building your subgraph. When making changes to the subgraph, you will mainly work with three files:

  • Manifest (subgraph.yaml) - The manifest defines what datasources your subgraphs will index. Make sure to add base-sepolia as the network name in manifest file to deploy your subgraph on Base Sepolia.
  • Schema (schema.graphql) - The GraphQL schema defines what data you wish to retreive from the subgraph.
  • AssemblyScript Mappings (mapping.ts) - Este es el código que traduce los datos de tus fuentes de datos a las entidades definidas en el esquema.

If you want to index additional data, you will need extend the manifest, schema and mappings.

For more information on how to write your subgraph, see Creating a Subgraph.

4. Deploy to Subgraph Studio

Enlace a esta sección

Before you can deploy your subgraph, you will need to authenticate with Subgraph Studio. You can do this by running the following command:

Authenticate the subgraph on studio

graph auth --studio <DEPLOY_KEY>

Next, enter your subgraph's directory.

cd <SUBGRAPH_DIRECTORY>

Build your subgraph with the following command:

```
graph codegen && graph build
```

Finally, you can deploy your subgraph using this command:

```
graph deploy --studio <SUBGRAPH_SLUG>
```

5. Query your subgraph

Enlace a esta sección

Once your subgraph is deployed, you can query it from your dapp using the Development Query URL in Subgraph Studio.

Note - Studio API is rate-limited. Hence should preferably be used for development and testing.

To learn more about querying data from your subgraph, see the Querying a Subgraph page.

Editar página

Anterior
Actualizando un Subgrafo Existente a la Red de The Graph
Siguiente
How to Secure API Keys Using Next.js Server Components
Editar página