Save time developing smart-contracts with brownie
Published: 2022-06-21 03:10:55

#Seting up the Environment

First download python in my windows machine using the link below

[https://www.python.org/](https://www.python.org/)

The most important thing is to get the build tools necessary for the web3 library in my case i needed V++ 2019 build tools

Use the link bellow and scroll down to Tools for Visual Studio to get the online installer and install the needed version 

[https://visualstudio.microsoft.com/downloads/?q=build+tools](https://visualstudio.microsoft.com/downloads/?q=build+tools)


{% embed https://www.youtube.com/watch?v=qsvwg-cC928 %}

Get a metamask wallet for the test only and create an account and register down

the passphrase, Account adress, The private keys

in the metamask network section reveal the testnet because we will use rinkbey test for this purpose

Get some  etherium for the test using youre metamasc wallet here:

[https://www.youtube.com/watch?v=qsvwg-cC928](https://www.youtube.com/watch?v=qsvwg-cC928)

[https://faucets.chain.link/](https://faucets.chain.link/)

Create an account in [https://infura.io/](https://infura.io/)

Create a project and name it however you like Just dont forget to turn the rinkby network after the creation process.
and write down the Project_id and the Project secret and the first endpoint

If you dont like Infura you can use : [https://t.co/Ky3JWP8GPZ](https://alchemyapi.io)

Install Visualstudio code in the link bellow

[https://code.visualstudio.com/](https://code.visualstudio.com/)

Install some usefull extensions for solidity and python

Open a new project and start creatingthose files:

- .env
- brownie-config.yaml

You dont need to create a virtual environement just install pipx with administrator rules:

```
python -m pip install --user pipx
```

reopen powershell and continue

```
python -m pipx ensurepath
```

reopen powershell and install brownie

```
pipx install eth-brownie
```
reopen powershell again and init required files for brownie

```
brownie init
```
Now you have those new folders

- build
- contracts
- interfaces
- reports
- scripts
- tests

#Environement variable

Add a required environement variable with the name 
> WEB3_INFURA_PROJECT_ID
most be equal to youre INFURA_ID in the project created

{% embed https://youtu.be/gb9e3m98avk %}

#SimpleStorage.sol

The code for the SimpleStorage.sol is :
create the file under contracts folder
```


 // SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.9.0;

contract SimpleStorage {

    uint256 favoriteNumber;

    // This is a comment!
    struct People {
        uint256 favoriteNumber;
        string name;
    }

    People[] public people;
    mapping(string => uint256) public nameToFavoriteNumber;

    function store(uint256 _favoriteNumber) public {
        favoriteNumber = _favoriteNumber;
    }
    
    function retrieve() public view returns (uint256){
        return favoriteNumber;
    }

    function addPerson(string memory _name, uint256 _favoriteNumber) public {
        people.push(People(_favoriteNumber, _name));
        nameToFavoriteNumber[_name] = _favoriteNumber;
    }
}
```

> In the code above we just have a favoriteNumber variable that we can check with retrieve function and modify with the store functionand we can associate someone name with a favorite number with addPerson function

#Deploy.py

the code of the deploy.pyshould be the same as:
create the file under scripts folder

```
#Security
from dotenv import load_dotenv

#Necessary



from brownie import accounts, config, SimpleStorage, network





def get_account():

    if network.show_active() == "development":
        return accounts.add(config["wallets"]["from_key"])
    else:
        return accounts.add(config["wallets"]["from_key_metama"])

def deploy_ss():


    print("ACCOUNT  =>",get_account())

    ss= SimpleStorage.deploy({"from": get_account()})

    trx = ss.store(7, {"from": get_account()})

    trx.wait(1)

    fav = ss.retrieve()


    print("FAV NOW   =>",fav)

    


def main():


    deploy_ss()

```


> In the code above we are deploying the smart contract in the blockchain and interacting with it by storing the number 7 in favoriteNumber and the checking it


#.env/Private keys


Now we should fill the necessary keys, you can escape the first two variables because it only needed in a local interaction with Ganache 

.env file

```
ACCOUNT="0xRandom3216547986549687"
PRIVAT_KEY="0xRandom3216547986549687"


INFURA_ID="654random65465"
INFURA_SEC="654random65465"

META_ADRESS="0x654random65465"
META_PRV="654random65465"

ENDPOINT_RINKBY="654random65465"

ENV_TYPE="INFURA"
```

#Configs

In the file brownie-config.yaml load where .env file are abd what the variable name for the private key

```
dotenv: "../.env"


wallets:
  from_key: ${PRIVAT_KEY}
  from_key_metama: ${META_PRV}
```


#Deploying/Interaction

You just need a final step is running :

brownie run scripts/deploy.py --network rinkeby

I hope everything fine

if so check you contract here :

> https://rinkeby.etherscan.io/address/write_the_adress_of_the_contract

you can get the contract adress with the info given by youre brownie output

> SimpleStorage deployed at: 0xsdfsdfrandomsdfsdf


for the needygreedy tutorialplease follow it here:

{% embed https://www.youtube.com/watch?v=M576WGiDBdQ %}