Skip to content
This repository was archived by the owner on Jan 18, 2023. It is now read-only.

Commit 9606a86

Browse files
authored
Lev 64 update subgraph schema to capture new (#2)
* Subgraph schema update to capture new events, and deployment refactor to support rapid subgraph development and testing * Resolved local Hardhat network state deployment and updated subgraph schema to index new StreamingFeeModule and TradeModule events * Add subgraph query example * [lev-173-subgraph-migration-rollout] Added old subgraph schema entities/fields to prevent breaking changes to front-end on new subgraph sync. Deprecated entries must be removed after new subgraph sync and test.
1 parent eb0cfbe commit 9606a86

34 files changed

+915
-238
lines changed

README.md

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,25 @@
22

33
Indexer of Set Protocol v2 events. Built on [The Graph](https://thegraph.com/).
44

5+
<!--
6+
TO-DO:
7+
- Tutorials
8+
- Deploy a local subgraph
9+
- Deploy a subgraph to Hosted Service
10+
- Deploy a subgraph to Subgraph Studio
11+
- How-To Guides
12+
- Update the subgraph schema
13+
- Update the subgraph mappings
14+
- Test the subgraph locally
15+
- Set up a Postman query
16+
- Technical Reference
17+
- Docker compose usage
18+
- Task usage
19+
- Background Information
20+
- Schema structure
21+
- Query structure
22+
-->
23+
524
## SETUP
625

726
### Requirements:
@@ -10,19 +29,21 @@ Indexer of Set Protocol v2 events. Built on [The Graph](https://thegraph.com/).
1029

1130
### Local Deployment (Hardhat)
1231

13-
1. Build the subgraph Docker image
32+
1. Build the Set Protocol Docker base and hardhat images
1433

1534
`task docker-build`
1635

17-
1. Deploy a Hardhat node (use `task deploy-hardhat -- detach` to start container in detached mode)
36+
1. Deploy a Hardhat node and custom script to the network
37+
38+
`task deploy-hardhat -- /full/path/to/test/script.ts`
1839

19-
`task deploy-hardhat`
40+
e.g., deploy the subgraph test state setup script with
2041

21-
1. Monitor the Hardhat node until fully deployed and tests are executed, e.g., if running detached use
42+
`task deploy-hardhat -- $(pwd)/test/deploy-state.ts`
2243

23-
`docker logs docker-hardhat-1 --follow`
44+
1. Monitor the Hardhat node until fully deployed and tests are executed
2445

25-
1. Compile the Set Protocol ABIs
46+
1. In a new terminal, compile the Set Protocol ABIs
2647

2748
`task gen-abi`
2849

@@ -32,6 +53,8 @@ Indexer of Set Protocol v2 events. Built on [The Graph](https://thegraph.com/).
3253

3354
1. Once deployed, query the subgraph in the browser at (by default) http://127.0.0.1:8000/subgraphs/name/SetProtocol/set-protocol-v2
3455

56+
Example query to run can be found in `test/sample-query.txt`
57+
3558
### [TO-DO] External Deployment (Graph Hosted Service / Subgraph Studio)
3659

3760
TBD
@@ -40,19 +63,19 @@ TBD
4063

4164
Available tasks for this project:
4265

43-
| COMMAND [OPTS] | DESCRIPTION |
44-
|------------------------------|---------------------------------------------------------------------------------|
45-
| `clean [-- all]` | Clean up local subgraph deployment; `all` arg additionally removes all volumes and the Hardhat node. |
46-
| `deploy-hardhat [-- detach]` | Deploy a Hardhat node and subgraph tests; `detach` runs container detached. |
47-
| `deploy-hosted [-- detach]` | Build and deploy subgraph on Hosted Service; `detach` runs container detached. |
48-
| `deploy-local [-- detach]` | Build and deploy subgraph on local network; `detach` runs container detached. |
49-
| `destroy-hardhat` | Tear down the deployed Hardhat node. |
50-
| `docker-build` | Build subgraph Docker image on defined node version base (default: 16-slim). |
51-
| `gen-abi` | Pull latest Set Protocol ABIs into the build environment. |
66+
| COMMAND [OPTS] | DESCRIPTION |
67+
|--------------------------------------|---------------------------------------------------------------------------------|
68+
| `clean [-- all\|subgraph\|hardhat]` | Clean up local subgraph deployment; `all` arg additionally removes all volumes and the Hardhat node. |
69+
| `deploy-hardhat -- /path/to/file.ts` | Deploy a local Hardhat node and run a test script. Must specify full path to file as task input argument. |
70+
| `deploy-hosted [-- detach]` | Build and deploy subgraph on Hosted Service; `detach` runs container detached. |
71+
| `deploy-local [-- detach]` | Build and deploy subgraph on local network; `detach` runs container detached. |
72+
| `docker-build` | Build subgraph Docker image on defined node version base (default: 16-slim). |
73+
| `gen-abi` | Pull latest Set Protocol ABIs into the build environment. |
74+
| `gen-schema` | Compile the subgraph schema but do not deploy the subgraph. |
5275

5376
## [TO-DO] ADVANCED DEPLOYMENT GUIDES
5477

55-
TBD: Things to be covered in this section
78+
TBD: Ideas to be covered in this section
5679

5780
- custom override of args (requires custom untracked .env configs or CLI arg overrides)
5881
- the [Set Protocol V2 repo](https://github.com/SetProtocol/set-protocol-v2.git) currently requires node <= 16; therefore, Node 16 is the default target base image used in the Subgraph Docker image.
@@ -61,9 +84,12 @@ TBD: Things to be covered in this section
6184

6285
## [TO-DO] SUBGRAPH DEVELOPMENT
6386

64-
### Tutorial
87+
### [TEMP] Dev Notes
6588

66-
TBD
89+
- Each named dataSource or template entry should be in its own mappings/<entity>.ts file
90+
- Entity names cannot end with "s" due to conflict with query API (not currently documented)
91+
- Use `setToken` for schema fields, not `set` as will conflict will built-in callers
92+
- Templates must be initialized appropriately (see `ModuleInitialize` event handler for example)
6793

6894
### Reference Guide
6995

@@ -81,7 +107,7 @@ To Be Completed
81107

82108
`src/mappings/` - Event handlers
83109

84-
`src/entities/` - Entity helper functions
110+
`src/utils/` - Entity helper functions and other utilities
85111

86112
#### Historical Entities
87113

@@ -114,12 +140,12 @@ Prefixes:
114140

115141
#### Event/Call/Block Handlers
116142

117-
Process events, function calls, and block data to update the subgraph. Must register handlers in `templates/subgraph.yaml`.
143+
Process events, smart contract function calls, and block data to update the subgraph. Must register handlers in `templates/subgraph.yaml`.
118144

119145
#### Template spawners
120146

121147
Not all contract addresses are known at the time of subgraph deployment. To track contracts as they are deployed, use contract templates.
122-
Tell the subgraph to watch a newly created contract by calling create() on imports from `generated/templates`. Ex the SetToken factory contract (SetTokenCreator) emits an event when a new SetToken is created, so we register that address as a new SetToken contract to watch. Templates are defined in `templates/subgraph.yaml`.
148+
Tell the subgraph to watch a newly created contract by calling `create()` on imports from `generated/templates`. For example, the SetToken factory contract (SetTokenCreator) emits an event when a new SetToken is created, so we register that address as a new SetToken contract to watch. Templates are defined in `templates/subgraph.yaml`.
123149

124150
## References
125151

Taskfile.yml

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,35 @@ dotenv: ["./docker/.env"]
55
tasks:
66

77
clean:
8-
desc: "Clean up local subgraph deployment; deletes all locally deployed subgraph data."
8+
desc: "Clean up deployments. Options: 'subgraph' (default), 'hardhat', or 'all'."
99
dir: docker/
1010
cmds:
11-
- docker-compose -f docker-compose.local.yml down --volumes {{.ARGS}}
11+
- docker-compose -f {{.TARGET}} down --volumes {{.ARGS}}
12+
- rm -rf ../generated ../build
1213
vars:
14+
TARGET:
15+
sh: if [ "{{.CLI_ARGS}}" = "hardhat" ]; then
16+
echo "docker-compose.hardhat.yml";
17+
else
18+
echo "docker-compose.local.yml";
19+
fi
1320
ARGS:
14-
sh: if [ "{{.CLI_ARGS}}" = "all" ]; then echo "--remove-orphans"; else echo ""; fi
21+
sh: if [ "{{.CLI_ARGS}}" = "all" ]; then
22+
echo "--remove-orphans";
23+
else
24+
echo "";
25+
fi
1526

1627
deploy-hardhat:
17-
desc: "Deploy a Hardhat node and subgraph tests."
28+
desc: "Deploy a local Hardhat node and test script. Must specify full path to file as task input argument."
1829
dir: docker/
1930
cmds:
20-
- docker-compose -f docker-compose.hardhat.yml up {{.ARGS}}
31+
- docker-compose -f docker-compose.hardhat.yml run --name {{.HARDHAT_IP}} --rm -w /app -v {{.CLI_ARGS}}:/tmp/{{.DEPLOY_SCRIPT}} hardhat-build bash /app/scripts/deploy-hardhat.sh
2132
vars:
22-
ARGS:
23-
sh: if [ "{{.CLI_ARGS}}" = "detach" ]; then echo "-d"; else echo ""; fi
33+
DEPLOY_SCRIPT:
34+
sh: basename "{{.CLI_ARGS}}"
35+
env:
36+
DEPLOY_SCRIPT: "{{.DEPLOY_SCRIPT}}"
2437

2538
deploy-hosted:
2639
# TO-DO: untested
@@ -56,21 +69,22 @@ tasks:
5669
- sh: if [ -z "$(ls -A ../abis)" ]; then exit -1; fi
5770
msg: "ERROR: No ABIs found. Run 'task gen-abi' first."
5871

59-
destroy-hardhat:
60-
desc: "Tear down the deployed Hardhat node."
61-
dir: docker/
62-
cmds:
63-
- docker-compose -f docker-compose.hardhat.yml down --volumes
64-
6572
docker-build:
66-
desc: "Build subgraph Docker image on defined node version base (default: 16-slim)."
73+
desc: "Build Docker images based on defined node version base (default: 16-slim)."
6774
cmds:
68-
- docker build -t {{.SET_IMAGE_NAME}}:node-${NODE_VER} -f docker/Dockerfile --build-arg NODE_VER=$NODE_VER .
69-
env:
70-
NODE_VER: "{{.NODE_VER}}"
75+
- docker build -t {{.SET_IMAGE_NAME}}:node-{{.NODE_VER}} -f docker/Dockerfile.base --build-arg NODE_VER={{.NODE_VER}} .
76+
- docker build -t {{.HARDHAT_IMAGE_NAME}}:node-{{.NODE_VER}} -f docker/Dockerfile.hardhat --build-arg SET_IMAGE_NAME={{.SET_IMAGE_NAME}} --build-arg NODE_VER={{.NODE_VER}} .
7177

7278
gen-abi:
7379
desc: "Pull latest Set Protocol ABIs into the build environment."
7480
cmds:
7581
# Note the following command requires node ver 16 or below (only 12 and 16 tested working)
7682
- docker run --rm -v $(pwd)/abis:/subgraph/abis -v $(pwd)/scripts:/subgraph/scripts -w /subgraph {{.SET_IMAGE_NAME}}:node-{{.NODE_VER}} bash scripts/update-abis.sh
83+
84+
gen-schema:
85+
desc: "Generate schema intermediate artifacts for development without a full deploy."
86+
cmds:
87+
- rm -rf generated/ build/
88+
- docker run --rm -e NETWORK_NAME={{.NETWORK_LOCAL}} -v $(pwd)/:/subgraph -v setprotocol-subgraph-node_modules:/subgraph/node_modules -w /subgraph {{.SET_IMAGE_NAME}}:node-{{.NODE_VER}} sh scripts/gen-schema.sh
89+
vars:
90+
NETWORK_NAME: "{{.NETWORK_LOCAL}}"

docker/.env

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Node version to use for deployment
22
NODE_VER=16
33

4-
# Docker image name to target for builds and container deployments
5-
SET_IMAGE_NAME=setprotocol/subgraph
4+
# Docker image names to target for builds and container deployments
5+
HARDHAT_IMAGE_NAME=setprotocol/hardhat
6+
SET_IMAGE_NAME=setprotocol/base
67

78
# Subgraph version label to deploy
89
# See: https://thegraph.com/docs/en/studio/deploy-subgraph-studio#deploying-a-subgraph-to-subgraph-studio
@@ -13,7 +14,8 @@ GRAPH_NAME=set-protocol-v2
1314

1415
# HARDHAT NODE (LOCAL)
1516
# --------------------
16-
HARDHAT_IP=http://docker-hardhat-1
17+
HARDHAT_IP=docker-hardhat-1
18+
# HARDHAT_IP=docker_hardhat_run_45d1437ad006
1719
HARDHAT_PORT=8545
1820

1921
# LOCAL DEPLOYMENT PARAMS

docker/Dockerfile renamed to docker/Dockerfile.base

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# Build SetProtocol Subgraph Image
1+
# Build SetProtocol Base Image
22
#
3-
# Builds a minimal image with dependencies required for The Graph deployments.
3+
# Builds a minimal image with dependencies required for various deployments.
44
# Expectation is to build in the repo root directory by running, e.g.,
55
#
6-
# docker build -t setprotocol/subgraph:${NODE_VER}-slim -f docker/Dockerfile .
6+
# docker build -t setprotocol/base:${NODE_VER}-slim -f docker/Dockerfile.base .
77
#
88
# This is handled by the task `docker-build` in the Taskfile.
99

docker/Dockerfile.hardhat

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Build SetProtocol Hardhat Image
2+
#
3+
# Builds a minimal image with dependencies required for a Hardhat node deployment.
4+
# Expectation is to build in the repo root directory by running, e.g.,
5+
#
6+
# docker build -t setprotocol/hardhat:${NODE_VER}-slim -f docker/Dockerfile.hardhat .
7+
#
8+
# This is handled by the task `docker-build` in the Taskfile.
9+
10+
ARG NODE_VER=16
11+
ARG SET_IMAGE_NAME=setprotoco/base
12+
13+
FROM ${SET_IMAGE_NAME}:node-${NODE_VER} as base
14+
15+
COPY ./docker/scripts/deploy-set-protocol-v2.sh /tmp
16+
17+
WORKDIR /app
18+
19+
RUN /bin/bash /tmp/deploy-set-protocol-v2.sh \
20+
&& rm -rf /tmp/*
21+

docker/docker-compose.hardhat.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ version: '3'
22

33
services:
44

5-
hardhat:
6-
image: ${SET_IMAGE_NAME}:node-${NODE_VER}
7-
command: ["sh", "/scripts/deploy-hardhat.sh"]
5+
hardhat-build:
6+
image: ${HARDHAT_IMAGE_NAME}:node-${NODE_VER}
87
environment:
9-
HARDHAT_PORT: ${HARDHAT_PORT}
10-
working_dir: /hardhat
8+
HARDHAT_PORT: ${HARDHAT_PORT:-"8545"}
9+
DEPLOY_SCRIPT: ${DEPLOY_SCRIPT:-""}
10+
working_dir: /app
1111
volumes:
12-
- ../scripts:/scripts
13-
- hardhat:/hardhat
12+
- ../scripts:/app/scripts
13+
- hardhat:/app
1414

1515
volumes:
1616
hardhat:

docker/docker-compose.hosted.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ services:
55
setprotocol-subgraph:
66
image: ${SET_IMAGE_NAME}:node-${NODE_VER}
77
environment:
8-
DEPLOYMENT: ${DEPLOYMENT}
9-
IPFS_IP: ${IPFS_HOSTED}
10-
GRAPH_NODE_IP: ${GRAPH_NODE_HOSTED}
11-
ACCESS_TOKEN: ${ACCESS_TOKEN}
12-
SUBGRAPH_VERSION: ${SUBGRAPH_VERSION}
13-
NETWORK_NAME: ${NETWORK_NAME}
14-
GITHUB_REPO: ${GITHUB_REPO}
15-
GRAPH_NAME: ${GRAPH_NAME}
8+
DEPLOYMENT: ${DEPLOYMENT:-""}
9+
IPFS_IP: ${IPFS_HOSTED:-""}
10+
GRAPH_NODE_IP: ${GRAPH_NODE_HOSTED:-""}
11+
ACCESS_TOKEN: ${ACCESS_TOKEN:-""}
12+
SUBGRAPH_VERSION: ${SUBGRAPH_VERSION:-""}
13+
NETWORK_NAME: ${NETWORK_NAME:-""}
14+
GITHUB_REPO: ${GITHUB_REPO:-""}
15+
GRAPH_NAME: ${GRAPH_NAME:-""}
1616
volumes:
1717
- ../:/subgraph
1818
- setprotocol-subgraph-node_modules:/subgraph/node_modules

docker/docker-compose.local.yml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ services:
1616
postgres_db: graph-node
1717
ipfs: 'ipfs:${IPFS_PORT}'
1818
GRAPH_LOG: info
19-
ethereum: hardhat:${HARDHAT_IP}:${HARDHAT_PORT}
19+
ethereum: hardhat:http://${HARDHAT_IP}:${HARDHAT_PORT}
2020

2121
ipfs:
2222
image: ipfs/go-ipfs:v0.4.23
@@ -38,14 +38,14 @@ services:
3838
depends_on:
3939
- graph-node
4040
environment:
41-
DEPLOYMENT: ${DEPLOYMENT}
42-
IPFS_IP: ${IPFS_IP}
43-
GRAPH_NODE_IP: ${GRAPH_NODE_IP}
44-
ACCESS_TOKEN: ${ACCESS_TOKEN}
45-
SUBGRAPH_VERSION: ${SUBGRAPH_VERSION}
46-
NETWORK_NAME: ${NETWORK_NAME}
47-
GITHUB_REPO: ${GITHUB_REPO}
48-
GRAPH_NAME: ${GRAPH_NAME}
41+
DEPLOYMENT: ${DEPLOYMENT:-""}
42+
IPFS_IP: ${IPFS_IP:-""}
43+
GRAPH_NODE_IP: ${GRAPH_NODE_IP:-""}
44+
ACCESS_TOKEN: ${ACCESS_TOKEN:-""}
45+
SUBGRAPH_VERSION: ${SUBGRAPH_VERSION:-""}
46+
NETWORK_NAME: ${NETWORK_NAME:-""}
47+
GITHUB_REPO: ${GITHUB_REPO:-""}
48+
GRAPH_NAME: ${GRAPH_NAME:-""}
4949
volumes:
5050
- ../:/subgraph
5151
- setprotocol-subgraph-node_modules:/subgraph/node_modules
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
if [ ! -d "set-protocol-v2" ]; then
6+
git clone -q https://github.com/SetProtocol/set-protocol-v2.git
7+
fi
8+
9+
cd set-protocol-v2
10+
11+
# Set up default env vars as required
12+
if [ ! -f ".env" ]; then
13+
cp .env.default .env
14+
fi
15+
16+
# Install the dependencies
17+
yarn install

package.json

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,5 @@
33
"version": "0.1.0",
44
"repository": "https://github.com/SetProtocol/set-protocol-v2-subgraph",
55
"description": "Set Protocol subgraph deployment repo",
6-
"license": "MIT",
7-
"dependencies": {
8-
"@graphprotocol/graph-cli": "^0.26.0",
9-
"@graphprotocol/graph-ts": "^0.24.1",
10-
"handlebars": "^4.7.7",
11-
"ts-node": "^10.5.0",
12-
"typescript": "^4.5.5"
13-
}
6+
"license": "MIT"
147
}

0 commit comments

Comments
 (0)