remix使用

tech2022-09-07  116

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 simpletoken

Next, create the actual project files using:

接下来,使用以下命令创建实际的项目文件:

truffle init

This 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-flattener

Next, inside your Truffle project, run the following command to flatten the SimpleToken.sol file:

接下来,在您的Truffle项目中,运行以下命令以展平SimpleToken.sol文件:

truffle-flattener contracts/SimpleToken.sol > FlattenedSimpleToken.sol

truffle-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使用

最新回复(0)