Skip to content

Commit

Permalink
L10-12 Chinese translation (CryptozombiesHQ#437)
Browse files Browse the repository at this point in the history
* Lesson 12 Chinese

* L10-11

* move zh-cn to it's own folder

Co-authored-by: Hank X <[email protected]>
  • Loading branch information
tinatang1021 and hankxdev committed Dec 30, 2019
1 parent d91e89f commit cf6b655
Show file tree
Hide file tree
Showing 68 changed files with 6,515 additions and 2,659 deletions.
15 changes: 8 additions & 7 deletions en/10/00-overview.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
---
title: "Deploying Ethereum DApps with Truffle"
header: "Deploying Ethereum DApps with Truffle"
title: "Deploying DApps with Truffle"
header: "Deploying DApps with Truffle"
roadmap: roadmap.jpg
path: solidity_advanced
position: 2
publishedOn: Cryptozombies
---

Remember that we promised to teach you how to deploy smart contracts to **_Ethereum_**?
Remember that we promised to teach you how to deploy a smart contract?

It's been a while, but the time has come!

In this lesson, we will be teaching you how to deploy to **_Ethereum_** using **_Truffle_**.

And this is not all. You are going to learn how to deploy your smart contracts to **Loom**, and how to interact with them using **_web3.js_**.
And this is not all. You are going to learn how to deploy your smart contracts to **_Loom_** as well😎.

Why deploy to **Loom**? After all, **_Ethereum_** is the most secure network.
Why deploy to **_Loom_**? After all, **_Ethereum_** is the most secure network.

Yes, we totally agree with that. But on Ethereum each transaction costs _gas_, so your users will have to pay fees for each and every transaction. Also, they will have to wait at least 10 seconds for each transaction to be confirmed.

In a nutshell, **_on Ethereum, all transactions benefit from the same security guarantees_**. For something like a game, this level of security isn't a requirement. In fact, it just harms the user experience.
In a nutshell, **_on Ethereum, all transactions benefit from the same security guarantees_**. For something like a user-facing DApp or a game, this level of security isn't always a requirement. In fact, it just harms the user experience.

On **Loom**, users will have access to much speedier and gas-free transactions. This makes DAppChains a much better fit for something like a game or a user-facing DApp.
On **_Loom_**, users have access to much speedier and gas-free transactions. This makes **_Loom_** a much better fit for something like a game or a user-facing DApp.

Enough talking! Let's get started😉
18 changes: 9 additions & 9 deletions en/10/01.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,36 @@ material:
added 81 packages from 311 contributors in 5.104s
---

If you're new to **CryptoZombies**, it's highly recommended that you go over the first six lessons before starting this one. Please take the time to get familiar with writing a smart contract and interacting with it using `Web3.js`. Otherwise, it will be quite hard to follow this lesson.
If you're new to **CryptoZombies**, it's highly recommended that you go over the first six lessons before starting this one. Please take the time to get familiar with writing a smart contract. Otherwise, it will be quite hard to follow this lesson.

## The Missing Piece

So you've worked your way through our previous lessons. Awesome! This means you've almost mastered the art of building DApps.

But an important piece of the puzzle is still missing.

That's right... you still have to learn how to **_deploy a smart contract to Ethereum_**.
That's right... you still have to learn how to **_deploy a smart contract_**.

Now, if you have a background in front-end development, you are probably well accustomed to the multitude of tools like *Webpack, Gulp, or Browserify* that make a developer's life simpler.

But what tools do `Solidity` developers use?
But what tools do **Solidity** developers use?

## Truffle

`Truffle` is the most popular blockchain development framework for good reason - it's packed with lots of useful features:
**_Truffle_** is the most popular blockchain development framework for good reason - it's packed with lots of useful features:

- easy smart contract compilation
- automated ABI generation
- integrated smart contract testing - there's even support for `Mocha` and `Chai`!
- support for multiple networks - code can be deployed to Rinkeby, **_Ethereum_** or even to **Loom**. We'll walk you through this later😉
- integrated smart contract testing - there's even support for **Mocha** and **Chai**!
- support for multiple networks - code can be deployed to Rinkeby, **_Ethereum_** or even to **_Loom_**. We'll walk you through this later😉

Provided that `npm` and `node` have been installed, we'll want you to install `truffle` and make it available globally.
Provided that `npm` and `node` have been installed on your computer, we'll want you to install **Truffle** and make it available globally.

# Put It to the Test

We'll start with the basics. Fire up a new terminal window, create a directory called "CryptoZombies", and `cd` into it.
We'll start with the basics. Fire up a new terminal window, create a directory called `CryptoZombies`, and `cd` into it.

1. Now, let's install `truffle` and make it available globally.
1. Now, let's install **Truffle** and make it available globally.

>Note: Here's how you can use `npm` to install a package and make it available globally:
Expand Down
27 changes: 14 additions & 13 deletions en/10/02.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ material:
found 0 vulnerabilities
---

Now that we've installed `truffle`, it's time to initialize our new project by running `truffle init`. All it is doing is to create a set of folders and config files with the following structure:
Now that we've installed **Truffle**, it's time to initialize our new project by running `truffle init`. All it is doing is to create a set of folders and config files with the following structure:

```
├── contracts
Expand All @@ -42,40 +42,41 @@ truffle.js

Contracts, migrations, tests... this is pretty complicated😟

Don't worry, learning to use `truffle` won't eat your brains. This chapter will walk you through `truffle`'s default project structure, and once you know how to use `truffle`, deploying smart contracts will be a breeze.
Don't worry, learning to use **Truffle** won't eat your brains. This chapter will walk you through **Truffle**'s default project structure, and once you know how to use **Truffle**, deploying smart contracts will be a breeze.

## Truffle's Default Directory Structure

So, inside of "CryptoZombies" directory, `truffle init` should create several directories and some JavaScript and Solidity files. Let's have a closer look:
So, running the `truffle init` command inside of the `CryptoZombies` directory, should create several directories and some JavaScript and Solidity files. Let's have a closer look:

- **_contracts_**: this is the place where `truffle` expects to find all our smart contracts. To keep the code organized, we can even create nested folders such as `contracts/tokens`. Pretty neat😉.
>Note: `truffle init` should automatically add a contract called `Migrations.sol` and the corresponding migration file. We'll explain them a bit later.
- **_contracts_**: this is the place where **Truffle** expects to find all our smart contracts. To keep the code organized, we can even create nested folders such as `contracts/tokens`. Pretty neat😉.

- **_migrations_**: a migration is a JavaScript file that tells `truffle` how to deploy the smart contract.
>Note: `truffle init` should automatically create a contract called `Migrations.sol` and the corresponding migration file. We'll explain them a bit later.
- **_migrations_**: a migration is a JavaScript file that tells **Truffle** how to deploy a smart contract.

- **_test_**: here we are expected to put the unit tests which will be JavaScript or Solidity files. Remember, once a contract is deployed it can't be changed, making it essential that we test our smart contracts before we deploy them.

- **_truffle.js_** and **_truffle-config.js_**: config files used to store the network settings for deployment. `Truffle` needs two config files because on Windows having both `truffle.js` and `truffle.exe` in the same folder might generate conflicts. Long story short - if you are running Windows, it is advised to delete `truffle.js` and use `truffle-config.js` as the default config file. Check out _Truffle's_ official <a href="https://truffleframework.com/docs/truffle/reference/configuration" target=_blank>documentation</a> to further your understanding.
- **_truffle.js_** and **_truffle-config.js_**: config files used to store the network settings for deployment. **Truffle** needs two config files because on Windows having both `truffle.js` and `truffle.exe` in the same folder might generate conflicts. Long story short - if you are running Windows, it is advised to delete `truffle.js` and use `truffle-config.js` as the default config file. Check out **Truffle**'s <a href="https://truffleframework.com/docs/truffle/reference/configuration" target=_blank>official documentation</a> to further your understanding.


But why should I use this directory structure? I'm not used to it and it looks complicated...

Well, there's are a few good reasons. First, `Truffle` will not work as expected if you change the names of these folders.
Well, there's are a few good reasons. First, **Truffle** will not work as expected if you change the names of these folders.

Second, by adhering to this convention your projects will be easily understood by other developers. To put it short, using standard folder structures and code conventions make it easier if you expand or change your team in the future.
Second, by adhering to this convention your projects will be easily understood by other developers. To put it short, using a standard folder structures and code conventions make it easier if you expand or change your team in the future.

## truffle-hdwallet-provider

In this lesson, we will be using _Infura_ to deploy our code to **_Ethereum_**. This way, our users will be allowed to run the application without needing to set up their own **_Ethereum_** node or wallet.
However, to keep things secure, _Infura_ does not manage private keys, which means it can't sign transactions on our behalf. Since deploying a smart contract requires `truffle` to sign transactions, we are going to need a tool called `truffle-hdwallet-provider`. Its only purpose is to handle the transaction signing.
In this lesson, we will be using _Infura_ to deploy our code to **_Ethereum_**. This way, we can run the application without needing to set up our own **_Ethereum_** node or wallet.
However, to keep things secure, _Infura_ does not manage the private keys, which means it can't sign transactions on our behalf. Since deploying a smart contract requires **Truffle** to sign transactions, we are going to need a tool called `truffle-hdwallet-provider`. Its only purpose is to handle the transaction signing.

>Note: Maybe you are asking why we chose not to install `truffle-hdwallet-provider` in the previous chapter using something like:
```JavaScript
npm install truffle truffle-hdwallet-provider -g
npm install truffle truffle-hdwallet-provider
```

Well... `truffle init` expects to find an empty directory. If there's any file there, it will error out, so we need to do everything in the correct order and install `truffle-hdwallet-provider` after we run `truffle init`.
Well... the `truffle init` command expects to find an empty directory. If there's any file there, it will error out. Thus, we need to do everything in the correct order and install `truffle-hdwallet-provider` after we run `truffle init`.


# Put it to the test:
Expand Down
10 changes: 5 additions & 5 deletions en/10/03.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Curious about how does the bytecode look like? Let's take a look:
"0x60806040526010600155600154600a0a6002556201518060035566038d7ea4c6800060085560006009556046600a55336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1..."
```

A you can see, a human is about as likely to be able to read bytecode as a real life zombie!
As you can see, a human is about as likely to be able to read bytecode as a real life zombie!

## Using the Solidity Compiler

Expand All @@ -59,11 +59,11 @@ safemath.sol:110:11: Warning: Function state mutability can be restricted to pur
^ (Relevant source part starts here and spans across multiple lines).
```

What the compiler is trying to say is that our function does not read or write to the blockchain and that we should use the `pure` modifier.
What the compiler is trying to say is that our function does not read or write from/to the blockchain and that we should use the `pure` modifier.

Why is this important?

Well, making a function `pure` or `view` will save us gas. Since these functions are not going to modify the state of the blockchain, there is no need for miners to execute them. To put it in a few words, `pure` and `view` functions can be `call`ed for free.
Well, making a function `pure` or `view` saves us gas. Since these functions are not going to modify the state of the blockchain, there is no need for miners to execute them. To put it in a few words, `pure` and `view` functions can be `call`ed for free.


## CryptoZombies- The Game
Expand Down Expand Up @@ -110,6 +110,6 @@ Everything is set up properly. Let's compile our project.

# Put it to the test:

1. Execute `truffle compile`. This command should create the "build artifacts" and place them in the `./build/contracts` directory.
1. Execute `truffle compile`. This command should create the build artifacts and place them in the `./build/contracts` directory.

>Note: The build artifacts are comprised of the "bytecode" versions of the smart contracts, ABIs, and some internal data `truffle` is using to correctly deploy the code. Avoid editing these files, or `truffle` might stop working correctly.
>Note: The build artifacts are comprised of the "bytecode" versions of the smart contracts, ABIs, and some internal data **Truffle** is using to correctly deploy the code. Avoid editing these files, or **Truffle** might stop working correctly.
10 changes: 6 additions & 4 deletions en/10/04.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ material:

Normally at this point, before deploying to **_Ethereum_**, you would want to test your smart contract locally. You can do this using a tool called <a href="https://truffleframework.com/ganache" target=”_blank”>Ganache</a>, which sets up a local **_Ethereum_** network.

However, while testing is very important, it would require an entire lesson to cover — so we’re just going to stick to deployment in this lesson.
However, while testing is very important, it requires an entire lesson to cover — so we’re just going to stick to deployment in this lesson. If you're so inclined, to learn more about testing, I recommend our very own <a href="http://cryptozombies.io/en/lesson/10" target=”_blank”>Testing Smart Contracts with Truffle</a> lesson.

<!-- TODO: update ^^ link, if needed -->

To deploy to **_Ethereum_** we will have to create something called a **migration**.

Migrations are JavaScript files that help `truffle` deploy the code to **_Ethereum_**. Note that `truffle init` created a special contract called `Migrations.sol` that keeps track of the changes you're making to your code. The way it works is that the history of changes is saved onchain. Thus, there's no way you will ever deploy the same code twice.
Migrations are JavaScript files that help **Truffle** deploy the code to **_Ethereum_**. Note that `truffle init` created a special contract called `Migrations.sol` that keeps track of the changes you're making to your code. The way it works is that the history of changes is saved onchain. Thus, there's no way you will ever deploy the same code twice.

## Creating a New Migration

Expand All @@ -40,9 +42,9 @@ module.exports = function(deployer) {

Pretty straightforward, isn't it?

First, the script tells `truffle` that we'd want to interact with the `Migrations` contract.
First, the script tells **Truffle** that we'd want to interact with the `Migrations` contract.

Next, it exports a function that accepts an object called `deployer` as a parameter. This object acts as an interface between you (the developer) and `truffle`'s deployment engine. Even though the `deployer` provides a multitude of useful functions, we won't be using them in the scope of this lesson. Once you've finished, feel free to check out Truffle's <a href="https://truffleframework.com/docs/truffle/getting-started/running-migrations" target=”_blank”>documentation</a> if you're inclined to learn more about _Truffle's_ abilities.
Next, it exports a function that accepts an object called `deployer` as a parameter. This object acts as an interface between you (the developer) and **Truffle**'s deployment engine. Even though the `deployer` provides a multitude of useful functions, we won't be using them in the scope of this lesson. Once you've finished, feel free to check out **Truffle**'s <a href="https://truffleframework.com/docs/truffle/getting-started/running-migrations" target=”_blank”>documentation</a> if you're inclined to learn more about **Truffle**'s abilities.

To get everything ready for deployment, we've gone ahead and created a new file `./contracts/2_crypto_zombies.js`, and copied and pasted the content from `./contracts/1_initial_migration.js`.

Expand Down
Loading

0 comments on commit cf6b655

Please sign in to comment.