@silentswap/sdk 0.0.43 → 0.0.45
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 +20 -1
- package/dist/chain.js +73 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/rpc.d.ts +16 -0
- package/dist/rpc.js +37 -0
- package/package.json +1 -1
package/dist/chain.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { WalletClient, SendTransactionParameters, Hex, PublicClient } from 'viem';
|
|
1
|
+
import type { WalletClient, SendTransactionParameters, Hex, PublicClient, Chain } from 'viem';
|
|
2
2
|
import type { Connector } from 'wagmi';
|
|
3
3
|
/**
|
|
4
4
|
* Ensure the correct chain is active
|
|
@@ -9,6 +9,25 @@ import type { Connector } from 'wagmi';
|
|
|
9
9
|
* @returns The wallet client (after ensuring correct chain)
|
|
10
10
|
*/
|
|
11
11
|
export declare function ensureChain(chainId: number, walletClient: WalletClient, connector: Connector): Promise<WalletClient>;
|
|
12
|
+
/**
|
|
13
|
+
* Create a public client with RPC fallback configuration
|
|
14
|
+
*
|
|
15
|
+
* This function creates a viem public client for a given chain ID, using custom RPC URLs
|
|
16
|
+
* from H_RPCS configuration when available. If the custom RPC is unavailable, it falls back
|
|
17
|
+
* to the default RPC from the chain definition and logs an error.
|
|
18
|
+
*
|
|
19
|
+
* @param chainId - The chain ID to create a public client for
|
|
20
|
+
* @param chain - Optional chain definition (will be looked up if not provided)
|
|
21
|
+
* @returns A configured public client for the chain
|
|
22
|
+
* @throws Error if chain is not supported
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```typescript
|
|
26
|
+
* const publicClient = createPublicClientWithRpc(43114); // Avalanche
|
|
27
|
+
* const receipt = await publicClient.waitForTransactionReceipt({ hash });
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
export declare function createPublicClientWithRpc(chainId: number, chain?: Chain): PublicClient;
|
|
12
31
|
/**
|
|
13
32
|
* Wait for transaction confirmation using viem's waitForTransactionReceipt
|
|
14
33
|
*
|
package/dist/chain.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import { createPublicClient, http, fallback } from 'viem';
|
|
2
|
+
import { getChainById } from './chains.js';
|
|
3
|
+
import { H_RPCS } from './rpc.js';
|
|
1
4
|
/**
|
|
2
5
|
* Ensure the correct chain is active
|
|
3
6
|
*
|
|
@@ -35,6 +38,76 @@ export async function ensureChain(chainId, walletClient, connector) {
|
|
|
35
38
|
}
|
|
36
39
|
return walletClient;
|
|
37
40
|
}
|
|
41
|
+
/**
|
|
42
|
+
* Create a public client with RPC fallback configuration
|
|
43
|
+
*
|
|
44
|
+
* This function creates a viem public client for a given chain ID, using custom RPC URLs
|
|
45
|
+
* from H_RPCS configuration when available. If the custom RPC is unavailable, it falls back
|
|
46
|
+
* to the default RPC from the chain definition and logs an error.
|
|
47
|
+
*
|
|
48
|
+
* @param chainId - The chain ID to create a public client for
|
|
49
|
+
* @param chain - Optional chain definition (will be looked up if not provided)
|
|
50
|
+
* @returns A configured public client for the chain
|
|
51
|
+
* @throws Error if chain is not supported
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* ```typescript
|
|
55
|
+
* const publicClient = createPublicClientWithRpc(43114); // Avalanche
|
|
56
|
+
* const receipt = await publicClient.waitForTransactionReceipt({ hash });
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export function createPublicClientWithRpc(chainId, chain) {
|
|
60
|
+
const chainDefinition = chain || getChainById(chainId);
|
|
61
|
+
if (!chainDefinition) {
|
|
62
|
+
throw new Error(`Unsupported chain ID: ${chainId}. Please ensure the chain is supported.`);
|
|
63
|
+
}
|
|
64
|
+
// Get custom RPC URL if available
|
|
65
|
+
const customRpcUrl = H_RPCS[chainId];
|
|
66
|
+
// Create transport with fallback: try custom RPC first, then default RPC
|
|
67
|
+
// The fallback transport will automatically try the default if custom RPC fails
|
|
68
|
+
let transport;
|
|
69
|
+
if (customRpcUrl) {
|
|
70
|
+
// Create a custom fetch function that logs errors for the custom RPC
|
|
71
|
+
const customFetch = async (input, init) => {
|
|
72
|
+
try {
|
|
73
|
+
const response = await fetch(input, init);
|
|
74
|
+
if (!response.ok) {
|
|
75
|
+
// Log error when custom RPC returns non-OK response
|
|
76
|
+
if (typeof console !== 'undefined' && console.error) {
|
|
77
|
+
console.error(`[SilentSwap SDK] Custom RPC request failed for chain ${chainId} (${customRpcUrl}):`, `HTTP ${response.status} ${response.statusText}`);
|
|
78
|
+
console.error(`[SilentSwap SDK] Falling back to default RPC for chain ${chainId}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return response;
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
// Log error when custom RPC fails
|
|
85
|
+
if (typeof console !== 'undefined' && console.error) {
|
|
86
|
+
console.error(`[SilentSwap SDK] Custom RPC unavailable for chain ${chainId} (${customRpcUrl}):`, error instanceof Error ? error.message : String(error));
|
|
87
|
+
console.error(`[SilentSwap SDK] Falling back to default RPC for chain ${chainId}`);
|
|
88
|
+
}
|
|
89
|
+
throw error; // Re-throw to let fallback transport handle it
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
const customTransport = http(customRpcUrl, {
|
|
93
|
+
fetchFn: customFetch,
|
|
94
|
+
});
|
|
95
|
+
const defaultTransport = http();
|
|
96
|
+
// Create fallback transport: tries custom RPC first, falls back to default if it fails
|
|
97
|
+
transport = fallback([customTransport, defaultTransport], {
|
|
98
|
+
rank: false, // Try custom first, then default
|
|
99
|
+
retryCount: 0, // Don't retry failed transports, just fall back
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
// No custom RPC, use default
|
|
104
|
+
transport = http();
|
|
105
|
+
}
|
|
106
|
+
return createPublicClient({
|
|
107
|
+
chain: chainDefinition,
|
|
108
|
+
transport,
|
|
109
|
+
});
|
|
110
|
+
}
|
|
38
111
|
/**
|
|
39
112
|
* Wait for transaction confirmation using viem's waitForTransactionReceipt
|
|
40
113
|
*
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/rpc.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom RPC endpoint configuration for EVM chains and Solana
|
|
3
|
+
*
|
|
4
|
+
* This allows you to specify custom RPC endpoints (like QuickNode) for specific chains
|
|
5
|
+
* to improve performance and reliability. These will be used as fallback transports
|
|
6
|
+
* when creating Viem public clients.
|
|
7
|
+
*
|
|
8
|
+
* Usage:
|
|
9
|
+
* - Add chain ID as key and RPC URL as value
|
|
10
|
+
* - Chain IDs: 1 = Ethereum, 43114 = Avalanche, etc.
|
|
11
|
+
* - Applications can override these values by merging with their own configuration
|
|
12
|
+
*
|
|
13
|
+
* Note: For environment variable overrides, applications should handle this in their
|
|
14
|
+
* own configuration layer (e.g., Next.js config) and merge with this default config.
|
|
15
|
+
*/
|
|
16
|
+
export declare const H_RPCS: Record<number, string>;
|
package/dist/rpc.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { arbitrum, avalanche, base, berachain, bsc, mainnet, optimism, polygon } from 'viem/chains';
|
|
2
|
+
import { SOLANA_CHAIN_ID } from './chains.js';
|
|
3
|
+
/**
|
|
4
|
+
* Custom RPC endpoint configuration for EVM chains and Solana
|
|
5
|
+
*
|
|
6
|
+
* This allows you to specify custom RPC endpoints (like QuickNode) for specific chains
|
|
7
|
+
* to improve performance and reliability. These will be used as fallback transports
|
|
8
|
+
* when creating Viem public clients.
|
|
9
|
+
*
|
|
10
|
+
* Usage:
|
|
11
|
+
* - Add chain ID as key and RPC URL as value
|
|
12
|
+
* - Chain IDs: 1 = Ethereum, 43114 = Avalanche, etc.
|
|
13
|
+
* - Applications can override these values by merging with their own configuration
|
|
14
|
+
*
|
|
15
|
+
* Note: For environment variable overrides, applications should handle this in their
|
|
16
|
+
* own configuration layer (e.g., Next.js config) and merge with this default config.
|
|
17
|
+
*/
|
|
18
|
+
export const H_RPCS = {
|
|
19
|
+
// Ethereum Mainnet
|
|
20
|
+
[mainnet.id]: 'https://cosmopolitan-tiniest-mound.quiknode.pro/a0b29975a900134513a2b6fb3b623cca22308617/',
|
|
21
|
+
// Solana
|
|
22
|
+
[SOLANA_CHAIN_ID]: 'https://neat-ancient-glitter.solana-mainnet.quiknode.pro/fe9fef29e2e14ae248ca32e8dac33f423813c9d7/',
|
|
23
|
+
// Optimism
|
|
24
|
+
[optimism.id]: 'https://cold-white-mountain.optimism.quiknode.pro/dd00719b96a7c719aa6065c476ef928e465c6799/',
|
|
25
|
+
// Avalanche
|
|
26
|
+
[avalanche.id]: 'https://winter-nameless-surf.avalanche-mainnet.quiknode.pro/59d54d4b83077a8340587766212a5c641e94dd61/ext/bc/C/rpc/',
|
|
27
|
+
// Base
|
|
28
|
+
[base.id]: 'https://polished-winter-layer.base-mainnet.quiknode.pro/33626d7c49fda94bad79a0598606b0e87f4f66ba/',
|
|
29
|
+
// Polygon
|
|
30
|
+
[polygon.id]: 'https://alpha-responsive-pine.matic.quiknode.pro/9be752c95c9334332615e80c52b72a9a939fa0e2/',
|
|
31
|
+
// Arbitrum
|
|
32
|
+
[arbitrum.id]: 'https://convincing-dark-cloud.arbitrum-mainnet.quiknode.pro/e84b58f4fa4b4895282223f8170f0c7272737d26/',
|
|
33
|
+
// BSC
|
|
34
|
+
[bsc.id]: 'https://yolo-wandering-fire.bsc.quiknode.pro/07aae5c8f9c95c4d596a6997e6a28a786dc3d4a0/',
|
|
35
|
+
// Berachain
|
|
36
|
+
[berachain.id]: 'https://capable-purple-dream.bera-mainnet.quiknode.pro/11502780a6ec104db00d7940f6a61c90868a4402/',
|
|
37
|
+
};
|