I finally managed to transfer tokens between accounts.
It is very simple approach. I got it from one of the eattheblocks video.
in this case, I'm using ganache gui as the private network.
TO THE EXPERTS: could you please comment the code in terms of security or performance?
const Web3 = require('web3');
const UpToken = require('../build/contracts/UpToken.json');
if (typeof web3 !== 'undefined') {
var web3 = new Web3(web3.currentProvider)
} else {
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:7545'))
}
const address = '0x23c63608dDbB5EE00A0628196020CA353aE8351d';
const buyer = '0xe0788Ce42F19ae49A7817b865Ac8eed74D2D343A';
const seller = '0x5174b046F7d435bFCdc57374E7D6b0F8716bd65a';
const contract = '0x05d62294C5809800644D28939ea0177B432187df';
const init = async () => {
let accounts = await web3.eth.getAccounts();
web3.eth.defaultAccount = accounts[0];
const id = await web3.eth.net.getId();
const deployedNetwork = UpToken.networks[id];
const contrato = new web3.eth.Contract(
UpToken.abi,
deployedNetwork.address,
{
from: address, // default from address
gas: 1500000,
gasPrice: '20000000000'
} // default gas price in wei, 20 gwei in this case
);
console.log(await contrato.methods.approve(address, 10000000).call());
const receipt = await contrato.methods.transfer(seller, 1000000).send({ from: address });
console.log(Transaction hash: ${receipt.transactionHash});
balBuyer = await web3.eth.getBalance(address);
console.log('address ETH: ' + balBuyer.toString());
balBuyer = await contrato.methods.balanceOf(address).call();
console.log('address BTH: ' + balBuyer.toString());
balBuyer = await web3.eth.getBalance(buyer);
console.log('buyer ETH: ' + balBuyer.toString());
balBuyer = await contrato.methods.balanceOf(buyer).call();
console.log('buyer BTH: ' + balBuyer.toString());
balBuyer = await web3.eth.getBalance(seller);
console.log('seller ETH: ' + balBuyer.toString());
balBuyer = await contrato.methods.balanceOf(seller).call();
console.log('seller BTH: ' + balBuyer.toString());
}
init();
Jul 28, 2021, 11:44 AM