Create a Balancer v2 Pool with balpy

This is a step by step guide to using the balpy Python package to create your own Balancer v2 pool.

Please note that balpy is an alpha build; use at your own risk. Gerg, one of the developers at Balancer Labs, built it to help people interact with Balancer v2 smart contracts.

We’ll do our best to help answer questions but recognize there may be issues that cannot be resolved and you should not attempt this if you aren’t a somewhat competent user of DeFi and computers in general.

I‘ll be using windows 10, for Mac/Linux users please see this section of the balpy readme.

First, make sure you have python installed. You can get the latest windows installer here.

Now open Windows Powershell. I used the search bar in windows to find it. Copy/paste “python3 -m pip install balpy” and hit enter.

It should begin installing all the balpy dependencies required to run the script.

If you’re greeted with an error at this stage, you may need to install the latest version of visual studio build tools which you can find here.

Select these options when installing Visual Studio Build Tools to fix any red text errors when installing balpy

Once balpy is installed with no red text error messages, the next step is setting environment variables. Go to control panel -> system and security -> system -> Advanced system settings -> Environment Variables (at the bottom).

You’ll need a few of these and they can be added under user variables or system variables.

Variable name: KEY_API_ETHERSCAN

Variable value: <etherscan API key> Go to https://etherscan.io/ click sign in (top right corner), make an account, click API Keys, generate a free key.

Variable name: KEY_PRIVATE

Variable value: <your private key> You will want to create a new hot wallet specifically for creating a pool. I made a brand new metamask wallet. To get your private key, click the three dots -> account details -> export private key.

If you’re creating a pool on polygon, you’ll need the following variable…

Variable name: BALPY_CUSTOM_RPC

Variable value: https://rpc-mainnet.maticvigil.com (or any polygon RPC)

If you’re creating a pool on main net, you’ll need the follow variable…

Variable name: KEY_API_INFURA

Variable value: <infura project id> Go to https://infura.io/ make a free account, under Ethereum create a new project with whatever name. You’ll see a project ID and secret. Put the ID as the variable value.

The next step is to download this zip file containing the balpy files. Simplest thing to do is extract this into the directory that Powershell opens in by default. Inside Powershell, you’ll need to navigate to the samples/poolCreation directory as shown below.

Keep the Powershell window open. Open the sample file for what kind of pool you’re looking to create (most likely going to be sampleWeightedPool.json). This can be opened with notepad but Visual Studio Code is the best choice if you have it.

If you’re making a pool on main net, set network to mainnet. Name and symbol can be anything you want. Set your desired swap fee, 1% in this case, then it’s a matter of adding your tokens up to a maximum of 8. Easiest way to get contract addresses is to use coingecko — for assets that exist on multiple chains, you’ll see three dots by the contract field. Clicking the dots will bring up a list of all contract addresses on different chains for the asset. Make sure you select polygon contract address if you’re making a polygon pool.

After ensuring your contract addresses, weights (must add up to 1), and initialBalance (use a small amount of each asset to start with) are all correct, make sure you SAVE the file! The next step is to fund the hot wallet you’re using — in my case, the new metamask wallet I created at the beginning. Send over all the necessary token initial balances plus ETH on mainnet or MATIC on polygon for transaction fees.

Note: If you created an environmental variable for KEY_API_INFURA then you want to make a pool on polygon, you must delete KEY_API_INFURA before opening Powershell to force the system to use the polygon RPC.

Return to the Powershell window and execute this command:

python3 poolCreationSample.py sample{Weighted, Oracle, Stable}Pool.json (Weighted in our case)

With any luck, you’ll be greeted by something like the following:

It will confirm your wallet has sufficient amounts of each token (the initialBalance you set earlier), then execute transactions to approve each token if they haven’t been approved before, then create the pool and finally in step 4 initialize the pool to enable adding of liquidity.

If you encounter an error here, ensure you’ve done all previous steps in this guide. If you’re still stuck, reach out in the balancer discord in #support.

If all is successful, all that is left is to send the pool tokens you received & any leftover ETH/MATIC back to your main wallet. Easiest way to send the pool tokens is using metamask, go to Assets -> scroll to the bottom and hit add token -> copy/paste the token contract address. You can find the contract address by using https://etherscan.io/ or https://polygonscan.com/, navigating to your hot wallet address, and checking ERC-20 token txns. You should see an incoming transaction with the new pool tokens paired with your deposit of tokens to start the pool. Once the token has been added to metamask, you can send it using the metamask UI the same as you would any regular token.

To find your pool in the main UI, go back to the sampleWeightedPool.json file and at the bottom you should see a new field called poolID. Copy that value and paste it on the end of the main UI URL when viewing a pool after the “0x”. For example, the poolID for my WETH/USDC pool I just made on polygon is “72ab6ff76554f90532e2809cee019ade724e029a000200000000000000000020” so I go to https://polygon.balancer.fi/#/pool/0x72ab6ff76554f90532e2809cee019ade724e029a000200000000000000000020

If you’re on main net then instead of polygon.balancer.fi it would be app.balancer.fi of course.

Now you can return to your main wallet and add more liquidity if desired, and other LP’s can join you by adding liquidity themselves.