Skip to content

Commit

Permalink
coin tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
TateB committed Jan 18, 2024
1 parent 2e6acb3 commit b37ec1c
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 167 deletions.
Binary file modified bun.lockb
Binary file not shown.
9 changes: 6 additions & 3 deletions src/async.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { eth } from "./coins.js";
import { coinNameToTypeMap, coinTypeToNameMap } from "./consts/coinTypeMap.js";
import { coinNameToTypeMap } from "./consts/coinNameToTypeMap.js";
import { coinTypeToNameMap } from "./consts/coinTypeToNameMap.js";
import type {
CoinName,
CoinType,
Expand Down Expand Up @@ -42,10 +43,12 @@ export const getCoderByCoinTypeAsync = async <
>(
coinType: TCoinType
): Promise<GetCoderByCoinType<TCoinType>> => {
const name =
const names =
coinTypeToNameMap[String(coinType) as keyof typeof coinTypeToNameMap];

if (!name) throw new Error(`Unsupported coin type: ${coinType}`);
if (!names) throw new Error(`Unsupported coin type: ${coinType}`);

const [name] = names;

if (coinType >= SLIP44_MSB) {
// EVM coin
Expand Down
26 changes: 26 additions & 0 deletions src/consts/coinNameToTypeMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { ParseInt } from "../types.js";
import {
evmCoinTypeToNameMap,
nonEvmCoinTypeToNameMap,
} from "./coinTypeToNameMap.js";

export const evmCoinNameToTypeMap = Object.freeze(
Object.fromEntries(
Object.entries(evmCoinTypeToNameMap).map(([k, [v]]) => [v, parseInt(k)])
) as {
readonly [key in keyof typeof evmCoinTypeToNameMap as (typeof evmCoinTypeToNameMap)[key][0]]: ParseInt<key>;
}
);

export const nonEvmCoinNameToTypeMap = Object.freeze(
Object.fromEntries(
Object.entries(nonEvmCoinTypeToNameMap).map(([k, [v]]) => [v, parseInt(k)])
) as {
readonly [key in keyof typeof nonEvmCoinTypeToNameMap as (typeof nonEvmCoinTypeToNameMap)[key][0]]: ParseInt<key>;
}
);

export const coinNameToTypeMap = Object.freeze({
...evmCoinNameToTypeMap,
...nonEvmCoinNameToTypeMap,
} as const);
150 changes: 0 additions & 150 deletions src/consts/coinTypeMap.ts

This file was deleted.

166 changes: 166 additions & 0 deletions src/consts/coinTypeToNameMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
export const evmCoinTypeToNameMap = Object.freeze({
/* Chain ID: 10 */
"2147483658": ["op", "Optimism"],
/* Chain ID: 25 */
"2147483673": ["cro", "Cronos"],
/* Chain ID: 56 */
"2147483704": ["bsc", "BNB Smart Chain"],
/* Chain ID: 60 */
"2147483708": ["go", "GoChain"],
/* Chain ID: 61 */
"2147483709": ["etc", "Ethereum Classic"],
/* Chain ID: 88 */
"2147483736": ["tomo", "TomoChain"],
/* Chain ID: 99 */
"2147483747": ["poa", "POA"],
/* Chain ID: 100 */
"2147483748": ["gno", "Gnosis"],
/* Chain ID: 108 */
"2147483756": ["tt", "ThunderCore"],
/* Chain ID: 137 */
"2147483785": ["matic", "Polygon"],
/* Chain ID: 246 */
"2147483894": ["ewt", "Energy Web"],
/* Chain ID: 250 */
"2147483898": ["ftm", "Fantom Opera"],
/* Chain ID: 361 */
"2147484009": ["theta", "Theta"],
/* Chain ID: 820 */
"2147484468": ["clo", "Callisto"],
/* Chain ID: 39797 */
"2147523445": ["nrg", "Energi"],
/* Chain ID: 42161 */
"2147525809": ["arb1", "Arbitrum One"],
/* Chain ID: 42220 */
"2147525868": ["celo", "Celo"],
/* Chain ID: 43114 */
"2147526762": ["avaxc", "Avalanche C-Chain"],
} as const);

export const nonEvmCoinTypeToNameMap = Object.freeze({
"0": ["btc", "Bitcoin"],
"2": ["ltc", "Litecoin"],
"3": ["doge", "Dogecoin"],
"4": ["rdd", "Reddcoin"],
"5": ["dash", "Dash"],
"6": ["ppc", "Peercoin"],
"7": ["nmc", "Namecoin"],
"14": ["via", "Viacoin"],
"20": ["dgb", "DigiByte"],
"22": ["mona", "Monacoin"],
"42": ["dcr", "Decred"],
"43": ["xem", "NEM"],
"55": ["aib", "AIB"],
"57": ["sys", "Syscoin"],
"60": ["eth", "Ethereum"],
"61": ["etcLegacy", "[LEGACY] Ethereum Classic"],
"74": ["icx", "ICON"],
"77": ["xvg", "Verge"],
"105": ["strat", "Stratis"],
"111": ["ark", "ARK"],
"118": ["atom", "Atom"],
"121": ["zen", "Zencash"],
"128": ["xmr", "Monero"],
"133": ["zec", "Zcash"],
"134": ["lsk", "Lisk"],
"135": ["steem", "Steem"],
"136": ["firo", "Firo"],
"137": ["rbtc", "RSK"],
"141": ["kmd", "Komodo"],
"144": ["xrp", "Ripple"],
"145": ["bch", "Bitcoin Cash"],
"148": ["xlm", "Stellar Lumens"],
"153": ["btm", "Bytom"],
"156": ["btg", "Bitcoin Gold"],
"165": ["nano", "Nano"],
"175": ["rvn", "Ravencoin"],
"178": ["poaLegacy", "[LEGACY] POA"],
"192": ["lcc", "LitecoinCash"],
"194": ["eos", "EOS"],
"195": ["trx", "Tron"],
"204": ["bcn", "Bytecoin"],
"235": ["fio", "FIO"],
"236": ["bsv", "BitcoinSV"],
"242": ["nim", "Nimiq"],
"246": ["ewtLegacy", "[LEGACY] Energy Web"],
"283": ["algo", "Algorand"],
"291": ["iost", "IOST"],
"301": ["divi", "Divi Project"],
"304": ["iotx", "IoTeX"],
"308": ["bts", "Bitshares"],
"309": ["ckb", "Nervos CKB"],
"313": ["zil", "Zilliqa"],
"326": ["mrx", "Metrix Coin"],
"330": ["luna", "Terra"],
"354": ["dot", "Polkadot"],
"360": ["vsys", "V Systems"],
"367": ["abbc", "ABBC"],
"397": ["near", "NEAR Protocol"],
"415": ["etn", "Electroneum"],
"425": ["aion", "Aion"],
"434": ["ksm", "Kusama"],
"457": ["ae", "æternity"],
"459": ["kava", "Kava"],
"461": ["fil", "Filecoin"],
"472": ["ar", "Arweave"],
"489": ["cca", "Counos"],
"500": ["thetaLegacy", "[LEGACY] Theta"],
"501": ["sol", "Solana"],
"508": ["egld", "MultiversX"],
"535": ["xhv", "Haven Protocol"],
"539": ["flow", "Flow"],
"566": ["iris", "Irisnet"],
"568": ["lrg", "Large Coin"],
"569": ["sero", "Super Zero Protocol"],
"570": ["bdx", "Beldex"],
"571": ["ccxx", "Counos X"],
"573": ["srm", "Serum"],
"574": ["vlxLegacy", "[LEGACY] Velas"],
"576": ["bps", "BitcoinPoS"],
"589": ["tfuel", "Theta Fuel"],
"592": ["grin", "Grin"],
"700": ["gnoLegacy", "[LEGACY] Gnosis"],
"714": ["bnb", "BNB"],
"818": ["vet", "VeChain"],
"820": ["cloLegacy", "[LEGACY] Callisto"],
"825": ["hive", "Hive"],
"888": ["neo", "NEO"],
"889": ["tomoLegacy", "[LEGACY] TomoChain"],
"904": ["hnt", "Helium"],
"931": ["rune", "THORChain"],
"999": ["bcd", "Bitcoin Diamond"],
"1001": ["ttLegacy", "[LEGACY] ThunderCore"],
"1007": ["ftmLegacy", "[LEGACY] Fantom"],
"1023": ["one", "HARMONY-ONE"],
"1024": ["ont", "Ontology"],
"1237": ["nostr", "Nostr"],
"1729": ["xtz", "Tezos"],
"1815": ["ada", "Cardano"],
"1991": ["sc", "Sia"],
"2301": ["qtum", "QTUM"],
"2303": ["gxc", "GXChain"],
"2305": ["ela", "Elastos"],
"2718": ["nas", "Nebulas"],
"3030": ["hbar", "Hedera HBAR"],
"4218": ["iota", "IOTA"],
"5353": ["hns", "Handshake"],
"5757": ["stx", "Stacks"],
"6060": ["goLegacy", "[LEGACY] GoChain"],
"8444": ["xch", "Chia"],
"8964": ["nuls", "NULS"],
"9000": ["avax", "Avalanche"],
"9004": ["strk", "StarkNet"],
"9797": ["nrgLegacy", "[LEGACY] Energi"],
"16754": ["ardr", "Ardor"],
"19167": ["flux", "Flux"],
"52752": ["celoLegacy", "[LEGACY] Celo"],
"99999": ["wicc", "Waykichain"],
"5655640": ["vlx", "Velas"],
"5718350": ["wan", "Wanchain"],
"5741564": ["waves", "Waves"],
} as const);

export const coinTypeToNameMap = Object.freeze({
...nonEvmCoinTypeToNameMap,
...evmCoinTypeToNameMap,
} as const);
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as formats from "./coins.js";
import { coinNameToTypeMap, coinTypeToNameMap } from "./consts/coinTypeMap.js";
import { coinNameToTypeMap } from "./consts/coinNameToTypeMap.js";
import { coinTypeToNameMap } from "./consts/coinTypeToNameMap.js";
import type {
Coin,
CoinName,
Expand Down Expand Up @@ -52,11 +53,12 @@ export const getCoderByCoinType = <
>(
coinType: TCoinType
): GetCoderByCoinType<TCoinType> => {
const name =
const names =
coinTypeToNameMap[String(coinType) as keyof typeof coinTypeToNameMap];
if (!name) {
if (!names) {
throw new Error(`Unsupported coin type: ${coinType}`);
}
const [name] = names;
if (coinType >= SLIP44_MSB) {
// EVM coin
const evmChainId = coinTypeToEvmChainId(coinType);
Expand Down
Loading

0 comments on commit b37ec1c

Please sign in to comment.