Developers Forum for XinFin XDC Network

ruslan wing
ruslan wing

Posted on

How to deploy a smart contract with Brownie on XDC Network


Python is one of the most versatile programming languages; from researchers running their test models to developers using it in heavy production environments, it has use cases in every possible technical field. Today’s guide will teach us about Brownie, a Python-based tool for writing and deploying smart contracts on XDC Network.


  • Python3 installed
  • Text editor
  • Command-line
  • Love for brownies
  • What is Brownie?

JavaScript-based libraries like XDC3.js, ethers.js, Truffle, and Hardhat dominate smart contract development. Python is a versatile, highly used language and can also be used for smart contracts/web3 development; is a compelling Python library that fulfills web3 needs. The Brownie framework is built on top of

Brownies are small rectangular confectionery items loved by everyone, but the Brownie we are talking about today is a Python-based framework to develop and test smart contracts. Brownie has support for both Solidity and Vyper contracts, and it even provides contract testing via pytest.

To demonstrate the process of writing and deploying a smart contract with Brownie, we will use Brownie mixes which are template projects. Specifically, we will use a token mix, a template of the XRC-20 implementation.

Installing dependencies

Image description

This should return the version of python3 installed. If not installed, download and install it from the official Python website

Let us make a project directory before installing Brownie and make that project directory our current working directory.

Brownie is built on top of Python3, so we need it installed to work with Brownie; let us check if we have Python3 installed on our system. To do so, type the following in your terminal/cmd:

python3 -V

Enter fullscreen mode Exit fullscreen mode
mkdir brownieDemo
cd brownieDemo
Enter fullscreen mode Exit fullscreen mode

Now that you have installed Python3 on your system, let us install Brownie using pip, Python’s package manager. Pip is similar to what npm is for JavaScript. Type the following in your terminal/cmd:

pip3 install eth-brownie
## If the install failS, use the following command for better luck.
sudo pip3 install eth-brownie
Enter fullscreen mode Exit fullscreen mode

To check if Brownie was installed correctly, type brownie in your terminal/cmd, and it should give the following output:

Image description

To get the token mix, type the following in your terminal/cmd:

brownie bake token

Enter fullscreen mode Exit fullscreen mode

This will create a new directory token/ in our brownieDemo directory.

File Structure

First, let’s cd into the token directory:

cd token

Enter fullscreen mode Exit fullscreen mode

Now, open the token directory in your text editor. Under the contracts/ folder, you will find Token.sol, which is our main contract; you can write your own contracts or modify this. Under the scripts/ folder, you will find the python script; this script will be used to deploy the contract, and modifications are needed based on the contracts.

Image description

The contract is an XRC-20 contract; you can learn more about the XRC-20 standards and contracts in this guide on XRC-20 tokens.

We will deploy our contract on the XDC Apothem testnet instead of running our own node and setting up a dedicated RPC, and we will be using the public RPC:-

Network and Account Set Up.

We need to set up the XDC APothem Network with Brownie. To do so, type the following in your terminal/cmd:

brownie networks add XDC Apothem Testnet host= chainid=51
Enter fullscreen mode Exit fullscreen mode

Image description

The next thing we need to do here is to create a new wallet using Brownie. To do so, type the following in your terminal/cmd. You will be asked to set up a password for your account.

brownie accounts generate testac

Enter fullscreen mode Exit fullscreen mode

This will generate an account along with a mnemonic phrase and save it offline. The name testac is the name for our account. You can choose any name that you would like.

Image description

Note: Mnemonic phrases can be used to recover an account or import the account to other non-custodial wallets. The account you see in the image above was just created for this guide.

Copy the account address so that we can get some test XDC, which will be required to deploy our contract.

Getting test XDC

As our contract will be deployed on the Apothem testnet, we will require some Apothem test XDC to pay for the gas fee. Head over to the Apothem faucet at paste your address in the field and change the address prefix from ox to xdc and click on “Send me test XDC”.

Image description

Deploying our contract

Before deploying the contract, we need to compile it using:

brownie compile

Enter fullscreen mode Exit fullscreen mode

Image description

Now open the *scripts/* in your text editor, and make the following changes:


from brownie import Token, accounts

def main():
    acct = accounts.load('testac')
    return Token.deploy("Test Token", "TST", 18, 1e21, {'from': acct})   
Enter fullscreen mode Exit fullscreen mode

Line 6: We added this line to import the testac account we created earlier and stored it in the acct variable.

Line 7: On this line, we edited the ‘From’: part to have our acct variable.

FINALLY, we will deploy our contract using the deployment script (scripts/ here):

brownie run — network Apothem

Enter fullscreen mode Exit fullscreen mode

Image description

Here is the TX hash for the contract which has been deployed on the Apothem Network


So, today we learned brownies are good, but Brownie framework is the best. We learned how to import a Brownie-mix, add XDC Apothem Network, create an account, and compile and deploy a contract, and we used Brownie for the entire process!

If you have any questions, please feel free to post it on

Discussion (1)

cryptosandtokens profile image
Robert Aronovici

We found that Brownie has updated their documentation and deploy parameters when executing. Also, copying and pasting the code script was causing an odd bug from the keyboard characters.

brownie networks add XDC xdc-apothem chainid=51