Building Subgraphs on Arweave
Reading time: 7 min
Arweave support in Graph Node and on Subgraph Studio is in beta: please reach us on with any questions about building Arweave subgraphs!
In this guide, you will learn how to build and deploy Subgraphs to index the Arweave blockchain.
The Arweave protocol allows developers to store data permanently and that is the main difference between Arweave and IPFS, where IPFS lacks the feature; permanence, and files stored on Arweave can't be changed or deleted.
Arweave already has built numerous libraries for integrating the protocol in a number of different programming languages. For more information you can check:
The Graph allows you to build custom open APIs called "Subgraphs". Subgraphs are used to tell indexers (server operators) which data to index on a blockchain and save on their servers in order for you to be able to query it at any time using .
is now able to index data on Arweave protocol. The current integration is only indexing Arweave as a blockchain (blocks and transactions), it is not indexing the stored files yet.
To be able to build and deploy Arweave Subgraphs, you need two packages:
@graphprotocol/graph-cli
above version 0.30.2 - This is a command-line tool for building and deploying subgraphs. to download usingnpm
.@graphprotocol/graph-ts
above version 0.27.0 - This is library of subgraph-specific types. to download usingnpm
.
There are three components of a subgraph:
Defines the data sources of interest, and how they should be processed. Arweave is a new kind of data source.
Here you define which data you want to be able to query after indexing your Subgraph using GraphQL. This is actually similar to a model for an API, where the model defines the structure of a request body.
The requirements for Arweave subgraphs are covered by the .
This is the logic that determines how data should be retrieved and stored when someone interacts with the data sources you are listening to. The data gets translated and is stored based off the schema you have listed.
During subgraph development there are two key commands:
$ graph codegen # generates types from the schema file identified in the manifest$ graph build # generates Web Assembly from the AssemblyScript files, and prepares all the subgraph files in a /build folder
The subgraph manifest subgraph.yaml
identifies the data sources for the subgraph, the triggers of interest, and the functions that should be run in response to those triggers. See below for an example subgraph manifest for an Arweave subgraph:
specVersion: 0.0.5description: Arweave Blocks Indexingschema:file: ./schema.graphql # link to the schema filedataSources:- kind: arweavename: arweave-blocksnetwork: arweave-mainnet # The Graph only supports Arweave Mainnetsource:owner: 'ID-OF-AN-OWNER' # The public key of an Arweave walletstartBlock: 0 # set this to 0 to start indexing from chain genesismapping:apiVersion: 0.0.5language: wasm/assemblyscriptfile: ./src/blocks.ts # link to the file with the Assemblyscript mappingsentities:- Block- TransactionblockHandlers:- handler: handleBlock # the function name in the mapping filetransactionHandlers:- handler: handleTx # the function name in the mapping file
- Arweave subgraphs introduce a new kind of data source (
arweave
) - The network should correspond to a network on the hosting Graph Node. In Subgraph Studio, Arweave's mainnet is
arweave-mainnet
- Arweave data sources introduce an optional source.owner field, which is the public key of an Arweave wallet
Arweave data sources support two types of handlers:
blockHandlers
- Run on every new Arweave block. No source.owner is required.transactionHandlers
- Run on every transaction where the data source'ssource.owner
is the owner. Currently an owner is required fortransactionHandlers
, if users want to process all transactions they should provide "" as thesource.owner
The source.owner can be the owner's address, or their Public Key.
Transactions are the building blocks of the Arweave permaweb and they are objects created by end-users.
Schema definition describes the structure of the resulting subgraph database and the relationships between entities. This is agnostic of the original data source. There are more details on the subgraph schema definition .
The handlers for processing events are written in .
Arweave indexing introduces Arweave-specific data types to the .
class Block {timestamp: u64lastRetarget: u64height: u64indepHash: Bytesnonce: BytespreviousBlock: Bytesdiff: Byteshash: BytestxRoot: Bytestxs: Bytes[]walletList: BytesrewardAddr: Bytestags: Tag[]rewardPool: BytesweaveSize: BytesblockSize: BytescumulativeDiff: ByteshashListMerkle: Bytespoa: ProofOfAccess}class Transaction {format: u32id: ByteslastTx: Bytesowner: Bytestags: Tag[]target: Bytesquantity: Bytesdata: BytesdataSize: BytesdataRoot: Bytessignature: Bytesreward: Bytes}
Block handlers receive a Block
, while transactions receive a Transaction
.
Writing the mappings of an Arweave Subgraph is very similar to writing the mappings of an Ethereum Subgraph. For more information, click .
Once your subgraph has been created on your Subgraph Studio dashboard, you can deploy by using the graph deploy
CLI command.
graph deploy --access-token <your-access-token>
The GraphQL endpoint for Arweave subgraphs is determined by the schema definition, with the existing API interface. Please visit the for more information.
Here is an example subgraph for reference:
No, a subgraph can only support data sources from one chain/network.
Currently, The Graph is only indexing Arweave as a blockchain (its blocks and transactions).
This is not currently supported.
The source.owner can be the user's public key or account address.
Data is generally passed into the mappings as Bytes, which if stored directly is returned in the subgraph in a hex
format (ex. block and transaction hashes). You may want to convert to a base64
or base64 URL
-safe format in your mappings, in order to match what is displayed in block explorers like .
The following bytesToBase64(bytes: Uint8Array, urlSafe: boolean): string
helper function can be used, and will be added to graph-ts
:
const base64Alphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M","N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z","a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m","n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z","0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/"];const base64UrlAlphabet = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M","N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z","a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m","n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z","0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "-", "_"];function bytesToBase64(bytes: Uint8Array, urlSafe: boolean): string {let alphabet = urlSafe? base64UrlAlphabet : base64Alphabet;let result = '', i: i32, l = bytes.length;for (i = 2; i < l; i += 3) {result += alphabet[bytes[i - 2] >> 2];result += alphabet[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];result += alphabet[((bytes[i - 1] & 0x0F) << 2) | (bytes[i] >> 6)];result += alphabet[bytes[i] & 0x3F];}if (i === l + 1) { // 1 octet yet to writeresult += alphabet[bytes[i - 2] >> 2];result += alphabet[(bytes[i - 2] & 0x03) << 4];if (!urlSafe) {result += "==";}}if (!urlSafe && i === l) { // 2 octets yet to writeresult += alphabet[bytes[i - 2] >> 2];result += alphabet[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];result += alphabet[(bytes[i - 1] & 0x0F) << 2];if (!urlSafe) {result += "=";}}return result;}