Skip to content

Commit 4b34a0c

Browse files
azf20kruspy
andauthored
Add Cosmos docs (#142)
* First draft of the Cosmos documentation * Apply feedback * Remove translated pages * Apply feedback * Update naming from Cosmos Hub to Cosmos * Rework to a general Cosmos document, adding blockchains at the bottom. * Add Osmosis * Added feedback updates * Update example links * Add Osmosis example * Apply PR review changes * Update tx type from bytes to Tx * Add clarification on transaction decoding Co-authored-by: Marc Puig <krusspy@gmail.com>
1 parent a6ec3f1 commit 4b34a0c

File tree

2 files changed

+231
-0
lines changed

2 files changed

+231
-0
lines changed

navigation/navigation.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ export const navigation: (locale: AppLocale) => NavItemDefinition[] = (locale) =
135135
{
136136
slug: 'near',
137137
},
138+
{
139+
slug: 'cosmos',
140+
},
138141
{
139142
slug: 'arweave',
140143
},
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
---
2+
title: Building Subgraphs on Cosmos
3+
---
4+
5+
This guide is an introduction on building subgraphs indexing [Cosmos](https://docs.cosmos.network/) based blockchains.
6+
7+
## What are Cosmos subgraphs?
8+
9+
The Graph allows developers to process blockchain events and make the resulting data easily available via an open GraphQL API, known as a subgraph. [Graph Node](https://github.com/graphprotocol/graph-node) is now able to process Cosmos events, which means Cosmos developers can now build subgraphs to easily index on-chain events.
10+
11+
There are currently three types of handlers supported for the Cosmos subgraphs:
12+
13+
- Block handlers: run whenever a new block is appended to the chain.
14+
- [Event](https://docs.cosmos.network/master/core/events.html) handlers: run when a specific event is emitted.
15+
- [Transaction](https://docs.cosmos.network/master/core/transactions.html) handlers: run when a transaction occurs.
16+
17+
Based on the [official Cosmos documentation](https://docs.cosmos.network/):
18+
19+
> Events are objects that contain information about the execution of the application. They are mainly used by service providers like block explorers and wallets to track the execution of various messages and index transactions.
20+
21+
> Transactions are objects created by end-users to trigger state changes in the application.
22+
23+
## Building a Cosmos subgraph
24+
25+
### Subgraph Dependencies
26+
27+
[graph-cli](https://github.com/graphprotocol/graph-cli) is a CLI tool to build and deploy subgraphs, version `>=0.30.0` is required in order to work with Cosmos subgraphs.
28+
29+
[graph-ts](https://github.com/graphprotocol/graph-ts) is a library of subgraph-specific types, version `>=0.27.0` is required in order to work with Cosmos subgraphs.
30+
31+
### Subgraph Main Components
32+
33+
There are three main key parts or files when it comes to defining a subgraph:
34+
35+
**subgraph.yaml**: a YAML file containing the subgraph manifest, which identifies which events to track and how to process them.
36+
37+
**schema.graphql**: a GraphQL schema that defines what data is stored for your subgraph, and how to query it via GraphQL.
38+
39+
**AssemblyScript Mappings**: [AssemblyScript](https://github.com/AssemblyScript/assemblyscript) code that translates from blockchain data to the entities defined in your schema.
40+
41+
### Subgraph Manifest Definition
42+
43+
The subgraph manifest (`subgraph.yaml`) identifies the data sources for the subgraph, the triggers of interest, and the functions (`handlers`) that should be run in response to those triggers. See below for an example subgraph manifest for a Cosmos subgraph:
44+
45+
```yaml
46+
specVersion: 0.0.5
47+
description: Cosmos Subgraph Example
48+
schema:
49+
file: ./schema.graphql # link to the schema file
50+
dataSources:
51+
- kind: cosmos
52+
name: CosmosHub
53+
network: cosmoshub-4 # This will change for each cosmos-based blockchain. In this case, the example uses the CosmosHub mainnet.
54+
source:
55+
startBlock: 0 # Required for Cosmos, set this to 0 to start indexing from chain genesis
56+
mapping:
57+
apiVersion: 0.0.7
58+
language: wasm/assemblyscript
59+
blockHandlers:
60+
- handler: handleNewBlock # the function name in the mapping file
61+
eventHandlers:
62+
- event: rewards # the type of the event that will be handled
63+
handler: handleReward # the function name in the mapping file
64+
transactionHandlers:
65+
- handler: handleTransaction # the function name in the mapping file
66+
file: ./src/mapping.ts # link to the file with the Assemblyscript mappings
67+
```
68+
69+
- Cosmos subgraphs introduce a new `kind` of data source (`cosmos`).
70+
- The `network` should correspond to a network on the hosting Graph Node. In the example, the CosmosHub mainnet is used.
71+
72+
Cosmos data sources support three types of handlers:
73+
74+
- `blockHandlers`: run on every new block appended to the chain. The handler will receive a full block and all its data containing, among other things, all the events and transactions.
75+
- `eventHandlers`: run on every event contained in a block that matches the event type specified in the manifest. Block data is also passed onto the mapping in order to have the context of the event within the chain.
76+
- `transactionHandlers`: run for every transaction executed. The mapping is provided with all the relevant data related to the transaction and a block abstraction that can be used to acquire the context of the transaction within a block and within the chain.
77+
78+
Event and Transaction handlers are a way to process meaningful data from the chain without the need of processing a whole block. The data processed by them can also be found in the block handlers, since events and transactions are also part of a block, but removes the need of processing unnecessary data.
79+
80+
### Schema Definition
81+
82+
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 subgraph schema definition [here](https://thegraph.com/docs/en/developer/create-subgraph-hosted/#the-graph-ql-schema).
83+
84+
### AssemblyScript Mappings
85+
86+
The handlers for processing events are written in [AssemblyScript](https://www.assemblyscript.org/).
87+
88+
Cosmos indexing introduces Cosmos-specific data types to the [AssemblyScript API](https://thegraph.com/docs/en/developer/assemblyscript-api/).
89+
90+
```tsx
91+
class Block {
92+
header: Header
93+
evidence: EvidenceList
94+
resultBeginBlock: ResponseBeginBlock
95+
resultEndBlock: ResponseEndBlock
96+
transactions: Array<TxResult>
97+
validatorUpdates: Array<Validator>
98+
}
99+
100+
class EventData {
101+
event: Event
102+
block: HeaderOnlyBlock
103+
}
104+
105+
class TransactionData {
106+
tx: TxResult
107+
block: HeaderOnlyBlock
108+
}
109+
110+
class HeaderOnlyBlock {
111+
header: Header
112+
}
113+
114+
class Header {
115+
version: Consensus
116+
chainId: string
117+
height: u64
118+
time: Timestamp
119+
lastBlockId: BlockID
120+
lastCommitHash: Bytes
121+
dataHash: Bytes
122+
validatorsHash: Bytes
123+
nextValidatorsHash: Bytes
124+
consensusHash: Bytes
125+
appHash: Bytes
126+
lastResultsHash: Bytes
127+
evidenceHash: Bytes
128+
proposerAddress: Bytes
129+
hash: Bytes
130+
}
131+
132+
class TxResult {
133+
height: u64
134+
index: u32
135+
tx: Tx
136+
result: ResponseDeliverTx
137+
hash: Bytes
138+
}
139+
140+
class Event {
141+
eventType: string
142+
attributes: Array<EventAttribute>
143+
}
144+
```
145+
146+
The types above are just the general ones that mappings use. You can find the full list of types for the Cosmos integration [here](https://github.com/graphprotocol/graph-ts/blob/4c064a8118dff43b110de22c7756e5d47fcbc8df/chain/cosmos.ts).
147+
148+
Each type of handler will receive a different type based on the relevant data. For both event and transaction handlers, a reference to the block they are contained in is passed as well. These are the exact types that are passed as a parameter to each mapping function:
149+
150+
`Block` is passed to the blockHandler.
151+
152+
`EventData` is passed to the eventHandler.
153+
154+
`TransactionData` is passed to the transactionHandler. Transactions will need to be decoded in the subgraph, [here](https://github.com/graphprotocol/example-subgraph/blob/cosmos-validator-delegations/src/decoding.ts) is an example on how it can be done.
155+
156+
## Creating and building a Cosmos subgraph
157+
158+
The first step before starting to write the subgraph mappings is to generate the type bindings based on the entities that have been defined in the subgraph schema file (`schema.graphql`). This will allow the mapping functions to create new objects of those types and save them to the store. This is done by using the `codegen` CLI command:
159+
160+
```bash
161+
$ graph codegen
162+
```
163+
164+
Once the mappings are ready, the subgraph needs to be built. This step will highlight any errors the manifest or the mappings might have. A subgraph needs to build successfully in order to be deployed to the Graph Node. It can be done using the `build` CLI command:
165+
166+
```bash
167+
$ graph build
168+
```
169+
170+
## Deploying a Cosmos subgraph
171+
172+
Once your subgraph has been created, you can deploy your subgraph by using the `graph deploy` CLI command after running the `graph create` CLI command:
173+
174+
**Hosted Service**
175+
```bash
176+
graph create subgraph-name --product hosted-service # creates a subgraph on a local Graph Node (on the Hosted Service, this is done via the UI)
177+
```
178+
```bash
179+
graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ --access-token <your-access-token>
180+
```
181+
182+
**Local Graph Node (based on default configuration):**
183+
```bash
184+
graph create subgraph-name --node http://localhost:8020
185+
```
186+
```bash
187+
graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001
188+
```
189+
190+
## Querying a Cosmos subgraph
191+
192+
The GraphQL endpoint for Cosmos subgraphs is determined by the schema definition, with the existing API interface. Please visit the [GraphQL API documentation](https://thegraph.com/docs/en/developer/graphql-api/) for more information.
193+
194+
## Supported Cosmos Blockchains
195+
196+
### Cosmos Hub
197+
198+
##### What is Cosmos Hub?
199+
200+
The [Cosmos Hub blockchain](https://hub.cosmos.network/) is the first blockchain in the [Cosmos](https://cosmos.network/) ecosystem. You can visit the [official documentation](https://docs.cosmos.network/) for more information.
201+
202+
##### Networks
203+
204+
Cosmos Hub mainnet is `cosmoshub-4`. Cosmos Hub current testnet is `theta-testnet-001`. <br/>Other Cosmos Hub networks, i.e. `cosmoshub-3`, are halted, therefore no data is provided for them.
205+
206+
### Osmosis
207+
208+
> Osmosis support in Graph Node and on the Hosted Service is in beta: please contact the graph team with any questions about building Osmosis subgraphs!
209+
210+
##### What is Osmosis?
211+
212+
[Osmosis](https://osmosis.zone/) is a decentralized, cross-chain automated market maker (AMM) protocol built on top of the Cosmos SDK. It allows users to create custom liquidity pools and trade IBC-enabled tokens. You can visit the [official documentation](https://docs.osmosis.zone/) for more information.
213+
214+
##### Networks
215+
216+
Osmosis mainnet is `osmosis-1`. Osmosis current testnet is `osmo-test-4`.
217+
218+
## Example Subgraphs
219+
220+
Here are some example subgraphs for reference:
221+
222+
[Block Filtering Example](https://github.com/graphprotocol/example-subgraph/tree/cosmos-block-filtering)
223+
224+
[Validator Rewards Example](https://github.com/graphprotocol/example-subgraph/tree/cosmos-validator-rewards)
225+
226+
[Validator Delegations Example](https://github.com/graphprotocol/example-subgraph/tree/cosmos-validator-delegations)
227+
228+
[Osmosis Token Swaps Example](https://github.com/graphprotocol/example-subgraph/tree/osmosis-token-swaps)

0 commit comments

Comments
 (0)