0

Multichain applications setup a network/chain config that could be simplified to an object where each key is a unique ID for a supported chain such as the chainId as follows:

// typescript:
enum ChainIds {
  Ethereum = 1,
  BSC = 56,
  AVAX = 43114,
};

const NetworkConfig = {
  [ChainIds.Ethereum]: {
    blockTime: 15_000, // 15s/block
    gasLimit: 15_000_000 // 15m/block
  },
  [ChainIds.BSC]: {
    blockTime: 3_000, // 3s/block
    gasLimit: 100_000_000 // 100m/block
  },
  [ChainIds.AVAX]: {
    blockTime: 3_000, // 3s/block
    gasLimit: 8_000_000 // 8m/block
  },
};

const chainId = 1; // how to get the chainId in subgraph environment

const gasPerS = NetworkConfig[chainId].gasLimit/NetworkConfig[chainId].blockTime;

This is one way of developing a subgraph in a network agnostic way, however I couldn't find any way to get the chainId from within a subgraph in thegraphs documentation. Is it possible to do so and if not how could/should a subgraph be developed in a chain agnostic manner.

1 Answer 1

1

The network name can be accessed via dataSource as follows:

import { dataSource } from "@graphprotocol/graph-ts";

const chainName = dataSource.network(); // returns network name

const chainId = ChainIds[chainName];

The list of network's supported on the graphs hosted service can be found here.

You can also configure the data source addresses listed in the subgraph.yaml for multiple networks using a networks.json config as shown in thegraph's docs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.