我想转移 erc20token,我在 node js 中尝试了一些代码。交易成功,但收款人没有收到代币,也没有从主钱包中扣除代币。
这是我正在尝试的完整代码:
var Tx = require('ethereumjs-tx').Transaction;
const Web3 = require('web3');
const provider = new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/project");
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'));
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 = "senderaddress";
var destAddress = "receiver address";
var contractAddress = "contract address";
var abiArray = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"INITIAL_SUPPLY","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_subtractedValue","type":"uint256"}],"name":"decreaseApproval","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_addedValue","type":"uint256"}],"name":"increaseApproval","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}];
// 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 = 1000;
// 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": web3.utils.toHex(10),
"gasLimit": web3.utils.toHex(90000),
"to": destAddress,
"value": "0x09",
"data": contract.methods.transfer(destAddress, transferAmount).encodeABI(),
"chainId": 0x04
};
let privKey_ = "private key of sender wallet";
let privKey = new Buffer.from(privKey_, "hex")
var tx = new Tx(rawTransaction, {chain:'ropsten', hardfork: 'petersburg'});
//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();
使用此代码传输成功,但正如我所提到的,接收方没有收到令牌。
这是我昨天使用此代码在 ropsten 上创建的交易链接。你可以检查一下:
https://ropsten.etherscan.io/tx/0xb0a4b7923c304d282608601e4e217434440521e4c0504b534becf9a1cdbe142b