Note
This guide is now maintained in the documentation:
https://docs.openzeppelin.com/sdk/2.5/public-deploy
Deploy your contracts to a public network
This guide builds on the first tutorial where we created a new OpenZeppelin SDK project, and created a simple Counter
contract in a local development network. We will now see how to deploy this contract to a public network.
We will use the Ropsten public test network for this tutorial, though you could use other public test networks.
NOTE: Ethereum public test networks include:
- Ropsten (Proof of Work, multi-client, ~ 15 second blocktimes);
- Rinkeby (Proof of Authority, geth client only, 15 second blocktimes);
- Kovan (Proof of Authority, parity client only, 4 second blocktimes);
- Goerli (Proof of Authority, multi-client, 15 second blocktimes)
Setup
We will use the my-project
project folder we created earlier.
Test account
We use mnemonics to generate a 12 word mnemonic. Run the following to generate a mnemonic for testing purposes:
npx mnemonics
Create Infura account
We will use Infura to interact with public Ethereum nodes (if you are not running your own nodes). Infura Core is free to use. Follow the instructions on the Infura website to sign up and create a new project.
Install dotenv
We will use dotenv to store your Infura Project ID and your development mnemonic. Run the following in your project folder to install as a development dependency:
$ npm install --save-dev dotenv
Configure .gitignore
We configure .gitignore to ensure that values in .env
(Infura Project ID and development mnemonic) don’t get accidentally committed to our source code repository. Create a .gitignore
file in your project folder with the following contents:
# Dependency directory
node_modules
# local env variables
.env
# truffle build directory
build
Configure .env
We configure .env
to store our Infura Project ID and development Mnemonic used for testing. Create a .env
file in your project folder with the following contents, using the Infura Project ID and the development Mnemonic you created earlier:
INFURA_PROJECT_ID="ENTER INFURA PROJECT ID"
DEV_MNEMONIC="ENTER 12 WORD SEED PHRASE"
Install HD Wallet Provider
We will use truffle-hdwallet-provider to sign transactions for addresses derived from a 12 or 24 word mnemonic. Run the following in your project folder to install as a development dependency:
$ npm install --save-dev truffle-hdwallet-provider
Configure networks.js
Update networks.js
to configure additional networks to connect to. We require dotenv
and truffle-hdwallet-provider
and then specify the provider for the network using the Infura Project ID and development mnemonic. Your networks.js
should look like the following:
require('dotenv').config();
const HDWalletProvider = require('truffle-hdwallet-provider');
const infuraProjectId = process.env.INFURA_PROJECT_ID;
module.exports = {
networks: {
development: {
protocol: 'http',
host: 'localhost',
port: 8545,
gas: 5000000,
gasPrice: 5e9,
networkId: '*',
},
ropsten: {
provider: () => new HDWalletProvider(process.env.DEV_MNEMONIC, "https://ropsten.infura.io/v3/" + infuraProjectId),
networkId: 3, // Ropsten's id
},
},
};
NOTE: If you want to use an account other than the first account generated by the mnemonic then you can provide the account index (zero based) as the third parameter to HDWalletProvider. See the truffle tutorial for details: https://www.trufflesuite.com/tutorials/using-infura-custom-provider
NOTE: To expose additional addresses from the same mnemonic set num_addresses
in your config.
To change the derivation path to derive addresses set wallet_hdpath
in your config.
See the truffle-hdwallet-provider repository for details.
Fund test account
Use the interactive command openzeppelin accounts
, selecting the desired network to view the configured accounts for that network:
$ npx openzeppelin accounts
? Pick a network ropsten
Accounts for ropsten:
Default: 0x581f96c12064e2dfb72E8B9722a18731D756Fe73
All:
- 0: 0x581f96c12064e2dfb72E8B9722a18731D756Fe73
Add test Ether to the account that you want to use e.g. the default/first account derived from the mnemonic. You can add funds using a faucet (e.g. https://faucet.ropsten.be).
If the account doesn’t have enough test Ether when you attempt to deploy, you will get the error deployment failed with error: insufficient funds for gas * price + value
.
You can also import the 12 word seed phrase into MetaMask and then you can request test Ether from the MetaMask faucet. (https://faucet.metamask.io)
Deploy contract
We deploy our contract using OpenZeppelin SDK interactive commands by running openzeppelin create
. Select the Counter contract, the ropsten network and press N for no to calling a function on the instance after creating it.
$ npx openzeppelin create
Nothing to compile, all contracts are up to date.
? Pick a contract to instantiate Counter
? Pick a network ropsten
✓ Contract Counter deployed
All contracts have been deployed
? Do you want to call a function on the instance after creating it? No
✓ Setting everything up to create contract instances
✓ Instance created at 0x584Fcb424b17d3505B21c881d57EF9Bf1B18c4A7
0x584Fcb424b17d3505B21c881d57EF9Bf1B18c4A7
Interact
We can send transactions to our contract using OpenZeppelin SDK interactive commands by running openzeppelin send-tx
. Select the Counter contract, the ropsten network, the function to use and an amount to increase the Counter by. e.g. 23.
$ npx openzeppelin send-tx
? Pick a network ropsten
? Pick an instance Counter at 0x584Fcb424b17d3505B21c881d57EF9Bf1B18c4A7
? Select which function increase(amount: uint256)
? amount (uint256): 23
✓ Transaction successful. Transaction hash: 0x5f3449b06aee60146ccb3c63d4bdbc8f03bf9140ce9b23b51defe98e32b81a74
We can call functions on our contract using OpenZeppelin SDK interactive commands by running openzeppelin call
. Select the Counter contract, the ropsten network and the function to call value()
.
$ npx openzeppelin call
? Pick a network ropsten
? Pick an instance Counter at 0x584Fcb424b17d3505B21c881d57EF9Bf1B18c4A7
? Select which function value()
✓ Method 'value()' returned: 23
23
View your transactions on a blockchain explorer
You can view your transactions on a blockchain explorer that supports the network you used. For instance, Etherscan supports Ropsten at https://ropsten.etherscan.io/. You can search Etherscan using the contract address of your instance of Counter
, remember that the contract address is displayed during deployment and when interacting with it (Instance created at 0x...
).
That’s it! You now know how to deploy an OpenZeppelin SDK contract to a public network and interact with it using OpenZeppelin SDK interactive commands.