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