使用 web3 将 ERC20 代币从地址 A 转移到地址 B
Posted
技术标签:
【中文标题】使用 web3 将 ERC20 代币从地址 A 转移到地址 B【英文标题】:Transfer ERC20 Token from Address A to Adress B with web3 【发布时间】:2020-07-24 10:25:14 【问题描述】:web3 库的 1.2.6,我正在尝试使用 infura 节点与 Ropsten Text 网络上的标准 ERC-20 令牌进行交互。即使我成功地广播了交易,它也会被还原。我经常收到的错误消息如下:“错误:事务已被 EVM 还原:”
当我通过 remix 与合同交互时,它工作正常。请看下面的完整代码:
const path = require('path');
const fs = require('fs');
const Web3 = require('web3');
const provider = new Web3.providers.HttpProvider("https://rinkeby.infura.io/v3/cb1****");
const web3 = new Web3(provider);
/* NETWORK AND LIB VERSION CHECKS*/
web3.eth.net.isListening()
.then(() => console.log('web3 is connected'))
.catch(e => console.log('Wow. Something went wrong'));
var Tx = require('ethereumjs-tx');
web3.eth.net.getNetworkType()
.then(console.log);
// Create an async function so I can use the "await" keyword to wait for things to finish
const main = async () =>
// This code was written and tested using web3 version 1.0.0-beta.26
console.log(`web3 version: $web3.version`)
/* ADDRESSES */
var myAddress = "0x77Bd******";
var destAddress = "0x4f*****";
var contractAddress = "0x67b7fecf8b02506d4692d9895959c42d7e7511bb";
var abiArray = JSON.parse(fs.readFileSync(path.resolve(__dirname, './abi.json'), 'utf-8'));
// var contract = new web3.eth.Contract(abiArray, contractAddress, from: myAddress );
var contract = new web3.eth.Contract(abiArray, contractAddress);
/* INPUTS TO RAW TRANSACTION */
// 1.Nonce: Determine the nonce
var count = await web3.eth.getTransactionCount(myAddress);
console.log(`num transactions so far: $count`);
// 2.token amount - token is divisible to 0 decimals hence 1 = 1 token
var transferAmount = 10000;
// How many tokens do I have before sending?
var balance = await contract.methods.balanceOf(myAddress).call();
console.log(`Balance before send: $balance`);
// I chose gas price and gas limit based on what ethereum wallet was recommending for a similar transaction. You may need to change the gas price!
var rawTransaction =
"from": myAddress,
"nonce": "0x" + count.toString(16),
"gasPrice": "0x003B9ACA00",
"gasLimit": "0x250CA",
"to": contractAddress,
"value": "0x09",
"data": contract.methods.transfer(destAddress, transferAmount).encodeABI(),
"chainId": 0x04
;
let privKey_ = "CDA******";
let privKey = new Buffer.from(privKey_, "hex")
var tx = new Tx(rawTransaction);
//var tx = new Tx(rawTransaction,chain:'rinkeby', hardfork: 'petersburg' ); // https://ethereum.stackexchange.com/questions/61771/error-returned-error-invalid-sender
tx.sign(privKey);
var serializedTx = tx.serialize();
console.log(`Attempting to send signed tx: $serializedTx.toString('hex')`);
var receipt = await web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'));
console.log(`Receipt info: $JSON.stringify(receipt, null, '\t')`);
// The balance may not be updated yet, but let's check
balance = await contract.methods.balanceOf(myAddress).call();
console.log(`Balance after send: $balance`);
main();
你知道怎么解决吗?
【问题讨论】:
【参考方案1】:在您的交易选项中添加“Handle Revert: true”。这根据 web3 文档,将输出还原原因。
我遇到这个错误很长时间了,只有通过更改我使用的地址来解决它。我猜在 EVM 上没有同步,我目前的设置
【讨论】:
以上是关于使用 web3 将 ERC20 代币从地址 A 转移到地址 B的主要内容,如果未能解决你的问题,请参考以下文章
转账ERC20代币失败:web3.exceptions.ContractLogicError:执行恢复:ERC20:从零地址转账