@gfxlabs/oku-chains 1.12.3 → 1.12.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index-mjs.js CHANGED
@@ -6842,6 +6842,111 @@ const zkSync = makeConfig({
6842
6842
  },
6843
6843
  });
6844
6844
 
6845
+ /**
6846
+ * Placeholder numeric `id` used by non-EVM chains.
6847
+ *
6848
+ * viem's `Chain` (which {@link IChainInfo} extends) requires a numeric `id`,
6849
+ * but non-EVM chains (e.g. Bitcoin) have no EVM chain id. They use `0` as a
6850
+ * sentinel and rely on `caip2Namespace` + `caip2Reference` for identity and
6851
+ * resolution instead. `0` is never a valid EVM chain id, so it cannot collide.
6852
+ */
6853
+ const NON_EVM_CHAIN_ID = 0;
6854
+ /**
6855
+ * True if the chain is non-EVM (i.e. uses the {@link NON_EVM_CHAIN_ID}
6856
+ * placeholder id and a non-`eip155` CAIP-2 namespace). Non-EVM chains live in
6857
+ * the same {@link IChainInfo} shape as EVM chains, so EVM-only fields
6858
+ * (contracts, uniswap metadata, etc.) will be present but empty.
6859
+ */
6860
+ function isNonEvmChain(c) {
6861
+ return c.id === NON_EVM_CHAIN_ID && c.caip2Namespace !== "eip155";
6862
+ }
6863
+ /** True if the chain is an EVM chain. Inverse of {@link isNonEvmChain}. */
6864
+ function isEvmChain(c) {
6865
+ return !isNonEvmChain(c);
6866
+ }
6867
+
6868
+ /**
6869
+ * Bitcoin mainnet.
6870
+ *
6871
+ * A non-EVM chain expressed within the EVM-shaped {@link IChainInfo} so it
6872
+ * flows through the existing resolvers and consumers unchanged. Because viem's
6873
+ * `Chain` requires a numeric `id`, Bitcoin uses the {@link NON_EVM_CHAIN_ID}
6874
+ * (`0`) placeholder and is identified/resolved via CAIP-2 instead:
6875
+ * `bip122:000000000019d6689c085ae165831e93`, where the reference is the first
6876
+ * 32 hex chars of the genesis block hash per the CAIP-2 `bip122` namespace.
6877
+ *
6878
+ * EVM-only fields (uniswap, morpho, contracts, etc.) are present but empty.
6879
+ *
6880
+ * Refs:
6881
+ * - https://github.com/ChainAgnostic/namespaces/blob/main/bip122/caip2.md
6882
+ */
6883
+ const chain = defineChain({
6884
+ id: NON_EVM_CHAIN_ID,
6885
+ name: "Bitcoin",
6886
+ nativeCurrency: {
6887
+ name: "Bitcoin",
6888
+ symbol: "BTC",
6889
+ decimals: 8,
6890
+ },
6891
+ rpcUrls: {
6892
+ default: {
6893
+ http: ["https://bitcoin-rpc.publicnode.com"],
6894
+ },
6895
+ },
6896
+ blockExplorers: {
6897
+ default: {
6898
+ name: "mempool.space",
6899
+ url: "https://mempool.space",
6900
+ apiUrl: "https://mempool.space/api",
6901
+ },
6902
+ },
6903
+ });
6904
+ const bitcoin = makeConfig({
6905
+ ...chain,
6906
+ caip2Namespace: "bip122",
6907
+ caip2Reference: "000000000019d6689c085ae165831e93",
6908
+ internalName: "bitcoin",
6909
+ transactionType: "bitcoin",
6910
+ sortIndex: 1000,
6911
+ launchTime: 1231006505,
6912
+ blockTimeSeconds: 600,
6913
+ deprecated: false,
6914
+ logoUrl: "https://cms.oku.trade/cdn/public/chains/bitcoin-logo.webp",
6915
+ nativeLogoUrl: "https://cms.oku.trade/cdn/public/natives/btc.png",
6916
+ blockAid: "bitcoin",
6917
+ estimatedSwapGas: 0,
6918
+ estimatedBridgeGas: 0,
6919
+ estimatedWrapGas: 0,
6920
+ initCodeHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
6921
+ defaultPool: "0x0000000000000000000000000000000000000000",
6922
+ defaultToken0: "0x0000000000000000000000000000000000000000",
6923
+ defaultToken1: "0x0000000000000000000000000000000000000000",
6924
+ tokenList: [],
6925
+ stables: [],
6926
+ watchlist: [],
6927
+ v4Watchlist: [],
6928
+ externalId: {
6929
+ coingecko: "bitcoin",
6930
+ },
6931
+ markets: {},
6932
+ bridges: {},
6933
+ oracles: {
6934
+ coingecko: {
6935
+ slug: "bitcoin",
6936
+ native: "bitcoin",
6937
+ },
6938
+ },
6939
+ uniswap: {},
6940
+ morpho: {},
6941
+ token: {},
6942
+ oku: {},
6943
+ contracts: {
6944
+ nftManager: {
6945
+ address: "0x0000000000000000000000000000000000000000",
6946
+ },
6947
+ },
6948
+ });
6949
+
6845
6950
  /**
6846
6951
  * Parse a CAIP-2 chain ID string into its namespace and reference components.
6847
6952
  *
@@ -6875,20 +6980,33 @@ function parseCAIP2(caip2) {
6875
6980
  function formatCAIP2(id) {
6876
6981
  return `${id.namespace}:${id.reference}`;
6877
6982
  }
6983
+ /**
6984
+ * The CAIP-2 reference component for a chain.
6985
+ *
6986
+ * If the chain sets an explicit `caip2Reference` (e.g. a non-EVM chain like
6987
+ * Bitcoin, whose reference is a genesis hash prefix), it is used verbatim.
6988
+ * Otherwise the reference is derived from the numeric `id` (the historical
6989
+ * behavior for EVM chains, e.g. "1" for Ethereum mainnet).
6990
+ */
6991
+ function caip2Reference(chain) {
6992
+ return chain.caip2Reference ?? String(chain.id);
6993
+ }
6878
6994
  /**
6879
6995
  * Convert a chain config to its CAIP-2 identifier string.
6880
6996
  *
6881
6997
  * @example
6882
6998
  * ```ts
6883
- * import { mainnet } from "@gfxlabs/oku-chains";
6999
+ * import { mainnet, bitcoin } from "@gfxlabs/oku-chains";
6884
7000
  * toCAIP2(mainnet)
6885
7001
  * // => "eip155:1"
7002
+ * toCAIP2(bitcoin)
7003
+ * // => "bip122:000000000019d6689c085ae165831e93"
6886
7004
  * ```
6887
7005
  */
6888
7006
  function toCAIP2(chain) {
6889
7007
  return formatCAIP2({
6890
7008
  namespace: chain.caip2Namespace,
6891
- reference: String(chain.id),
7009
+ reference: caip2Reference(chain),
6892
7010
  });
6893
7011
  }
6894
7012
  /**
@@ -6905,13 +7023,147 @@ function toCAIP2(chain) {
6905
7023
  */
6906
7024
  function fromCAIP2(caip2, chains) {
6907
7025
  const { namespace, reference } = parseCAIP2(caip2);
6908
- const chain = chains.find((c) => c.caip2Namespace === namespace && String(c.id) === reference);
7026
+ const chain = chains.find((c) => c.caip2Namespace === namespace && caip2Reference(c) === reference);
6909
7027
  if (!chain) {
6910
7028
  throw new Error(`No chain found for CAIP-2 identifier: "${caip2}"`);
6911
7029
  }
6912
7030
  return chain;
6913
7031
  }
6914
7032
 
7033
+ /**
7034
+ * Error thrown when a network cannot be found by any lookup method.
7035
+ */
7036
+ class NetworkNotFoundError extends Error {
7037
+ constructor(input) {
7038
+ super(`chain not found: ${String(input)}`);
7039
+ this.name = "NetworkNotFoundError";
7040
+ }
7041
+ }
7042
+ /**
7043
+ * Build a lookup index from a list of chains for fast repeated lookups.
7044
+ * Pre-computes maps keyed by chain ID, internal name, and CAIP-2 identifier.
7045
+ *
7046
+ * Non-EVM chains use the {@link NON_EVM_CHAIN_ID} (`0`) placeholder id and are
7047
+ * intentionally NOT registered in `byId` (they are not resolvable by numeric
7048
+ * id, and would otherwise all collide on `0`). They remain resolvable by
7049
+ * internal name and by their explicit CAIP-2 identifier.
7050
+ */
7051
+ function buildNetworkIndex(chains) {
7052
+ const byId = new Map();
7053
+ const byName = new Map();
7054
+ const byCAIP2 = new Map();
7055
+ for (const chain of chains) {
7056
+ if (chain.id !== NON_EVM_CHAIN_ID) {
7057
+ byId.set(chain.id, chain);
7058
+ }
7059
+ byName.set(chain.internalName, chain);
7060
+ byCAIP2.set(`${chain.caip2Namespace}:${caip2Reference(chain)}`, chain);
7061
+ }
7062
+ return { byId, byName, byCAIP2 };
7063
+ }
7064
+ /**
7065
+ * Look up a chain by its numeric chain ID. Non-EVM chains use the
7066
+ * {@link NON_EVM_CHAIN_ID} placeholder and are not resolvable here.
7067
+ */
7068
+ function networkById$1(id, idx) {
7069
+ const chain = idx.byId.get(id);
7070
+ if (!chain) {
7071
+ throw new NetworkNotFoundError(id);
7072
+ }
7073
+ return chain;
7074
+ }
7075
+ /**
7076
+ * Look up a chain by its internal name (e.g. "arbitrum", "mainnet",
7077
+ * "bitcoin").
7078
+ */
7079
+ function networkByName$1(name, idx) {
7080
+ const chain = idx.byName.get(name);
7081
+ if (!chain) {
7082
+ throw new NetworkNotFoundError(name);
7083
+ }
7084
+ return chain;
7085
+ }
7086
+ /**
7087
+ * Look up a chain by its CAIP-2 identifier string (e.g. "eip155:1" or
7088
+ * "bip122:000000000019d6689c085ae165831e93"). This is the way to resolve
7089
+ * non-EVM chains.
7090
+ */
7091
+ function networkByCAIP2$1(caip2, idx) {
7092
+ // Validate the format
7093
+ parseCAIP2(caip2);
7094
+ const chain = idx.byCAIP2.get(caip2);
7095
+ if (!chain) {
7096
+ throw new NetworkNotFoundError(caip2);
7097
+ }
7098
+ return chain;
7099
+ }
7100
+ /**
7101
+ * Look up a chain from a string. Tries, in order:
7102
+ * 1. CAIP-2 identifier (if the string contains ":")
7103
+ * 2. Internal name
7104
+ * 3. Numeric chain ID (parsed from string)
7105
+ *
7106
+ * Non-EVM chains are only reachable via the CAIP-2 or internal-name paths,
7107
+ * never via the numeric-id fallback.
7108
+ *
7109
+ * Mirrors the Go `NetworkByString` function.
7110
+ */
7111
+ function networkByString$1(s, idx) {
7112
+ if (s === "") {
7113
+ throw new NetworkNotFoundError("empty string");
7114
+ }
7115
+ // Try CAIP-2
7116
+ if (s.includes(":")) {
7117
+ return networkByCAIP2$1(s, idx);
7118
+ }
7119
+ // Try internal name
7120
+ const byName = idx.byName.get(s);
7121
+ if (byName) {
7122
+ return byName;
7123
+ }
7124
+ // Try numeric chain ID
7125
+ const parsed = Number(s);
7126
+ if (!Number.isNaN(parsed) && Number.isFinite(parsed)) {
7127
+ const byId = idx.byId.get(Math.trunc(parsed));
7128
+ if (byId) {
7129
+ return byId;
7130
+ }
7131
+ }
7132
+ throw new NetworkNotFoundError(s);
7133
+ }
7134
+ /**
7135
+ * Resolve a chain from an arbitrary input. Accepts:
7136
+ * - `number`: treated as a numeric chain ID (EVM-only)
7137
+ * - `string`: tried as CAIP-2 identifier (if it contains ":"), then as
7138
+ * internal name, then as a numeric chain ID string
7139
+ * - `IChainInfo`: returned directly (pass-through)
7140
+ *
7141
+ * Non-EVM chains have a placeholder numeric id, so they can only be resolved
7142
+ * via their CAIP-2 identifier or internal name (or passed through directly).
7143
+ *
7144
+ * Mirrors the Go `NetworkByAny` function.
7145
+ *
7146
+ * @throws {NetworkNotFoundError} if no matching chain is found
7147
+ */
7148
+ function networkByAny$1(v, idx) {
7149
+ if (v == null) {
7150
+ throw new NetworkNotFoundError("null");
7151
+ }
7152
+ // Pass-through if already a chain config object
7153
+ if (typeof v === "object" && "id" in v && "internalName" in v) {
7154
+ return v;
7155
+ }
7156
+ // Numeric chain ID
7157
+ if (typeof v === "number") {
7158
+ return networkById$1(Math.trunc(v), idx);
7159
+ }
7160
+ // String: CAIP-2, name, or numeric string
7161
+ if (typeof v === "string") {
7162
+ return networkByString$1(v, idx);
7163
+ }
7164
+ throw new NetworkNotFoundError(v);
7165
+ }
7166
+
6915
7167
  const MAINNET_CHAINS = [
6916
7168
  arbitrum,
6917
7169
  base,
@@ -6962,5 +7214,97 @@ const MAINNET_CHAINS = [
6962
7214
  gensyn,
6963
7215
  pharos,
6964
7216
  ];
7217
+ /**
7218
+ * Non-EVM chains (e.g. Bitcoin). These share the {@link IChainInfo} shape as
7219
+ * EVM chains but use the `NON_EVM_CHAIN_ID` (`0`) placeholder id and are
7220
+ * resolvable only via CAIP-2 (or internal name). Kept as a separate array so
7221
+ * the EVM-only surface (and the Go codegen, which reads `MAINNET_CHAINS`) is
7222
+ * unaffected.
7223
+ */
7224
+ const NON_EVM_CHAINS = [
7225
+ bitcoin,
7226
+ ];
7227
+ /**
7228
+ * All networks, EVM and non-EVM. Use this when you need to enumerate every
7229
+ * supported chain regardless of type.
7230
+ */
7231
+ const ALL_NETWORKS = [
7232
+ ...MAINNET_CHAINS,
7233
+ ...NON_EVM_CHAINS,
7234
+ ];
7235
+ /**
7236
+ * Pre-built lookup index over all networks (EVM + non-EVM), like Go's
7237
+ * module-level maps. Resolution functions below bind to this index.
7238
+ */
7239
+ const _idx = buildNetworkIndex(ALL_NETWORKS);
7240
+ /**
7241
+ * Resolve a chain from an arbitrary input. Accepts:
7242
+ * - `number`: treated as chain ID
7243
+ * - `string`: tried as CAIP-2 identifier (if it contains ":"), then as
7244
+ * internal name, then as a numeric chain ID string
7245
+ * - `IChainInfo`: returned directly (pass-through)
7246
+ *
7247
+ * Mirrors the Go `NetworkByAny` function.
7248
+ *
7249
+ * Non-EVM chains (e.g. Bitcoin) use a placeholder numeric id and can only be
7250
+ * resolved via their CAIP-2 identifier or internal name. Use `isNonEvmChain`
7251
+ * if you need to distinguish them.
7252
+ *
7253
+ * @example
7254
+ * ```ts
7255
+ * import { networkByAny } from "@gfxlabs/oku-chains";
7256
+ *
7257
+ * networkByAny(1) // by chain ID (EVM)
7258
+ * networkByAny("mainnet") // by internal name
7259
+ * networkByAny("eip155:1") // by CAIP-2 (EVM)
7260
+ * networkByAny("42161") // by chain ID string (EVM)
7261
+ * networkByAny("bip122:000000000019d6689c085ae165831e93") // by CAIP-2 (non-EVM)
7262
+ * networkByAny("bitcoin") // by internal name (non-EVM)
7263
+ * ```
7264
+ *
7265
+ * @throws {NetworkNotFoundError} if no matching chain is found
7266
+ */
7267
+ function networkByAny(v) {
7268
+ return networkByAny$1(v, _idx);
7269
+ }
7270
+ /**
7271
+ * Look up a chain by its numeric chain ID. Non-EVM chains use a placeholder id
7272
+ * and are not resolvable here.
7273
+ *
7274
+ * @throws {NetworkNotFoundError} if no matching chain is found
7275
+ */
7276
+ function networkById(id) {
7277
+ return networkById$1(id, _idx);
7278
+ }
7279
+ /**
7280
+ * Look up a chain by its internal name (e.g. "arbitrum", "mainnet",
7281
+ * "bitcoin").
7282
+ *
7283
+ * @throws {NetworkNotFoundError} if no matching chain is found
7284
+ */
7285
+ function networkByName(name) {
7286
+ return networkByName$1(name, _idx);
7287
+ }
7288
+ /**
7289
+ * Look up a chain from a string. Tries, in order:
7290
+ * 1. CAIP-2 identifier (if the string contains ":")
7291
+ * 2. Internal name
7292
+ * 3. Numeric chain ID (parsed from string)
7293
+ *
7294
+ * @throws {NetworkNotFoundError} if no matching chain is found
7295
+ */
7296
+ function networkByString(s) {
7297
+ return networkByString$1(s, _idx);
7298
+ }
7299
+ /**
7300
+ * Look up a chain by its CAIP-2 identifier string (e.g. "eip155:1" or
7301
+ * "bip122:000000000019d6689c085ae165831e93"). This is the way to resolve
7302
+ * non-EVM chains.
7303
+ *
7304
+ * @throws {NetworkNotFoundError} if no matching chain is found
7305
+ */
7306
+ function networkByCAIP2(caip2) {
7307
+ return networkByCAIP2$1(caip2, _idx);
7308
+ }
6965
7309
 
6966
- export { MAINNET_CHAINS, arbitrum, avalanche, base, blast, bob, boba, bsc, celo, corn, etherlink, filecoin, formatCAIP2, fromCAIP2, gensyn, gnosis, goat, hemi, hyperevm, lens, lightlink, linea, lisk, mainnet, manta, mantle, matchain, metal, monad, moonbeam, nibiru, optimism, parseCAIP2, pharos, plasma, polygon, polygonZkEvm, redbelly, ronin, rootstock, saga, scroll, sei, sonic, taiko, telos, toCAIP2, tronShasta, unichain, worldchain, xdc, zerog, zkSync };
7310
+ export { ALL_NETWORKS, MAINNET_CHAINS, NON_EVM_CHAINS, NON_EVM_CHAIN_ID, NetworkNotFoundError, arbitrum, avalanche, base, bitcoin, blast, bob, boba, bsc, buildNetworkIndex, caip2Reference, celo, corn, etherlink, filecoin, formatCAIP2, fromCAIP2, gensyn, gnosis, goat, hemi, hyperevm, isEvmChain, isNonEvmChain, lens, lightlink, linea, lisk, mainnet, makeConfig, manta, mantle, matchain, metal, monad, moonbeam, networkByAny, networkByCAIP2, networkById, networkByName, networkByString, nibiru, optimism, parseCAIP2, pharos, plasma, polygon, polygonZkEvm, redbelly, ronin, rootstock, saga, scroll, sei, sonic, taiko, telos, toCAIP2, tronShasta, unichain, worldchain, xdc, zerog, zkSync };