How to compile the smart contract

Blockchain
1

                         How to Compile the smart contract


In this tutorial we are going to use brownie for compiling smart contract

What you will learn:

  • What is Solidity
  • What is brownie
  • What is Ganache
  • How to connect brownie with ganache-cli using cmd

Solidity is an object-oriented, high-level language it is similar to java so if you have a background in java it will make your life more easier to learn it. 

Smart contracts are programs which govern the behavior of accounts within Ethereum state. There are other smart contract languages, such as Vyper even though Vyper is more secure than solidity but solidity must well known than others.

Let us begin with the basic example:

Open Visual Studio Code and install solidity extension, afterward name the file of the smart contract which take the extension of (.sol) 

                                                           HelloWorld.sol


 

there's many tools out there for compiling smart contract such as (Remix IDE , Brownie , HardHat) but in this tutorial we are going to use brownie.

Brownie is a Python-based development and testing framework for smart contracts targeting the Ethereum Virtual Machine(EVM), and is full support by solidity and Vyper; for more information about brownie go to this link here.

Requirements Tools

  1. Python 3.9 or later (to install brownie)
  2. Node js (to install ganache-cli)
  3. Ganache-Cli (Ethereum Network)
 
$ pip install eth-brownie
 
once you have installed, type brownie to verify that it worked, you may need to install Tkinter if you need brownie GUI , by using this command:

$ pip install tk

 you can use the following command to check that TK has been correctly installed:

$ python -m tkinter

now if all dependencies has installed in your system, let's initialize it by creating a new folder and type:

$ brownie init

if it doesn't work try this :

$ brownie init --force

Now you should see the following structure:

  •  build/: Project data
  • contracts/: Contract sources
  • interface/: Interface sources
  • reports/: JSON report files for use in the GUI
  • scripts/: Scripts for deployment and interaction
  • tests/: Scripts for testing the project


Now paste the smart contract file in the contracts folder and open the CMD in the same directory

To see our brownie network list type this:

$ brownie networks list

 

 


 you may consider that ganache doesn't exits in the list , so let's add it by :

 open another CMD and type :

$ ganache-cli



Let's add Ganach-Cli by typing this in the brownie console:

$ brownie networks add live ganache-gui host=http://127.0.0.1:7545 chainid=5777

now if you check brownie networks list you should see ganache-cli exits on the list.

afterward, let's connect it with brownie by typing this :

$ brownie console --network ganache-gui 

the console is useful when you want to interact directly with contracts deployed on a non-local chain, or quick testing as you develop.

let's see ganache accounts 

$ brownie accounts

if you see an empty brackets [], just instead of typing brownie console --network ganache-gui , try typing

$ brownie console

 >>  accounts

>>  type accounts

 








Post a Comment

1Comments
Post a Comment

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !