DEV Community

ilija
ilija

Posted on • Updated on

Web3 backend and smart contract development for Python developers Musical NFTs part 6: smart contract deployment

In attempt to deploy our contracts to Polygon Mumbai test-net we will need to create few files: 1) .env (with account private keys); 2) brownie-config.yaml with Brownie related settings; 3) Python helper file with function which will define from which account we will deploy our contracts. And last 4) deploy.py file which we will run from command line ones we decide to make a deployment. Also we will need some Mumbai testnet MATIC tokens that we can get from faucet

When it comes to deployment we have two possibilities: deployment to local dev net like Ganache or to test-net like Mumbai. In our case we will first deploy to local Ganache blockchain and if everything goes well we will move to Mumbai test-net.

In root of smart-contract development folder create .env file and add

PRIVATE_KEY=here_you_should_pass_prvivate_keys_from_your_test_metamask_account_previously_funded_with_matic_mumbai_tokens
MNEMONICS=here_you_can_copy_your_menmonics_from_meta_mask
Enter fullscreen mode Exit fullscreen mode

IMPORTANT: accounts you are using here must be the one you use only for development purpose!

Second, fact that we are adding here mnemonics will allow Brownie to generate eth address we use in MetaMask when we run Ganache.

Add .env file to your .gitignore

Now we will create brownie-config.yaml in our root directory. This file is used to define basic settings for our project and it should look something like this:

dotenv: .env
networks:
    development:
        cmd_settings:
            accounts: 20
            mnemonic: ${MNEMONIC}
            default_balance: 200

compiler:
solc:
    version: 0.8.13


wallets:
from_key: ${PRIVATE_KEY}
Enter fullscreen mode Exit fullscreen mode

In this moment we only need simply deployment script and one helper function and we are ready for deployment.

In folder ./scripts add two new files: helpers.py and deploy.py. Inside helpers.py write following code (I will give more detail with comments)

from brownie import accounts, config, network

def get_account():
    # cheking if devleopment neetwork is active. If yes return account on index position zero
    if network.show_active() == "development":
        return accounts[0], account[1]
    else:
        # if development network is not active then generate account from .env file and details youb pass there 
        return accounts.add(config["wallets"]["from_key"]), "mock return value"
Enter fullscreen mode Exit fullscreen mode

Then in deploy.py you can write

#!usr/bin/python3
from brownie import MusicNFT, MockUSDC
from scripts.helpers import get_account

def main():
    # using get_account function from helpers.py file to pick proper eth account 
    account = get_account()

    # deploy MockUSDC contract and assigne deployment address to mockDeployed variable 
    mockDeployed = MockUSDC.deploy({"from": account})

    # pass address of MockUSDC contract to MusicNFT contract constructor 
    musciNFTdeployed = MusicNFT.deploy(mockDeployed, 5, {"from": account})
Enter fullscreen mode Exit fullscreen mode

It is time to deploy to local development blockchain and to check if everything went well. We will run our deployment script with -i flag which will allow us to go to interactive mode and play with our contracts

$brownie run deploy.py -i
Brownie v1.19.3 - Python development framework for Ethereum

BrownieMusicalNftsProject is the active project.
This version of µWS is not compatible with your Node.js build:

Error: Cannot find module './uws_linux_x64_111.node'
Falling back to a NodeJS implementation; performance may be degraded.



Launching 'ganache-cli --chain.vmErrorsOnRPCResponse true --server.port 8545 --miner.blockGasLimit 12000000 --wallet.totalAccounts 20 --hardfork istanbul --wallet.mnemonic attitude grant adjust accuse mail visual hammer potato nest interest breeze crime --wallet.defaultBalance 200'...

Running 'scripts/deploy.py::main'...
Transaction sent: 0xc8e3a13e26b0e5ce455143bd40e0646cb4bf7f1b66e2abdfae1c11f45dbdc656
Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 0
MockUSDC.constructor confirmed   Block: 1   Gas used: 775127 (6.46%)
MockUSDC deployed at: 0xC155AfddDA33e079Dbf55093bc7bb7a6f695e29e

Transaction sent: 0x40e5aa305e41c93b21ab70f28ec05c85b62e6a7a666026b77268e815ab82a25d
Gas price: 0.0 gwei   Gas limit: 12000000   Nonce: 1
MusicNFT.constructor confirmed   Block: 2   Gas used: 1899815 (15.83%)
MusicNFT deployed at: 0x76326f78fd4B4fc34D34f5d731fb13773e3494C6


Interactive mode enabled. Use quit() to close.
>>> MusicNFT[0].name()
'MuscialNFT'
>>> MockUSDC[0].name()
'MockUSDC'
>>>
Enter fullscreen mode Exit fullscreen mode

In interactive mode you can now test your contracts and see all functionalities we already coded. Next step in this moment is to deploy our contracts to Polygon Mumbai testnet. With this new contract addresses later on we will be able to make intrgation bewteen Django backend and smart contracts.

Add to your .env file WEB3_INFURA_PROJECT_ID variable.

WEB3_INFURA_PROJECT_ID=xxxxxxxxxxxxxx
Enter fullscreen mode Exit fullscreen mode

Infura id you can obtain if you have Infura profile (Infura is Ethereum node provider. If you don't have one go and open profile on Infura or Alchemy and then come back here)

Before we run deploy command make sure that you have enough MATIC test tokens on your eth account (generated from mnemonics). You can always get some MATIC test tokens from Polygon faucet

If everything is in place we can finally run:

$brownie run deploy.py --network polygon-test

Brownie v1.19.3 - Python development framework for Ethereum

BrownieMusicalNftsProject is the active project.

Running 'scripts/deploy.py::main'...
Transaction sent: 0xd25ec7cc6e549c50c6a697c27a430c4e897117a5d9ed00b36432474407f6a71f
Gas price: 1.529429313 gwei   Gas limit: 859789   Nonce: 439
MockUSDC.constructor confirmed   Block: 40511103   Gas used: 781627 (90.91%)
MockUSDC deployed at: 0xF79ca28ec6aCB479aa9F41d40FD53004278a2DAF

Transaction sent: 0x07db75670172f7c999c186b68b86b3199a1689a4fe1907b173e7981acd65f75b
Gas price: 1.609371306 gwei   Gas limit: 2094086   Nonce: 440
MusicNFT.constructor confirmed   Block: 40511106   Gas used: 1903715 (90.91%)
MusicNFT deployed at: 0x5fe829F6a5383D4088b8252F4B71B9889F97b13c
Enter fullscreen mode Exit fullscreen mode

Now we have our contracts live on Mumbai testnet ready to be integrated into our backend.

Code can be found in github repo

Top comments (0)