@gibs/bridge-client 1.19.0 → 1.26.0

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/endpoints.js CHANGED
@@ -1,75 +1,23 @@
1
1
  import { demoRpcUrlsFor } from '@gibs/bridge-sdk/chains';
2
2
  import { Chains } from '@gibs/bridge-sdk/config';
3
3
  import { chainTable } from './chain-table.js';
4
- /**
5
- * The storage key holding the endpoint table.
6
- *
7
- * The literal must not change: users have their own nodes saved under it, and a
8
- * rename silently reverts every one of them to the defaults on the next load.
9
- */
10
4
  export const ENDPOINTS_STORAGE_KEY = 'rpcs';
11
- /**
12
- * EVERY SUPPORTED CHAIN GETS AN ENTRY, and the entry comes from viem.
13
- *
14
- * This used to seed three chains — PulseChain, Ethereum and Binance Smart Chain
15
- * — while the aggregators quote routes from seventy-odd. A chain absent from
16
- * this table is not refused; `clientFromChain` falls through to viem's own
17
- * public nodes for it. So every read on an aggregator origin went to a node
18
- * nobody chose, which is where the rate limiting came from, and the endpoint
19
- * editor could not show the chain at all: a reader whose Base node was throttled
20
- * had no way to replace it, because Base was not in the list they were editing.
21
- *
22
- * Seeding from `chainTable` fixes both at once. The urls are viem's own
23
- * defaults, which is what the three seeded chains always used — the change is
24
- * the breadth, not the source.
25
- */
26
5
  const seedFor = (chains) => chains.map((chain) => [Number(chain), demoRpcUrlsFor(Number(chain))]);
27
- /** The bridge's own chains, kept first so a picker built from this reads well. */
28
6
  const BRIDGE_CHAIN_IDS = [
29
7
  Number(Chains.PLS),
30
8
  Number(Chains.ETH),
31
9
  Number(Chains.BNB),
32
10
  ];
33
- /**
34
- * Every chain the application can reach, the bridge's own first.
35
- *
36
- * The bridge's three keep taking their urls from `demoRpcUrlsFor`, which is
37
- * where the product's own node choices live and which must win over viem's
38
- * generic default for them.
39
- */
40
11
  export const productionSeed = [
41
12
  ...seedFor([Chains.PLS, Chains.ETH, Chains.BNB]),
42
13
  ...chainTable
43
- .filter((entry) =>
44
- // The bridge's own three are seeded above, from `demoRpcUrlsFor`, and
45
- // must not be seeded twice. That map is where the product's node
46
- // choices live — demoting a rate-limited host there has to take effect,
47
- // and a table entry added after it would silently win.
48
- !BRIDGE_CHAIN_IDS.includes(entry.id) &&
49
- // A PRODUCTION BUILD PROMISES NOT TO OFFER A TEST NETWORK, and the
50
- // endpoint table is one of the places that promise can be broken: the
51
- // chains seeded here are the chains the endpoint editor lists. The
52
- // table carries test networks deliberately, because a client still has
53
- // to be buildable for them on a testnet build; this is where they are
54
- // held back.
14
+ .filter((entry) => !BRIDGE_CHAIN_IDS.includes(entry.id) &&
55
15
  !entry.isTestnet)
56
16
  .map((entry) => [entry.id, [...entry.http]]),
57
17
  ];
58
- /** The bridge's test chains, for a build that is not production. */
59
18
  export const testnetSeed = seedFor([Chains.V4PLS, Chains.SEP]);
60
- /** The seed the next load starts from. */
61
19
  let seed = productionSeed;
62
- /**
63
- * Where the table is persisted. `undefined` means "use the browser default,
64
- * resolved on first use"; null means "nowhere".
65
- */
66
20
  let storage;
67
- /**
68
- * Resolves the browser's `localStorage` without letting a failure escape.
69
- *
70
- * Touching the global can throw (private windows, Node 22 without a storage
71
- * file) or print a warning, so nothing calls this at import time.
72
- */
73
21
  const browserStorage = () => {
74
22
  try {
75
23
  return typeof globalThis.localStorage === 'undefined'
@@ -80,14 +28,12 @@ const browserStorage = () => {
80
28
  return null;
81
29
  }
82
30
  };
83
- /** The persistence target in effect, resolving the default on first call. */
84
31
  const activeStorage = () => {
85
32
  if (storage === undefined) {
86
33
  storage = browserStorage();
87
34
  }
88
35
  return storage;
89
36
  };
90
- /** The live table, or null while it has not been loaded yet. */
91
37
  let value = null;
92
38
  const listeners = new Set();
93
39
  const emit = () => {
@@ -95,17 +41,6 @@ const emit = () => {
95
41
  listener();
96
42
  }
97
43
  };
98
- /**
99
- * Replaces the seed table and the persistence target.
100
- *
101
- * Both fields are optional so a host can override one without restating the
102
- * other. The loaded table is discarded either way, so the next read rebuilds
103
- * from the new seed merged with whatever is stored — which means this is safe to
104
- * call after something has already read, not only at startup.
105
- *
106
- * @param options.seed - the entries to start from when storage holds nothing
107
- * @param options.storage - where the table is persisted, or null for nowhere
108
- */
109
44
  export const configureEndpoints = ({ seed: nextSeed, storage: nextStorage, }) => {
110
45
  if (nextSeed !== undefined) {
111
46
  seed = nextSeed;
@@ -116,14 +51,7 @@ export const configureEndpoints = ({ seed: nextSeed, storage: nextStorage, }) =>
116
51
  value = null;
117
52
  emit();
118
53
  };
119
- /** Serializes one chain's list the way it is persisted: `chainId,url,url`. */
120
54
  export const key = (chain, list) => `${chain},${list.join(',')}`;
121
- /**
122
- * Parses a stored blob back into a table, merged onto the seeded entries.
123
- *
124
- * @param entry - the persisted blob, or null when nothing is stored
125
- * @returns the merged table, or null when there was nothing to parse
126
- */
127
55
  export const parse = (entry) => {
128
56
  if (!entry) {
129
57
  return null;
@@ -134,13 +62,6 @@ export const parse = (entry) => {
134
62
  });
135
63
  return new Map([...seed, ...stored]);
136
64
  };
137
- /**
138
- * Reads the persisted table, or null when there is nothing usable to read.
139
- *
140
- * Any failure falls back to the seeded defaults rather than propagating: a
141
- * corrupt or unreadable entry must not be the reason the application cannot
142
- * reach a chain at all.
143
- */
144
65
  const readStored = () => {
145
66
  const target = activeStorage();
146
67
  if (!target) {
@@ -154,41 +75,22 @@ const readStored = () => {
154
75
  return null;
155
76
  }
156
77
  };
157
- /** The table, loaded from storage the first time it is asked for. */
158
78
  const loaded = () => {
159
79
  if (!value) {
160
80
  value = readStored() ?? new Map(seed);
161
81
  }
162
82
  return value;
163
83
  };
164
- /** Serializes every entry, one chain per line. */
165
84
  export const stringify = () => [...loaded().entries()].map(([chainId, list]) => key(chainId, list)).join('\n');
166
- /**
167
- * Subscribes to changes in the endpoint table.
168
- *
169
- * @param listener - called after any mutation
170
- * @returns the unsubscribe function
171
- */
172
85
  export const subscribe = (listener) => {
173
86
  listeners.add(listener);
174
87
  return () => {
175
88
  listeners.delete(listener);
176
89
  };
177
90
  };
178
- /**
179
- * The current table.
180
- *
181
- * The identity is stable until something changes it — every mutating accessor
182
- * replaces the map rather than mutating in place — which is what lets a caller
183
- * use this directly as a `useSyncExternalStore` snapshot without re-rendering
184
- * forever.
185
- */
186
91
  export const getSnapshot = () => loaded();
187
- /** The url list for a chain, or null when it has no entry. */
188
92
  export const get = (chain) => loaded().get(chain) ?? null;
189
- /** All `[chainId, urls]` entries as a fresh array. */
190
93
  export const entries = () => [...loaded().entries()];
191
- /** Replaces a chain's url list, persists it, and notifies subscribers. */
192
94
  export const setEndpoints = (chain, list) => {
193
95
  const next = new Map(loaded());
194
96
  next.set(chain, list);
@@ -196,7 +98,6 @@ export const setEndpoints = (chain, list) => {
196
98
  activeStorage()?.setItem(ENDPOINTS_STORAGE_KEY, stringify());
197
99
  emit();
198
100
  };
199
- /** Replaces the url at index `i`; a no-op when the value is unchanged. */
200
101
  export const update = (chain, i, url) => {
201
102
  const list = get(chain) ?? [];
202
103
  if (list[i] === url) {
@@ -206,22 +107,15 @@ export const update = (chain, i, url) => {
206
107
  next[i] = url;
207
108
  setEndpoints(chain, next);
208
109
  };
209
- /** Removes the url at index `i`. */
210
110
  export const remove = (chain, i) => {
211
111
  const next = (get(chain) ?? []).slice(0);
212
112
  next.splice(i, 1);
213
113
  setEndpoints(chain, next);
214
114
  };
215
- /**
216
- * Applies `addition` to a chain's url list and stores the result, keeping only
217
- * the first item if it is empty (the leading "add new" slot) and dropping every
218
- * other empty. A no-op when the filtered list is unchanged.
219
- */
220
115
  export const add = (chain, addition = (urls) => [''].concat(urls)) => {
221
116
  const before = get(chain) ?? [];
222
117
  const list = addition(before.slice(0));
223
118
  const filteredList = list.filter((val, i) => {
224
- // only the first item in the list may be empty
225
119
  if (!val && !i) {
226
120
  return true;
227
121
  }
@@ -233,14 +127,12 @@ export const add = (chain, addition = (urls) => [''].concat(urls)) => {
233
127
  }
234
128
  setEndpoints(chain, filteredList);
235
129
  };
236
- /** Whether `list` already contains the chain's canonical default endpoint. */
237
130
  export const hasDefault = (chain, list) => {
238
131
  const defaultUrl = demoRpcUrlsFor(chain)[0];
239
132
  if (!defaultUrl)
240
133
  return false;
241
134
  return list.includes(defaultUrl);
242
135
  };
243
- /** Appends the chain's canonical default endpoints back onto its list. */
244
136
  export const restoreDefault = (chain) => {
245
137
  add(chain, (l) => l.concat(demoRpcUrlsFor(chain)));
246
138
  };
package/dist/image.js CHANGED
@@ -2,62 +2,7 @@ import * as imageLinks from '@gibs/bridge-sdk/image-links';
2
2
  import { ecosystemByChainId, relayChainIds } from '@gibs/bridge-sdk/relay-quote';
3
3
  import { zeroAddress } from 'viem';
4
4
  import { chainTable } from './chain-table.js';
5
- /**
6
- * Chain-logo resolution, in one place.
7
- *
8
- * gib.show serves almost every chain a route can start from — including the
9
- * non-Ethereum ones — but it keys them by a CAIP-2 style identifier rather than
10
- * by Relay's invented decimal ids, so the two have to be mapped. Only Eclipse
11
- * and Hyperliquid are genuinely absent from it, and those alone fall back to
12
- * Relay's own icon service.
13
- *
14
- * This is a module rather than an inline template string because getting the
15
- * key wrong is invisible: a 404 in an `<img src>` is drawn by the browser as a
16
- * broken-image glyph, so the mistake surfaces as a blemish on a token pill
17
- * rather than as an error anyone would go looking for.
18
- */
19
- /**
20
- * Relay's public chain-icon service, keyed on Relay's own chain identifiers —
21
- * the authority for the ids it invented.
22
- *
23
- * The `light` variant is the full-colour brand mark (the pair is light/dark
24
- * BACKGROUND, not a monochrome inversion), so one variant serves both of our
25
- * themes. Not every id is populated — The Open Network answers 403, which is
26
- * what the bundled asset used to exist for — so callers must still tolerate a
27
- * badge that fails to load. Only Eclipse and Hyperliquid reach this now.
28
- *
29
- * @param chainId - a Relay chain identifier
30
- * @returns the icon url
31
- */
32
5
  export const relayChainIconUrl = (chainId) => `https://assets.relay.link/icons/${chainId}/light.png`;
33
- /**
34
- * gib.show's own key for a chain that Relay reaches us under a pseudo id.
35
- *
36
- * THE SERVICE WAS NEVER MISSING THESE CHAINS — WE WERE ASKING BY THE WRONG KEY.
37
- * gib.show keys every chain by a CAIP-2 style identifier: `eip155-1`,
38
- * `solana-501`, `bip122-0`. Relay invented its own decimal ids for the
39
- * non-Ethereum origins (Solana is 792703809, Bitcoin 8253038), and asking
40
- * gib.show for one of those 404s — which read as "gib.show does not serve
41
- * Solana" and sent every non-Ethereum badge to a second service. It serves them
42
- * all, under its own key.
43
- *
44
- * Each entry below was verified twice: the url returns 200 with a real PNG, and
45
- * the picture was LOOKED AT and is the chain's genuine mark. A 200 alone proves
46
- * nothing here — `/image/999` returns 200 and hands back a completely different
47
- * chain's logo, which is why an identifier is only listed once someone has seen
48
- * what comes back.
49
- *
50
- * ECLIPSE AND HYPERLIQUID ARE ABSENT ON PURPOSE. Neither appears anywhere in
51
- * gib.show's network list under any key, so they keep Relay's service. Guessing
52
- * a plausible identifier for them would be worse than falling back: Hyperliquid
53
- * in particular collides with an unrelated chain at id 999, so a guess renders
54
- * the wrong company's logo rather than none at all.
55
- *
56
- * The pay-by-hand chains (Zcash and, later, its siblings) are NOT listed by
57
- * hand here. `bridge-sdk`'s `image-links.ts` is the one place that key is
58
- * declared — see its `pseudoChainGibShowIdentifier` — and this table spreads
59
- * that map in, so a pay-by-hand chain added there needs no matching edit here.
60
- */
61
6
  const gibShowChainIdentifier = {
62
7
  [relayChainIds.solana]: 'solana-501',
63
8
  [relayChainIds.bitcoin]: 'bip122-0',
@@ -65,29 +10,9 @@ const gibShowChainIdentifier = {
65
10
  [relayChainIds.ton]: 'ton-607',
66
11
  ...imageLinks.pseudoChainGibShowIdentifier,
67
12
  };
68
- /**
69
- * Resolves the logo url for a chain, choosing the service that can serve it.
70
- *
71
- * A chain gib.show knows under its own identifier wins outright
72
- * ({@link gibShowChainIdentifier}). Otherwise an Ethereum-Virtual-Machine chain
73
- * goes to gib.show by number, honouring the configured image root so a
74
- * self-hosted deployment is respected rather than hard-coding the public host.
75
- * What remains is Eclipse and Hyperliquid, which gib.show does not carry, and
76
- * they go to Relay.
77
- *
78
- * @param chainId - the decimal chain id, Relay's pseudo identifiers included
79
- * @param displayPx - the CSS pixel size the logo renders at; when supplied,
80
- * gib.show is asked for twice that in webp per its retina guidance. Ignored
81
- * for a Relay-served icon and for an identifier-keyed mark, neither of which
82
- * takes sizing parameters.
83
- * @returns an image url for the chain
84
- */
85
13
  export const chainLogoUrl = ({ chainId, displayPx, }) => {
86
14
  const identifier = gibShowChainIdentifier[chainId];
87
15
  if (identifier !== undefined) {
88
- // gib.show serves this chain, just not under Relay's number. No sizing
89
- // parameters: these are fixed-size marks rather than the resizable images
90
- // the numeric endpoint serves.
91
16
  return `${imageLinks.getImageRoot()}/image/${identifier}`;
92
17
  }
93
18
  if (ecosystemByChainId(chainId) !== 'evm') {
@@ -99,44 +24,8 @@ export const chainLogoUrl = ({ chainId, displayPx, }) => {
99
24
  }
100
25
  return `${base}?w=${displayPx * 2}&h=${displayPx * 2}&format=webp`;
101
26
  };
102
- /**
103
- * The chain a native coin actually belongs to, found by its symbol.
104
- *
105
- * WHY THIS IS NEEDED. gib.show serves a coin's own picture under
106
- * `<chain>/<zero address>`, and it carries that picture only for the chain the
107
- * coin is native to — not for every chain the coin has been bridged onto. Ether
108
- * on Base is the case that exposed it: `8453/<zero address>` is a 404, so the
109
- * lookup fell through to the chain mark and drew Base's blue disc where the
110
- * ether diamond belongs. The same picture is one source away under `1/<zero
111
- * address>`, and gib.show's multi-source lookup will cross chains to reach it.
112
- *
113
- * READ FROM THE TABLE, NOT FROM A LIST WRITTEN HERE. The generated chain table
114
- * orders the bridge's own chains first and the rest in the order the product
115
- * chose, so the EARLIEST entry carrying a symbol is the chain that coin came
116
- * from: ether resolves to Ethereum ahead of the dozen chains that also call
117
- * their coin ETH, and the mapping stays right as the table grows. The table is
118
- * reversed before the map is built because a later `set` wins — reversing makes
119
- * the earliest entry the last one written.
120
- *
121
- * A coin whose home chain has no dedicated picture — Binance Coin, Polygon
122
- * Ecosystem Token, Avalanche — loses nothing. Their home chain IS the chain
123
- * they are asked about, so the list collapses to what it already was, and for
124
- * a chain's own coin the chain mark is the coin mark.
125
- */
126
27
  const homeChainOfSymbol = new Map([...chainTable].reverse().map((entry) => [entry.nativeCurrency.symbol, entry.id]));
127
- /** Every chain's own native symbol, for resolving a coin from its chain alone. */
128
28
  const symbolOfChain = new Map(chainTable.map((entry) => [entry.id, entry.nativeCurrency.symbol]));
129
- /**
130
- * The gib.show sources for a chain's NATIVE COIN, best first.
131
- *
132
- * The service serves the first source that resolves: the coin as this chain
133
- * keys it, then the coin as its home chain keys it, then the chain's own mark.
134
- * The last is an honest fallback rather than a mistake — for a chain's own coin
135
- * the two marks are the same picture.
136
- *
137
- * @param chainId - the chain the coin is being shown on
138
- * @returns the ordered source list, for {@link imageLinks.images}
139
- */
140
29
  export const nativeCoinImageSources = (chainId) => {
141
30
  const sources = [`${chainId}/${zeroAddress}`];
142
31
  const symbol = symbolOfChain.get(chainId);
@@ -147,10 +36,4 @@ export const nativeCoinImageSources = (chainId) => {
147
36
  sources.push(`${chainId}/`);
148
37
  return sources;
149
38
  };
150
- /**
151
- * Resolves the picture of a chain's native coin, as opposed to of the chain.
152
- *
153
- * @param chainId - the chain the coin is being shown on
154
- * @returns an image url
155
- */
156
39
  export const nativeCoinLogoUrl = (chainId) => imageLinks.images(nativeCoinImageSources(chainId));
package/dist/index.js CHANGED
@@ -1,18 +1,3 @@
1
- /**
2
- * Browser-side machinery for talking to the chains the bridge supports.
3
- *
4
- * Everything here is framework-free. It is the layer between `@gibs/bridge-sdk`,
5
- * which knows about pathways, quotes and fees but performs no input or output,
6
- * and an application, which renders. Endpoint resolution, public clients, chain
7
- * reads, token metadata, caching and logo resolution all live here so that a
8
- * second application does not have to rebuild them — and so that the rules they
9
- * encode (which node answers for a chain, which image service knows a chain's
10
- * mark) have exactly one home.
11
- *
12
- * Subpath exports are the intended way in — `@gibs/bridge-client/client`,
13
- * `/endpoints`, `/image` and so on — so that importing one does not pull the
14
- * rest. This barrel exists for consumers that would rather not think about it.
15
- */
16
1
  export { Cache } from './cache.js';
17
2
  export { checkAllowance } from './chain-read.js';
18
3
  export { clientFromChain, rpcBatchSetting } from './client.js';
package/dist/networks.js CHANGED
@@ -1,24 +1,5 @@
1
1
  import { registerChainNames } from '@gibs/bridge-sdk/chain-names';
2
2
  import { chainTable } from './chain-table.js';
3
- /**
4
- * Turns one row of the generated table into the shape viem wants.
5
- *
6
- * NAMED VIEM IMPORTS USED TO LIVE HERE — 37 of them, each pulling that chain's
7
- * whole definition including fee logic, formatters and contract addresses this
8
- * application never reads. That cost about 3.7 kB compressed for a third of the
9
- * chains the aggregators quote; naming all 73 would have cost about 7.3 kB
10
- * against roughly 7.8 kB of headroom under the first-load budget. The generated
11
- * table carries the same 73 in 2.6 kB, so the whole supported set now weighs
12
- * less than the third of it that shipped before.
13
- *
14
- * A namespace import is still never used, for the reason the old comment here
15
- * gave: `Object.values(chains)` defeats tree-shaking and emitted all ~674
16
- * definitions, measured at 255 kB. The generator does that read in Node, at
17
- * build time, where it costs a reader nothing.
18
- *
19
- * `blockExplorers` is omitted rather than faked when a chain has no explorer —
20
- * viem treats the field as optional and an empty url would render a dead link.
21
- */
22
3
  const toViemChain = (entry) => ({
23
4
  id: entry.id,
24
5
  name: entry.name,
@@ -27,53 +8,11 @@ const toViemChain = (entry) => ({
27
8
  ...(entry.explorer === null
28
9
  ? {}
29
10
  : { blockExplorers: { default: { name: 'Explorer', url: entry.explorer } } }),
30
- // WITHOUT THIS, NOTHING CAN BATCH A READ. viem's `multicall` action reads the
31
- // Multicall3 address off the chain and throws `ChainDoesNotSupportContract`
32
- // when it is absent, so every chain built here refused — including
33
- // PulseChain, whose token-metadata fallback failed with exactly that.
34
- //
35
- // It went missing when this table replaced viem's own chain objects to keep
36
- // 255 kB of unused per-chain data out of the build. The table kept the four
37
- // fields a TRANSPORT needs and dropped the one field an ACTION needs.
38
- //
39
- // Read per chain, never assumed: the canonical `0xcA11bde0…` covers most, but
40
- // there are 8 distinct deployments across the chains listed here.
41
11
  ...(entry.multicall3 === null
42
12
  ? {}
43
13
  : { contracts: { multicall3: { address: entry.multicall3 } } }),
44
14
  });
45
- /**
46
- * Every Ethereum-Virtual-Machine chain this client can build a public client for.
47
- *
48
- * The leading entries are the bridge's own chains; the rest are the routing
49
- * destinations reachable through the aggregators. Ordering is preserved because
50
- * chain pickers render it directly.
51
- *
52
- * Solana and Bitcoin are deliberately absent. They are supported ORIGINS, but
53
- * they are not Ethereum-style chains: they are keyed by a genesis hash and by
54
- * their own string rather than by a number, and nothing here can build a client
55
- * for them. Anything wallet-facing that needs them keys off the aggregator's
56
- * chain identifiers instead.
57
- */
58
15
  export const evmChains = chainTable.map(toViemChain);
59
- /**
60
- * Lookup from a chain id to its viem chain definition, for supported chains only.
61
- *
62
- * Keyed as `string | number` because callers hold ids that have been through a
63
- * url, a wallet, or a quote response, and a lookup that silently misses on a
64
- * numeric-versus-string mismatch reads as "chain not supported".
65
- */
66
16
  export const evmChainsById = new Map(evmChains.map((chain) => [chain.id, chain]));
67
- // Hand these names to the SDK's display-name table so ONE function answers
68
- // "what is this chain called" everywhere. Done at module load, before anything
69
- // renders, because a name that arrives late is a name that changes under the
70
- // reader — which is precisely the flicker this removes. The SDK's own entries
71
- // win on conflict, so the product's chosen spellings are not overwritten.
72
17
  registerChainNames(evmChains.map((chain) => [chain.id, chain.name]));
73
- /**
74
- * Whether this client can reach a chain over Ethereum-style remote procedure calls.
75
- *
76
- * @param chainId - the chain id to test
77
- * @returns true when the chain has a definition here
78
- */
79
18
  export const isSupportedEvmChain = (chainId) => evmChainsById.has(chainId);
@@ -1,4 +1,4 @@
1
- import type { TokenMetadata } from '@gibs/bridge-sdk/types';
1
+ import type { Token, TokenMetadata } from '@gibs/bridge-sdk/types';
2
2
  import type { Hex } from 'viem';
3
3
  export type ChainScopedAddress = `${number}:${Hex}`;
4
4
  export declare function getStoreKey(chainId: number, tokenAddress: string): ChainScopedAddress;
@@ -19,3 +19,50 @@ export declare function getStoreSize(): number;
19
19
  export declare function getTokenAddressFromBridge(bridge: any): string;
20
20
  export declare function getTokenChainIdFromBridge(bridge: any): number;
21
21
  export declare function getBridgeTokenMetadata(bridge: any): TokenMetadata | null;
22
+ /** What {@link readDecimalsOnChain} needs to find one token. */
23
+ export type DecimalsReadTarget = {
24
+ chainId: number;
25
+ address: Hex;
26
+ };
27
+ /** Asks a chain how many decimals one token has. See {@link readDecimalsOnChain}. */
28
+ export type ReadDecimals = (target: DecimalsReadTarget) => Promise<number>;
29
+ /**
30
+ * Reads `decimals()` from the token contract and keeps the answer for the session.
31
+ *
32
+ * A failed read is forgotten at once, so the next caller asks again. An answer
33
+ * that is not a whole number from 0 to 255 is a failure too: it means the
34
+ * address is not a token.
35
+ *
36
+ * @param target - the chain and token address to read
37
+ * @returns the decimals the contract reports
38
+ */
39
+ export declare const readDecimalsOnChain: ReadDecimals;
40
+ /**
41
+ * Whether the chain, not a list, is the authority for this token's decimals.
42
+ *
43
+ * Three kinds of token have no chain read: a native coin (its decimals are
44
+ * chain configuration and are already known), a token on a chain this client
45
+ * cannot build a client for (Solana, Bitcoin), and an address that is not an
46
+ * Ethereum-style address. Those keep the decimals they carry.
47
+ *
48
+ * @param token - the token to test
49
+ * @returns true when `decimals()` must be read from the contract
50
+ */
51
+ export declare const decimalsComeFromChain: (token: Pick<Token, "chainId" | "address">) => boolean;
52
+ /**
53
+ * Returns the token with the decimals the chain reports.
54
+ *
55
+ * WHY THIS EXISTS. A token list is a claim about a contract, and some lists
56
+ * carry the wrong `decimals` (gib.show listed ALI with 8 where the contract
57
+ * says 18). Every amount the reader types, every balance and every dollar
58
+ * figure is scaled by that number. This asks the contract, once, for the ONE
59
+ * token the reader selected. It never walks a whole list.
60
+ *
61
+ * A failed read rejects. It does NOT fall back to the list value: a number the
62
+ * list may have wrong is worse than no number.
63
+ *
64
+ * @param token - the token, with whatever decimals its source carried
65
+ * @param readDecimals - how to ask the chain; defaults to the session-cached read
66
+ * @returns the same token carrying the chain's decimals
67
+ */
68
+ export declare const withChainDecimals: <TokenShape extends Pick<Token, "chainId" | "address" | "decimals">>(token: TokenShape, readDecimals?: ReadDecimals) => Promise<TokenShape>;