Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send ERC20 token with web3

I'm currently using 0.2x.x version of Web3 Javascript API. I deployed my custom ERC20 token by creating smart contract in solidity (on REMIX IDE). I installed the MetaMask and had a test on https://wallet.ethereum.org/ to send some custom ERC token to another my account. It worked well. I want to add the 'send custom ERC20 Token' function in my javascript code using Web3.

Here is my code below.

var http = require('http');
var Web3 = require('web3');
var Tx = require('ethereumjs-tx');

var abi = [{"...."}]; 
var data = '0x00..';
var contract_addr = '0x00..';
var owner = '0x00..';

var web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/")); 
web3.eth.getBlock("latest", (error, result) => {
//console.log('error:', error);
//console.log('results', result);
});

var myContract = web3.eth.contract(abi);
var myContractInstance = myContract.at(address);

eb3.eth.getDefaultAccount = owner;
var defaultAccount = web3.eth.getDefaultAccount;
console.log('DefaultAccount => ', defaultAccount); 

var total = myContractInstance.totalSupply();
var balance = myContractInstance.balanceOf(defaultAccount);

console.log('DefulatAccount total => ',total);
console.log('DefaultAccount balance => ',balance);

var to = '0x00..';
var isAddress = web3.isAddress(to);

console.log('isAddress(to) => ',isAddress);
console.log('balanceOf(to) => ',myContractInstance.balanceOf(to));

I'm working on the 'Ropsten Testnet' and I sent some 'ether' for test using sendRawTransaction(). But what I want to do is just sending my custom ERC20 token. not the ether. How can I send my own token in this script? I have no idea. I tried to use sendTransaction() like below.

var rawTx = {
    from : defaultAccount,
    nonce : nonceHex,
    gasPrice : gasPriceHex,
    gas : gasHex,
    to : to,
    value : 100,
    data : data
};

web3.eth.sendTransaction(rawTx, function(err, transactionHash) {
    if (!err)
        console.log('transactionHash => ',transactionHash);
    else
        console.log(err);
});

I continuously received the error.

Error: Invalid JSON RPC response: ""
at Object.InvalidResponse (/Users/rachel/dev/test/node_modules/web3/lib/web3/errors.js:38:16)
at XMLHttpRequest.request.onreadystatechange (/Users/rachel/dev/test/node_modules/web3/lib/web3/httpprovider.js:119:24)
at XMLHttpRequestEventTarget.dispatchEvent (/Users/rachel/dev/test/node_modules/xhr2/lib/xhr2.js:64:18)
at XMLHttpRequest._setReadyState (/Users/rachel/dev/test/node_modules/xhr2/lib/xhr2.js:354:12)
at XMLHttpRequest._onHttpResponseEnd (/Users/rachel/dev/test/node_modules/xhr2/lib/xhr2.js:509:12)
at IncomingMessage.<anonymous> (/Users/rachel/dev/test/node_modules/xhr2/lib/xhr2.js:469:24)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1056:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)

I really can't understand what is wrong with it. Please let me know some way to deal with this. Thanks!

like image 289
rachel_hong Avatar asked Jan 10 '18 05:01

rachel_hong


People also ask

How do I transfer ERC20 tokens to Web3?

Create your Web3 constructor (Web3) Create your web3 instance: Here we use a Coinbase Cloud Query and Transact Node on the Bison Trails platform, with its Access Keys for private access. privateKey: This is the private key for your Ethereum Ropsten wallet. Store it as Environment variable $YOUR_PRIVATE_KEY.

Can you send ERC20 tokens?

You can enable custom fees for ETH and ERC20 tokens. To send or exchange an ERC20 token, use an ERC20 token such as DAI in Compound Finance, or send an ETH NFT, you need some ETH in your Exodus wallet to pay for network transaction fees, also known as gas.

Can I send any ERC20 token to MetaMask?

To deposit ETH or ERC-20 tokens to your MetaMask wallet from an exchange or another wallet, you will simply need to submit a transaction pointed to the address of one of your accounts in MetaMask.

Can I send ERC20 token to ETH address?

You can now send and receive ERC20 tokens. Get started by sending your ERC20 tokens to the Ethereum account of your choice. The token accounts will only appear after the token transaction has been confirmed on the Ethereum blockchain.


1 Answers

Error: Invalid JSON RPC response: "" means web3 cant connect to rpc node. Try running one with ::

-for testnet

geth --testnet --rpc --rpcaddr 127.0.0.1 --rpcport 8545 --rpccorsdomain '*' --rpcapi personal,admin,db,eth,net,web3,miner,shh,txpool,debug --ws --wsaddr 127.0.0.1 --wsport 8546 --wsorigins '*' --wsapi personal,admin,db,eth,net,web3,miner,shh,txpool,debug --maxpeers 25 0 --gasprice 100

-for mainnet

geth --rpc --rpcaddr 127.0.0.1 --rpcport 8545 --rpccorsdomain '*' --rpcapi personal,admin,db,eth,net,web3,miner,shh,txpool,debug --ws --wsaddr 127.0.0.1 --wsport 8546 --wsorigins '*' --wsapi personal,admin,db,eth,net,web3,miner,shh,txpool,debug --maxpeers 25 0 --gasprice 100

Now in your case (using infura testnet node), get a id from infura.io. It should be like this ::

https://ropsten.infura.io/id
like image 84
J0rdAn Avatar answered Oct 13 '22 21:10

J0rdAn