You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hedera Wallet Connect查询旧账户余额遇BUSY错误求助

解决方案:修复HashPack旧账户余额查询BUSY错误

针对旧HashPack账户查询余额时出现的max attempts of 10 was reached for request with last error being: BUSY错误,可通过以下步骤排查修复:

1. 调整查询的重试与超时策略

默认10次重试可能不足以应对节点繁忙场景,自定义重试规则、间隔及超时时间,避免快速耗尽重试次数:

const balanceQuery = new AccountBalanceQuery()
  .setAccountId(accountId)
  .setRetry({
    maxAttempts: 15, // 增加重试次数
    delay: (attempt) => attempt * 1000, // 指数退避,每次重试间隔递增
    maxDelay: 5000, // 最大间隔限制为5秒
  })
  .setTimeout(30000); // 设置30秒全局超时

2. 配置客户端连接多节点集群

单一节点繁忙会直接导致查询失败,初始化客户端时配置多个Hedera节点,SDK会自动切换可用节点:

// 以测试网为例,添加多节点地址
const client = Client.forTestnet()
  .setNetwork({
    "https://testnet.mirrornode.hedera.com:443": "0.0.3",
    "https://testnet.hedera.com:50211": "0.0.1",
    "https://testnet.hedera.com:50212": "0.0.2",
  });

3. 验证旧账户状态

旧账户可能处于冻结、已删除或异常状态,可通过Hedera区块链浏览器查询账户的accountStatus,确认账户是否正常活跃。

4. 升级SDK版本

当前使用的@hashgraph/sdk@2.62.0存在节点重试逻辑的已知问题,升级至最新稳定版(如v2.70+)可修复部分BUSY错误的处理逻辑:

npm install @hashgraph/sdk@latest

修改后的完整代码示例

import { AccountBalanceQuery, AccountId, Client, Hbar } from "@hashgraph/sdk";
import { useQuery } from "@tanstack/react-query";
import { BigNumber } from "bignumber.js";

export function useHederaBalance({
  accountId,
  client,
  wcTopic,
  hbarxId,
}: {
  accountId: AccountId | null;
  client: Client | null;
  wcTopic: string | null;
  hbarxId: string;
}) {
  return useQuery<
    { hbar: Hbar; hbarx: BigNumber },
    Error,
    { hbar: Hbar; hbarx: BigNumber },
    [
      string,
      string,
      string,
      string,
      { accountId: string | undefined; wcTopic: string | null }
    ]
  >({
    queryKey: [
      "dapp-sdk",
      "hedera",
      "hashgraph",
      "balance",
      { accountId: accountId?.toString(), wcTopic },
    ],

    queryFn: async () => {
      if (!accountId || !client) throw new Error("Wallet not connected");

      try {
        const balanceQuery = new AccountBalanceQuery()
          .setAccountId(accountId)
          .setRetry({
            maxAttempts: 15,
            delay: (attempt) => attempt * 1000,
            maxDelay: 5000,
          })
          .setTimeout(30000);

        const result = await balanceQuery.execute(client);

        const hbarxBal = result.tokens?.get(hbarxId)?.toNumber() ?? 0;
        const hbarxDec = result.tokenDecimals?.get(hbarxId) ?? 1;

        let hbarx = new BigNumber(hbarxBal.toString());
        hbarx = hbarx.div(new BigNumber(10).pow(hbarxDec));

        return {
          hbar: result.hbars,
          hbarx,
        };
      } catch (error) {
        console.error("Balance query failed:", error);
        throw error; // 抛出错误让react-query参与重试逻辑
      }
    },
    enabled: !!accountId && !!client && !!wcTopic,
    staleTime: 5 * 60 * 1000,
    refetchInterval: 5 * 60 * 1000,
    retry: (failureCount, error) => {
      // 针对BUSY错误额外增加重试次数
      if (error instanceof Error && error.message.includes("BUSY")) {
        return failureCount < 3;
      }
      return failureCount < 2; // 其他错误默认重试2次
    },
  });
}

关键优化点

  • 给查询添加自定义重试策略,适配节点繁忙场景
  • 抛出错误让react-query参与重试,增强整体容错性
  • 配置多节点客户端,提升查询成功率

内容的提问来源于stack exchange,提问作者Fenil Shah

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 08:53:11