# SushiSwap Subgraph

## Setup

Clone required repositories:

```bash
laconic-so --stack sushiswap-subgraph setup-repositories --pull
```

Checkout to a non-default branch in the cloned repos if required:

```bash
# Default repo base dir
cd ~/cerc

# Example
cd graph-node
git checkout <your-branch> && git pull

# Remove the corresponding docker image if it already exists
docker image rm cerc/graph-node:local
# Remove any dangling images
docker image prune
```

Build the container images:

```bash
laconic-so --stack sushiswap-subgraph build-containers
```

## Create a deployment

Initialize deployment and create "spec" file:

```bash
laconic-so --stack sushiswap-subgraph deploy init --output sushiswap-subgraph-spec.yml
```

We need to assign a fixed port `8000` for graph-node subgraph GQL endpoint. The values can be
customized by editing the "spec" file generated by `laconic-so deploy init`.
```
$ cat sushiswap-subgraph-spec.yml
stack: sushiswap-subgraph
ports:
  graph-node:
   - '8000:8000'
   - '8001'
   - '8020'
   - '8030'
...
```

Create deployment:

```bash
laconic-so --stack sushiswap-subgraph deploy create --spec-file sushiswap-subgraph-spec.yml --deployment-dir sushiswap-subgraph-deployment
```

## Start the stack

Deploy the stack:

```bash
laconic-so deployment --dir sushiswap-subgraph-deployment start

# Note: Remove any existing volumes for the cluster for a fresh start
```

After all services have started:

* Follow `graph-node` logs:

  ```bash
  laconic-so deployment --dir sushiswap-subgraph-deployment logs -f graph-node
  ```

* Check that the subgraphs have been deployed:

  ```bash
  laconic-so deployment --dir sushiswap-subgraph-deployment logs -f sushiswap-subgraph-v3

  # Expected output:
  # .
  # .
  # sushigraph-sushiswap-subgraph-v3-1  | - Deploying to Graph node http://graph-node:8020/
  # sushigraph-sushiswap-subgraph-v3-1  | Deployed to http://graph-node:8000/subgraphs/name/sushiswap/blocks/graphql
  # sushigraph-sushiswap-subgraph-v3-1  |
  # sushigraph-sushiswap-subgraph-v3-1  |
  # sushigraph-sushiswap-subgraph-v3-1  | Subgraph endpoints:
  # sushigraph-sushiswap-subgraph-v3-1  | Queries (HTTP):     http://graph-node:8000/subgraphs/name/sushiswap/blocks
  # .
  # .
  # sushigraph-sushiswap-subgraph-v3-1  | - Deploying to Graph node http://graph-node:8020/
  # sushigraph-sushiswap-subgraph-v3-1  | Deployed to http://graph-node:8000/subgraphs/name/sushiswap/v3-filecoin/graphql
  # sushigraph-sushiswap-subgraph-v3-1  |
  # sushigraph-sushiswap-subgraph-v3-1  |
  # sushigraph-sushiswap-subgraph-v3-1  | Subgraph endpoints:
  # sushigraph-sushiswap-subgraph-v3-1  | Queries (HTTP):     http://graph-node:8000/subgraphs/name/sushiswap/v3-filecoin
  # sushigraph-sushiswap-subgraph-v3-1  |
  # sushigraph-sushiswap-subgraph-v3-1  |
  # sushigraph-sushiswap-subgraph-v3-1  | Done
  ```

After `graph-node` has fetched the latest blocks from upstream, use the subgraph (GQL) endpoints for querying:

```bash
# Blocks subgraph endpoint:
http://127.0.0.1:8000/subgraphs/name/sushiswap/blocks/graphql

# v3 subgraph endpoint:
http://127.0.0.1:8000/subgraphs/name/sushiswap/v3-filecoin/graphql
```

## Set environment variables

* The graph-node environment variable `ETHEREUM_REORG_THRESHOLD` can be set in the deployment compose file
  ```bash
  $ cat sushiswap-subgraph-deployment/compose/docker-compose-graph-node.yml
  services:
    graph-node:
      image: cerc/graph-node:local
      ...
      environment:
        ...
        GRAPH_LOG: debug
        ETHEREUM_REORG_THRESHOLD: 16
  ```
  Change `ETHEREUM_REORG_THRESHOLD` to desired value

  * To restart graph-node with updated values, we need to restart only graph-node compose services
    * Comment `sushiswap-subgraph-v3` pod in stack.yml so that subgraphs are not deployed again
      ```bash
      $ cat sushiswap-subgraph-deployment/stack.yml
      version: "1.0"
      name: sushiswap-subgraph
      ...
      pods:
        - graph-node
        # - sushiswap-subgraph-v3
      ```
    * Stop the stack first
      ```bash
      laconic-so deployment --dir sushiswap-subgraph-deployment stop
      ```
    * Start the stack again (will not start `sushiswap-subgraph-v3` pod)
      ```
      laconic-so deployment --dir sushiswap-subgraph-deployment start
      ```
  * To check if environment variable has been updated in graph-node container
      ```bash
      $ laconic-so deployment --dir sushiswap-subgraph-deployment exec graph-node bash
      root@dc4d3abe1615:/# echo $ETHEREUM_REORG_THRESHOLD
      16
      ```

## Clean up

Stop all the services running in background run:

```bash
laconic-so deployment --dir sushiswap-subgraph-deployment stop
```

Clear volumes created by this stack:

```bash
laconic-so deployment --dir sushiswap-subgraph-deployment stop --delete-volumes
```
