7 minutes
Bygga subgrafer på Arweave
Arweave support in Graph Node and on Subgraph Studio is in beta: please reach us on Discord with any questions about building Arweave subgraphs!
I den här guiden kommer du att lära dig hur du bygger och distribuerar subgrafer för att indexera Weaver-blockkedjan.
Vad är Arweave?
Arweave-protokollet tillåter utvecklare att lagra data permanent och det är den största skillnaden mellan Arweave och IPFS, där IPFS saknar funktionen; beständighet och filer lagrade på Arweave kan inte ändras eller raderas.
Arweave har redan byggt ett flertal bibliotek för att integrera protokollet i ett antal olika programmeringsspråk. För mer information kan du kolla:
Vad är Arweave-subgrafer?
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 GraphQL.
Graph Node 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.
Bygga en Arweave-subgraf
För att kunna bygga och distribuera Arweave Subgraphs behöver du två paket:
@graphprotocol/graph-cli
above version 0.30.2 - This is a command-line tool for building and deploying subgraphs. Click here to download usingnpm
.@graphprotocol/graph-ts
above version 0.27.0 - This is library of subgraph-specific types. Click here to download usingnpm
.
Subgraphs komponenter
Det finns tre komponenter i en subgraf:
1. Manifest - subgraph.yaml
Definierar datakällorna av intresse och hur de ska behandlas. Arweave är en ny typ av datakälla.
2. Schema - schema.graphql
Här definierar du vilken data du vill kunna fråga efter att du har indexerat din subgrafer med GraphQL. Detta liknar faktiskt en modell för ett API, där modellen definierar strukturen för en begäran.
The requirements for Arweave subgraphs are covered by the existing documentation.
3. AssemblyScript Mappings - mapping.ts
Detta är logiken som avgör hur data ska hämtas och lagras när någon interagerar med datakällorna du lyssnar på. Data översätts och lagras utifrån det schema du har listat.
Under subgrafutveckling finns det två nyckelkommandon:
$ 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
Definition av subgraf manifestet
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: arweave name: arweave-blocks network: arweave-mainnet # The Graph only supports Arweave Mainnet source: owner: 'ID-OF-AN-OWNER' # The public key of an Arweave wallet startBlock: 0 # set this to 0 to start indexing from chain genesis mapping: apiVersion: 0.0.5 language: wasm/assemblyscript file: ./src/blocks.ts # link to the file with the Assemblyscript mappings entities: - Block - Transaction blockHandlers: - handler: handleBlock # the function name in the mapping file transactionHandlers: - 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 datakällor introducerar ett valfritt source.owner fält, som är den publika nyckeln till en Arweave plånbok
Arweave datakällor stöder två typer av hanterare:
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
De source.owner kan vara ägarens adress eller deras publika nyckel.
Transaktioner är byggstenarna i Arweave permaweb och de är objekt skapade av slutanvändare.
Note: Irys (previously Bundlr) transactions are not supported yet.
Schema Definition
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 here.
AssemblyScript mappningar
The handlers for processing events are written in AssemblyScript.
Arweave indexing introduces Arweave-specific data types to the AssemblyScript API.
class Block { timestamp: u64 lastRetarget: u64 height: u64 indepHash: Bytes nonce: Bytes previousBlock: Bytes diff: Bytes hash: Bytes txRoot: Bytes txs: Bytes[] walletList: Bytes rewardAddr: Bytes tags: Tag[] rewardPool: Bytes weaveSize: Bytes blockSize: Bytes cumulativeDiff: Bytes hashListMerkle: Bytes poa: ProofOfAccess}class Transaction { format: u32 id: Bytes lastTx: Bytes owner: Bytes tags: Tag[] target: Bytes quantity: Bytes data: Bytes dataSize: Bytes dataRoot: Bytes signature: Bytes reward: 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 here.
Deploying an Arweave Subgraph in Subgraph Studio
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>
Fråga efter en Arweave-subgraf
The GraphQL endpoint for Arweave subgraphs is determined by the schema definition, with the existing API interface. Please visit the GraphQL API documentation for more information.
Exempel på subgrafer
Här är ett exempel på subgraf som referens:
FAQ
Kan en subgraf indexera Arweave och andra kedjor?
Nej, en subgraf kan bara stödja datakällor från en kedja/nätverk.
Kan jag indexera de lagrade filerna på Arweave?
För närvarande indexerar The Graph bara Arweave som en blockkedja (dess block och transaktioner).
Kan jag identifiera Bundlr buntar i min subgraf?
Detta stöds inte för närvarande.
Hur kan jag filtrera transaktioner till ett specifikt konto?
Source.owner kan vara användarens publika nyckel eller kontoadress.
Vad är det aktuella krypteringsformatet?
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 Arweave Explorer.
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 write result += alphabet[bytes[i - 2] >> 2]; result += alphabet[(bytes[i - 2] & 0x03) << 4]; if (!urlSafe) { result += "=="; } } if (!urlSafe && i === l) { // 2 octets yet to write result += 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;}