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

Commit eb0cfbe

Browse files
authored
LI-306: Containerization of Local Subgraph Deployment (#1)
* Initial containerization refactor * Move scripts out of docker folder since independent of Docker * Update README with subgraph endpoint access instructions * Refactored containerization to include Hardhat and graph-node deployments, and default env var file
1 parent 0e61c7c commit eb0cfbe

36 files changed

+597
-19568
lines changed

.eslintrc.js

Lines changed: 0 additions & 51 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ node_modules/
1717
# Yarn Integrity file
1818
.yarn-integrity
1919

20-
# dotenv environment variables file
20+
# dotenv environment variable file
2121
.env
22+
!docker/.env
2223

2324
# VS Code config
2425
.vscode/

.husky/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

.husky/pre-commit

Lines changed: 0 additions & 1 deletion
This file was deleted.

.prettierignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

.prettierrc.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

README.md

Lines changed: 49 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -2,150 +2,74 @@
22

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

5-
## Setup
5+
## SETUP
66

7-
Requirements:
7+
### Requirements:
88

9-
- Bash >= 5.0 [Mac](https://gist.github.com/Rican7/44081a9806595704fa7b289c32fcd62c) / [Win](https://nickjanetakis.com/blog/a-linux-dev-environment-on-windows-with-wsl-2-docker-desktop-and-more)
10-
- [Node.js >= 14.0](https://nodejs.org/en/download/)
11-
- Yarn 1.x (`npm install -g yarn`)
12-
- [Docker >= 19.0](https://www.docker.com/get-started)
9+
- Docker >= 20.10
1310

14-
Steps:
11+
### Local Deployment (Hardhat)
1512

16-
1. `git clone https://github.com/SetProtocol/set-protocol-v2-subgraph.git && cd set-protocol-v2-subgraph`
17-
2. `yarn install`
18-
3. `yarn gen-deployment <NETWORK_NAME>` hardhat or mainnet
19-
4. (If deploying to hosted service) `yarn graph auth https://api.thegraph.com/deploy/ <ACCESS_TOKEN>`
20-
5. `graph deploy --product hosted-service justinkchen/set-protocol-v2-matic` (replace set-protocol-v2-matic with whatever graph is being deployed)
13+
1. Build the subgraph Docker image
2114

22-
Current subgraphs:
23-
- justinkchen/set-protocol-v2 (Ethereum Mainnet)
24-
- justinkchen/set-protocol-v2-staging (Ethereum Mainnet)
25-
- justinkchen/set-protocol-v2-matic (Matic)
26-
- justinkchen/set-protocol-v2-matic-staging (Matic)
27-
- justinkchen/set-protocol-v2-arbitrum (Arbitrum)
28-
- justinkchen/set-protocol-v2-arbitrum-staging (Arbitrum)
29-
- justinkchen/set-protocol-v2-avalanche (Avalanche)
30-
- justinkchen/set-protocol-v2-avax-staging (Avalanche)
31-
- justinkchen/set-protocol-v2-optimism-staging (Optimism)
15+
`task docker-build`
3216

33-
Using Graph Subgraph Studio (Future steps once Subgraph studio supports more networks):
34-
1. `graph auth --studio <API KEY>` (see more at https://thegraph.com/studio/subgraph/set-protocol-v2/)
35-
2. `yarn gen-deployment <NETWORK_NAME>` hardhat or mainnet or matic or staging-mainnet
36-
3. `cp generated/addresses.ts .` Copy generated addresses file to root location
37-
3. `graph codegen && graph build`
38-
4. `graph deploy --studio set-protocol-v2` for mainnet or `graph deploy --studio set-protocol-v2-matic` for Matic/Polygon or `graph deploy --studio set-protocol-v2-staging` for staging mainnet
17+
1. Deploy a Hardhat node (use `task deploy-hardhat -- detach` to start container in detached mode)
3918

19+
`task deploy-hardhat`
4020

41-
## Commands
21+
1. Monitor the Hardhat node until fully deployed and tests are executed, e.g., if running detached use
4222

43-
Usage:
23+
`docker logs docker-hardhat-1 --follow`
4424

45-
`yarn <COMMAND>`
25+
1. Compile the Set Protocol ABIs
4626

47-
Commands:
27+
`task gen-abi`
4828

49-
`build` - Compile subgraph
29+
1. Deploy local subgraph
5030

51-
`codegen` - Generate types (if schema or ABI changed)
31+
`task deploy-local`
5232

53-
`deploy-local` - Deploy subgraph to localhost
33+
1. Once deployed, query the subgraph in the browser at (by default) http://127.0.0.1:8000/subgraphs/name/SetProtocol/set-protocol-v2
5434

55-
`deploy-to <IP>` - Deploy subgraph to Graph Node by IP
35+
### [TO-DO] External Deployment (Graph Hosted Service / Subgraph Studio)
5636

57-
`deploy-hosted` - Deploy subgraph to hosted service
37+
TBD
5838

59-
`gen-abis` - Pull contract ABIs from Set Protocol V2 repo (only needed if changed)
39+
## USAGE
6040

61-
`gen-deployment <NETWORK_NAME>` - Generate deployment-specific files
41+
Available tasks for this project:
6242

63-
`lint` - Format code
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. |
6452

65-
## Local development (hardhat)
53+
## [TO-DO] ADVANCED DEPLOYMENT GUIDES
6654

67-
### Clone Set Protocol v2 fork
55+
TBD: Things to be covered in this section
6856

69-
In separate directory:
57+
- custom override of args (requires custom untracked .env configs or CLI arg overrides)
58+
- 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.
59+
- node dependencies installation into a named Docker volume, build the subgraph mappings, create the subgraph, then deploy it to the target graph node and IPFS database. Not that consecutive runs of this command will use the existing named volume for the `node_modules` unless it is manually removed.
60+
- the subgraph is deployed to the local graph-node and IPFS containers defined in the `subgraph.env` file. The subgraph endpoints given by the deployment are relative to the Docker container and not accessible externally as given. To access the subgraph, instead navigate to: http://127.0.0.1:8000/subgraphs/name/SetProtocol/setprotocolv2.
7061

71-
1. `git clone https://github.com/jgrizzled/set-protocol-v2.git -b subgraph-dev && cd set-protocol-v2`
72-
2. `cp .env.default .env`
73-
3. `yarn install`
62+
## [TO-DO] SUBGRAPH DEVELOPMENT
7463

75-
To run the hardhat node:
64+
### Tutorial
7665

77-
1. `yarn chain --hostname 0.0.0.0`
78-
2. Wait for node to start
79-
3. (in separate terminal) `yarn deploy-mock`
66+
TBD
8067

81-
### Install Graph Node
68+
### Reference Guide
8269

83-
In separate directory:
70+
To Be Completed
8471

85-
1. `git clone -q --depth=1 https://github.com/graphprotocol/graph-node.git && cd graph-node/docker`
86-
2. Edit line 20 of docker-compose.yml to `ethereum: hardhat:http://host.docker.internal:8545` (May need to replace host.docker.internal with LAN IP)
87-
3. Run with `sudo docker-compose up`
88-
89-
`rm -rf ./data` and restart containers if blockchain changes.
90-
91-
`sudo docker-compose build` if updated via `git pull`
92-
93-
### Deploy subgraph locally
94-
95-
From subgraph repo:
96-
97-
1. `yarn gen-deployment hardhat`
98-
2. `yarn deploy-local`
99-
100-
Graph-node may take a few minutes to sync the subgraph.
101-
102-
Visit `http://localhost:8000/subgraphs/name/desert-defi/setprotocolv2/graphql` to view subgraph data
103-
104-
## Syncing to mainnet
105-
106-
Syncing the subgraph to mainnet requires an Ethereum archive node. We recommend [Turbogeth](https://github.com/ledgerwatch/turbo-geth) as it syncs fast and will fit on a 2TB SSD at present. Note that it takes a few hours for the subgraph to sync and re-deploying the subgraph will re-sync from scratch.
107-
108-
### Turbogeth
109-
110-
In separate directory:
111-
112-
1. `git clone -q --depth=1 https://github.com/ledgerwatch/turbo-geth.git && cd turbo-geth`
113-
2. `sudo docker-compose build` (re-run if updated via `git pull`)
114-
3. `sudo XDG_DATA_HOME=/preferred/data/folder docker-compose up -d`
115-
116-
Watch logs with:
117-
118-
`sudo docker logs $(sudo docker container ls | grep tg | cut -d' ' -f1) -f --since 10m`
119-
120-
### Graph Node
121-
122-
From graph-node repo:
123-
124-
Ensure `docker/docker-compose.yml` is configured for mainnet on line 20: `ethereum: mainnet:`
125-
126-
If you previously synced to hardhat, `rm -rf docker/data`.
127-
128-
Don't start Graph Node until Turbogeth is fully synced.
129-
130-
Start with:
131-
132-
1. `cd docker`
133-
2. `sudo docker-compose up -d`
134-
135-
Watch logs with:
136-
137-
`sudo docker logs $(sudo docker container ls | grep graph-node | cut -d' ' -f1) -f --since 10m`
138-
139-
### Deploy to mainnet
140-
141-
From subgraph repo:
142-
143-
1. `yarn gen-deployment mainnet`
144-
2. `yarn deploy-local` or `yarn deploy-to <IP>` if Graph Node on another machine.
145-
146-
Watch graph-node logs for sync status and errors. Subgraph URL same as above.
147-
148-
## Files
72+
#### Key Files
14973

15074
`schema.graphql` - Subgraph schema
15175

@@ -159,25 +83,23 @@ Watch graph-node logs for sync status and errors. Subgraph URL same as above.
15983

16084
`src/entities/` - Entity helper functions
16185

162-
## Design
163-
164-
### Historical Entities
86+
#### Historical Entities
16587

166-
#### Events
88+
##### Events
16789

16890
Individual transaction log events referenced by TxID and log index. Can have multiple events of the same type with the same timestamp (IE same block). Ex TradeEvent.
16991

170-
#### States
92+
##### States
17193

17294
Final state of an entity at the end of a block. Referenced by block number. Multiple events in the same block will be consolidated to one state update. Ex TotalSupplyState.
17395

17496
Events are better for tracking important actions while States are better for timeseries data in which multiple data points per timestamp would be inconveinent.
17597

176-
### Current Entities
98+
##### Current Entities
17799

178100
Tracks the most recent state of a contract. Usually references the latest event or state update entity.
179101

180-
### Entity Helpers
102+
##### Entity Helpers
181103

182104
Most of the logic regarding manipulation of entities should be in helper functions. Create helper functions based on the following nomenclature as needed.
183105

@@ -190,16 +112,16 @@ Prefixes:
190112
- ensure: create or update existing entity. Useful for states
191113
- track: execute contract call and store result in new entity. Useful for data that is not provided via event logs.
192114

193-
### Event Handlers
115+
#### Event/Call/Block Handlers
194116

195-
Process event data and call entity helper functions to update the subgraph. Must register event handlers in `templates/subgraph.yaml`.
117+
Process events, function calls, and block data to update the subgraph. Must register handlers in `templates/subgraph.yaml`.
196118

197-
### Template spawners
119+
#### Template spawners
198120

199121
Not all contract addresses are known at the time of subgraph deployment. To track contracts as they are deployed, use contract templates.
200122
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`.
201123

202-
## Reference
124+
## References
203125

204126
[Discord: Index Co-op #set-subgraph](https://discord.gg/8FYPP7ebbw)
205127

0 commit comments

Comments
 (0)