Overview
Have you ever heard about Tron Blockchain? Tron was founded by Justin Sun and launched in May 2018 on Mainnet. The Tron code base was originally forked from Ethereum and it uses a fork of the Solidity language, which is a programming language for developing a smart contract on Ethereum Blockchain. As a result, Ethereum token standards and smart contracts are compatible with Tron ecosystem.
We will cover the following topics today:
Introduction To Tron Blockchain
The Tron Blockchain is an ambitious project that aims to decentralize the web through Blockchain Technology. TRX(Troinix) is the official cryptocurrency of Tron Blockchain. Unlike Bitcoin and Ethereum TRX cannot be mined. But we can buy TRX on major cryptocurrency exchanges like Binance.
Tron has already achieved more Transaction Process System(TPS) than Ethereum and Bitcoin. Its network can handle 2000 transactions per second whereas the Bitcoin can handle 6 TPS and Ethereum can handle 25 TPS respectively.
(Source: https://tron.network/)
Let’s see how Tron Blockchain works.
How Tron Blockchain Works?
To understand how the Tron Blockchain works, we first have to understand its architecture.
The Tron Architecture consists of Three layers.
Storage Layer:
A Distributed Storage protocol that consist blocks storage, state storage, and GRPC.
Core Layer:
This layer consists of Smart Contract, Account Management, Wallet API, Consensus and SDK. Java is the designated language for development for smart contracts.
Application Layer:
A Developer can easily develop DApp and customized wallets in Tron Network using Application Layer.
TRON uses Delegated Proof of Stack (DPoS) consensus algorithm. So, Tron Network is governed by 27 Super Representatives that are voted by TRX holders. This 27 Super Representatives validates the blocks and keeps running the Tron Blockchain. Super Representatives are rewarded in TRX for that.
So, we got enough knowledge for developing the Decentralized App Tron Blockchain. Let’s build DApp on Tron Blockchain.
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.
Conclusion
I hope this will help you to understand the Tron Blockchain, it’s architecture and DApp development process. If you have any issues, please leave your comment below.
That’s all for now. Thank you for reading this blog.