|
| 1 | +# Dymension Quick Start |
| 2 | + |
| 3 | +Dymension works like a big web system. Users use RollApps (front-end), the Dymension Hub (back-end) manages everything, and data networks (database) store information. RollApps are like interactive apps in Dymension. |
| 4 | + |
| 5 | +The goal of this quick start guide is to index all transfer events and messages on the [Dymension network](https://dymension.network/). |
| 6 | + |
| 7 | +<!-- @include: ../snippets/cosmos-quickstart-reference.md --> |
| 8 | + |
| 9 | +::: tip |
| 10 | +The final code of this project can be found [here](https://github.com/subquery/cosmos-subql-starter/tree/main/Dymension/dymension-starter). |
| 11 | +::: |
| 12 | + |
| 13 | +<!-- @include: ../snippets/cosmos-manifest-intro.md --> |
| 14 | + |
| 15 | +::: code-tabs |
| 16 | + |
| 17 | +@tab `project.ts` |
| 18 | + |
| 19 | +```ts |
| 20 | +dataSources: [ |
| 21 | + { |
| 22 | + kind: CosmosDatasourceKind.Runtime, |
| 23 | + startBlock: 1326903, |
| 24 | + mapping: { |
| 25 | + file: "./dist/index.js", |
| 26 | + handlers: [ |
| 27 | + { |
| 28 | + handler: "handleEvent", |
| 29 | + kind: CosmosHandlerKind.Event, |
| 30 | + filter: { |
| 31 | + type: "transfer", |
| 32 | + messageFilter: { |
| 33 | + type: "/cosmos.bank.v1beta1.MsgSend", |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + ], |
| 38 | + }, |
| 39 | + }, |
| 40 | +]; |
| 41 | +``` |
| 42 | + |
| 43 | +::: |
| 44 | + |
| 45 | +Here we are in search of a single type of – namely, `transfer` representing the transfers. |
| 46 | + |
| 47 | +<!-- @include: ../snippets/cosmos-manifest-note.md --> |
| 48 | + |
| 49 | +<!-- @include: ../snippets/schema-intro.md --> |
| 50 | + |
| 51 | +```graphql |
| 52 | +type Transfer @entity { |
| 53 | + id: ID! |
| 54 | + blockHeight: BigInt |
| 55 | + txHash: String |
| 56 | + fromAddress: String |
| 57 | + toAddress: String |
| 58 | + amount: String |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +The single enity is the `Transfer`. |
| 63 | + |
| 64 | +<!-- @include: ../snippets/cosmos-codegen.md --> |
| 65 | + |
| 66 | +<!-- @include: ../snippets/cosmos-mapping-intro.md --> |
| 67 | + |
| 68 | +::: code-tabs |
| 69 | +@tab:active `mappingHandlers.ts` |
| 70 | + |
| 71 | +```ts |
| 72 | +import { Transfer } from "../types"; |
| 73 | +import { CosmosEvent } from "@subql/types-cosmos"; |
| 74 | + |
| 75 | +export async function handleEvent(event: CosmosEvent): Promise<void> { |
| 76 | + const eventRecord = Transfer.create({ |
| 77 | + id: `${event.tx.hash}-${event.msg.idx}-${event.idx}`, |
| 78 | + blockHeight: BigInt(event.block.block.header.height), |
| 79 | + txHash: event.tx.hash, |
| 80 | + toAddress: "", |
| 81 | + amount: "", |
| 82 | + fromAddress: "", |
| 83 | + }); |
| 84 | + for (const attr of event.event.attributes) { |
| 85 | + switch (attr.key) { |
| 86 | + case "recipient": |
| 87 | + eventRecord.toAddress = attr.value; |
| 88 | + break; |
| 89 | + case "amount": |
| 90 | + eventRecord.amount = attr.value; |
| 91 | + break; |
| 92 | + case "sender": |
| 93 | + eventRecord.fromAddress = attr.value; |
| 94 | + break; |
| 95 | + default: |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + await eventRecord.save(); |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +::: |
| 104 | + |
| 105 | +In the Dymension SubQuery project, we have two a single function, namely `handleEvent`. The `handleEvent` function is also triggered when a `/cosmos.bank.v1beta1.MsgSend` type message is detected for a transfer. It receives an event of type `CosmosEvent`, and then it also extracts blockHeight, transaction hash, from, to and amount from the `event` object. |
| 106 | + |
| 107 | +<!-- @include: ../snippets/cosmos-mapping-note.md --> |
| 108 | + |
| 109 | +::: tip |
| 110 | +The final code of this project can be found [here](https://github.com/subquery/cosmos-subql-starter/tree/main/Dymension/dymension-starter). |
| 111 | +::: |
| 112 | + |
| 113 | +<!-- @include: ../snippets/build.md --> |
| 114 | + |
| 115 | +<!-- @include: ../snippets/run-locally.md --> |
| 116 | + |
| 117 | +<!-- @include: ../snippets/query-intro.md --> |
| 118 | + |
| 119 | +#### Request |
| 120 | + |
| 121 | +```graphql |
| 122 | +{ |
| 123 | + query { |
| 124 | + transfers(first: 5) { |
| 125 | + nodes { |
| 126 | + id |
| 127 | + blockHeight |
| 128 | + txHash |
| 129 | + recipient |
| 130 | + sender |
| 131 | + amount |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | +} |
| 136 | +``` |
| 137 | + |
| 138 | +#### Response |
| 139 | + |
| 140 | +```json |
| 141 | +{ |
| 142 | + "data": { |
| 143 | + "query": { |
| 144 | + "transfers": { |
| 145 | + "nodes": [ |
| 146 | + { |
| 147 | + "id": "56C9A9D33C8C222A25D16AFF1C032561703F765D3DB1DD2B217B07DDB394A24C-0-0", |
| 148 | + "blockHeight": "1651519", |
| 149 | + "txHash": "56C9A9D33C8C222A25D16AFF1C032561703F765D3DB1DD2B217B07DDB394A24C", |
| 150 | + "fromAddress": "dym1g8sf7w4cz5gtupa6y62h3q6a4gjv37pgefnpt5", |
| 151 | + "toAddress": "dym1a4gqlkq2qu8fnncxfv9wdt67zthcfyqwx92g0g", |
| 152 | + "amount": "200000000000000000000udym" |
| 153 | + } |
| 154 | + ] |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | +``` |
| 160 | + |
| 161 | +<!-- @include: ../snippets/whats-next.md --> |
0 commit comments