Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/build/
/generated/

# The generated subgraph manifest
/subgraph.yaml

# Logs
logs
*.log
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
# Validator Delegations Example
# Cosmoshub Validator Delegations Example

This example subgraph stores `Delegation` objects that represent delegations being made in a Cosmos chain. In order to do that, the handler function checks all messages within the transaction and filters them in order to decode and save just the ones that represent a delegation in the chain.

## Generating a manifest

The subgraph is compatible with multiple Cosmos networks so before building the subgraph you need to generate a manifest file for the network of your choice. In case of the Cosmos Hub network, run the following command:

```shell
$ yarn prepare:cosmoshub
```

For the list of supported networks, see the scripts in the [`package.json`](package.json) file.
This example subgraph stores `Delegation` objects that represent delegations being made in the Cosmos Hub chain. In order to do that, the handler function decodes `/cosmos.staking.v1beta1.MsgDelegate` messages.

## Querying the subgraph

Expand Down
25 changes: 25 additions & 0 deletions cosmos/cosmoshub-validator-delegations/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "cosmoshub-validator-delegations",
"version": "0.1.0",
"repository": "https://github.com/graphprotocol/example-subgraph",
"license": "MIT",
"scripts": {
"codegen": "graph codegen",
"build": "graph build",
"create-local": "graph create cosmoshub-validator-delegations --node http://127.0.0.1:8020",
"deploy-local": "graph deploy cosmoshub-validator-delegations -l v0.1.0 --ipfs http://127.0.0.1:5001 --node http://127.0.0.1:8020",
"remove-local": "graph remove cosmoshub-validator-delegations --node http://127.0.0.1:8020",
"create": "graph create cosmoshub-validator-delegations --node https://api.thegraph.com/deploy/",
"deploy": "graph deploy cosmoshub-validator-delegations --ipfs https://api.thegraph.com/ipfs/ --node https://api.thegraph.com/deploy/"
},
"devDependencies": {
"@graphprotocol/graph-cli": "https://github.com/figment-networks/graph-cli.git#cosmos-v1.1",
"@graphprotocol/graph-ts": "https://github.com/figment-networks/graph-ts.git#cosmos-v1.1",
"mustache": "^4.2.0"
},
"dependencies": {
"@graphprotocol/cosmoshub-ts": "https://github.com/graphprotocol/cosmoshub-ts.git",
"babel-polyfill": "^6.26.0",
"babel-register": "^6.26.0"
}
}
31 changes: 31 additions & 0 deletions cosmos/cosmoshub-validator-delegations/src/mapping.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { cosmos } from "@graphprotocol/graph-ts";
import { cosmos as cosmos_sdk } from "@graphprotocol/cosmoshub-ts";
import { Delegation, Coin } from "../generated/schema";

export function handleMsgDelegate(messageData: cosmos.MessageData): void {
const message = cosmos_sdk.staking.v1beta1.decodeMsgDelegate(messageData.message.value);
// Without the txId we could have collision on the Id. To be updated once we have access to ResultTx.index in MessageData
const id = `${messageData.block.header.hash.toHexString()}-${message.delegator_address}-${message.validator_address}`;

saveDelegation(id, message);
}

function saveDelegation(id: string, message: cosmos_sdk.staking.v1beta1.MsgDelegate): void {
const msg = new Delegation(id);

msg.delegatorAddress = message.delegator_address;
msg.validatorAddress = message.validator_address;
msg.amount = saveCoin(id, message.amount as cosmos_sdk.base.v1beta1.Coin);
msg.save();
}

function saveCoin(id: string, c: cosmos_sdk.base.v1beta1.Coin): string {
const coin = new Coin(id);

coin.amount = c.amount;
coin.denom = c.denom;

coin.save();

return id;
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
specVersion: 0.0.5
description: Validator Delegations Example
description: Cosmoshub Validator Delegations Example
repository: https://github.com/graphprotocol/example-subgraph
schema:
file: ./schema.graphql
dataSources:
- kind: cosmos
name: {{ name }}
network: {{ network }}
name: cosmoshub-validator-delegations
network: cosmoshub-4
source:
startBlock: 0
mapping:
Expand All @@ -15,6 +15,7 @@ dataSources:
entities:
- Delegation
- Coin
transactionHandlers:
- handler: handleTx
messageHandlers:
- message: /cosmos.staking.v1beta1.MsgDelegate
handler: handleMsgDelegate
file: ./src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,15 @@
"@ethersproject/properties" "^5.6.0"
"@ethersproject/strings" "^5.6.0"

"@graphprotocol/graph-cli@^0.30.0":
"@graphprotocol/cosmoshub-ts@https://github.com/graphprotocol/cosmoshub-ts.git":
version "0.1.0"
resolved "https://github.com/graphprotocol/cosmoshub-ts.git#ce1f2efa8622d08c4d0675f3543c66668a05eb56"
dependencies:
as-proto "^0.2.3"

"@graphprotocol/graph-cli@https://github.com/figment-networks/graph-cli.git#cosmos-v1.1":
version "0.30.0"
resolved "https://registry.yarnpkg.com/@graphprotocol/graph-cli/-/graph-cli-0.30.0.tgz#ed660426b10a319f5724b1e58336f66b795ef3a9"
integrity sha512-dpj3cy6/49+MKcQxtuZoiFVW/GwiFD+2p12I3Ly59vNtO0rxP/PoEXp9T3SRFkcxyFEOD74IGA3mcHOkYi0jDg==
resolved "https://github.com/figment-networks/graph-cli.git#7e4bb47de8c180510d082dc493aeb8d235b8e19f"
dependencies:
assemblyscript "0.19.10"
binary-install-raw "0.0.13"
Expand Down Expand Up @@ -237,10 +242,9 @@
which "2.0.2"
yaml "1.9.2"

"@graphprotocol/graph-ts@^0.27.0":
version "0.27.0"
resolved "https://registry.yarnpkg.com/@graphprotocol/graph-ts/-/graph-ts-0.27.0.tgz#948fe1716f6082964a01a63a19bcbf9ac44e06ff"
integrity sha512-r1SPDIZVQiGMxcY8rhFSM0y7d/xAbQf5vHMWUf59js1KgoyWpM6P3tczZqmQd7JTmeyNsDGIPzd9FeaxllsU4w==
"@graphprotocol/graph-ts@https://github.com/figment-networks/graph-ts.git#cosmos-v1.1":
version "0.27.0-alpha.1"
resolved "https://github.com/figment-networks/graph-ts.git#3996594afc78f36479cb55c39f0f2309a00eb57d"
dependencies:
assemblyscript "0.19.10"

Expand Down
4 changes: 0 additions & 4 deletions cosmos/validator-delegations/config/cosmoshub.json

This file was deleted.

4 changes: 0 additions & 4 deletions cosmos/validator-delegations/config/osmosis.json

This file was deleted.

27 changes: 0 additions & 27 deletions cosmos/validator-delegations/package.json

This file was deleted.

79 changes: 0 additions & 79 deletions cosmos/validator-delegations/src/decoding.ts

This file was deleted.

38 changes: 0 additions & 38 deletions cosmos/validator-delegations/src/mapping.ts

This file was deleted.