Skip to content

Commit b2dd6af

Browse files
committed
init subgraph
following along with https://thegraph.com/docs/en/developing/creating-a-subgraph/ ran `graph init` options selected: ✔ Protocol · near ✔ Subgraph name · TENK-DAO/v2.tenk.testnet ✔ Directory to create the subgraph in · subgraph ✔ NEAR network · near-testnet ✔ Contract account · v2.tenk.testnet ✔ Contract Name · Contract ——— Generate subgraph Write subgraph to directory ✔ Create subgraph scaffold ✔ Initialize networks config ✔ Initialize subgraph repository ✔ Install dependencies with yarn ✔ Generate ABI and schema types with yarn codegen ✔ Add another contract? (y/N) · false
1 parent 4c5e787 commit b2dd6af

File tree

7 files changed

+3590
-0
lines changed

7 files changed

+3590
-0
lines changed

subgraph/generated/schema.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
3+
import {
4+
TypedMap,
5+
Entity,
6+
Value,
7+
ValueKind,
8+
store,
9+
Bytes,
10+
BigInt,
11+
BigDecimal
12+
} from "@graphprotocol/graph-ts";
13+
14+
export class ExampleEntity extends Entity {
15+
constructor(id: string) {
16+
super();
17+
this.set("id", Value.fromString(id));
18+
}
19+
20+
save(): void {
21+
let id = this.get("id");
22+
assert(id != null, "Cannot save ExampleEntity entity without an ID");
23+
if (id) {
24+
assert(
25+
id.kind == ValueKind.STRING,
26+
`Entities of type ExampleEntity must have an ID of type String but the id '${id.displayData()}' is of type ${id.displayKind()}`
27+
);
28+
store.set("ExampleEntity", id.toString(), this);
29+
}
30+
}
31+
32+
static load(id: string): ExampleEntity | null {
33+
return changetype<ExampleEntity | null>(store.get("ExampleEntity", id));
34+
}
35+
36+
get id(): string {
37+
let value = this.get("id");
38+
return value!.toString();
39+
}
40+
41+
set id(value: string) {
42+
this.set("id", Value.fromString(value));
43+
}
44+
45+
get block(): Bytes {
46+
let value = this.get("block");
47+
return value!.toBytes();
48+
}
49+
50+
set block(value: Bytes) {
51+
this.set("block", Value.fromBytes(value));
52+
}
53+
54+
get count(): BigInt {
55+
let value = this.get("count");
56+
return value!.toBigInt();
57+
}
58+
59+
set count(value: BigInt) {
60+
this.set("count", Value.fromBigInt(value));
61+
}
62+
}

subgraph/networks.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"near-testnet": {
3+
"Contract": {}
4+
}
5+
}

subgraph/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "v2.tenk.testnet",
3+
"license": "UNLICENSED",
4+
"scripts": {
5+
"codegen": "graph codegen",
6+
"build": "graph build",
7+
"deploy": "graph deploy --node https://api.thegraph.com/deploy/ TENK-DAO/v2.tenk.testnet",
8+
"create-local": "graph create --node http://localhost:8020/ TENK-DAO/v2.tenk.testnet",
9+
"remove-local": "graph remove --node http://localhost:8020/ TENK-DAO/v2.tenk.testnet",
10+
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 TENK-DAO/v2.tenk.testnet",
11+
"test": "graph test"
12+
},
13+
"dependencies": {
14+
"@graphprotocol/graph-cli": "0.33.0",
15+
"@graphprotocol/graph-ts": "0.27.0"
16+
},
17+
"devDependencies": { "matchstick-as": "0.5.0" }
18+
}

subgraph/src/contract.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { near, BigInt } from "@graphprotocol/graph-ts"
2+
import { ExampleEntity } from "../generated/schema"
3+
4+
export function handleReceipt(
5+
receiptWithOutcome: near.ReceiptWithOutcome
6+
): void {
7+
// Entities can be loaded from the store using a string ID; this ID
8+
// needs to be unique across all entities of the same type
9+
let entity = ExampleEntity.load(receiptWithOutcome.receipt.id.toHex())
10+
11+
// Entities only exist after they have been saved to the store;
12+
// `null` checks allow to create entities on demand
13+
if (!entity) {
14+
entity = new ExampleEntity(receiptWithOutcome.receipt.id.toHex())
15+
16+
// Entity fields can be set using simple assignments
17+
entity.count = BigInt.fromI32(0)
18+
}
19+
20+
// BigInt and BigDecimal math are supported
21+
entity.count = entity.count + BigInt.fromI32(1)
22+
23+
// Entity fields can be set based on receipt information
24+
entity.block = receiptWithOutcome.block.header.hash
25+
26+
// Entities can be written to the store with `.save()`
27+
entity.save()
28+
29+
// Note: If a handler doesn't require existing field values, it is faster
30+
// _not_ to load the entity from the store. Instead, create it fresh with
31+
// `new Entity(...)`, set the fields that should be updated and save the
32+
// entity back to the store. Fields that were not set or unset remain
33+
// unchanged, allowing for partial updates to be applied.
34+
}

subgraph/subgraph.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
specVersion: 0.0.4
2+
schema:
3+
file: ./schema.graphql
4+
dataSources:
5+
- kind: near
6+
name: Contract
7+
network: near-testnet
8+
source:
9+
account: "v2.tenk.testnet"
10+
mapping:
11+
apiVersion: 0.0.6
12+
language: wasm/assemblyscript
13+
entities:
14+
- ExampleEntity
15+
receiptHandlers:
16+
- handler: handleReceipt
17+
file: ./src/contract.ts

subgraph/tsconfig.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"extends": "@graphprotocol/graph-ts/types/tsconfig.base.json",
3+
"include": ["src"]
4+
}

0 commit comments

Comments
 (0)