@gibs/bridge-client 1.20.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.
@@ -0,0 +1,6 @@
1
+ {
2
+ "main": "../dist/cjs/crossing-simulator.js",
3
+ "module": "../dist/crossing-simulator.js",
4
+ "types": "../dist/crossing-simulator.d.ts",
5
+ "react-native": "../dist/cjs/crossing-simulator.js"
6
+ }
@@ -0,0 +1,32 @@
1
+ import { createCrossingSimulator } from '@gibs/bridge-sdk/crossing-simulation';
2
+ import type { PublicClient } from 'viem';
3
+ /**
4
+ * The node that answers a crossing simulation, by chain id.
5
+ *
6
+ * WHY NOT THE CHAIN'S NORMAL NODES. A simulation needs `eth_simulateV1`, and
7
+ * the nodes this application reads with are chosen for plain reads. A node that
8
+ * lacks the method answers an error, and the simulator then reports
9
+ * "unavailable": no tax check, and no first crossing. A node that is known to
10
+ * serve the method keeps the check working. `ethereum-rpc.publicnode.com` was
11
+ * measured to serve it on 2026-10-21.
12
+ *
13
+ * Only Ethereum is listed because only the Ethereum entry can make a first
14
+ * crossing. A chain with no entry here has no simulation.
15
+ */
16
+ export declare const simulationEndpoints: Readonly<Record<number, string>>;
17
+ /**
18
+ * A client on the simulation node for one chain, built once.
19
+ *
20
+ * @param chainId - the chain whose entry mediator is simulated
21
+ * @returns the client, or null when no simulation node is listed for the chain
22
+ */
23
+ export declare const simulationClientOrNull: (chainId: number) => PublicClient | null;
24
+ /**
25
+ * The one simulator the whole interface shares. The route search and the
26
+ * warning beside the Output ask the same question for the same token and
27
+ * amount. A shared cache answers the second asker without a second request,
28
+ * and the shared pacing keeps the node from being asked in a burst.
29
+ *
30
+ * @returns the shared simulator
31
+ */
32
+ export declare const sharedCrossingSimulator: () => ReturnType<typeof createCrossingSimulator>;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.sharedCrossingSimulator = exports.simulationClientOrNull = exports.simulationEndpoints = void 0;
4
+ const crossing_simulation_1 = require("@gibs/bridge-sdk/crossing-simulation");
5
+ const client_1 = require("@gibs/common/client");
6
+ const client_js_1 = require("./client.js");
7
+ const networks_js_1 = require("./networks.js");
8
+ exports.simulationEndpoints = {
9
+ 1: 'https://ethereum-rpc.publicnode.com',
10
+ };
11
+ const simulationClients = new Map();
12
+ const simulationClientOrNull = (chainId) => {
13
+ const cached = simulationClients.get(chainId);
14
+ if (cached !== undefined)
15
+ return cached;
16
+ const url = exports.simulationEndpoints[chainId];
17
+ const chain = networks_js_1.evmChainsById.get(chainId);
18
+ const client = url === undefined || chain === undefined ? null : (0, client_1.clientFromChain)({ chain, urls: [url], batch: (0, client_js_1.rpcBatchSetting)(chainId) });
19
+ simulationClients.set(chainId, client);
20
+ return client;
21
+ };
22
+ exports.simulationClientOrNull = simulationClientOrNull;
23
+ let sharedSimulator;
24
+ const sharedCrossingSimulator = () => {
25
+ sharedSimulator ??= (0, crossing_simulation_1.createCrossingSimulator)({ clientForChain: exports.simulationClientOrNull });
26
+ return sharedSimulator;
27
+ };
28
+ exports.sharedCrossingSimulator = sharedCrossingSimulator;
@@ -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>;
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.withChainDecimals = exports.decimalsComeFromChain = exports.readDecimalsOnChain = void 0;
3
4
  exports.getStoreKey = getStoreKey;
4
5
  exports.parseChainScopedAddress = parseChainScopedAddress;
5
6
  exports.parseTokenKey = parseTokenKey;
@@ -11,6 +12,7 @@ exports.getTokenChainIdFromBridge = getTokenChainIdFromBridge;
11
12
  exports.getBridgeTokenMetadata = getBridgeTokenMetadata;
12
13
  const config_1 = require("@gibs/bridge-sdk/config");
13
14
  const ecosystems_1 = require("@gibs/bridge-sdk/ecosystems");
15
+ const erc20_1 = require("@gibs/common/erc20");
14
16
  const viem_1 = require("viem");
15
17
  const client_js_1 = require("./client.js");
16
18
  const networks_js_1 = require("./networks.js");
@@ -153,8 +155,8 @@ async function batchFetchTokenMetadata(chainId, tokenAddresses) {
153
155
  const symbol = batch[0];
154
156
  const name = batch[1];
155
157
  const decimals = Number(batch[2]);
156
- const symbolString = (0, viem_1.hexToString)(symbol);
157
- const nameString = (0, viem_1.hexToString)(name);
158
+ const symbolString = (0, erc20_1.decodeBytes32Text)({ value: symbol, fallback: (0, erc20_1.shortAddressLabel)(tokenAddress) });
159
+ const nameString = (0, erc20_1.decodeBytes32Text)({ value: name, fallback: (0, erc20_1.shortAddressLabel)(tokenAddress) });
158
160
  console.log(`Fallback token ${tokenAddress} results:`, {
159
161
  symbolString,
160
162
  nameString,
@@ -226,3 +228,34 @@ function getBridgeTokenMetadata(bridge) {
226
228
  return null;
227
229
  return getTokenMetadata(chainId, address);
228
230
  }
231
+ const decimalsReads = new Map();
232
+ const readDecimalsOnChain = ({ chainId, address }) => {
233
+ const key = getStoreKey(chainId, address);
234
+ const known = decimalsReads.get(key);
235
+ if (known)
236
+ return known;
237
+ const pending = (0, client_js_1.clientFromChain)(chainId)
238
+ .readContract({ address, abi: viem_1.erc20Abi, functionName: 'decimals' })
239
+ .then((answer) => {
240
+ const decimals = Number(answer);
241
+ if (!Number.isInteger(decimals) || decimals < 0 || decimals > 255) {
242
+ throw new Error(`Token ${address} on chain ${chainId} reported decimals ${String(answer)}`);
243
+ }
244
+ return decimals;
245
+ });
246
+ decimalsReads.set(key, pending);
247
+ pending.catch(() => decimalsReads.delete(key));
248
+ return pending;
249
+ };
250
+ exports.readDecimalsOnChain = readDecimalsOnChain;
251
+ const decimalsComeFromChain = (token) => networks_js_1.evmChainsById.has(token.chainId) &&
252
+ (0, viem_1.isAddress)(token.address, { strict: false }) &&
253
+ !(0, ecosystems_1.isNativeAsset)(token.address, 'evm');
254
+ exports.decimalsComeFromChain = decimalsComeFromChain;
255
+ const withChainDecimals = async (token, readDecimals = exports.readDecimalsOnChain) => {
256
+ if (!(0, exports.decimalsComeFromChain)(token))
257
+ return token;
258
+ const decimals = await readDecimals({ chainId: token.chainId, address: token.address });
259
+ return decimals === token.decimals ? token : { ...token, decimals };
260
+ };
261
+ exports.withChainDecimals = withChainDecimals;
@@ -0,0 +1,32 @@
1
+ import { createCrossingSimulator } from '@gibs/bridge-sdk/crossing-simulation';
2
+ import type { PublicClient } from 'viem';
3
+ /**
4
+ * The node that answers a crossing simulation, by chain id.
5
+ *
6
+ * WHY NOT THE CHAIN'S NORMAL NODES. A simulation needs `eth_simulateV1`, and
7
+ * the nodes this application reads with are chosen for plain reads. A node that
8
+ * lacks the method answers an error, and the simulator then reports
9
+ * "unavailable": no tax check, and no first crossing. A node that is known to
10
+ * serve the method keeps the check working. `ethereum-rpc.publicnode.com` was
11
+ * measured to serve it on 2026-10-21.
12
+ *
13
+ * Only Ethereum is listed because only the Ethereum entry can make a first
14
+ * crossing. A chain with no entry here has no simulation.
15
+ */
16
+ export declare const simulationEndpoints: Readonly<Record<number, string>>;
17
+ /**
18
+ * A client on the simulation node for one chain, built once.
19
+ *
20
+ * @param chainId - the chain whose entry mediator is simulated
21
+ * @returns the client, or null when no simulation node is listed for the chain
22
+ */
23
+ export declare const simulationClientOrNull: (chainId: number) => PublicClient | null;
24
+ /**
25
+ * The one simulator the whole interface shares. The route search and the
26
+ * warning beside the Output ask the same question for the same token and
27
+ * amount. A shared cache answers the second asker without a second request,
28
+ * and the shared pacing keeps the node from being asked in a burst.
29
+ *
30
+ * @returns the shared simulator
31
+ */
32
+ export declare const sharedCrossingSimulator: () => ReturnType<typeof createCrossingSimulator>;
@@ -0,0 +1,23 @@
1
+ import { createCrossingSimulator } from '@gibs/bridge-sdk/crossing-simulation';
2
+ import { clientFromChain as buildClient } from '@gibs/common/client';
3
+ import { rpcBatchSetting } from './client.js';
4
+ import { evmChainsById } from './networks.js';
5
+ export const simulationEndpoints = {
6
+ 1: 'https://ethereum-rpc.publicnode.com',
7
+ };
8
+ const simulationClients = new Map();
9
+ export const simulationClientOrNull = (chainId) => {
10
+ const cached = simulationClients.get(chainId);
11
+ if (cached !== undefined)
12
+ return cached;
13
+ const url = simulationEndpoints[chainId];
14
+ const chain = evmChainsById.get(chainId);
15
+ const client = url === undefined || chain === undefined ? null : buildClient({ chain, urls: [url], batch: rpcBatchSetting(chainId) });
16
+ simulationClients.set(chainId, client);
17
+ return client;
18
+ };
19
+ let sharedSimulator;
20
+ export const sharedCrossingSimulator = () => {
21
+ sharedSimulator ??= createCrossingSimulator({ clientForChain: simulationClientOrNull });
22
+ return sharedSimulator;
23
+ };
@@ -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>;
@@ -1,6 +1,7 @@
1
1
  import { nativeTokenName, nativeTokenSymbol, toChain } from '@gibs/bridge-sdk/config';
2
2
  import { isNativeAsset } from '@gibs/bridge-sdk/ecosystems';
3
- import { erc20Abi, erc20Abi_bytes32, hexToString } from 'viem';
3
+ import { decodeBytes32Text, shortAddressLabel } from '@gibs/common/erc20';
4
+ import { erc20Abi, erc20Abi_bytes32, isAddress } from 'viem';
4
5
  import { clientFromChain } from './client.js';
5
6
  import { evmChainsById } from './networks.js';
6
7
  const TOKENS_PER_MULTICALL = 100;
@@ -142,8 +143,8 @@ async function batchFetchTokenMetadata(chainId, tokenAddresses) {
142
143
  const symbol = batch[0];
143
144
  const name = batch[1];
144
145
  const decimals = Number(batch[2]);
145
- const symbolString = hexToString(symbol);
146
- const nameString = hexToString(name);
146
+ const symbolString = decodeBytes32Text({ value: symbol, fallback: shortAddressLabel(tokenAddress) });
147
+ const nameString = decodeBytes32Text({ value: name, fallback: shortAddressLabel(tokenAddress) });
147
148
  console.log(`Fallback token ${tokenAddress} results:`, {
148
149
  symbolString,
149
150
  nameString,
@@ -215,3 +216,31 @@ export function getBridgeTokenMetadata(bridge) {
215
216
  return null;
216
217
  return getTokenMetadata(chainId, address);
217
218
  }
219
+ const decimalsReads = new Map();
220
+ export const readDecimalsOnChain = ({ chainId, address }) => {
221
+ const key = getStoreKey(chainId, address);
222
+ const known = decimalsReads.get(key);
223
+ if (known)
224
+ return known;
225
+ const pending = clientFromChain(chainId)
226
+ .readContract({ address, abi: erc20Abi, functionName: 'decimals' })
227
+ .then((answer) => {
228
+ const decimals = Number(answer);
229
+ if (!Number.isInteger(decimals) || decimals < 0 || decimals > 255) {
230
+ throw new Error(`Token ${address} on chain ${chainId} reported decimals ${String(answer)}`);
231
+ }
232
+ return decimals;
233
+ });
234
+ decimalsReads.set(key, pending);
235
+ pending.catch(() => decimalsReads.delete(key));
236
+ return pending;
237
+ };
238
+ export const decimalsComeFromChain = (token) => evmChainsById.has(token.chainId) &&
239
+ isAddress(token.address, { strict: false }) &&
240
+ !isNativeAsset(token.address, 'evm');
241
+ export const withChainDecimals = async (token, readDecimals = readDecimalsOnChain) => {
242
+ if (!decimalsComeFromChain(token))
243
+ return token;
244
+ const decimals = await readDecimals({ chainId: token.chainId, address: token.address });
245
+ return decimals === token.decimals ? token : { ...token, decimals };
246
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gibs/bridge-client",
3
- "version": "1.20.0",
3
+ "version": "1.26.0",
4
4
  "description": "Browser-side endpoints, clients, and caching for a Gibs Finance bridge interface.",
5
5
  "keywords": [
6
6
  "ethereum",
@@ -84,6 +84,33 @@
84
84
  "types": "./dist/cache.d.ts",
85
85
  "default": "./dist/cache.js"
86
86
  },
87
+ "./crossing-simulator": {
88
+ "gibs-source": "./src/crossing-simulator.ts",
89
+ "react-native": {
90
+ "import": {
91
+ "types": "./dist/crossing-simulator.d.ts",
92
+ "default": "./dist/crossing-simulator.js"
93
+ },
94
+ "require": {
95
+ "types": "./dist/cjs/crossing-simulator.d.ts",
96
+ "default": "./dist/cjs/crossing-simulator.js"
97
+ },
98
+ "default": {
99
+ "types": "./dist/cjs/crossing-simulator.d.ts",
100
+ "default": "./dist/cjs/crossing-simulator.js"
101
+ }
102
+ },
103
+ "import": {
104
+ "types": "./dist/crossing-simulator.d.ts",
105
+ "default": "./dist/crossing-simulator.js"
106
+ },
107
+ "require": {
108
+ "types": "./dist/cjs/crossing-simulator.d.ts",
109
+ "default": "./dist/cjs/crossing-simulator.js"
110
+ },
111
+ "types": "./dist/crossing-simulator.d.ts",
112
+ "default": "./dist/crossing-simulator.js"
113
+ },
87
114
  "./image": {
88
115
  "gibs-source": "./src/image.ts",
89
116
  "react-native": {
@@ -256,6 +283,7 @@
256
283
  "LICENSE",
257
284
  "llms.txt",
258
285
  "cache",
286
+ "crossing-simulator",
259
287
  "image",
260
288
  "networks",
261
289
  "endpoints",
@@ -274,8 +302,8 @@
274
302
  "test:watch": "vitest"
275
303
  },
276
304
  "dependencies": {
277
- "@gibs/bridge-sdk": "^1.20.0",
278
- "@gibs/common": "^1.20.0"
305
+ "@gibs/bridge-sdk": "^1.26.0",
306
+ "@gibs/common": "^1.26.0"
279
307
  },
280
308
  "peerDependencies": {
281
309
  "viem": ">=2.35.0 <3"