config.ts 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { getSolanaRpcUrl } from '@/lib/solana-config'
  2. import { Connection, Keypair, PublicKey, clusterApiUrl } from '@solana/web3.js'
  3. import { BYREAL_CLMM_PROGRAM_ID } from '@/lib/byreal-clmm-sdk/src/constants'
  4. import { Chain } from '@/lib/byreal-clmm-sdk/src/client/index'
  5. const rpcUrl = getSolanaRpcUrl()
  6. // 配置 Connection,禁用 WebSocket 以避免 403 错误
  7. // 使用 HTTP-only 模式,不建立 WebSocket 连接
  8. // 通过不提供 wsEndpoint 或提供 null 来禁用 WebSocket
  9. export const connection = new Connection(rpcUrl, {
  10. commitment: 'confirmed',
  11. disableRetryOnRateLimit: false,
  12. // 明确禁用 WebSocket,只使用 HTTP RPC
  13. // @ts-ignore - wsEndpoint 可能不在类型定义中,但可以设置
  14. wsEndpoint: null,
  15. })
  16. // 捕获并忽略 WebSocket 相关错误(如果仍然出现)
  17. if (typeof process !== 'undefined') {
  18. // 在 Node.js 环境中,捕获未处理的 WebSocket 错误
  19. const originalUnhandledRejection = process.listeners('unhandledRejection')
  20. process.on('unhandledRejection', (reason: unknown) => {
  21. if (
  22. reason instanceof Error &&
  23. (reason.message.includes('ws error') ||
  24. reason.message.includes('WebSocket') ||
  25. reason.message.includes('403') ||
  26. reason.message.includes('Unexpected server response'))
  27. ) {
  28. // 静默忽略 WebSocket 错误,不打印到控制台
  29. return
  30. }
  31. // 其他错误正常处理
  32. if (originalUnhandledRejection.length > 0) {
  33. originalUnhandledRejection.forEach((listener) => {
  34. if (typeof listener === 'function') {
  35. listener(reason, Promise.resolve())
  36. }
  37. })
  38. }
  39. })
  40. }
  41. export const chain = new Chain({
  42. connection,
  43. programId: BYREAL_CLMM_PROGRAM_ID,
  44. })