@silentswap/sdk 0.1.2 → 0.1.4

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/chain.d.ts CHANGED
@@ -6,9 +6,13 @@ import type { Connector } from 'wagmi';
6
6
  * @param chainId - The target chain ID
7
7
  * @param walletClient - The wallet client
8
8
  * @param connector - The wagmi connector for chain switching
9
+ * @param options.required - If false, a failed chain switch won't throw (for signing-only
10
+ * operations where the chain doesn't need to match). Defaults to true.
9
11
  * @returns The wallet client (after ensuring correct chain)
10
12
  */
11
- export declare function ensureChain(chainId: number, walletClient: WalletClient, connector: Connector): Promise<WalletClient>;
13
+ export declare function ensureChain(chainId: number, walletClient: WalletClient, connector: Connector, options?: {
14
+ required?: boolean;
15
+ }): Promise<WalletClient>;
12
16
  /**
13
17
  * Create a public client with RPC fallback configuration
14
18
  *
package/dist/chain.js CHANGED
@@ -1,4 +1,4 @@
1
- import { createPublicClient, http, fallback } from 'viem';
1
+ import { createPublicClient, createWalletClient, custom, http, fallback } from 'viem';
2
2
  import { getChainById } from './chains.js';
3
3
  import { H_RPCS } from './rpc.js';
4
4
  /**
@@ -7,9 +7,12 @@ import { H_RPCS } from './rpc.js';
7
7
  * @param chainId - The target chain ID
8
8
  * @param walletClient - The wallet client
9
9
  * @param connector - The wagmi connector for chain switching
10
+ * @param options.required - If false, a failed chain switch won't throw (for signing-only
11
+ * operations where the chain doesn't need to match). Defaults to true.
10
12
  * @returns The wallet client (after ensuring correct chain)
11
13
  */
12
- export async function ensureChain(chainId, walletClient, connector) {
14
+ export async function ensureChain(chainId, walletClient, connector, options) {
15
+ const required = options?.required ?? true;
13
16
  // Check current chain
14
17
  let currentChainId;
15
18
  try {
@@ -25,16 +28,26 @@ export async function ensureChain(chainId, walletClient, connector) {
25
28
  await connector.switchChain?.({ chainId });
26
29
  }
27
30
  catch (switchError) {
28
- // If switch fails, try to add the chain
29
- try {
30
- // This would need viem chain definitions
31
- // For now, we'll throw the original error
32
- throw switchError;
33
- }
34
- catch {
31
+ if (required) {
35
32
  throw new Error(`Failed to switch to chain ${chainId}`);
36
33
  }
34
+ // Non-required: chain switch failed (e.g. Trust Wallet via WalletConnect
35
+ // doesn't support programmatic chain switching). Return the original client
36
+ // — signTypedData works regardless of the active chain since chainId is
37
+ // part of the signed EIP-712 data, not a prerequisite for signing.
38
+ return walletClient;
37
39
  }
40
+ // After chain switch, create a fresh walletClient bound to the new chain.
41
+ // Critical for WalletConnect: the old walletClient's chain scope (e.g. eip155:43114)
42
+ // doesn't match the new active chain, so signing requests get silently dropped
43
+ // by the mobile wallet.
44
+ const provider = await connector.getProvider();
45
+ const chain = getChainById(chainId);
46
+ return createWalletClient({
47
+ account: walletClient.account,
48
+ chain,
49
+ transport: custom(provider),
50
+ });
38
51
  }
39
52
  return walletClient;
40
53
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@silentswap/sdk",
3
3
  "type": "module",
4
- "version": "0.1.2",
4
+ "version": "0.1.4",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.js",
7
7
  "files": [