首页 > 解决方案 > 在 Infura Ropsten 测试网上使用 Web3JS 进行 ERC20 代币转移 - EVM 已恢复交易

问题描述

因此,经过一天的搜索和许多紫色链接后,我除了问一个关于该主题的问题十几次外,别无他法,但不知何故,每个问题都是独一无二的。

信息

重要提示:智能合约是通过 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();

有趣的事实:

标签: node.jsethereumweb3contracterc20

解决方案


推荐阅读