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

使用Alchemy SDK的parseEther触发TypeError,咨询是否为bug或文档过时

问题原因及解决方案
  • 这不是SDK的bug,也不是文档完全过时,是你对导出结构理解有误:Alchemy SDK中的parseEther函数并非顶层导出,而是挂载在Utils对象下的成员方法,当前顶层导入方式找不到该函数,才触发了TypeError。

修正后的代码示例

方式一:直接通过Utils对象调用

import { Network, Alchemy, Wallet, TokenBalanceType, Utils, fromHex } from 'alchemy-sdk';

export const estimateGasFees = async (toAddress, fromAddress, amount) => {
    const estimate = await alchemy.core.estimateGas({
        to: toAddress,
        from: fromAddress,
        value: Utils.parseEther(amount),
    });
    return estimate;
}

方式二:解构Utils中的parseEther

import { Network, Alchemy, Wallet, TokenBalanceType, fromHex, Utils } from 'alchemy-sdk';
const { parseEther } = Utils;

export const estimateGasFees = async (toAddress, fromAddress, amount) => {
    const estimate = await alchemy.core.estimateGas({
        to: toAddress,
        from: fromAddress,
        value: parseEther(amount),
    });
    return estimate;
}

内容的提问来源于stack exchange,提问作者Jake Mulhern

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 16:00:52