Web3-native tronlink
No results found
Published on 8 Jul 2021
Connect tron dapp to tronlink
Get tronweb instance in client side javascript
Ganesh Deshpande
If you are developing decentralised application on tron blockchain the very first thing after deploying smat contract is connecting our dapp environment to tronweb. This can be done with the help of tronlink wallet.
the following code explains the connection of dapp to tronlink:
code
window.onload = async function () < setTimeout(async function () < tronWeb = window.tronWeb; console.log("tronWeb : ", tronWeb); console.log("tronweb is successfully fetched from window"); try < var currentaddress = await tronWeb.address.fromHex((await tronWeb.trx.getAccount()).address.toString()); istronWeb = true; var balance = await tronWeb.trx.getBalance(currentaddress); balance = balance / (10 ** 6); >catch < console.log("Tronweb not defined"); istronWeb = false; >>, 1000); >//window onload
Explanation
After window is loaded, we have declared settimeout function (not required but used in case tronweb loads with delay). then we try to fetch tronweb from window.tronweb, if tronweb is found we set istronweb to true and get the current address of user. then the balance of the user, if tronweb not found we console message. you can use javascript alerts in this case