node.js - 在 Infura Ropsten 测试网上使用 Web3JS 进行 ERC20 代币转移 - EVM 已恢复交易
问题描述
因此,经过一天的搜索和许多紫色链接后,我除了问一个关于该主题的问题十几次外,别无他法,但不知何故,每个问题都是独一无二的。
信息:
节点 v12.16.1
通过 Infura API 的 Ropsten 测试网
“web3”:“1.2.6”
“ethereumjs-tx”:“2.1.2”
ERC20 代币示例,将代币名称和小数更改为 8 和
pragma solidity ^0.5.2;
重要提示:智能合约是通过 Remix(使用 0.5.3 编译器)编译和部署的,transfer()
方法通过 Metamask 测试成功。
此代码产生此错误: - EVM 已恢复事务
const Web3 = require("web3");
const compiled = require("./compile");
const Tx = require("ethereumjs-tx").Transaction;
const contractAddress = "0x27...";
const address = "0x02...";
const privateKey = "A475...";
const bufferedPrivKey = Buffer.from(privateKey, "hex");
const rpcUrl = "https://ropsten.infura.io/v3/{INFURA_API_KEY}";
const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl));
const call = async () => {
try {
const abi = compiled.abi; //copied from remix
const contract = await new web3.eth.Contract(abi, contractAddress, {
from: address
});
const gasPrice = await web3.eth.getGasPrice();
const nonce = await web3.eth.getTransactionCount(address);
const data = await contract.methods.transfer("0x2B0...", 1000).encodeABI();
const rawTx = {
from: address,
gasPrice: web3.utils.toHex(gasPrice),
gas: web3.utils.toHex(100000),
data: data,
nonce: web3.utils.toHex(nonce),
value: 0x00,
chainId: 0x03 //ropsten i guess?
};
const tx = new Tx(rawTx, { chain: "ropsten" });
tx.sign(bufferedPrivKey);
const serializedTx = tx.serialize();
await web3.eth.sendSignedTransaction("0x" + serializedTx.toString("hex"));
} catch (err) {
console.log(err);
console.log(err.stack);
}
};
call();
有趣的事实:
当我尝试通过部署智能合约时遇到同样的问题
web3.eth.sendSignedTransaction()
在兔子洞之后,我找到了将 web3 版本降低到1.0.0-beta.34 的潜在解决方案,当我这样做时,我遇到了 gas 问题——转账交易需要很长时间,最终我的 gas 用完了。大多数问题是:或
intrinsic gas too low
Transaction ran out of gas. Please provide more gas
解决方案
推荐阅读
- firebase - Flutter/Firebase - 创建用于收集 Firebase 图像的未来功能
- objective-c - 目标 C - 无法识别的选择器发送到实例 0x2829fe980
- plc - TwinCat 系统管理器:INIT 到 PREOP' 失败!错误:“检查产品代码”
- ruby - 预期:0,而不是:1 Ruby
- javascript - 使用 JavaScript 的拖放功能
- javascript - ES6 类中的 this 关键字,仅在包含在函数中时才有效?
- javascript - ReactJS 使用 if-rendering 时有组件消失
- python - 如何获取数据集中未选择的所有行
- reactjs - React hooks - 向 useCallback/useEffect 添加详尽的依赖会导致无限循环
- php - PHP从PNG图像中删除空格