Share Dialog
Share Dialog

Subscribe to kiralee.eth

Subscribe to kiralee.eth
<100 subscribers
<100 subscribers
Step by step teach you to write smart contracts and deploy to the Ethereum testnet
cryptozombies是一個開源免費solidity的教學網站,他透過簡單的教學及遊戲的方式,帶領人們走進web3的開發世界。在此我們將使用cryptozombies的程式碼,以及透過remix線上編譯器,實際將code部署到以太坊的測試網中。程式碼由Loom network所支持。
cryptozombies is an open source free solidity teaching website. It leads people into the world of web3 development through simple teaching and games.
Here we will use the code from cryptozombies and use the remix online compiler to actually deploy the code to the Ethereum testnet. The code powered by Loom Network
Remix是一個以太坊線上編譯器,可以在上面撰寫solidity的智能合約,他有著測試、部署、compile的完善功能。
Remix is an Ethereum online compiler that can write solidity smart contracts on it. It has perfect functions for testing, deploying, and compiling.

我們將在contracts資料夾底下,建立空白的智能合約,並使用cryptozombies的第一章節程式碼。程式碼在此
We will create a blank smart contract under the contracts folder and use the code from lesson 1 of cryptozombies. code here

之後在左側的面板中選到SOLIDITY COMPILER,勾選Auto compile及Enable optimization,讓remix自動幫我們編譯程式碼,以及優化Gas。
Then select SOLIDITY COMPILER in the left panel, check Auto compile and Enable optimization, let remix automatically compile the code for us, and optimize Gas.

如果程式碼有錯誤,就會在旁邊顯示驚嘆號,告訴你可能有語法問題。
If there is an error in the code, an exclamation mark will be displayed next to it, telling you that there may be a syntax problem.

確認無語法問題之後,我們在左邊選到DEPLOY & RUN TRANSACTIONS,選擇環境是Remix VM,先在虛擬機上測試程式功能是否完整,按下Deploy部署智能合約。
After confirming that there is no grammar problem, we select DEPLOY & RUN TRANSACTIONS on the left, select the environment as Remix VM, first test whether the program function is complete on the virtual machine, and press Deploy to deploy the smart contract.

在左邊下方Deployed Contracts,就是部署出來的智能合約,createRandomZombie與zombies則是智能合約中的兩個function,我們可以跟他互動。
Deployed Contracts at the bottom left is the deployed smart contract, createRandomZombie and zombies are two functions in the smart contract, we can interact with them.

例如我們在createRandomZombie,輸入一個名稱zombie1作為創建出的第一隻殭屍。
For example, in createRandomZombie, enter a name zombie1 as the first zombie created.

在zombies的function輸入編號0,就可以查出,我們的第一隻殭屍確實叫做zombie1,且他的dna會是keccak256,編譯字串zombie1後,產生出來的偽隨機數。若輸入的殭屍名稱不是zombie1,則會得到不同的dna結果。
Enter the number 0 in the zombies function, and you can find out that our first zombie is indeed called zombie1, and his DNA will be keccak256, after compiling the string zombie1, the generated pseudo-random number. If the input zombie name is not zombie1, you will get a different DNA result.

Deploy smart contracts to the Ethereum testnet
在開始之前,我們要先有一個區塊鏈錢包,例如MetaMask。
Before we start, we need to have a blockchain wallet, such as MetaMask.

並透過測試網水龍頭,領取一些測試網的以太幣。
And through the testnet faucet, receive some testnet ether.

等待一些時間後,將錢包切換到Goerli測試網,就可以看到接收的測試幣。
After waiting for some time, switch the wallet to the Goerli testnet, and you can see the test coins received.

回到remix,在DEPLOY & RUN TRANSACTIONS頁面,將環境選到Injected Provider,代表remix要實際將合約部署到區塊鏈上。
Back to remix, on the DEPLOY & RUN TRANSACTIONS page, select the environment as Injected Provider, which means that remix will actually deploy the contract to the blockchain.

remix會詢問是否連線到MetaMask。
remix will ask to connect to MetaMask.

連線成功後,可以檢查Remix上的Account與MetaMask的錢包地址與餘額是否一樣。
After the connection is successful, you can check whether the wallet address and balance of Account on Remix and MetaMask are the same.

點選Deploy就可以將智能合約部署到以太坊測試網。
Click Deploy to deploy the smart contract to the Ethereum testnet.

合約部署中
Contract Deployment

部署完成後,狀態列會顯示view on etherscan,就可以在區塊鏈上查看合約部署的狀態。
After the deployment is complete, the status column will display view on etherscan, and you can view the status of the contract deployment on the blockchain.

點選之後就會轉到etherscan的頁面,可以看到合約部署是否成功。
After clicking, it will go to the etherscan page, and you can see whether the contract deployment is successful.

點下合約ID來查看你的合約
Click Contract ID to see your contract

合約會顯示在區塊高度7264348時被部署,從錢包地址0xc0ABD7A0C38a5Ca4E6eAc39b49837a01A35A3FC4部署出來的。
The contract will appear to be deployed at block height 7264348 from wallet address 0xc0ABD7A0C38a5Ca4E6eAc39b49837a01A35A3FC4.

選擇contract,驗證和發布合約
Click Contract to Verify and Publish contract

我們是只有單一個合約,所以選Single file,Compiler版本選擇Remix上的版本,License選擇GPLv3。
We only have a single contract, so choose Single file, Compiler version choose Remix version, License choose GPLv3.

complier版本查看remix
The complier version is viewed on remix

將remix上的code貼過來,記得optimization要選擇yes,如果你在寫合約的時候沒有勾它則不用選擇。
Paste the code on remix, remember to choose yes for optimization.If you didn’t tick it when you wrote the contract, you don’t have to choose.


發佈之後,成功就代表你的合約已驗證,可以直接用etherscan跟合約互動。
After the publish, success means that your contract has been verified, and you can directly use etherscan to interact with the contract.

回到合約頁面,會發現有綠色勾。
Go back to the contract page, you will find a green tick.

往下可以看到你的code。
You can see your code below.

點選Write Contract就可以跟合約互動。
Click Write Contract to interact with the contract.

選擇**connect to Web3 **串接MeatMask。
Select connect to Web3 to connect MeatMask.

串接成功後,你就可以建立一個殭屍,需要付Gas Fee。
After the connected, you can create a zombie, you need to pay Gas Fee.

可以看交易是否成功。
You can see if the transaction is successful.

交易成功,代表成功跟合約互動,產生出殭屍.
If the transaction is successful, it means that it successfully interacts with the contract and generates zombies.

往下拉,可以看到我們呼叫的function,就是createRandomZombie。
Scroll down to see the function we called, which is createRandomZombie

輸入的值是kiraZombie1
Input Data is kiraZombie1

回到合約頁面,read contract,輸入0,可以查詢到我們剛剛建立的殭屍。
Go back to the contract page, read contract, enter 0, you can query the zombie we created.

到合約的Events,可以看到我們寫的event有成功記錄了,我們生成殭屍的訊息。
In the Event of the contract, we can see that the event we wrote has been successfully recorded, and we generate the message of the zombie.

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
//宣告solidity程式碼的版本
//Declares the version of the solidity code
contract ZombieFactory {
//建立一個智能合約,名為ZombieFactory
//Build a smart contract called ZombieFactory
event NewZombie(uint zombieId, string name, uint dna);
//宣告一個事件,名為NewZombie,有新疆屍被建立的時候會觸發此通知,顯示在區塊鏈交易的log記錄內。
//Declare an event called NewZombie that triggers the notification when a new zombie is created and shows up in the log records of blockchain transactions.
uint dnaDigits = 16;
uint dnaModulus = 10 ** dnaDigits;
//宣告殭屍的dna是16個位元
//The dna that declares a zombie is 16 bits
struct Zombie {
string name;
uint dna;
}
//宣告一個物件,名為Zombie,每一個物件有name跟dna兩個屬性。
//Declare an object named Zombie, and each object has two attributes, name and dna.
Zombie[] public zombies;
//宣告一個陣列,名為zombies,它是由物件Zombie組成的。
//Declare an array, named zombies, which is composed of the objects Zombies.
function _createZombie(string memory _name, uint _dna) private {
zombies.push(Zombie(_name, _dna));
uint id = zombies.length — 1;
emit NewZombie(id, _name, _dna);
}
//宣告一個function,名為_createZombie,用來把新殭屍放入陣列中,並獲得id,這個function傳入兩個值,這兩個值會作為Zombie物件的屬性,這一個新物件,將被放到陣列zombies中,之後透過zombies陣列的長度-1,取得殭屍的編號id,並觸發事件,把新殭屍的資訊記錄在區塊鏈交易的log內。
//Declare a function named _createZombie, which is used to put new zombies into the array and get the id. This function passes in two values, which will be used as attributes of the Zombie object. This new object will be put into the zombies array, and then obtain the ID of the zombie through the length of the zombies array -1, and trigger an event to record the information of the new zombie in the log of the blockchain transaction.
function _generateRandomDna(string memory _str) private view returns (uint) {
uint rand = uint(keccak256(abi.encodePacked(_str)));
return rand % dnaModulus;
}
//宣告一個function,名為_generateRandomDna,用來獲得殭屍的dna,這個function傳入一個值,這個值會經由keccak256計算之後,得出32位元的偽隨機數,這個偽隨機數,會只保留從右往左數的16個位元。
//Declare a function named _generateRandomDna, which is used to obtain the dna of the zombie. This function passes in a value, which will be calculated by keccak256 to obtain a 32-bit pseudo-random number. This pseudo-random number will only keep 16 bits from right to left.
function createRandomZombie(string memory _name) public {
uint randDna = _generateRandomDna(_name);
_createZombie(_name, randDna);
}
}
//宣告一個function,名為createRandomZombie,用來建立殭屍,此function傳入一個值,這個值會去拿到dna,之後把此值(殭屍的名字)與dna,拿去建立殭屍。
//Declare a function named createRandomZombie, which is used to create a zombie. This function passes in a value, which will go to get DNA, and then use this value (the zombie’s name) and DNA to create a zombie.
Step by step teach you to write smart contracts and deploy to the Ethereum testnet
cryptozombies是一個開源免費solidity的教學網站,他透過簡單的教學及遊戲的方式,帶領人們走進web3的開發世界。在此我們將使用cryptozombies的程式碼,以及透過remix線上編譯器,實際將code部署到以太坊的測試網中。程式碼由Loom network所支持。
cryptozombies is an open source free solidity teaching website. It leads people into the world of web3 development through simple teaching and games.
Here we will use the code from cryptozombies and use the remix online compiler to actually deploy the code to the Ethereum testnet. The code powered by Loom Network
Remix是一個以太坊線上編譯器,可以在上面撰寫solidity的智能合約,他有著測試、部署、compile的完善功能。
Remix is an Ethereum online compiler that can write solidity smart contracts on it. It has perfect functions for testing, deploying, and compiling.

我們將在contracts資料夾底下,建立空白的智能合約,並使用cryptozombies的第一章節程式碼。程式碼在此
We will create a blank smart contract under the contracts folder and use the code from lesson 1 of cryptozombies. code here

之後在左側的面板中選到SOLIDITY COMPILER,勾選Auto compile及Enable optimization,讓remix自動幫我們編譯程式碼,以及優化Gas。
Then select SOLIDITY COMPILER in the left panel, check Auto compile and Enable optimization, let remix automatically compile the code for us, and optimize Gas.

如果程式碼有錯誤,就會在旁邊顯示驚嘆號,告訴你可能有語法問題。
If there is an error in the code, an exclamation mark will be displayed next to it, telling you that there may be a syntax problem.

確認無語法問題之後,我們在左邊選到DEPLOY & RUN TRANSACTIONS,選擇環境是Remix VM,先在虛擬機上測試程式功能是否完整,按下Deploy部署智能合約。
After confirming that there is no grammar problem, we select DEPLOY & RUN TRANSACTIONS on the left, select the environment as Remix VM, first test whether the program function is complete on the virtual machine, and press Deploy to deploy the smart contract.

在左邊下方Deployed Contracts,就是部署出來的智能合約,createRandomZombie與zombies則是智能合約中的兩個function,我們可以跟他互動。
Deployed Contracts at the bottom left is the deployed smart contract, createRandomZombie and zombies are two functions in the smart contract, we can interact with them.

例如我們在createRandomZombie,輸入一個名稱zombie1作為創建出的第一隻殭屍。
For example, in createRandomZombie, enter a name zombie1 as the first zombie created.

在zombies的function輸入編號0,就可以查出,我們的第一隻殭屍確實叫做zombie1,且他的dna會是keccak256,編譯字串zombie1後,產生出來的偽隨機數。若輸入的殭屍名稱不是zombie1,則會得到不同的dna結果。
Enter the number 0 in the zombies function, and you can find out that our first zombie is indeed called zombie1, and his DNA will be keccak256, after compiling the string zombie1, the generated pseudo-random number. If the input zombie name is not zombie1, you will get a different DNA result.

Deploy smart contracts to the Ethereum testnet
在開始之前,我們要先有一個區塊鏈錢包,例如MetaMask。
Before we start, we need to have a blockchain wallet, such as MetaMask.

並透過測試網水龍頭,領取一些測試網的以太幣。
And through the testnet faucet, receive some testnet ether.

等待一些時間後,將錢包切換到Goerli測試網,就可以看到接收的測試幣。
After waiting for some time, switch the wallet to the Goerli testnet, and you can see the test coins received.

回到remix,在DEPLOY & RUN TRANSACTIONS頁面,將環境選到Injected Provider,代表remix要實際將合約部署到區塊鏈上。
Back to remix, on the DEPLOY & RUN TRANSACTIONS page, select the environment as Injected Provider, which means that remix will actually deploy the contract to the blockchain.

remix會詢問是否連線到MetaMask。
remix will ask to connect to MetaMask.

連線成功後,可以檢查Remix上的Account與MetaMask的錢包地址與餘額是否一樣。
After the connection is successful, you can check whether the wallet address and balance of Account on Remix and MetaMask are the same.

點選Deploy就可以將智能合約部署到以太坊測試網。
Click Deploy to deploy the smart contract to the Ethereum testnet.

合約部署中
Contract Deployment

部署完成後,狀態列會顯示view on etherscan,就可以在區塊鏈上查看合約部署的狀態。
After the deployment is complete, the status column will display view on etherscan, and you can view the status of the contract deployment on the blockchain.

點選之後就會轉到etherscan的頁面,可以看到合約部署是否成功。
After clicking, it will go to the etherscan page, and you can see whether the contract deployment is successful.

點下合約ID來查看你的合約
Click Contract ID to see your contract

合約會顯示在區塊高度7264348時被部署,從錢包地址0xc0ABD7A0C38a5Ca4E6eAc39b49837a01A35A3FC4部署出來的。
The contract will appear to be deployed at block height 7264348 from wallet address 0xc0ABD7A0C38a5Ca4E6eAc39b49837a01A35A3FC4.

選擇contract,驗證和發布合約
Click Contract to Verify and Publish contract

我們是只有單一個合約,所以選Single file,Compiler版本選擇Remix上的版本,License選擇GPLv3。
We only have a single contract, so choose Single file, Compiler version choose Remix version, License choose GPLv3.

complier版本查看remix
The complier version is viewed on remix

將remix上的code貼過來,記得optimization要選擇yes,如果你在寫合約的時候沒有勾它則不用選擇。
Paste the code on remix, remember to choose yes for optimization.If you didn’t tick it when you wrote the contract, you don’t have to choose.


發佈之後,成功就代表你的合約已驗證,可以直接用etherscan跟合約互動。
After the publish, success means that your contract has been verified, and you can directly use etherscan to interact with the contract.

回到合約頁面,會發現有綠色勾。
Go back to the contract page, you will find a green tick.

往下可以看到你的code。
You can see your code below.

點選Write Contract就可以跟合約互動。
Click Write Contract to interact with the contract.

選擇**connect to Web3 **串接MeatMask。
Select connect to Web3 to connect MeatMask.

串接成功後,你就可以建立一個殭屍,需要付Gas Fee。
After the connected, you can create a zombie, you need to pay Gas Fee.

可以看交易是否成功。
You can see if the transaction is successful.

交易成功,代表成功跟合約互動,產生出殭屍.
If the transaction is successful, it means that it successfully interacts with the contract and generates zombies.

往下拉,可以看到我們呼叫的function,就是createRandomZombie。
Scroll down to see the function we called, which is createRandomZombie

輸入的值是kiraZombie1
Input Data is kiraZombie1

回到合約頁面,read contract,輸入0,可以查詢到我們剛剛建立的殭屍。
Go back to the contract page, read contract, enter 0, you can query the zombie we created.

到合約的Events,可以看到我們寫的event有成功記錄了,我們生成殭屍的訊息。
In the Event of the contract, we can see that the event we wrote has been successfully recorded, and we generate the message of the zombie.

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
//宣告solidity程式碼的版本
//Declares the version of the solidity code
contract ZombieFactory {
//建立一個智能合約,名為ZombieFactory
//Build a smart contract called ZombieFactory
event NewZombie(uint zombieId, string name, uint dna);
//宣告一個事件,名為NewZombie,有新疆屍被建立的時候會觸發此通知,顯示在區塊鏈交易的log記錄內。
//Declare an event called NewZombie that triggers the notification when a new zombie is created and shows up in the log records of blockchain transactions.
uint dnaDigits = 16;
uint dnaModulus = 10 ** dnaDigits;
//宣告殭屍的dna是16個位元
//The dna that declares a zombie is 16 bits
struct Zombie {
string name;
uint dna;
}
//宣告一個物件,名為Zombie,每一個物件有name跟dna兩個屬性。
//Declare an object named Zombie, and each object has two attributes, name and dna.
Zombie[] public zombies;
//宣告一個陣列,名為zombies,它是由物件Zombie組成的。
//Declare an array, named zombies, which is composed of the objects Zombies.
function _createZombie(string memory _name, uint _dna) private {
zombies.push(Zombie(_name, _dna));
uint id = zombies.length — 1;
emit NewZombie(id, _name, _dna);
}
//宣告一個function,名為_createZombie,用來把新殭屍放入陣列中,並獲得id,這個function傳入兩個值,這兩個值會作為Zombie物件的屬性,這一個新物件,將被放到陣列zombies中,之後透過zombies陣列的長度-1,取得殭屍的編號id,並觸發事件,把新殭屍的資訊記錄在區塊鏈交易的log內。
//Declare a function named _createZombie, which is used to put new zombies into the array and get the id. This function passes in two values, which will be used as attributes of the Zombie object. This new object will be put into the zombies array, and then obtain the ID of the zombie through the length of the zombies array -1, and trigger an event to record the information of the new zombie in the log of the blockchain transaction.
function _generateRandomDna(string memory _str) private view returns (uint) {
uint rand = uint(keccak256(abi.encodePacked(_str)));
return rand % dnaModulus;
}
//宣告一個function,名為_generateRandomDna,用來獲得殭屍的dna,這個function傳入一個值,這個值會經由keccak256計算之後,得出32位元的偽隨機數,這個偽隨機數,會只保留從右往左數的16個位元。
//Declare a function named _generateRandomDna, which is used to obtain the dna of the zombie. This function passes in a value, which will be calculated by keccak256 to obtain a 32-bit pseudo-random number. This pseudo-random number will only keep 16 bits from right to left.
function createRandomZombie(string memory _name) public {
uint randDna = _generateRandomDna(_name);
_createZombie(_name, randDna);
}
}
//宣告一個function,名為createRandomZombie,用來建立殭屍,此function傳入一個值,這個值會去拿到dna,之後把此值(殭屍的名字)與dna,拿去建立殭屍。
//Declare a function named createRandomZombie, which is used to create a zombie. This function passes in a value, which will go to get DNA, and then use this value (the zombie’s name) and DNA to create a zombie.
No activity yet