A
A
Artyom2019-01-10 12:18:38
JavaScript
Artyom, 2019-01-10 12:18:38

Web3.js 1.0.0-beta.34 - how to send tokens from wallet to wallet?

Hello.
I am new to programming in general and JS in particular. However, I'm trying to work somehow. Now I'm trying to establish the interaction of the Vue.js application with the Ethereum blockchain using the Web3 library version 1.0.0-beta.34. I use this particular beta because more recent versions, starting from beta.35, do not allow me to use the getBalance method of the smart contract, with which I am trying to make friends with my application. On this beta , most of my application works flawlessly.
To implement the sending of tokens, I am guided by the corresponding article from the official documentation for Web3.js 1.0 , in addition to it, I used the data from the article Hideyoshi Moriya How to send ERC20 token with [email protected]. I also actively use Google.
Here is the relevant piece of code. ( I did not include in it a way to connect the axios and Web3.js libraries. Just in case, I note that my provider is node3.web3api.com . )

let
  tokenAddress = '0x5c....e4',
  fromAddress = '0xaA....6c',
  toAddress = '0xE3....f7',
  decimals = web3.utils.toBN(0),
  amount = web3.utils.toBN(5),
  value = amount.mul(web3.utils.toBN(10).pow(decimals));

const
  token = abi => new web3.eth.Contract(abi, tokenAddress).methods;

axios.get('https://api.etherscan.io/api?module=contract&action=getabi&address=' + tokenAddress)
.then(answer => {
  let
    abi = JSON.parse(answer.data.result);

  token(abi).name().call().then(console.log); //работает
  token(abi).symbol().call().then(console.log); //работает
  token(abi).balanceOf(fromAddress).call().then(console.log); //работает

  token(abi).transfer(toAddress, value)//не работает
  .send({from: fromAddress})
  .on('transactionHash', function(hash){
    console.log(hash);
  });
});

Mistake:
Uncaught (in promise) Error: Invalid JSON RPC response: ""

People, help to understand, please!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artyom, 2019-01-10
@yartem

I asked myself, and I will answer.
The solution, as it turned out, lay in a slightly different plane. Here is the code:

const
  tokenAddress = '0x58....e4',
  fromAddress = '0xaA....46c',
  privateKey = '0x09........79',
  toAddress = '0xE3....f7',
  decimals = web3.utils.toBN(0),
  amount = web3.utils.toBN(5),
  value = amount.mul(web3.utils.toBN(10).pow(decimals)), // value = 5 = 5*(10^0)
  sender = web3.eth.accounts.privateKeyToAccount(privateKey),
  token = abi => new web3.eth.Contract(abi, tokenAddress).methods;

axios.get('https://api.etherscan.io/api?module=contract&action=getabi&address=' + tokenAddress)
.then(answer => {
  const
    abi = JSON.parse(answer.data.result),
    data = token(abi)["transfer"](toAddress, value).encodeABI(),
    tx = {
      gas: '2000000',
      from: fromAddress,
      to: tokenAddress,
      data,
    };

  token(abi).name().call().then(console.log);
  token(abi).symbol().call().then(console.log);
  token(abi).balanceOf(fromAddress).call().then(console.log);

  sender.signTransaction(tx)
  .then(signedTx => {
    web3.eth.sendSignedTransaction(signedTx.rawTransaction)
    .then(console.log);
  });
});

Apparently, the problem was that the transaction was not signed. Since the transfer of tokens is not sending any message in the spirit of "Hello world!", but quite a transfer of real money, such a transaction cannot be carried out from the owner's wallet without his knowledge. In the article How to send ERC20 token with [email protected], the link to which I cited above, and which I followed in the process of writing the initial version of the code, this moment is somehow strangely bypassed by the author. If you meticulously study the demo site , Hideyoshi Moriya provides a link to it at the end of his article, you will notice that the author uses the MetaMask API, which explains a lot, but there is not a word about it in the article itself. Apparently, MetaMask allows you to solve the issue of authorization with its own methods.
I continued my research. Hardly digging the Internet, after a fairly short period of time, I came across a question similar to mine by a person with the nickname Dmitry on the Russian segment of StackOverflow.com : How to send ethereum tokens using web3.js . I won't quote him, but here is the answer of a person with the nickname user3655581 that caught my attention:
In order to transfer tokens, you need to call the corresponding method of the token contract, having previously signed it. Sample code:
const sender = web3.eth.accounts.privateKeyToAccount(privateKeyFrom);
const tokenContract = new web3.eth.Contract(abi, contractAddress);
const data = tokenContract.methods["transfer"](toAddress, amount).encodeABI();
const tx = { 
  gas: '2000000',
  from: fromAddress,
  to: contractAddress,      
  data,
};
const signedTx = await sender.signTransaction(tx);
const result = await web3.eth
  .sendSignedTransaction(signedTx.rawTransaction);

Guided by the given code, I built a solution for my problem. Tokens in the amount of 5 pieces were successfully transferred from one wallet to another.
Thank you very much to everyone who wanted to help me in this matter, but did not have time. If anyone has something to add, I will only be glad for your comments. because while very much and very vaguely I am guided in the raised question.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question