How To Build Decentralize App on Tron Blockchain?
Tron is an open source blockchain platform that lets developers build smart contracts and DApps.
Steps to developing DApp on Tron Blockchain:
- Creating Smart Contract.
- Setting up TronLink for Deployment of Smart Contract.
- Deploy Smart Contract on Mainnet or Testnet. (here we will deploy our smart contract on Tron test network called Shasta.)
- Developing a client-side application for interacting with our smart contract like fetching data from smart contract and writing data to smart contract.
- Integrating TronLink to our application so that users can interact with a smart contract using their accounts. (As we are developing very simple DApp, I will not explain this part in my blog.)
Let’s start from scratch.
Step 1: Creating Smart Contract
Just like Ethereum, Tron smart contracts written in Solidity language. If you want to learn solidity Click Here, this is the perfect place to start learning solidity.
Here we will write a very simple smart contract that will set and get a string message to a smart contract. Here is the code,
<script src="https://gist.github.com/meetsiraja/148eb5807da6d08a756851717faff0ef.js"></script>
By reading the above code, you can easily understand what this function will do. The postMessage function will set the string in a message variable that we had passed as an argument. The getMessage function will get that string that is stored in the message variable.
Step 2: Setting Up TronLink
TronLink is a bridge that connects your DApps to Tron blockchain. For deploying smart contract into test network you need to install TronLink Chrome Extension. You can download it from here.
After Setting up a wallet password and account you need some TRX to deploy the smart contract. You can get it from this Shasta Faucet. Go to the website and enter the account address that you just created then click submit. You will get 10,000 TRX token for development purposes.
Step 3: Deploying Smart Contract on Test Network
Now it’s time to deploy a smart contract on the Tron network. There are a variety of ways available for deploying a smart contract on Tron blockchain. You can deploy using TronBox (Which is just like a Truffle framework), from Tron wallet-cli and Tron IDE.
We will use tronsmartcontract.space for compilation and deployment. Go to the website and hit the Compile button. After compilation succeeds press the Deploy it button. Don’t forget to change the network mode in TronLink. You can change the Network mode by going to Settings > Switch Mode > Shasta Testnet from TronLink. Then one confirmation popup will appear from TronLink. Press Accept. You will see the contract address when your contract is successfully deployed. Keep it somewhere, we will use that later.
Now your contract is deployed on Tron Test Network.
Step 4: Node Application for Interacting with Smart Contract
You can get the code from this GitHub link. Enter below command in your terminal.
$ git clone https://github.com/YudizBlockchain/Tron-DAPP.git
You have to change a few things in the index.js file. After downloading the code open index.js file. You have to enter your account address, a private key of your account (you can get it from TronLink > Menu > Export Account) and address of the smart contract where it is deployed.
After that, you need to install dependencies. For that open terminal and got to the path where your code is then run below command,
$ npm install
When all modules are installed type command below in a terminal and go to 127.0.0.1:8001 in your browser.
$ node index.js
Now you can test the application and functions of a smart contract.
Executing the function will take some time for confirmation on the blockchain. You can check all the transactions of your smart contract. Go to Tron Blockchain Explorer and enter the address of your smart contract. You can see all transaction and it details.
Congratulations!
You have successfully deployed Tron DApp onto Tron Testnet.