Skip to content

Commit

Permalink
Deploying to TRON (CryptozombiesHQ#717)
Browse files Browse the repository at this point in the history
Deploying to TRON
  • Loading branch information
andreipope authored Jan 13, 2023
1 parent 6bb48f7 commit 611f15f
Show file tree
Hide file tree
Showing 15 changed files with 793 additions and 153 deletions.
38 changes: 38 additions & 0 deletions en/20/00-overview.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
title: Deploying to TRON
header: "Deploying to TRON"
roadmap: roadmap.jpg
path: tron
publishedOn: Cryptozombies
---

<img src="/course/static/image/lesson-20/tron-decentralize-the-web.png" alt="Decentralize the Web" height="150" width="469">

Welcome, CryptoZombie!

Now that you've got a bit of experience under your belt, we're going to deep dive into the blockchain ecosystem by teaching you how to deploy a smart contract to TRON.

But why would you want to deploy to TRON?🧐

To answer this, let's take a look at what TRON has to offer compared to other blockchains.

First, TRON uses the DPoS consensus algorithm which enables a maximum of 2000 transactions per second.

Second, transactions are cheaper compared to Ethereum.


Also, the team developed a lightweight Turing-complete virtual machine named TVM ("TRON Virtual Machine") that reduces resource consumption and improves system performance.

What is a Turing-complete virtual machine, you ask?

A virtual machine creates a level of abstraction between your code and the computer than runs it. This improves portability, and makes sure that applications are separated from their host. A Turing-complete virtual machine is a virtual machine that you can use to perform virtually any task as long as it has enough time and processing power.

First things first. If you're new to Cryptozombies, it's highly recommended that you go over the first six lessons before starting this one. Also, if you are not comfortable with JavaScript, consider going through a tutorial elsewhere before starting this lesson.

So you've worked your way through our previous lessons?

Awesome! That means you're ready to deploy your smart contract💪🏻.

Although you’ll be deploying the `ZombieFactory` smart contract you've written in the previous lessons, the steps you’ll learn can be used to deploy your own smart contracts.

That being said, do you want to get access to much speedier and cheaper transactions? If so, read on...
60 changes: 60 additions & 0 deletions en/20/01.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: Introduction
actions: ['checkAnswer', 'hints']
requireLogin: true
material:
terminal:
help:
You should probably run `mkdir CryptoZombies-TRON && cd CryptoZombies-TRON` followed by `npm install -g tronbox` and `npm install tronweb`.
commands:
"mkdir CryptoZombies-TRON && cd CryptoZombies-TRON":
hint: mkdir CryptoZombies-TRON && cd CryptoZombies-TRON
"npm install -g tronbox":
hint: npm install -g tronbox
output: |
+ [email protected]
added 801 packages from 852 contributors in 24.078s
"npm install -g tronweb":
hint: npm install -g tronweb
output:
+ [email protected]
added 55 packages from 19 contributors and audited 55 packages in 8.976s

32 packages are looking for funding
run `npm fund` for details

found 0 vulnerabilities
---

If you have a background in front-end development, you are probably well accustomed to the tools that make a web developer’s life simpler - Webpack, Gulp, or Browserify.

If you have a background in Solidity, and you’ve previously deployed a smart contract to Ethereum, you’re probably familiar with using Truffle or Hardhat.

But what tools can you use to deploy your smart contracts TRON?


## TronBox

<a href="https://github.com/tronprotocol/tronbox" target="_blank">TronBox</a> is a smart contract development platform that allows you to test, compile and deploy your smart contracts to the TRON network. TronBox aims to make the life of developers easier and is packed with useful features:
- Easy smart contract compilation
- Automated ABI generation
- Integrated smart contract testing
- Support for multiple networks


## TronWeb

<a href="https://github.com/tronprotocol/tronweb" target="_blank">TronWeb</a> delivers a seamless development experience influenced by Ethereum's Web3. The TRON team has taken the core ideas of Web3 and expanded upon them to unlock the functionality of the TRON blockchain.

Provided that <a href="https://nodejs.org" target="_blank">Node.js</a> has been installed on your computer, we'll want you to install TronBox and TronWeb and make them available globally.

To get started, you'll need to create a new directory and install both Tronbox and TronWeb.
## Put it to the test

1. Let’s start with the basics. In the terminal to the right, use the `mkdir` command to create a directory named `CryptoZombies-TRON`. Then, on the same line, use the `cd` command to make that the current directory.

> There are several ways in which you can enter multiple shell commands on the same line. To keep things simple, let's use `&&`. This makes sure that the second command runs only when the first one completes successfully. The following example makes a directory named `example` the current directory and lists its content: `cd example && ls`.
2. Install TronbBox and make it available globally. Enter the `npm install` command, followed by the `-g` flag and the name of the package (`tronbox`).

3. Lastly, you must install TronWeb globally by entering the `npm install` command, followed by the `-g` flag and the name of the package (`tronweb`).
69 changes: 69 additions & 0 deletions en/20/02.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
title: Initialize Your Project
actions: ['checkAnswer', 'hints']
requireLogin: true
material:
terminal:
help: |
You should probably run `tronbox init` followed by `tree`.
commands:
"tronbox init":
hint: tronbox init
output: |
Downloading...
Unpacking...
Setting up...
Unbox successful. Sweet!
Commands:
Compile: tronbox compile
Migrate: tronbox migrate
Test contracts: tronbox test
"tree":
hint: tree
output: |
├── contracts
├── Migrations.sol
├── migrations
├── 1_initial_migration.js
├── 2_deploy_contracts.js
└── test
sample-env
tronbox-config.js
tronbox.js
---

Now that you've installed TronBox and TronWeb, let's initialize a new project by running the `tronbox init` command. This should create a set of folders and configuration files with the following structure:

```
├── contracts
├── Migrations.sol
├── migrations
├── 1_initial_migration.js
├── 2_deploy_contracts.js
└── test
sample-env
tronbox-config.js
tronbox.js
```

Contracts, migrations, tests... this seems complicated😟


As you'll see in this chapter, this directory structure is straightforward and brings several important benefits. Let's take a closer look:

- **_contracts_**: The place where TronBox expects to find all your smart contracts. To keep the code organized, you can even create nested folders such as `contracts/tokens`. <!-- Note that running `tronbox init` created a smart contract named `Migrations.sol` and a corresponding migration file named `1_initial_migration`. We'll explain them a bit later. -->
- **_migrations_**: A directory containining a bunch of JavaScript files that provide all the necessary information and instructions for TronBox to deploy a smart contracts.
- **_test_**: The place where you should place your unit tests, which can be either JavaScript or Solidity files. Remember, once a contract is deployed, it can't be changed, making it essential that you test your smart contracts before you deploy them.

Now that we've shed some light on what these directories and files are, let's look at the benefits of using a standardized directory structure.

First and foremost, TronBox will not work as expected if you change the names of these directories or move the files around.

Second, using a standard directory structure and code convention makes it easier for other developers to understand your code.
## Put it to the test

1. To initialize a new project run the `tronbox init` command.

2. To make sure that the `tronbox init` command actually created all the files and folders explained above, enter the `tree` command. This will print your directory structure in the form of a tree.
123 changes: 123 additions & 0 deletions en/20/03.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
---
title: Migrating the Smart Contracts
actions: ['checkAnswer', 'hints']
requireLogin: true
material:
editor:
language: javascript
startingCode:
"ZombieHelper.sol": |
pragma solidity >=0.5.0 <0.6.0;
import "./zombiefeeding.sol";
contract ZombieHelper is ZombieFeeding {
uint levelUpFee = 0.001 ether;
modifier aboveLevel(uint _level, uint _zombieId) {
require(zombies[_zombieId].level >= _level);
_;
}
function withdraw() external onlyOwner {
address payable _owner = address(uint160(owner()));
_owner.transfer(address(this).balance);
}
function setLevelUpFee(uint _fee) external onlyOwner {
levelUpFee = _fee;
}
function levelUp(uint _zombieId) external payable {
require(msg.value == levelUpFee);
zombies[_zombieId].level = zombies[_zombieId].level.add(1);
}
function changeName(uint _zombieId, string calldata _newName) external aboveLevel(2, _zombieId) onlyOwnerOf(_zombieId) {
zombies[_zombieId].name = _newName;
}
function changeDna(uint _zombieId, uint _newDna) external aboveLevel(20, _zombieId) onlyOwnerOf(_zombieId) {
zombies[_zombieId].dna = _newDna;
}
function getZombiesByOwner(address _owner) external view returns(uint[] memory) {
uint[] memory result = new uint[](ownerZombieCount[_owner]);
uint counter = 0;
for (uint i = 0; i < zombies.length; i++) {
if (zombieToOwner[i] == _owner) {
result[counter] = i;
counter++;
}
}
return result;
}
}
answer: |
pragma solidity >=0.5.0 <0.6.0;
import "./zombiefeeding.sol";
contract ZombieHelper is ZombieFeeding {
uint levelUpFee = 26 trx;
modifier aboveLevel(uint _level, uint _zombieId) {
require(zombies[_zombieId].level >= _level);
_;
}
function withdraw() external onlyOwner {
address payable _owner = address(uint160(owner()));
_owner.transfer(address(this).balance);
}
function setLevelUpFee(uint _fee) external onlyOwner {
levelUpFee = _fee;
}
function levelUp(uint _zombieId) external payable {
require(msg.value == levelUpFee);
zombies[_zombieId].level = zombies[_zombieId].level.add(1);
}
function changeName(uint _zombieId, string calldata _newName) external aboveLevel(2, _zombieId) onlyOwnerOf(_zombieId) {
zombies[_zombieId].name = _newName;
}
function changeDna(uint _zombieId, uint _newDna) external aboveLevel(20, _zombieId) onlyOwnerOf(_zombieId) {
zombies[_zombieId].dna = _newDna;
}
function getZombiesByOwner(address _owner) external view returns(uint[] memory) {
uint[] memory result = new uint[](ownerZombieCount[_owner]);
uint counter = 0;
for (uint i = 0; i < zombies.length; i++) {
if (zombieToOwner[i] == _owner) {
result[counter] = i;
counter++;
}
}
return result;
}
}
---

Awesome, you've just initialized your new project! In this chapter, you'll learn how to migrate your smart contracts so you can deploy them to TRON. Although the TRON blockchain is EVM-compatible, there's a small difference that you must take into account when you're migrating your smart contracts from Ethereum- the native currency of the TRON blockchain is TRX.

As the smart contracts you've created so far use ETH, the native currency of the Ethereum blockchain, you must start by changing the ETH denominations to TRX.

Luckily, all you have to do is to update a single line of code in the `ZombieHelper` smart contract.

## Put it to the test

We've gone ahead and copied all the smart contracts you’ve written so far into the `./contracts` folder.

1. The first line of the `ZombieHelper` smart contract declares a `uint` named `levelUpFee` and sets its value to `0.001 ether`. Replace this value with `26 trx`.

> Note: When you'll actually compile and deploy the smart contract, feel free to use whatever value you think it's suitable. However, since our answer checker is really basic, it will only accept this answer as correct.
78 changes: 78 additions & 0 deletions en/20/04.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
title: Compiling the Source Code
actions: ['checkAnswer', 'hints']
requireLogin: true
material:
terminal:
help:
You should probably run `tronbox compile` followed by `tree ./build/contracts`.
commands:
"tronbox compile":
hint: tronbox compile
output: |
Compiling ./contracts/ERC721.sol...
Compiling ./contracts/Migrations.sol...
Compiling ./contracts/Ownable.sol...
Compiling ./contracts/SafeMath.sol...
Compiling ./contracts/ZombieAttack.sol...
Compiling ./contracts/ZombieFactory.sol...
Compiling ./contracts/ZombieFeeding.sol...
Compiling ./contracts/ZombieHelper.sol...
Compiling ./contracts/ZombieOwnership.sol...
Writing artifacts to ./build/contracts
"tree ./build/contracts":
hint: tree ./build/contracts
output: |
./build/contracts
├── ERC721.json
├── Migrations.json
├── Ownable.json
├── SafeMath.json
├── ZombieAttack.json
├── ZombieFactory.json
├── ZombieFeeding.json
├── ZombieHelper.json
└── ZombieOwnership.json
0 directories, 9 files
---

Now that you've migrated your smart contracts to TRON, you'll learn how to compile them.

Why do we need to compile, you ask?

The TRON Virtual Machine can't directly run Solidity source code as you write it. Thus, you must use a compiler that "translates" your source code into machine-readable **_bytecode_**. The virtual machine then executes the bytecode, and completes the actions required by your smart contract.

When you compile your smart contracts, TronBox creates a separate JSON file for each contract in the `./build/contracts/` directory. These files contain all the information TronBox needs to deploy your smart contracts: the ABI, bytecode, and compiler version. Note that the names of the JSON files do not reflect the names of the source files but the name of your smart contracts.

These files are collectively named artifacts. They are integral to the inner workings of TronBox and play an important part in the successful deployment of your smart contracts. Do not edit these files, or TronBox might stop working correctly.

Enough talking. Let's put the compiler's powers to some good use!

## Put it to the test

Before you compile the smart contracts, ensure that you're using the `0.5.18` version of the Solidity compiler. To do so, open the `tronbox.js` file in a plain-text editor and modify the `compilers` section to this:

```json
compilers: {
solc: {
version: '0.5.18'
}
}
```
1. Now you're ready to to compile your smart contracts. In the box to the right, enter the `tronbox compile` command.

2. Let's ensure that the build artifacts were created by entering the `tree ./build/contracts` command. The output should look similar to the following one:

```
./build/contracts
├── ERC721.json
├── Migrations.json
├── Ownable.json
├── SafeMath.json
├── ZombieAttack.json
├── ZombieFactory.json
├── ZombieFeeding.json
├── ZombieHelper.json
└── ZombieOwnership.json
```
Loading

0 comments on commit 611f15f

Please sign in to comment.