Skip to content

Commit

Permalink
init subgraph
Browse files Browse the repository at this point in the history
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
  • Loading branch information
chadoh committed Aug 10, 2022
1 parent 4c5e787 commit b2dd6af
Show file tree
Hide file tree
Showing 7 changed files with 3,590 additions and 0 deletions.
62 changes: 62 additions & 0 deletions subgraph/generated/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.

import {
TypedMap,
Entity,
Value,
ValueKind,
store,
Bytes,
BigInt,
BigDecimal
} from "@graphprotocol/graph-ts";

export class ExampleEntity extends Entity {
constructor(id: string) {
super();
this.set("id", Value.fromString(id));
}

save(): void {
let id = this.get("id");
assert(id != null, "Cannot save ExampleEntity entity without an ID");
if (id) {
assert(
id.kind == ValueKind.STRING,
`Entities of type ExampleEntity must have an ID of type String but the id '${id.displayData()}' is of type ${id.displayKind()}`
);
store.set("ExampleEntity", id.toString(), this);
}
}

static load(id: string): ExampleEntity | null {
return changetype<ExampleEntity | null>(store.get("ExampleEntity", id));
}

get id(): string {
let value = this.get("id");
return value!.toString();
}

set id(value: string) {
this.set("id", Value.fromString(value));
}

get block(): Bytes {
let value = this.get("block");
return value!.toBytes();
}

set block(value: Bytes) {
this.set("block", Value.fromBytes(value));
}

get count(): BigInt {
let value = this.get("count");
return value!.toBigInt();
}

set count(value: BigInt) {
this.set("count", Value.fromBigInt(value));
}
}
5 changes: 5 additions & 0 deletions subgraph/networks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"near-testnet": {
"Contract": {}
}
}
18 changes: 18 additions & 0 deletions subgraph/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "v2.tenk.testnet",
"license": "UNLICENSED",
"scripts": {
"codegen": "graph codegen",
"build": "graph build",
"deploy": "graph deploy --node https://api.thegraph.com/deploy/ TENK-DAO/v2.tenk.testnet",
"create-local": "graph create --node http://localhost:8020/ TENK-DAO/v2.tenk.testnet",
"remove-local": "graph remove --node http://localhost:8020/ TENK-DAO/v2.tenk.testnet",
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 TENK-DAO/v2.tenk.testnet",
"test": "graph test"
},
"dependencies": {
"@graphprotocol/graph-cli": "0.33.0",
"@graphprotocol/graph-ts": "0.27.0"
},
"devDependencies": { "matchstick-as": "0.5.0" }
}
34 changes: 34 additions & 0 deletions subgraph/src/contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { near, BigInt } from "@graphprotocol/graph-ts"
import { ExampleEntity } from "../generated/schema"

export function handleReceipt(
receiptWithOutcome: near.ReceiptWithOutcome
): void {
// Entities can be loaded from the store using a string ID; this ID
// needs to be unique across all entities of the same type
let entity = ExampleEntity.load(receiptWithOutcome.receipt.id.toHex())

// Entities only exist after they have been saved to the store;
// `null` checks allow to create entities on demand
if (!entity) {
entity = new ExampleEntity(receiptWithOutcome.receipt.id.toHex())

// Entity fields can be set using simple assignments
entity.count = BigInt.fromI32(0)
}

// BigInt and BigDecimal math are supported
entity.count = entity.count + BigInt.fromI32(1)

// Entity fields can be set based on receipt information
entity.block = receiptWithOutcome.block.header.hash

// Entities can be written to the store with `.save()`
entity.save()

// Note: If a handler doesn't require existing field values, it is faster
// _not_ to load the entity from the store. Instead, create it fresh with
// `new Entity(...)`, set the fields that should be updated and save the
// entity back to the store. Fields that were not set or unset remain
// unchanged, allowing for partial updates to be applied.
}
17 changes: 17 additions & 0 deletions subgraph/subgraph.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
specVersion: 0.0.4
schema:
file: ./schema.graphql
dataSources:
- kind: near
name: Contract
network: near-testnet
source:
account: "v2.tenk.testnet"
mapping:
apiVersion: 0.0.6
language: wasm/assemblyscript
entities:
- ExampleEntity
receiptHandlers:
- handler: handleReceipt
file: ./src/contract.ts
4 changes: 4 additions & 0 deletions subgraph/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "@graphprotocol/graph-ts/types/tsconfig.base.json",
"include": ["src"]
}
Loading

0 comments on commit b2dd6af

Please sign in to comment.