Flattening Contracts and Debugging with 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.

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.

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.

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.

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.

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.

Let’s now see how we can use Truffle and OpenZeppelin to create a simple custom Token that extends the standard token from 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.

Installing Truffle

Using your terminal, run the following command to install 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:

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.

Next, we’ll install 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:

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.

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:

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.

The post Flattening Contracts and Debugging with Remix appeared first on SitePoint.

Powered by WPeMatico

The featured image was randomly selected. It is an unlikely coincidence if it is related to the post.