This repository provides a robust and flexible structure for deploying subgraphs associated with multiple versions of smart contracts across various blockchain networks. The script and configuration files facilitate dynamic generation of event handlers, manage multiple contract versions, and support seamless deployment to different networks.
The complexity of managing multiple contract versions and networks necessitates a scalable and maintainable solution. This structure addresses the following challenges:
- Version Management: Automates the handling of multiple versions of contracts, ensuring consistent event processing across different deployments.
- Cross-Network Deployments: Simplifies deploying subgraphs to multiple blockchain networks by centralizing configurations and automating repetitive tasks.
- Event Handling: Dynamically generates event handlers based on schema dependencies, reducing the need for manual intervention and minimizing errors.
- Modular Configurations: Allows for module-specific customizations via
subgraph_config.json
, making the system adaptable to various use cases.
- Event Handling: Dynamically generates event handlers based on the schema and dependencies specified in the configuration files.
- Version Management: Handles multiple versions of contracts and ABIs, creating "fake" contracts where necessary to ensure all versions are covered.
- Flexible Deployment: Supports deployment to different networks, including specialized configurations for networks like Mantle.
- Dependency Resolution: Utilizes dependency files to map entity models to the required events, ensuring that all necessary event handlers are generated.
- Module Configuration: Incorporates
subgraph_config.json
for module-specific settings, allowing for flexible and targeted subgraph configurations.
-
Prepare Configuration File: Create a JSON configuration file that defines your contracts, ABIs, and deployment URLs.
-
Prepare
subgraph_config.json
: This file should reside in your target module's directory and define the models to be imported from the common directory. -
Prepare Dependency Files: Place dependency files in the appropriate directories to map entity models to events.
-
Run the Script:
python script_name.py config_file.json module_name [--create-src] [--deploy] [--mantle]
config_file.json
: Path to your configuration file.module_name
: Name of the target module.--create-src
: (Optional) Generates the source TypeScript files for event handling.--deploy
: (Optional) Deploys the subgraph after preparation.--mantle
: (Optional) Use this flag if deploying to the Mantle network.
-
Review Output: The script will generate necessary files and output the deployment status. It's important to review the generated files, especially
subgraph.yaml
andschema.graphql
, to ensure they are correctly configured.
-
Configuration Loading:
- The script loads the configuration from a specified JSON file and initializes the deployment process.
-
Module Preparation:
- Combines common models and target module schema into a unified
schema.graphql
. - Identifies the events required by the models in the schema based on the dependency files.
- Copies necessary ABI files to the appropriate locations.
- Combines common models and target module schema into a unified
-
Contract Processing:
- Processes each contract, including generating "fake" contracts for missing versions to ensure all versions are accounted for.
-
Subgraph Configuration:
- Generates a
subgraph.yaml
file, configuring data sources, event handlers, and mappings for each contract.
- Generates a
-
Code Generation:
- If the
--create-src
flag is used, generates TypeScript source files to handle the events specified in the configuration.
- If the
-
Build and Deploy:
- Executes
graph codegen
to generate AssemblyScript types andgraph build
to compile the subgraph. - If the
--deploy
flag is used, deploys the subgraph to the specified network, with additional options for Mantle deployment.
- Executes
-
Network-Specific Handling:
- The
--mantle
flag enables specialized deployment configurations for the Mantle network, including custom node and IPFS endpoints.
- The
The script uses dependency files to map entity models to the events they depend on. These files are crucial for ensuring that all necessary events are captured and processed.
- Common Dependencies: Located at
./common/deps_{abi}_{version}.json
. - Target Module Dependencies: Located at
./{target_module}/deps_{abi}_{version}.json
.
{
"Account": [
"AccountCreated",
"Deposited"
],
"Position": [
"PositionOpened",
"PositionClosed"
]
}
- The script loads both common and module-specific dependency files.
- It iterates through all models defined in the schema.
- For each model, the script determines the events it depends on, based on the dependency files.
- These events are then used to generate event handlers and configure the subgraph.
This file is central to module-specific configurations and should be placed in the directory of the target module.
{
"importModels": [
"ModelName1",
"ModelName2"
]
}
-
Model Imports:
- The
importModels
array specifies which models should be imported from the common directory into the module's schema. - These models are included in the final
schema.graphql
file.
- The
-
Schema Generation:
- The script first incorporates the imported models into the
schema.graphql
. - Then, it appends the module-specific schema.
- The script first incorporates the imported models into the
-
Custom Configurations:
- The file can be extended to include other settings that customize the subgraph preparation process.
-
Clean Up:
- The script begins by running a cleanup process to remove any old generated files.
-
Load Configuration:
- The JSON configuration file is parsed, and a
Config
object is created to manage the deployment.
- The JSON configuration file is parsed, and a
-
Prepare Module:
- The
schema.graphql
is generated by combining common models and the target module’s schema. - The script identifies all models in the schema and determines the necessary events using the dependency files.
- The
-
Process Contracts:
- Each contract is processed to handle multiple versions, creating "fake" contracts as needed.
- Event signatures and handler names are generated for each event.
-
Generate Subgraph Configuration:
- A
subgraph.yaml
file is created, detailing the data sources, event handlers, ABIs, and file paths for each contract.
- A
-
Generate Source Files (if
--create-src
flag is used):- TypeScript files (
src_{abi}_{version}.ts
) are generated for each contract version. - Import statements and handler functions for each event are created.
- TypeScript files (
-
Build Subgraph:
graph codegen
is executed to generate AssemblyScript types.graph build
compiles the subgraph.
-
Deploy Subgraph (if
--deploy
flag is used):- The script constructs the deployment command based on the network (including Mantle if specified) and executes the
graph deploy
command.
- The script constructs the deployment command based on the network (including Mantle if specified) and executes the
- Missing Events: If certain events are not indexed, ensure the dependency files correctly map the events to the relevant entities.
- Schema Errors: Verify that all models specified in
subgraph_config.json
exist in the common models directory. - Deployment Failures: Double-check network configurations and ensure that the necessary permissions are in place for deployment.
- Version Mismatches: Confirm that all required contract versions are included in the configuration file.