Connecting to public test networks – OpenZeppelin Docs
To connect our project to a public testnet, we will need to:
Head over to Alchemy (includes referral code), sign up, and jot down your assigned API key – we will use it later to connect to the network.
In this guide we will use Alchemy, though you can use Infura , or another public node provider of your choice.
While you can spin up your own Geth or OpenEthereum node connected to a testnet, the easiest way to access a testnet is via a public node service such as Alchemy or Infura . Alchemy and Infura provide access to public nodes for all testnets and the main network, via both free and paid plans.
To send transactions in a testnet, you will need a new Ethereum account. There are many ways to do this: here we will use the mnemonics package, which will output a fresh mnemonic (a set of 12 words) we will use to derive our accounts:
Configuring the network
Since we are using public nodes, we will need to sign all our transactions locally. We will configure the network with our mnemonic and an Alchemy endpoint.
This part assumes you have already set up a project. If you haven’t, head over to the guide on Setting up a Solidity project.
Let’s start by installing the @truffle/hdwallet-provider:
$ npm install --save-dev @truffle/hdwallet-provider
We need to update our configuration file with a new network connection to the testnet. Here we will use Goerli, but you can use whichever you want:
// truffle-config.js
+const { alchemyApiKey, mnemonic } = require('./secrets.json');
+const HDWalletProvider = require('@truffle/hdwallet-provider');
module.exports = {
...
networks: {
development: {
...
},
+ goerli: {
+ provider: () => new HDWalletProvider(
+ mnemonic, `https://eth-goerli.alchemyapi.io/v2/${alchemyApiKey}`,
+ ),
+ network_id: 5,
+ gasPrice: 10e9,
+ skipDryRun: true,
+ },
},
...
};
See the HDWalletProvider
documentation for information on configuration options.
// hardhat.config.js
+ const { alchemyApiKey, mnemonic } = require('./secrets.json');
...
module.exports = {
+ networks: {
+ goerli: {
+ url: `https://eth-goerli.alchemyapi.io/v2/${alchemyApiKey}`,
+ accounts: { mnemonic: mnemonic },
+ },
+ },
...
};
See the Hardhat networks configuration documentation for information on configuration options.
Note in the first line that we are loading the project id and mnemonic from a secrets.json
file, which should look like the following, but using your own values. Make sure to .gitignore
it to ensure you don’t commit secrets to version control!
{
"mnemonic": "drama film snack motion ...",
"alchemyApiKey": "JPV2..."
}
Instead of a secrets.json
file, you can use whatever secret-management solution you like for your project. A popular and simple option is to use dotenv
for injecting secrets as environment variables.
We can now test out that this configuration is working by listing the accounts we have available for the goerli network. Remember that yours will be different, as they depend on the mnemonic you used.
$ npx truffle console --network goerli
truffle(goerli)> accounts
[ '0xEce6999C6c5BDA71d673090144b6d3bCD21d13d4',
'0xC1310ade58A75E6d4fCb8238f9559188Ea3808f9',
... ]
$ npx hardhat console --network goerli
Welcome to Node.js v12.22.1.
Type ".help" for more information.
> accounts = await ethers.provider.listAccounts()
[
'0xEce6999C6c5BDA71d673090144b6d3bCD21d13d4',
'0xC1310ade58A75E6d4fCb8238f9559188Ea3808f9',
...
]
We can also test the connection to the node, by querying our account balance.
> await web3.eth.getBalance(accounts[0])
'0'
> (await ethers.provider.getBalance(accounts[0])).toString()
'0'
Empty! This points to our next task: getting testnet funds so that we can send transactions.