Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions packages/subgraph/.env.template
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good 👍🏻

Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
# environment to use for configuration (prod/staging)
ENV=prod
# The name of the network (e.g., mainnet, arbitrum, etc.)
NETWORK_NAME=...

# override the starting block for indexation of all contracts events
# START_BLOCK=0
# The URL of the Graph Node endpoint for self-hosted Graph Node
GRAPHNODE_URL=...

# AppRegistry contract address override
# APP_REGISTRY_ADDRESS=0x...
# The URL of the IPFS endpoint for self-hosted IPFS
IPFS_URL=...

# DatasetRegistry contract address override
# DATASET_REGISTRY_ADDRESS=0x...

# Dataprotector contract address override
# DATAPROTECTOR_ADDRESS=0x...

# DataprotectorSharing contract address override
# DATAPROTECTOR_SHARING_ADDRESS=0x...

# AddOnlyAppWhitelistRegistry contract address override
# ADD_ONLY_APP_WHITELIST_REGISTRY_ADDRESS=0x...
# The version label for the deployment (e.g., v1.0.0)
VERSION_LABEL=...
1 change: 1 addition & 0 deletions packages/subgraph/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ coverage.json
# subgraph
generated
subgraph.yaml
tests/.latest.json
100 changes: 58 additions & 42 deletions packages/subgraph/README.md
Original file line number Diff line number Diff line change
@@ -1,32 +1,69 @@
# Subgraph

This repository contains the code for the DataProtector subgraph, which indexes blockchain events to make them queryable through a GraphQL API.

## Build the Subgraph

And you have finishing editing the schema.graphql & subgraph.yaml files. Type the following command
After editing the schema.graphql & subgraph.yaml files, build the subgraph with:

```bash
npm run codegen
npm run build
```

Then you have to write ./src/bays.ts files in order to update the graph when new events appear. In this way, it will always be up to date.
Then implement event handlers in the ./src files to keep the graph updated when new events occur on the blockchain.

## Deployment Options

## Local
### Local Development

### Deploy on Local Graph node
To deploy on a local Graph Node:

```bash
npm run create-local
npm run deploy-local
```

### Test Subgrah API
The subgraph will be available at: http://localhost:8000/subgraphs/name/DataProtector/graphql

### Hosted Production Environments

We use CI/CD pipelines to deploy our subgraphs to hosted environments.

#### Docker Image Tags

When building and pushing Docker images, the following tag generation strategy is used:

| Trigger | Environment | Tag Format | Example | Push? |
|---------|-------------|------------|---------|-------|
| Manual workflow dispatch | Production | `{package.json version}` | `1.2.3` | Yes |
| Manual workflow dispatch | Development | `dev-{commit SHA}` | `dev-8e7d3f2` | Yes |
| Push to `main` branch | Production | `{package.json version}` | `1.2.3` | Yes |
| Push to `develop` branch | Development | `dev-{commit SHA}` | `dev-8e7d3f2` | Yes |
| Tag push | N/A | `{tag name}` | `v1.2.3-beta` | Yes |
| Other branch push | Development | `dev-{commit SHA}` | `dev-8e7d3f2` | No |

Deployed to : <http://localhost:8000/subgraphs/name/DataProtector/graphql>
### Self-Hosted Subgraph Deployment Process

Request Example on GraphiQL :
For zero-downtime updates to the production subgraph:

Query for GraphiQL API
1. **Index the New Version (Temporary Deployment)**
- Trigger deployment with target: `subgraph-deploy-tmp`
- This creates a separate instance for indexing

2. **Wait for Indexing Completion**
- Monitor the temporary deployment until it's fully synced

3. **Deploy to Production (Zero Downtime)**
- Once temporary deployment is ready, trigger: `subgraph-deploy-prod`
- This swaps the deployments with no service interruption

4. **Verify the Deployment**
- Access the production subgraph at: https://thegraph.iex.ec/subgraphs/name/bellecour/dataprotector-v2/graphql

## Query Examples

### Sample GraphQL Query

```graphql
query MyQuery($requiredSchema: [String!]!, $start: Int!, $range: Int!) {
Expand Down Expand Up @@ -57,48 +94,27 @@ query MyQuery($requiredSchema: [String!]!, $start: Int!, $range: Int!) {
}
```

```graphql
Query Variables :
### Query Variables

```json
{
"start": 0,
"range": 1000,
"requiredSchema": []
}
```

## Hosted

You can trigger a deployment using the promote action on the CI.

### Self Hosted Subgraph

To deploy a new version of a subgraph on the iExec self-hosted service, follow these steps:

1. Index the New Subgraph

First, index the new version of the subgraph using the temporary subgraph deployment.
Trigger its deployment with the target:

```sh
subgraph-deploy-tmp
```

2. Wait for Indexing Completion

Once the temporary subgraph has finished indexing, you can proceed to the production deployment.

3. Deploy to Production (No Downtime)

Trigger the production deployment with :

```sh
subgraph-deploy-prod
```
## Development Workflow

This ensures a seamless transition with no downtime.
1. Update schema.graphql and subgraph.yaml as needed
2. Run codegen to generate TypeScript types: `npm run codegen`
3. Implement mapping handlers in src/ files
4. Build the subgraph: `npm run build`
5. Test locally before deploying to production environments

4. Verify the Deployment
## CI/CD Integration

Visit the following URL to check the new version of the subgraph:
<https://thegraph.iex.ec/subgraphs/name/bellecour/dataprotector-v2/graphql>
Our repository uses automated workflows to build, test, and deploy the subgraph:
- ABI validation checks ensure contract ABIs are up-to-date
- Docker images are built and pushed with appropriate tags based on the source branch
- Deployment follows a staged approach to ensure zero downtime
17 changes: 17 additions & 0 deletions packages/subgraph/config/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'dotenv/config';
import { z } from 'zod';

const envSchema = z.object({
NETWORK_NAME: z.string().min(1, 'NETWORK_NAME is required').default('bellecour'),

GRAPHNODE_URL: z
.string()
.url('GRAPHNODE_URL must be a valid URL')
.default('http://localhost:8020'),

IPFS_URL: z.string().url('IPFS_URL must be a valid URL').default('http://localhost:5001'),

VERSION_LABEL: z.string().min(1, 'VERSION_LABEL is required').default('bellecour/poco-v5'),
});

export const env = envSchema.parse(process.env);
68 changes: 68 additions & 0 deletions packages/subgraph/networks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"fuji": {
"DataProtector": {
"address": "0x2296daeDD3090750a80fFB2D0147669984909ED2",
"startBlock": 39768073
},
"DatasetRegistry": {
"address": "0x3441A0C9FE488c51fcABa2bAAA048720f4D4F72D",
"startBlock": 39768073
},
"DataProtectorSharing": {
"address": "0x0000000000000000000000000000000000000000",
"startBlock": 0
},
"AppRegistry": {
"address": "0x4a6531ce5150ee716b2d93865D0fbB9ce5492D17",
"startBlock": 0
},
"AddOnlyAppWhitelistRegistry": {
"address": "0x0000000000000000000000000000000000000000",
"startBlock": 0
}
},
"arbitrum-sepolia": {
"DataProtector": {
"address": "0x2296daeDD3090750a80fFB2D0147669984909ED2",
"startBlock": 145960686
},
"DatasetRegistry": {
"address": "0x3441A0C9FE488c51fcABa2bAAA048720f4D4F72D",
"startBlock": 145960686
},
"DataProtectorSharing": {
"address": "0x0000000000000000000000000000000000000000",
"startBlock": 0
},
"AppRegistry": {
"address": "0x4a6531ce5150ee716b2d93865D0fbB9ce5492D17",
"startBlock": 0
},
"AddOnlyAppWhitelistRegistry": {
"address": "0x0000000000000000000000000000000000000000",
"startBlock": 0
}
},
"bellecour": {
"DataProtector": {
"address": "0x3a4Ab33F3D605e75b6D00A32A0Fa55C3628F6A59",
"startBlock": 25455501
},
"DatasetRegistry": {
"address": "0x0000000000000000000000000000000000000000",
"startBlock": 25455501
},
"DataProtectorSharing": {
"address": "0x1390c3c6a545198809F1C7c5Dd2600ef74D60925",
"startBlock": 28566236
},
"AppRegistry": {
"address": "0x0000000000000000000000000000000000000000",
"startBlock": 28566236
},
"AddOnlyAppWhitelistRegistry": {
"address": "0x498D324F711b8998Be81818742e268dEE30347c6",
"startBlock": 28566234
}
}
}
Loading