Environment Setup

Environment setup details for the web3 portion of the Bnomial project

This page explains how to set up your development environment in order to work on the web3 part of bnomial.

Code Repository

The web3 code of Bnomial is currently hosted in a separate repository: https://github.com/underfitted/bnomial-web3. We may merge it with the main repository in the future.

Local Setup

Developing and testing smart contracts locally is quite easy to setup. You can do this both in windows as well as Unix/Linux machines however both operating systems have it's own caveat's to look into and adapt the instructions as applicable

VS Code Extension

I use VS Code as IDE and I can recommend installing this solidity extension. GitHub Copilot understands Solidity as well, so make sure you have it activated if you have access to it. VS code can be installed in either Linux or windows as applicable.

Installing NodeJs

If you already have a working nodejs installed in your environment, you can skip this. To install nodejs, you can use the following link.

Windows

NodeJs Windows Installation Steps

Linux

NodeJs Ubuntu Installation Steps

Installing npm packages for Hardhat

The next step after cloning the repository is to install all the required npm packages. This will install everything you need to compile, test and deploy smart contracts with Solidity.

npm install --save-dev hardhat

This is pretty much everything you need to write, compile and test smart contracts locally. FYI, Hardhat is a development environment to compile, deploy, test, and debug your Ethereum software.

Verifying the Install

You can verify your installation by switching to the bnomial folder in your local environment (after cloning from the repository (https://github.com/underfitted/bnomial-web3) as indicated earlier and executing

npm run build

if everything is working fine you will see a successful compilation message

Known Issues Reported

Windows

  1. some users have faced the below error

Error HH12: Trying to use a non-local installation of Hardhat, which is not supported. Please install Hardhat locally using npm or Yarn, and try again

This error may persist even if the above described solution is attempted, to work around this and to have a more stable execution environment, we recommend enabling windows sub system for Linux or WSL2 in your windows machine, so that you can take advantage of both the worlds.

To install /enable WSL2, you may refer to the link below

Windows Subsystem for Linux

Linux

<< I am not sure what was reported, can be updated later>>

WSL (e.g. Ubuntu LTS1604 )

Executing npm build resulted in the below error, you can try the solutions listed in the article and one of them might work to resolve this issue

Stackoverflow Article related to the error

Tip: to access Unix file system from windows explorer, simply type \\wsl$ in your explorer bar and you will see the Unix home directory inside windows and allows drag and drop functionality, which makes it easier to move the files across.

Post hardhat installation, we can execute the project and check if everything is fine before attempting to work on specific issues.

To compile type

npm run build

after successful compile message, you can test it.

npm run test

and you will see results similar to the below screen capture

Test Output

npm run coverage

Istanbul Coverage report

npm run gas

Gas Report

The above commands are scripted into the package.json file which is reproduced below for better understanding (open the image in a new tab for better clarity)

package.json for bnomial NFT

Deploying on a Blockchain

If you want to deploy on the blockchain, there are some more things you need to do.

One important thing to consider is which network to use. The good thing is that this setup allows to easily deploy on any network in the Ethereum eco-system without chaining the code! You should also always the first test on a testnet (free of charge) before deploying to the mainnet (you need to pay the fee).

Some of the networks to consider:

  • Ethereum mainnet - the most popular blockchain for smart contracts, but gas fees are really high. You will easily pay hundreds of dollars for deploying a contract there.

  • Rinkeby testnet - one of the most popular testnets for Ethereum. Supported by OpenSea, which is nice if you are creating an NFT.

  • Polygon mainnet - Polygon is a Proof-of-Stack Layer 2 network running on top of Ethereum. The native token there is MATIC and gas fees are very low. Supported by OpenSea.

  • Mumbai testnet - the most used test network for Polygon. Supported by OpenSea.

  • Optimism and Arbitrum - alternative Layer 2 networks, which may be interesting for ERC-20 tokens.

Create a Wallet

You need an Ethereum wallet to deploy a smart contract to the blockchain - you can create one with Metamask. If you already have a wallet there, I recommend creating a second one for testing in order to reduce the risk of leaking your private key.

Switching to the right network

If you are going to work with Polygon and Mumbai (which we currently use), you may also need to add these networks in Metamask manually: https://docs.polygon.technology/docs/develop/metamask/config-polygon-on-metamask/.

Funding your wallet

You will also need some Ethereum or MATIC tokens to pay for the deployment gas fees. On the testnets you can use a faucet to get some free tokens:

Finding your private key

You will need your private key in order to sign the deployment transaction. In Metamask, click the 3 dots in the upper right corner next to your account, choose "Account details" and then "Export private key".

Creating an Alchemy Account

In order to deploy to the blockchain you need a node to connect to it. When you use a dApp this is done by the Metamask extension. However, we are going to deploy from a script, so we need an alternative. There are services that offer API for that like Alchemy or Infura.

Creating an application on Alchemy

First, head over to https://www.alchemy.com/ and create an account. After that, you need to create a new application, where you need to specify a name and most importantly the network you want to use.

Alchemy API URL

Go to your application and in the upper right corner you will see a button called "View Key". After clicking it you will find the URL with the integrated

Alchemy Key

Also do not forget to change the network to Polygon Mumbai, by default the URL will point to Ethereum main network. You can change the network by clicking on Edit App and following instructions.

Creating a Polygonscan account

In order to verify your smart contract after it is deployed, you will also need an API key for Polygonscan (or Etherscan in case of Ethereum/Rinkeby). If you don't verify your contract with Polygonscan, you will not be able to see its code or interact with it.

Creating an account is straight forward and adding a new API key is easy as well - see the screenshot below.

Beware that after you get your API key, it will take about 5-10 minutes for it to get activated.

Creating the .env file

Now you are finally ready to create your .env file! Make sure you don't commit it to the repository. It is already ignored in .gitignore, but still, be careful. You are now basically going to just copy/paste the keys from the accounts you just created.

MUMBAI_API_URL="<Alchemy URL for a Mumbai app>"
POLYGON_API_URL="<Alchemy URL for a Polygon app, optional>"
PRIVATE_KEY="<your private key from the Metamask wallet>"
POLYSCAN_KEY="<your Polygonscan API key>"

You are now ready to deploy your smart contract!

A sample .env file will look like below

.env file sample

npm run deploy:mumbai

you will see a screen similar to below

Binomial NFT deployed

The last and final step is to verify the contract which we will do next

npm run verify:mumbai --<<smart contract address as displayed above>>

Contract Verification

Now we have completed deployment of smart contract "as is" basis and successful completion of all steps is a pre-requisite before you can start work on the Github issues.

Last updated