In the last tutorial, you had created and deployed your ERC20 token locally. In this tutorial, you will learn to deploy your ERC20 token on a testnet.
What is a testnet?
Ethereum, as a whole, has multiple networks. Mainnet is the actual network that Ethereum uses, but there are other networks, which are used by developers to test their contracts. In this tutorial, we will be using the Kovan testnet.
Requirements
You will need a MetaMask browser wallet.
You will need an Infura account.
Get test Ether
Each testnet has its own type of Ether. We will be using the Kovan testnet in this tutorial, so you will need some Kovan ETH. To get Kovan ETH, you will need to use a faucet. Faucet, as understandable by its name, is a source where you can earn test ETH by accomplishing simple tasks.
The faucet I personally use is the Chainlink faucet. From the following link, you can get free Kovan ETH by just typing your address that you get from MetaMask and solving an easy captcha.
Click this to go to the faucet
About environment variables
When working with projects, you might have some sensitive data which should not be published at anywhere, as an example, we can think of API keys. To not publish sensitive data, but still reach to it in your local project, you will need environment variables. Brownie has an option for environment variables and this can be very helpful in some situations.
In the base directory, create a file named .env
. In this file, you are going to store your project's environment variables.
In brownie-config.yaml
which we had created earlier, add these options:
dotenv: .env
wallets:
from_key: ${PRIVATE_KEY}
This will be helpful later in the tutorial.
Get Infura Project ID
To connect to the Kovan testnet, you will need an endpoint. Infura is a service which provides simple and reliable access to Ethereum. If you have already opened your Infura account, create a new project, open the project settings and copy the PROJECT ID. Paste in into the .env file we created earlier, like this:
WEB3_INFURA_PROJECT_ID=your_project_id
Now brownie will use this project to reach to the Kovan endpoint.
Get Metamask Private Key
If you have already installed the MetaMask extension and opened an account, go to the extension window, click to the three dots at the top right, enter to 'Account Details', click 'Export Private Key', enter your password, copy your private key and paste it into the .env file like this:
PRIVATE_KEY=your_private_key
At the end, your .env file should look like this:
WEB3_INFURA_PROJECT_ID=your_project_id
PRIVATE_KEY=your_private_key
You have successfully set up the project environment variables.
Deploy the contract
As you might recall, we had used a local account at the previous tutorial. It is time to change that into our MetaMask account.
Open the scripts/deploy.py file again and change it to look like the following code.
from brownie import MyToken, accounts, config
from web3 import Web3
initial_supply = 1000000
def main():
account = accounts.add(config["wallets"]["from_key"])
my_token = MyToken.deploy(initial_supply, {'from': account})
print(my_token.name())
We only changed the accounts, as we have set the private key path in our config earlier in the tutorial, we can get our account easily.
Now, let's deploy our contract. Open a command line window at the base directory, and enter this command:
> brownie run scripts/deploy.py --network kovan
If no errors occur, your contract will be deployed, and you will be given info about the transaction:
Transaction sent: 0x8175029649fc9d6bacf90b1d4fb3789cc4e626d2cfe57c670b4b787dee296726
Gas price: 2.500000007 gwei Gas limit: 704322 Nonce: 26
MyToken.constructor confirmed Block: 29779219 Gas used: 640293 (90.91%)
MyToken deployed at: 0x5BD01Af26009232e51A012C874881051Eee6239d
Congratulations, you have successfully deployed your token to a testnet! If you want to see your token on Etherscan, copy the address after MyToken deployed at:
, which is the smart contract address, paste it into Kovan Etherscan explorer, and your token is visibly there.
I hope this tutorial series was helpful for you to begin developing smart contracts. If you have any questions, or have noticed any problems, feel free to comment!
Top comments (0)