Skip to content

Commit

Permalink
Refactor network configuration to support multiple environments
Browse files Browse the repository at this point in the history
Introduced a type-safe `Network` and `Networks` structure to manage multiple network environments (testnet and mainnet). The selected network is now determined dynamically based on the `CHAIN_ID` environment variable, simplifying the configuration process and reducing hard-coded values. Added error handling for unsupported `CHAIN_ID` values.
  • Loading branch information
justinbarry committed Nov 21, 2024
1 parent 4852609 commit c0b151c
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 20 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# SubQuery - Xion Chain Indexer

## To Publish run the following

```bash
CHAIN_ID="<chain_id>" yarn subql:publish
```

Take the resulting IPFS hash and update it in the [managed dashboard](https://managedservice.subquery.network/orgs/burnt-labs/projects/).



[SubQuery](https://subquery.network) is a fast, flexible, and reliable open-source data indexer that provides you with custom APIs for your web3 project across all of our supported networks. To learn about how to get started with SubQuery, [visit our docs](https://academy.subquery.network).
Expand Down
73 changes: 53 additions & 20 deletions project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,63 @@ import {
CosmosRuntimeHandler,
} from "@subql/types-cosmos";

// These defaults are the testnet values
let SMART_ACCOUNT_CONTRACT_CODE_ID = process.env
.SMART_ACCOUNT_CONTRACT_CODE_ID || ["21", "793"];
type Network = {
name: string;
codeIds: string[];
chainId: string;
endpoint: string;
startBlock: number;
bypassBlocks: number[];
};

type Networks = {
[key: string]: Network;
};

const networks: Networks = {
"xion-testnet-1": {
name: "xion-indexer",
codeIds: ["21", "793"],
chainId: "xion-testnet-1",
endpoint: "https://rpc.xion-testnet-1.burnt.com:443",
startBlock: 3371922,
bypassBlocks: [4962232, 8247887],
},
"xion-mainnet-1": {
name: "xion-mainnet-indexer",
codeIds: ["1", "5"],
chainId: "xion-mainnet-1",
endpoint: "https://rpc.xion-api.com/",
startBlock: 1825347,
bypassBlocks: [],
},
};

if (!process.env.CHAIN_ID) {
throw new Error("CHAIN_ID is not set");
}

SMART_ACCOUNT_CONTRACT_CODE_ID = Array.isArray(SMART_ACCOUNT_CONTRACT_CODE_ID)
? SMART_ACCOUNT_CONTRACT_CODE_ID
: [SMART_ACCOUNT_CONTRACT_CODE_ID];
const selectedNetwork = networks[process.env.CHAIN_ID];

const CHAIN_ID = process.env.CHAIN_ID || "xion-testnet-1";
const ENDPOINT_URL =
process.env.ENDPOINT_URL || "https://rpc.xion-testnet-1.burnt.com:443";
const START_BLOCK = Number(process.env.START_BLOCK || "3371922");
if (!selectedNetwork) {
throw new Error(
`Chain ID ${
process.env.CHAIN_ID
} is not supported. Supported networks are ${Object.keys(networks).join(
", ",
)}`,
);
}

const project: CosmosProject = {
specVersion: "1.0.0",
version: "1.0.0",
name: "xion-indexer",
description:
"Xion SubQuery project for account abstraction and hub/seat contracts.",
name: selectedNetwork.name,
description: "Xion SubQuery project for account abstraction",
runner: {
node: {
name: "@subql/node-cosmos",
version: ">=3.0.0",
version: ">=4.0.0",
},
query: {
name: "@subql/query",
Expand All @@ -38,15 +72,15 @@ const project: CosmosProject = {
file: "./schema.graphql",
},
network: {
chainId: CHAIN_ID,
chainId: selectedNetwork.chainId,
/**
*
* These endpoint(s) should be non-pruned archive nodes
* Public nodes may be rate limited, which can affect indexing speed
* When developing your project we suggest getting a private API key
* We suggest providing an array of endpoints for increased speed and reliability
*/
endpoint: [ENDPOINT_URL],
endpoint: [selectedNetwork.endpoint],
chaintypes: new Map([
[
"abstractaccount.v1",
Expand All @@ -65,16 +99,15 @@ const project: CosmosProject = {
},
],
]),
bypassBlocks: selectedNetwork.bypassBlocks,
},
dataSources: [
{
kind: CosmosDatasourceKind.Runtime,
startBlock: START_BLOCK,
startBlock: selectedNetwork.startBlock,
mapping: {
file: "./dist/index.js",
handlers: SMART_ACCOUNT_CONTRACT_CODE_ID.reduce<
Array<CosmosRuntimeHandler>
>(
handlers: selectedNetwork.codeIds.reduce<Array<CosmosRuntimeHandler>>(
(result, codeId) =>
result.concat([
{
Expand Down

0 comments on commit c0b151c

Please sign in to comment.