5 dakika
Bir Sözleşmeyi Değiştirin ve Graftlama ile Geçmişini Koruyun
In this guide, you will learn how to build and deploy new Subgraphs by grafting existing Subgraphs.
Graftlama Nedir?
Grafting reuses the data from an existing Subgraph and starts indexing it at a later block. This is 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. Also, it can be used when adding a feature to a Subgraph that takes long to index from scratch.
Aşılanan subgraph, temel subgraphla tamamen aynı olmayan, ancak onunla uyumlu olan bir GraphQL şeması kullanabilir. Kendi başına geçerli bir subgraph şeması olmalıdır, ancak şu şekillerde temel subgraph şemasından sapabilir:
- Varlık türlerini ekler veya kaldırır
- Varlık türlerinden öznitelikleri kaldırır
- Varlık türlerine null yapılabilir öznitelikler ekler
- Null yapılamayan öznitelikleri null yapılabilir özniteliklere dönüştürür
- Numaralandırmalara değerler ekler
- Arayüzleri ekler veya kaldırır
- Arayüzün hangi varlık türleri için uygulandığını değiştirir
Daha fazla bilgi için kontrol edebilirsiniz:
In this tutorial, we will be covering a basic use case. We will replace an existing contract with an identical contract (with a new address, but the same code). Then, graft the existing Subgraph onto the “base” Subgraph that tracks the new contract.
Ağa Yükseltme Durumunda Graftlamaya İlişkin Önemli Not
Caution: It is recommended to not use grafting for Subgraphs published to The Graph Network
Bu Neden Önemli?
Grafting is a powerful feature that allows you to “graft” one Subgraph onto another, effectively transferring historical data from the existing Subgraph to a new version. It is not possible to graft a Subgraph from The Graph Network back to Subgraph Studio.
En İyi Uygulamalar
Initial Migration: when you first deploy your Subgraph to the decentralized network, do so without grafting. Ensure that the Subgraph is stable and functioning as expected.
Subsequent Updates: once your Subgraph is live and stable on the decentralized network, you may use grafting for future versions to make the transition smoother and to preserve historical data.
Bu yönergelere uyarak riskleri en aza indirebilir ve daha sorunsuz bir taşıma süreci geçirebilirsiniz.
Mevcut Bir Subgraph’ı Oluşturma
Building Subgraphs is an essential part of The Graph, described more in depth here. To be able to build and deploy the existing Subgraph used in this tutorial, the following repo is provided:
Note: The contract used in the Subgraph was taken from the following Hackathon Starterkit.
Subgraph Manifest Tanımı
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 that you will use:
1specVersion: 1.3.02schema:3 file: ./schema.graphql4dataSources:5 - kind: ethereum6 name: Lock7 network: sepolia8 source:9 address: '0xb3aabe721794b85fe4e72134795c2f93b4eb7e63'10 abi: Lock11 startBlock: 595569012 mapping:13 kind: ethereum/events14 apiVersion: 0.0.915 language: wasm/assemblyscript16 entities:17 - Withdrawal18 abis:19 - name: Lock20 file: ./abis/Lock.json21 eventHandlers:22 - event: Withdrawal(uint256,uint256)23 handler: handleWithdrawal24 file: ./src/lock.tsLockveri kaynağı, sözleşmeyi derleyip dağıttığımızda elde edeceğimiz “abi” ve sözleşme adresidir- Ağ, sorgulanan endekslenmiş bir ağa karşılık gelmelidir. Sepolia testnet üzerinde çalıştığımız için, ağ
sepolia’dır mappingbölümü, ilgili tetikleyicileri ve bu tetikleyicilere yanıt olarak çalıştırılması gereken fonksiyonları tanımlar. Bu durumda,Withdrawalolayını dinliyoruz ve yayarkenhandleWithdrawalfonksiyonunu çağırıyoruz.
Graftlama Manifest Tanımı
Grafting requires adding two new items to the original Subgraph manifest:
1---2features:3 - grafting # feature name4graft:5 base: Qm... # Subgraph ID of base Subgraph6 block: 5956000 # block numberfeatures:tüm kullanılan özellik adlarının bir listesidir.graft:is a map of thebaseSubgraph and the block to graft on to. Theblockis the block number to start indexing from. The Graph 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 and block values can be found by deploying two Subgraphs: one for the base indexing and one with grafting
Temel Subgraph’ı Dağıtma
- Go to Subgraph Studio and create a Subgraph on Sepolia testnet called
graft-example - Follow the directions in the
AUTH & DEPLOYsection on your Subgraph page in thegraft-examplefolder from the repo - Once finished, verify the Subgraph is indexing properly. If you run the following command in The Graph Playground
1{2 withdrawals(first: 5) {3 id4 amount5 when6 }7}Şuna benzer bir şey döndürür:
1{2 "data": {3 "withdrawals": [4 {5 "id": "0xe8323d21c4f104607b10b0fff9fc24b9612b9488795dea8196b2d5f980d3dc1d0a000000",6 "amount": "0",7 "when": "1716394824"8 },9 {10 "id": "0xea1cee35036f2cacb72f2a336be3e54ab911f5bebd58f23400ebb8ecc5cfc45203000000",11 "amount": "0",12 "when": "1716394848"13 }14 ]15 }16}Once you have verified the Subgraph is indexing properly, you can quickly update the Subgraph with grafting.
Graftlama Subgraph’ını Dağıtma
Graft yerine geçen subgraph.yaml yeni bir sözleşme adresine sahip olacaktır. Bu, merkeziyetsiz uygulamanızı güncellediğinizde, bir sözleşmeyi yeniden dağıttığınızda vb. gerçekleşebilir.
- Go to Subgraph Studio and create a Subgraph on Sepolia testnet called
graft-replacement - Create a new manifest. The
subgraph.yamlforgraph-replacementcontains a different contract address and new information about how it should graft. These are theblockof the last event emitted you care about by the old contract and thebaseof the old Subgraph. ThebaseSubgraph ID is theDeployment IDof your originalgraph-exampleSubgraph. You can find this in Subgraph Studio. - Follow the directions in the
AUTH & DEPLOYsection on your Subgraph page in thegraft-replacementfolder from the repo - Once finished, verify the Subgraph is indexing properly. If you run the following command in The Graph Playground
1{2 withdrawals(first: 5) {3 id4 amount5 when6 }7}Aşağıdakileri döndürmelidir:
1{2 "data": {3 "withdrawals": [4 {5 "id": "0xe8323d21c4f104607b10b0fff9fc24b9612b9488795dea8196b2d5f980d3dc1d0a000000",6 "amount": "0",7 "when": "1716394824"8 },9 {10 "id": "0xea1cee35036f2cacb72f2a336be3e54ab911f5bebd58f23400ebb8ecc5cfc45203000000",11 "amount": "0",12 "when": "1716394848"13 },14 {15 "id": "0x2410475f76a44754bae66d293d14eac34f98ec03a3689cbbb56a716d20b209af06000000",16 "amount": "0",17 "when": "1716429732"18 }19 ]20 }21}You can see that the graft-replacement Subgraph is indexing from older graph-example data and newer data from the new contract address. The original contract emitted two Withdrawal events, Event 1 and Event 2. The new contract emitted one Withdrawal after, Event 3. The two previously indexed transactions (Event 1 and 2) and the new transaction (Event 3) were combined together in the graft-replacement Subgraph.
Congrats! You have successfully grafted a Subgraph onto another Subgraph.
Ek Kaynaklar
Aşılama konusunda daha fazla deneyim kazanmak istiyorsanız, yaygın kullanılan sözleşmeler için aşağıda birkaç örnek bulunmaktadır:
Daha da iyi bir Graph uzmanı olmak için, temel veri kaynaklarındaki değişikliklerle başa çıkmanın diğer yollarını öğrenmeyi değerlendirin. Veri Kaynağı Şablonları gibi alternatifler benzer sonuçlar elde edebilir
Not: Bu makaledeki materyalin büyük bir kısmı, daha önce yayımlanmış olan Arweave makalesinden alınmıştır