使用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
相关产品推荐
相关产品推荐

