Answer the question
In order to leave comments, you need to log in
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);
});
});
Uncaught (in promise) Error: Invalid JSON RPC response: ""
Answer the question
In order to leave comments, you need to log in
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);
});
});
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 questionAsk a Question
731 491 924 answers to any question