| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- import { getSolanaRpcUrl } from '@/lib/solana-config'
- import { Connection, Keypair, PublicKey, clusterApiUrl } from '@solana/web3.js'
- import { BYREAL_CLMM_PROGRAM_ID } from '@/lib/byreal-clmm-sdk/src/constants'
- import { Chain } from '@/lib/byreal-clmm-sdk/src/client/index'
- const rpcUrl = getSolanaRpcUrl()
- // 配置 Connection,禁用 WebSocket 以避免 403 错误
- // 使用 HTTP-only 模式,不建立 WebSocket 连接
- // 通过不提供 wsEndpoint 或提供 null 来禁用 WebSocket
- export const connection = new Connection(rpcUrl, {
- commitment: 'confirmed',
- disableRetryOnRateLimit: false,
- // 明确禁用 WebSocket,只使用 HTTP RPC
- // @ts-ignore - wsEndpoint 可能不在类型定义中,但可以设置
- wsEndpoint: null,
- })
- // 捕获并忽略 WebSocket 相关错误(如果仍然出现)
- if (typeof process !== 'undefined') {
- // 在 Node.js 环境中,捕获未处理的 WebSocket 错误
- const originalUnhandledRejection = process.listeners('unhandledRejection')
- process.on('unhandledRejection', (reason: unknown) => {
- if (
- reason instanceof Error &&
- (reason.message.includes('ws error') ||
- reason.message.includes('WebSocket') ||
- reason.message.includes('403') ||
- reason.message.includes('Unexpected server response'))
- ) {
- // 静默忽略 WebSocket 错误,不打印到控制台
- return
- }
- // 其他错误正常处理
- if (originalUnhandledRejection.length > 0) {
- originalUnhandledRejection.forEach((listener) => {
- if (typeof listener === 'function') {
- listener(reason, Promise.resolve())
- }
- })
- }
- })
- }
- export const chain = new Chain({
- connection,
- programId: BYREAL_CLMM_PROGRAM_ID,
- })
|