Skip to content

How to calc stub chainID

jowenshaw edited this page Feb 21, 2023 · 1 revision

How to calc stub chainID

Our router product use distinct chainID to identify each blockchain.

For evm chains, we use the existing chainID, eg. 1 for ethereum mainnet, 5 for ethereum goerli, etc. You can find more from the ChainList (may be different for some unwellknown chains).

Note: some non-evm blockchains (like solona use 245022934) have its well known chainID (maybe asssigned by some evm compatilbe implementation), we’ll use these chainIDs.

For non-evm chains, we specify the following algotirhm to assign each blockchain a unique chainID number which we call stub chainID.

1. calculation algorithm

For each non-evm chain, we’ll give it two parameters: chainName and network.

chainName is assigned by our developer, often it’s the blockchain’s name or the blockchain’s gas token’s symbol.

network is often one of mainnet, testnet, devnet.

Let’s take an example here by assigning them:

chainName = "XRP"
network = "testnet"

And we use the following constants:

stubChainIDBase = 1000000000000

Now we can calc the stub chainID by the following steps:

  1. convert chainName to all upper letters

  2. convert chainName to big number

    • use golang to convert

      new(big.Int).SetBytes([]byte(chainName))
    • use python to convert

      int.from_bytes(bytes(chainName, 'ascii'), "big")

    when chainName is XRP, the result is 5788240.

  3. add an offset by network type

    network type offset value
    mainnet 0
    testnet 1
    devnet 2

    when network is testnet, the result is 5788241

  4. mod stubChainIDBase

    the result after mod is 5788241

  5. add base offset of stubChainIDBase

    the final result is 1000005788241

2. stub chainID examples

bockchain chainName mainnet stubchainID testnet stubchainID devnet stubchainID
bitcoin BTC 1000004346947 1000004346948 1000004346949
ripple XRP 1000005788240 1000005788241 1000005788242
near NEAR 1001313161554 1001313161555 1001313161556
aptos APT 1000004280404 1000004280405 1000004280406
cardano CARDANO 1645027868239 1645027868240 1645027868241
cosmos series OSMOSIS 1114668587347 1114668587348 1114668587349
cosmos series COREUM 1007961752909 1007961752910 1007961752911
cosmos series SEI 1000005457225 1000005457226 1000005457227

we list also these well known chainIds we used for some special non-evm blockchains

blockchain mainnet stubchainID testnet stubchainID devnet stubchainID
solana 245022934 245022940 245022926