Error: invalid hexlify value (argument="value", value="PRIVATE_KEY", code=INVALID_ARGUMENT, version=bytes/5.7.0)
我在构建应用程序以使用 Ethers.js 和 Node.js 将代币从一个钱包发送到另一个钱包时遇到错误。我正在使用私钥进行代币转移。
我的回答:
请仔细检查您的私钥。它应该是一个 64 个字符的字符串,因此请确保它是正确的。
我尝试使用前缀“0x”,但结果仍然相同。 我尝试更新和降级 ethers.js 的版本,但没有帮助我。
我也有同样的错误。如果您要发送交易,交易中以太坊的值在哪里,请不要在未使用 parseEther 对其进行格式化的情况下输入“0”。例如我的代码中:
导致错误:
const txData = {
chainId: chainId,
to: uniswapRouterAddress,
data: routerContract.interface.encodeFunctionData(method, params),
value: type === 'buy' ? ethers.utils.parseEther(buyAmount) : "0",
gasLimit: ethers.utils.hexlify(1000000), // Estimate gas limit
gasPrice: await provider.getGasPrice() // Current gas price
};
错误已解决:
const txData = {
chainId: chainId,
to: uniswapRouterAddress,
data: routerContract.interface.encodeFunctionData(method, params),
value: type === 'buy' ? ethers.utils.parseEther(buyAmount) : ethers.utils.parseEther("0"),
gasLimit: ethers.utils.hexlify(1000000), // Estimate gas limit
gasPrice: await provider.getGasPrice() // Current gas price
};