toCoinType
Converts a chain ID to an ENSIP-9 compliant coin type
toCoinType
Converts a chain ID to an ENSIP-9 compliant coin type.
Import
import "github.com/ChefBingbong/viem-go/utils/ens"Usage
import "github.com/ChefBingbong/viem-go/utils/ens"
// Ethereum mainnet (chainId 1) uses SLIP-44 coin type 60coinType, err := ens.ToCoinType(1)if err != nil { log.Fatal(err)}// 60
// Optimism (chainId 10)coinType, _ = ens.ToCoinType(10)// 2147483658
// Polygon (chainId 137)coinType, _ = ens.ToCoinType(137)// 2147483785
// Panic on errorcoinType = ens.MustToCoinType(1)// 60Returns
- Type:
(uint64, error)forToCoinType,uint64forMustToCoinType
The coin type for the chain ID.
Parameters
chainId (required)
- Type:
int
The chain ID to convert. Must be >= 0 and < 0x80000000.
coinType, _ := ens.ToCoinType(1)// 60
coinType, _ = ens.ToCoinType(10)// 2147483658Conversion Rules
- Ethereum Mainnet (chainId 1): Returns
60(ETH's SLIP-44 coin type) - Other Chains: Applies ENSIP-9 formula:
0x80000000 | chainId
Functions
ToCoinType
Converts chain ID to coin type:
coinType, _ := ens.ToCoinType(1)
MustToCoinType
Converts chain ID to coin type or panics:
coinType := ens.MustToCoinType(1)
Notes
- Ethereum mainnet (chainId 1) uses SLIP-44 coin type 60
- Other chains use the ENSIP-9 formula:
0x80000000 | chainId - Chain ID must be >= 0 and < 0x80000000
- Used in ENS address resolution for multi-chain addresses