這是之前寫的技術文,因為當時是用英文寫的,請容許我就不翻譯了 XD如果真的看不懂英文,可以下載 Chrome 的 Google 翻譯外掛,會有助於理解。建議使用 Linux 系統開發,如果你是使用 Windows 作業系統,可以先設定好開發環境。 這篇會手把手教學,教你部署一個 ERC-20 智能合約(發幣)到以太坊的測試鏈上!
👉 如果你想跟其他幣圈投資人一起討論資訊,歡迎加入社群:漫談加密貨幣二群
目錄
Preparation & Setup
Before starting, make sure you complete below installation on the device:
Open the terminal/command line, and type below commands for checking:
>> solcjs --version // check solidity
>> truffle version // check truffle
>> ganache-cli --version // check Ganache CLI (optional)

If you don’t finish the installation, using npm
command:
>> npm install -g truffle // install truffle
>> npm install -g solc // install solidity
>> npm install -g ganache-cli // install Ganache CLI (optional)
>> npm install -g web3
>> npm install -g webpack
>> npm install -g webpack-cli
Besides, you also need to install the MetaMask on your Chrome browser.

1. Create a truffle project
First of all, create an empty directory.
You can do a right-click on the mouse button to create it, or open the terminal and type commands below:
>> mkdir simple_erc20 // create empty folder naming "simple_erc20"
>> cd simple_erc20 // move to that folder

To initialize the smart contract project, you can use the command:
>> truffle init //initialize a smart contract project

As the showed screenshot above, the truffle init command helps you to create a smart contract framework in the empty folder, with the advantage of easy d&d — development and deployment.
2. Install openzeppelin-solidity (ERC-20 standards)
ERC-20 is a technical standard used for smart contracts on the Ethereum blockchain for implementing tokens. There is another popular standard called ERC-721, and the different feature between both can be shortly described:
- ERC-20: mostly for money and money-like tokens. (coins)
- ERC-721: mostly for things and thing-like tokens. (games)
For the main purpose of this article, the ERC-20 standard will be the best choice if we want to issue our own cryptocurrency.
Luckily, there is an open-source library that allows developers to build up a secure smart contract on ERC-20 or ERC-721 standards: openzeppelin-solidity.
Thus, in this case, I will apply that library to help developing an ERC-20 smart contract application (i.e., issue our own cryptocurrency).
The method to install this library is simply typing two commands below:
>> npm init -f // create package.js
>> npm install openzeppelin-solidity // install openzeppelin
![the package.json file is necessary for installing openzeppelin-solidity [you can ignore the warnings]](https://rayskyinvest.com/wp-content/uploads/2020/06/1v7ami8A9Qge2vLSaJtf6Cw.png)
openzeppelin-solidity [you can ignore the warnings]
![You will notice that a folder node_modules is created, and the openzeppelin-solidity library is under it. [you can ignore the warnings]](https://rayskyinvest.com/wp-content/uploads/2020/06/1nYdGiKKbEzu8jRyrL3ZUCw.png)
openzeppelin-solidity library is under it. [you can ignore the warnings]
The truffle initialization of the ERC-20 project is done.
In the next step, let’s start to do some coding and to customize a cryptocurrency!
3. Coding & customize your own cryptocurrency
Remember that we are under the simple_erc20/
project, and it was initialized by the Truffle framework in the previous steps.
If you want to add/write some solidity codes, please make sure that all coding files(*.sol
) are put under the directory path contract/
.
I create a solidity file contract/DemoToken.sol
, and write codes to customize a cryptocurrency — called TCI Coin:

That is a simple program —
- [Line 3, 4]: call the open-source ERC-20 standard library.
- [Line 6]: create a contract DemoToken by inheriting ERC-20 standard.
- [Line 9]: customize a cryptocurrency named TCI coin.
You can change the contract name, initial supply amount, coin details (coin name, abbreviation, decimals) for your own case.
For the coding part, this is all you need to do — that is, we success in customizing a cryptocurrency. Congratulations!
In the next step, we will learn how to deploy this smart contract program to the local private network!
4. Deploy smart contracts to the private network, and transfer coins to wallets (testing)!
The deploying procedure will be a little bit complicated, but don’t worry, let’s make it accomplished in simple ways.
Firstly, create migrations/2_deploy_contracts.js
.
In this file, we assign which smart contracts will be deployed to the Ethereum blockchain network:

Secondly, open the Ganache-GUI, choose NEW WORKSPACE, and add simple_erc20/truffle-config.js
to the workspace project — it will open a test blockchain network for deploying the solidity codes.

Since that we didn’t set up any public test network parameters in truffle-config.js
, the default setting will be the local private network opening with port 7545.

Now go back to the terminal, compile the solidity codes:
>> truffle compile // compile smart contract codes to bytecodes

Then deploy the bytecodes (compiled codes) to the local private network:
>> truffle migrate // deploy compiled codes on local network(by default)


Some important information you should notice (below is my case):
- contract address: 0x2Cea006780Ac113B230eB42dfA384a3e463fE4f7
- account (address): 0x51bf312C36C86245812DeFA10657A709D013cf56
- Final cost: 0.03096222 ETH
Note that when we deploy smart contracts to the net, the Ganache will listen to the network, and update the information simultaneously:
![[Left] The first account loses some ETH (about 0,03 ETH) because the deployment procedure needs costs; the first account address is as same as the account address(yellow-underline) shown on the second terminal screenshots above. [Right] the contract is successfully deployed to the network, with the red-underline address which is as same as contract address(red-underline) shown on the second terminal screenshots above;](https://rayskyinvest.com/wp-content/uploads/2020/06/1OvxJKJooPYwMP0O20ioWsQ.png)
So far, we successfully deploy the smart contracts to the local private network, creating our cryptocurrency coins!
However, there are still some questions: how can we inspect whether these coins are real in the wallet? How to transfer coins to others?
Now the MetaMask helps!
Open your chrome browser, and click the MetaMask; type the 12 wallet seed phrases to restore the account
(Tips: you can find out the 12 seed words in Ganache-GUI):


Besides, note that the MetaMask account is on the public Ethereum network, we need to switch it to the local private network created in the previous steps (HTTP://127.0.0.1:7545) by setting the ERC network:

It’s almost closed!
One more step should be taken: click the add coin function, then copy & paste the contract address.
Finally, we can see the customized coins (TCI coins) in the wallet on the private network:
![[Left] Choose the add coin function, copy&paste the contract address [Right] Congrats! You have the issued coins in the wallet!](https://rayskyinvest.com/wp-content/uploads/2020/06/1bq4RTEkbem-AL7HsToPpjA.png)
Bingo!
We have the issued coins in the wallet (on the private network)!
And what would you do when you have money?
That right, let’s start to spend these coins — which means sending these coins to others (wallets).
For example, I transferred a huge amount of TCI coin to another wallet address:
- From: 0x51bf312C36C86245812DeFA10657A709D013cf56
- To: 0xB9A940D2C090780DC415492B67cFaF76edFF052C
- Amount: 100000000 TCI
And we can inspect the TX(transaction) on either MetaMask or Ganache interface:

It’s really interesting, isn’t it?
5. Deploy smart contracts to the Ethereum Testnet (Rinkeby) and testing!
In this step, we will issue our own crypto-coin.
We have successfully created our cryptocurrency coins to the private network (i.e., in the local device).
However, if we want to issue these coins to the Real Ethereum Network, it’s necessary to deploy smart contracts to Ethereum Testnets first.
The reason is that any program should be tested and verified efficiently before the official deployment.
In Ethereum networks, there are three famous test networks:
- Ropsten
- Kovan
- Rinkeby
For their difference and features, please refer to this article!
In the following steps, I will choose Rinkeby as the Ethereum test network where we deploy the smart contracts.
But before deploying smart contracts to Rinkeby (one of the Testnets), we need to first create a legal account on Rinkeby and beg some ETH for deploying gas.
The applying steps are below:
- Copy your MetaMask wallet address, post the address to twitter or Facebook publicly.
- Go to the Rinkeby Faucet Web, paste the public post URL for some ETHs.
I use the same account address(as in private network deployment) and beg 3 ETH(only be legal on Rinkeby).
We now have 3 ETH in the wallet, and the account address can be found on public Rinkeby network:
![[Account Address]: 0x51bf312C36C86245812DeFA10657A709D013cf56; you can check this account on RINKEBY network](https://rayskyinvest.com/wp-content/uploads/2020/06/1UDo65gYz0wBQr0clkpFffQ.png)
Besides, we also need Ethereum nodes to deploy the smart!
There are two ways to access the Ethereum nodes:
- Run your own Ethereum node on your computer (using
geth
) - Register Infura Service to access Ethereum nodes by API (I choose this method)

For security reasons, Infura does not manage the private keys.
Thus, we need to use Truffle HDWallet Provide — a library that allows us topreviously sign transactions through Truffle before sending them to Infura nodes.
We can install the Truffle-HDWallet-Provider via command:
>> npm install truffle-hdwallet-provider@web3-one --save

Open the fule truffle-config.js
, paste the following codes below and fill the two variables according to your situation:
- mnemonic: your account 12 remember words(12 seed phrases)
- infura_url: Infura Rinkeby Endpoint-API (you can copy & paste this URL from the Infura project you created)
var HDWalletProvider = require("truffle-hdwallet-provider");
var mnemonic = "<your account twelve words(seed phrases) ...>";
var infura_url = "<Infura Rinkeby Endpoint-API>";
module.exports = {
// See <http://truffleframework.com/docs/advanced/configuration>
// to customize your Truffle configuration!
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*" // Match any network id
},
rinkeby: {
provider: function() {
return new HDWalletProvider(mnemonic, infura_url)
},
network_id: "*"
}
}
};
After the above setting, it’s ready to deploy the smart contracts to Rinkeby with the following command below:
>> truffle migrate --network rinkeby
For the windows system, you might encounter some errors like Cannot find module ‘bindings’ & ‘any-promise’.
It’s easy to solve the problem by commands:
npm install any-promise --save
andnpm install bindings --save
The successful deployment to Rinkeby will be like this:


On the public Rinkeby test network, we successes deploy the smart contract (i.e., issuing TCI coin).
Note that there is two important information:
- contract address: 0x2Cea006780Ac113B230eB42dfA384a3e463fE4f7
- account address: 0x51bf312C36C86245812DeFA10657A709D013cf56
Besides, we(everyone) can check this deployment to Rinkeby Testnet publicly!

On MetaMask wallet, we can also check out how much TCI coin we have:

About testing, we can spend/send these TCI coins to others.
For example, I create a transaction and its detail is below:
- From: 0x51bf312C36C86245812DeFA10657A709D013cf56
- To: 0x2Cea006780Ac113B230eB42dfA384a3e463fE4f7
- Amount: 1000 TCI
The related TX information can be inspected on either MetaMask or Rinkeby network:


If you want the TCI coin, please feel free to leave the account address, and I will transfer 100000000 TCI coin to you (but only in Rinkeby test network ❤)
Conclusion
In this article, we learned three things:
- how to build a customized cryptocurrency depending on ERC-20 standard!
- how to deploy a smart contract to both local private network & one of the public Ethereum Testnets (Rinkeby)!
- how to check TX records and test(transfer) the issued coins!
The result is publicly checked on the Rinkeby TestNet (with account address 0x51bf312C36C86245812DeFA10657A709D013cf56).
The whole deployment concept is not difficult; however, I still encounter several unexpected errors about environment preparation & installation.
Just because my device OS is windows — an unfriendly system for developers LOL.
Thus, please choose the Linux-based system for developing Dapp if it’s possible. Otherwise, you will be suffering!!!
Thanks for your time, and please feel free to leave feedback & comment if you like this article or you find anything wrong!
合作工商
加密貨幣其實就像台股一樣,你可以享有交易手續費折扣,不過取得方式卻不一樣。
在台股,你可以親自和業務員談手續費減免;但在幣圈,你必須透過推薦連結開戶才能享有手續費減免。
手續費減免的幅度也有分等級,這裡提供的都是折扣幅度最高的推薦連結,也就是說你在交易時能夠享受最大幅度的手續費折扣,而且保證是全球最大幅度的。
如果你今天在交易上多省 1% 的手續費,就代表你在投資績效上可以多賺 1%,更不用說下面的推薦連結都不只 1%,動輒 5% – 20% 的任君選擇。所以在幣圈投資時,用越好的手續費折扣連結來註冊,你在交易時能取得的投資績效也就越好。
交易所 | 這間交易所適合做什麼操作? | 終身交易手續費折扣 | 註冊推薦碼 |
幣安 Binance | 現貨、合約交易,以及質押領息 | 最高 40%(20%折扣 +20%BNB費率抵免) | YIX7CMSZ |
派網 Pionex | 開網格天地單、抄底寶,用期現套利領高額利息 | 20% | EhYBkd6v |
FTX 交易所 | 購買股權通證和 8% 活存利息 | 5% | rayskyinvest |
Bitfinex | 美元放貸,獲取 18~25% 的利息收入 | 6% | MkvZwqTRR |
Gate.io | 買平台幣 GT、小幣交易,提前布局潛力項目 | 首年享 30% 費率折扣 | 5961249 |
BingX | 合約交易與實體福利,享受一百多間在地店家優惠 | 90 天享 20% 費率折扣 | HTDTMS5H |
Kucoin | 閒幣賺息 | 享 0.02% 手續費 | rJ6X7EW |
Bybit | 現貨、合約交易 | 享 0.1% 吃單手續費 | 21215 |
【精選專欄】
【其他區塊鏈知識文章】
- 區塊鏈教學:用白話文看懂比特幣與挖礦是什麼?
- 加密貨幣投資第一步:台灣最完整買賣 USDT 入金方式教學
- 新手加密貨幣教學:區塊鏈原理、避免詐騙、可靠交易所統整
- 區塊鏈教學:以太坊 Dapps 事前準備-如何在 Windows 作業系統上設定好開發環境?
- 區塊鏈的資安議題:常見的駭客攻擊有哪些?論文導讀《Exploring the Attack Surface of Blockchain: A Systematic Overview》
- 區塊鏈教學:利用 Windows 部署、開發 ERC-20 加密貨幣
- 區塊鏈知識:比特幣披薩日!第一筆比特幣交易
- The SandBox 是什麼?虛擬土地價值與應用,附帶土地購買教學詳解
- 區塊鏈介紹:PoS 機制是什麼?如何運作?有何風險?
- 公鏈什麼意思?又有哪些加密貨幣可以投資?
【訂閱、追蹤、更了解雷司紀的小道投資在做些什麼】