remix使用_使用Remix展平合同和调试
remix使用
Smart contracts on the Ethereum main-net use real money, so building error-free smart contracts is crucial and requires special tools like debuggers.
以太坊主網上的智能合約使用真實資金,因此構建無錯誤的智能合約至關重要,并且需要調試器之類的特殊工具。
Remix IDE is the most fully-featured IDE for Solidity. Among other tools, it has an excellent step-by-step debugger. You can perform various tasks such as moving in time, changing the current step, exploring local variables and current state by expanding various panels.
Remix IDE是Solidity中功能最齊全的IDE。 在其他工具中,它具有出色的逐步調試器。 您可以執行各種任務,例如移動時間,更改當前步驟,通過展開各種面板來瀏覽局部變量和當前狀態。
You can also set breakpoints to move between different points in the code. And the terminal allows you to display transactions executed from Remix and debug them.
您還可以設置斷點以在代碼中的不同點之間移動。 并且終端允許您顯示從Remix執行的事務并調試它們。
Throughout this tutorial, we’ll use Truffle and OpenZeppelin to build a simple custom token. We’ll see why and how to flatten the contract, and finally we’ll use Remix IDE to start debugging the contract.
在整個教程中,我們將使用Truffle和OpenZeppelin構建簡單的自定義令牌。 我們將了解為什么以及如何展平合同,最后,我們將使用Remix IDE開始調試合同。
為什么要展平智能合約? (Why Flatten a Smart Contract?)
Flattening a smart contract refers to combining all Solidity code in one file instead of multiple source files such that, instead of having imports, the imported code is embedded in the same file. We need to flatten smart contracts for various reasons, such as manually reviewing the contract, verifying the contract on Etherscan, or debugging the contract with Remix IDE, as it currently doesn’t support imports.
展平智能合約是指將所有Solidity代碼組合在一個文件中,而不是多個源文件中,從而使導入的代碼被嵌入同一文件中,而不是進行導入。 由于各種原因,我們需要展平智能合約,例如手動檢查合約,在Etherscan上驗證合約或使用Remix IDE調試合約,因為該合約目前不支持導入。
使用Truffle和OpenZeppelin編寫簡單令牌 (Writing a Simple Token Using Truffle and OpenZeppelin)
Remix IDE is officially recommended for building small contracts or for the sake of learning Solidity, but once you need to build a larger contract or need advanced compilation options, you’ll have to use the Solidity compiler or other tools/frameworks such as Truffle.
正式建議使用Remix IDE來構建小型合同或為了學習Solidity,但是一旦您需要構建較大的合同或需要高級編譯選項,則必須使用Solidity編譯器或其他工具/框架(例如Truffle)。
Besides error-free contracts, security is also a crucial part of building a smart contract. For this reason, using battle-tested frameworks like OpenZeppelin that provides reusable, well-tested, community-reviewed smart contracts, will help you reduce the vulnerabilities in your Dapps.
除了無差錯合同,安全性也是構建智能合同的關鍵部分。 因此,使用經過測試的框架(如OpenZeppelin)提供可重復使用,經過良好測試,經過社區審查的智能合約,將幫助您減少Dapps中的漏洞。
Let’s now see how we can use Truffle and OpenZeppelin to create a simple custom Token that extends the standard token from OpenZeppelin.
現在,讓我們看看如何使用Truffle和OpenZeppelin創建一個簡單的自定義令牌,該令牌擴展了OpenZeppelin的標準令牌。
要求 (Requirements)
This tutorial requires some knowledge of Truffle, Ethereum and Solidity. You can read the Blockchain Introduction and Ethereum Introduction.
本教程要求對松露,以太坊和堅實性有所了解。 您可以閱讀區塊鏈簡介和以太坊簡介 。
You also need to have Node.js 5.0+ and npm installed on your system. Refer to their download page for instructions.
您還需要在系統上安裝Node.js 5.0+和npm。 有關說明,請參閱其下載頁面 。
安裝松露 (Installing Truffle)
Using your terminal, run the following command to install Truffle:
使用您的終端,運行以下命令來安裝Truffle:
npm install -g truffle創建一個新的松露項目 (Creating a New Truffle Project)
Start by creating a new directory for your project. Let’s call it simpletoken and navigate into it:
首先為您的項目創建一個新目錄。 讓我們將其稱為simpletoken并導航至其中:
mkdir simpletoken cd simpletokenNext, create the actual project files using:
接下來,使用以下命令創建實際的項目文件:
truffle initThis command will create multiple folders, such as contracts/ and migrations/, and files that will be used when deploying the contract to the blockchain.
該命令將創建多個文件夾,例如contracts/和migrations/ ,以及在將合同部署到區塊鏈時將使用的文件。
Next, we’ll install OpenZeppelin:
接下來,我們將安裝OpenZeppelin:
npm install openzeppelin-solidity創建一個簡單的代幣合約 (Creating a Simple Token Contract)
Inside the contracts/ folder, create a file named SimpleToken.sol and add the following content:
在contracts/文件夾中,創建一個名為SimpleToken.sol文件,并添加以下內容:
pragma solidity ^0.4.23;import 'openzeppelin-solidity/contracts/token/ERC20/StandardToken.sol';contract SimpleToken is StandardToken {address public owner;string public name = 'SimpleToken';string public symbol = 'STt';uint8 public decimals = 2;uint public INITIAL_SUPPLY = 10000;constructor() public {totalSupply_ = INITIAL_SUPPLY;balances[owner] = INITIAL_SUPPLY;}}This is the contract that we’re going to flatten and debug. We’re importing the OpenZeppelin StandardToken.sol contract and declaring our SimpleToken contract which extends StandardToken.sol using the is operator.
這是我們要展平和調試的合同。 我們正在導入OpenZeppelin StandardToken.sol合同,并聲明使用is運算符擴展了StandardToken.sol SimpleToken合同。
Our contract will inherit a bunch of variables and functions, which need to be overridden in order to customize the contract.
我們的合同將繼承一堆變量和函數,需要對其進行重寫以自定義合同。
For the sake of completing our Truffle project, inside the migrations/ folder of your project, create a file called 2_deploy_contract.js and add the following content:
為了完成我們的Truffle項目,請在項目的migrations/文件夾中創建一個名為2_deploy_contract.js的文件,并添加以下內容:
var SimpleToken = artifacts.require("SimpleToken");module.exports = function(deployer) {deployer.deploy(SimpleToken); };Using this file, we can deploy/migrate our smart contract into the blockchain, but we don’t actually need this for this example, because we’re going to use Remix IDE to deploy the smart contract after flattening it.
使用此文件,我們可以將智能合約部署/遷移到區塊鏈中,但是在本示例中我們實際上并不需要它,因為在扁平化之后,我們將使用Remix IDE來部署智能合約。
使用松露壓平器壓平合同 (Flattening the Contract Using Truffle Flattener)
Truffle Flattener is an npm utility that flattens or combines Solidity files developed under Truffle with all of their dependencies in the right order.
Truffle Flattener是一個npm實用程序,可以按正確的順序對Truffle下開發的Solidity文件及其所有依賴項進行展平或合并。
First, start by installing truffle-flattener from npm globally using:
首先,首先使用以下命令從npm全局安裝truffle-flattener :
npm install -g truffle-flattenerNext, inside your Truffle project, run the following command to flatten the SimpleToken.sol file:
接下來,在您的Truffle項目中,運行以下命令以展平SimpleToken.sol文件:
truffle-flattener contracts/SimpleToken.sol > FlattenedSimpleToken.soltruffle-flattener outputs the flattened contract into the standard output or the terminal. Using the > operator we save the output in the FlattenedSimpleToken.sol file inside the current folder.
truffle-flattener器將展平合約輸出到標準輸出或終端中。 使用>運算符,我們將輸出保存在當前文件夾內的FlattenedSimpleToken.sol文件中。
使用Remix IDE編譯和部署合同 (Compiling and Deploying the Contract Using Remix IDE)
You can access your flattened smart contract from the FlattenedSimpleToken.sol file. Open the file and copy its content.
您可以從FlattenedSimpleToken.sol文件訪問展平的智能合約。 打開文件并復制其內容。
Next, open Remix IDE from https://remix.ethereum.org and paste the flattened smart contract into a new file in the IDE.
接下來,從https://remix.ethereum.org打開Remix IDE,然后將展平的智能合約粘貼到IDE中的新文件中。
If you get an error saying Mock compiler : Source not found, make sure to select a newer compiler version from Settings tab > Solidity version pane > select new compiler version dropdown.
如果出現錯誤提示“ 模擬編譯器:找不到源” ,請確保從“設置”選項卡>“實體版本”窗格中選擇一個較新的編譯器版本,然后選擇“新編譯器版本”下拉列表 。
If Auto compile is disabled, click on the Start to compile button in the Compile panel.
如果禁用了自動編譯 ,請在“ 編譯”面板中單擊“ 開始編譯”按鈕。
Next, activate the Run panel. First make sure you have JavaScript VM selected for environment. On the second box, select the SimpleToken contract from the drop-down, then click on the red Deploy button, right below the drop-down.
接下來,激活“ 運行”面板。 首先,請確保已為環境選擇JavaScript VM 。 在第二個框中,從下拉菜單中選擇SimpleToken合同,然后單擊下拉菜單正下方的紅色Deploy按鈕。
Your contract is deployed and running on the JavaScript VM, which simulates a blockchain. We can now start debugging it in different ways.
您的合同已部署并在模擬區塊鏈JavaScript VM上運行。 現在,我們可以用不同的方式開始調試它。
調試自定義令牌合同 (Debugging the Custom Token Contract)
To see how we can debug the contract, we’ll first introduce some errors.
為了了解如何調試合同,我們將首先介紹一些錯誤。
You can start debugging smart contracts in Remix IDE using two different ways, depending on the use case.
您可以根據使用情況使用兩種不同的方式在Remix IDE中開始調試智能合約。
When you first deploy your smart contract or perform any transaction afterwards, some information will be logged in the terminal and you can start debugging it from the white Debug button next to the log. Let’s see that in action.
首次部署智能合約或之后執行任何交易時,一些信息將記錄在終端中,您可以從日志旁邊的白色“ 調試”按鈕開始對其進行調試 。 讓我們看看實際情況。
使用assert() (Using assert())
SafeMath is an OpenZeppelin library for Math operations with safety checks that throw an error. The sub(a,b) function subtracts two numbers and returns an unsigned number. The first parameter should be greater than the second parameter so we can always get a positive number. The function enforces this rule using an assert() function. This is the code for the sub function:
SafeMath是用于數學運算的OpenZeppelin庫,具有引發錯誤的安全檢查。 sub(a,b)函數將兩個數字相減并返回一個無符號數字。 第一個參數應大于第二個參數,以便我們始終可以獲得正數。 該函數使用assert()函數強制執行此規則。 這是子函數的代碼:
function sub(uint256 a, uint256 b) internal pure returns (uint256) {assert(b <= a);return a - b;}As long as you provide values for a and b that verify the condition b <= a, the execution continues without problems, but once you provide a value of b greater that a, the assert() function will throw an error that says:
只要提供a和b的值來驗證條件b <= a ,執行就不會出現問題,但是一旦您提供的b值大于a ,則assert()函數將拋出錯誤:
VM error: invalid opcode. invalid opcode The execution might have thrown. Debug the transaction to get more information.So let’s add a call to the sub() with wrong parameters in the SimpleToken contract:
因此,讓我們在SimpleToken合同中使用錯誤的參數添加對sub()的調用:
constructor() public {totalSupply_ = INITIAL_SUPPLY;balances[owner] = INITIAL_SUPPLY;SafeMath.sub(10, 1000);}If you redeploy the contract, you’re going to get the the invalid opcode error in the terminal:
如果重新部署合同,您將在終端中看到無效的操作碼錯誤:
Once you click the Debug button you’ll be taken to the Debugger panel where you can start debugging your code.
單擊“ 調試”按鈕后,將轉到“ 調試器”面板,您可以在其中開始調試代碼。
In the code editor, the first line/instruction will be highlighted marking where we are currently at the code.
在代碼編輯器中,第一行/指令將突出顯示,以標記我們當前在代碼中的位置。
Click on the Step over forward button to step through the code.
單擊逐步前進按鈕以逐步瀏覽代碼。
You can also jump right in the exception using the Jump to exception button.
您也可以使用“ 跳轉到例外”按鈕在例外中直接跳轉 。
Whatever way you’re using, the debugger will take you to the code causing the problem, and will then stop.
無論使用哪種方式,調試器都會帶您到導致問題的代碼,然后停止。
You can inspect the state of the local variables at the current step.
您可以在當前步驟檢查局部變量的狀態。
The Solidity Locals panel displays local variables associated with the current context.
“ Solidity Locals”面板顯示與當前上下文關聯的局部變量。
From the source code and Solidity Locals, you can conclude that the source of the problem is related to the assert() method and the value of b being greater than the value of a.
從源代碼和密實度當地人 ,可以得出這樣的結論問題的源極相關的斷言()方法和B是更大的比a的值的值。
You can stop debugging using the stop debugging button.
您可以使用“ 停止調試”按鈕停止調試 。
It’s also worth looking at the other panels of the debugger.
同樣值得一看的是調試器的其他面板。
使用說明 (Instructions)
The Instructions panel displays the byte-code of the debugged contract. The byte-code of the current step is highlighted.
“ 指令”面板顯示調試合約的字節碼。 當前步驟的字節碼突出顯示。
固結狀態 (Solidity State)
The Solidity State panel displays state variables of the currently debugged contract.
Solidity State面板顯示當前調試合同的狀態變量。
低層面板 (Low Level Panels)
These panels display low level information about the execution such as step details, memory, stack and return value of functions.
這些面板顯示有關執行的低級信息,例如步驟詳細信息,內存,堆棧和函數的返回值。
結論 (Conclusion)
In this tutorial, we’ve used Truffle and OpenZeppelin to build a simple token, then used truffle-flattener to flatten the custom contract and Remix IDE to start debugging the contract for errors.
在本教程中,我們使用Truffle和OpenZeppelin構建了一個簡單的令牌,然后使用truffle-flattener擴展了自定義合同,并使用Remix IDE開始調試該合同中的錯誤。
Hopefully this will help you fearlessly dive into step-by-step debugging of your own contracts. Let us know how it works out for you!
希望這將幫助您無所畏懼地逐步調試自己的合同。 讓我們知道如何為您服務!
翻譯自: https://www.sitepoint.com/flattening-contracts-debugging-remix/
remix使用
總結
以上是生活随笔為你收集整理的remix使用_使用Remix展平合同和调试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php钩子技术
- 下一篇: input表单标签和label标签以及常