0

I try to send erc20 tokens in this way

var Tx = require('ethereumjs-tx');

var Web3 = require('web3')

var web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io/v3/2a7d7b55629d44119f069a9239ea96fd'))

// set token source, destination and amount

var myAddress = ""

var toAddress = ""

var amount = web3.utils.toHex(1e16)

// get transaction count, later will used as nonce

web3.eth.getTransactionCount(myAddress).then(function(v){console.log(v); count = v})  


var privateKey = new Buffer('privatekey', 'hex')  
var abiArray = abiarray
var contractAddress = ''
var contract = new web3.eth.Contract(abiArray, contractAddress, {from: myAddress})
var rawTransaction = {"from":myAddress, "gasPrice":web3.utils.toHex(2 * 1e9),"gasLimit":web3.utils.toHex(210000),"to":contractAddress,"value":"0x0","data":contract.methods.transfer(toAddress, amount).encodeABI(),"nonce":web3.utils.toHex(count)} 
var transaction = new Tx(rawTransaction)
transaction.sign(privateKey)
web3.eth.sendSignedTransaction('0x' + transaction.serialize().toString('hex'))
contract.methods.balanceOf(myAddress).call().then(function(balance){console.log(balance)})

but I get an error

var rawTransaction = {"from":myAddress, "gasPrice":web3.utils.toHex(2 * 1e9),"ga
sLimit":web3.utils.toHex(210000),"to":contractAddress,"value":"0x0","data":contr
act.methods.transfer(toAddress, amount).encodeABI(),"nonce":web3.utils.toHex(cou
nt)}


                                                                             ^

ReferenceError: count is not defined

What is the problem?

2

As the error says 'count is not defined' ... you set the nonce to 'count' but did not define that. So you need to do something like this:

var count = web3.eth.getTransactionCount();
| improve this answer | |
  • Thanks! Buy now i get another error:` throw new Error('Provided address "'+ address +'" is invalid, the capitaliza tion checksum test failed, or its an indrect IBAN address which can\'t be conver ted.');` – Ayurpwnz Dec 4 '19 at 1:01
  • you're welcome. Just make sure the address is in checksum capitalization (mixed upper/lowercase) , that should fix it? – Ossip Dec 5 '19 at 21:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.