Truffle is an amazing suite of tools created by Consensys to develop smart contracts for the Ethereum blockchain network. However, it can be a bit jarring to make the leap from local development to the real test network, ropsten.
Required Setup
For this walk through, I have installed:
I will be using the default example truffle project, MetaCoin, that you can walk through how to unbox here or follow along using your own project.
First things first, if you do NOT have a package.json
file yet, make sure to run npm init
. This will turn the directory into a node package that we can easily manage with the npm
package manager. So we don’t have to download dependices into the global package scope.
Now we can download all the things we are going to need:
npm install bip39 dotenv --save
- bip39 – used to generate wallet mnemonic
- dotenv – simple way to read environment variable files
We got everything development wise we need now.
Storing Secrets outside the code
We will have to create a private key or mnemonic, and that means we need somewhere relatively secure to store it. For testnet stuff, this can be as simple as making sure it’s not being put into version control alongside the code. To that end, we are going to use Environment Variables, and will to store them in a file called .env
(that’s it, just an extension basically. Make sure to add it to your .gitignore
if you’re using git
). To learn more, check out the github page for dotenv
. But for our purposes, all you need to know is that this file will have a format of:
ENV_VARIABLE_NAME=someting ANOTHER_ENV=something else
Accessing testnet
The easiest way to reach out to testnet is by using a provider. I personally like using infura.io (free, just requires registration). After you register and have your API key emailed to you, make sure you select the URL for the test network and add to the .env
file using a variable named ROPSTEN_URL
.
ROPSTEN_URL=https://ropsten.infura.io/<your-api-key>
It’s also possible to use your own geth
node set to testnet
, but that is not required.
Next we are going to create our own wallet, if you already have one set up, like with MetaMask, you can skip this next part.
Creating your testnet wallet
So now you have an place to put your secrets, lets create some. This is where bip39
comes in, it will create random mnemonics which can be used as the basis for private key of a wallet. It will be a series of 12 random words.
We could put this generation in a file, but it’s easy enough to just do straight from the command line:
node -e "console.log(require('bip39').generateMnemonic())"
This will output 12 words, DO NOT SHARE THESE ANYWHERE. The ones I am using below are example ones, and also shout NOT be used. Put them in .env
file as the variable MNEMONIC
. So now your .env
file should now contain:
MNEMONIC=candy maple cake sugar pudding cream honey rich smooth crumble sweet treat ROPSTEN_URL=https://ropsten.infura.io/<your-api-key>
We have our seed, so it’s time to hook it into our code. In your truffle.js
or truffle-config.js
file, you will need to now import the environment variables and a wallet provider at the top of the file.
require('dotenv').config() const HDWalletProvider = require('truffle-hdwallet-provider')
After that is added, we will move down to the the exports section, we are going to add a new network, named ropsten
. Then are going to use the HDWalletProvider
and supply it with the mnemonic and Ifura url provided via environment variables.
module.exports = { networks: { ropsten: { provider: () => new HDWalletProvider( process.env.MNEMONIC, process.env.ROPSTEN_URL), network_id: 3 }, }, }
Test and make sure everything’s working by opening a truffle console, specifying our new network.
truffle console --network ropsten
We can then get our public account address via the console.
truffle(ropsten)> web3.eth.getAccounts((err, accounts) => console.log(accounts)) [ '0x627306090abab3a6e1400e9345bc60c78a8bef57' ]
If you are seeing this same wallet address, you did it wrong. Go back and make your own mnemonic, don’t copy the candy
one from above.
Funding the wallet
In your development environment, the wallet already has ETH in it to pay for gas and deploying the contract. On the mainnet, you will have to buy some real ETH. On testnet, you can get some for free by using a Faucet, such as https://faucet.ropsten.be/ or if you’re using MetaMask just use https://faucet.metamask.io/.
Make sure to use the address you gathered from the console for the faucet, and soon you should have test funds to play around with and actually deploy your contract.
Deploying the Contract
Now where the rubber meets the road, getting your contract out into the real (test) world.
truffle deploy --network ropsten
If everything is successful, you’ll get messages like these:
Using network 'ropsten'. Running migration: 1_initial_migration.js Deploying Migrations... ... 0xefe70115c578c92bfa97154f70f9c3fbaa2b8400b1da1ee7cdxxxxxxxxxxxxxx Migrations: 0x6eeedefb64bd6ee6618ac54623xxxxxxxxxxxxxx Saving successful migration to network... ... 0xd4294e35c166e2dca771ba9bf5eb3801bc1793e30db6a53d4dxxxxxxxxxxxxxx Saving artifacts... Running migration: 2_deploy_contracts.js Deploying Capture... ... 0x446d5e92d6976bb05c85bb95b243d6f7405af6bb12b3b6fe08xxxxxxxxxxxxxx Capture: 0x1d2f60c6ef979ca86f53af1942xxxxxxxxxxxxxx Saving successful migration to network... ... 0x0b6f918ccc8e3b82cdf43038a2c32fe1fef66d0fa9aeb2260bxxxxxxxxxxxxxx Saving artifacts...
Tada! You now have your custom contracts deployed to testnet!
Or, you got an out of gas
error, as it is not uncommon to have to adjust the gas price to get it onto the network, as truffle does not automatically figure that out for you. A follow up post will show how to calculate and adjust gas price as needed.
Great article, but its not working for me.
Im able to get the account address from the console. When I run,
$truffle deploy –network ropsten
It fails silently. 🙁
Any ideas on resolving this