NibiruChain / nibiru

Nibiru Chain: The breakthrough smart contract platform ushering in the next era of money. Nibiru powers an ecosystem of dApps including perps, RWAs, and more.
https://nibiru.fi
Apache License 2.0
179 stars 200 forks source link

feat(eth): Make ParseEthChainID compatible with existing testnets. #1885

Closed Unique-Divine closed 1 month ago

Unique-Divine commented 2 months ago

Some of our canonical chain IDs like "cataclysm-1" (mainnet) and "nibiru-testnet-1" (testnet) are not compatible with with EIP155 standard. This makes the parsed Ethereum Chain ID (*big.Int) impossible to compute by standard means. We need a sufficient workaround for backwards compatibility with our live Chain IDs that will work with the Nibiru EVM.

Unique-Divine commented 1 month ago

Completed using an approach with a map instead of a regular expression:

eth/chain_id.go:

// ParseEthChainID parses a string chain identifier's epoch to an
// Ethereum-compatible chain-id in *big.Int format.
//
// This function uses Nibiru's map of chain IDs defined in Nibiru/app/appconst
// rather than the regex of EIP155, which is implemented by
// ParseEthChainIDStrict.
func ParseEthChainID(chainID string) (*big.Int, error) {
    return appconst.GetEthChainID(chainID), nil
}

appconst.go:


// EIP 155 Chain IDs exported for tests.
const (
    ETH_CHAIN_ID_MAINNET int64 = 420
    ETH_CHAIN_ID_LOCAL   int64 = 256
    ETH_CHAIN_ID_DEVNET  int64 = 500
    ETH_CHAIN_ID_DEFAULT int64 = 3000
)

var knownEthChainIDMap = map[string]int64{
    "cataclysm-1":       ETH_CHAIN_ID_MAINNET,
    "nibiru-localnet-0": ETH_CHAIN_ID_LOCAL,
    "nibiru-localnet-1": ETH_CHAIN_ID_LOCAL,
    "nibiru-localnet-2": ETH_CHAIN_ID_LOCAL,
    "nibiru-testnet-0":  ETH_CHAIN_ID_DEVNET,
    "nibiru-testnet-1":  ETH_CHAIN_ID_DEVNET,
    "nibiru-testnet-2":  ETH_CHAIN_ID_DEVNET,
    "nibiru-devnet-0":   ETH_CHAIN_ID_DEVNET,
    "nibiru-devnet-1":   ETH_CHAIN_ID_DEVNET,
    "nibiru-devnet-2":   ETH_CHAIN_ID_DEVNET,
}

// GetEthChainID: Maps the given chain ID from the block's `sdk.Context` to an
// EVM Chain ID (`*big.Int`).
func GetEthChainID(ctxChainID string) (ethChainID *big.Int) {
    ethChainIdInt, found := knownEthChainIDMap[ctxChainID]
    if !found {
        ethChainID = big.NewInt(ETH_CHAIN_ID_DEFAULT)
    } else {
        ethChainID = big.NewInt(ethChainIdInt)
    }
    return ethChainID
}