Course lesson
Deploy a Solidity Smart Contract with Hardhat to a Local Ethereum Network
Now you need a way to keep a local network alive, deploy the smart contract there and then interact with it. You can think on this like a local web server. When you develop an API you have a local server running it so the client application can talk to it so...
- Duration
- 3 min
- Access
- Included
- Transcript
- Retained from source evidence
Now you need a way to keep a local network alive, deploy the smart contract there and then interact with it. You can think on this like a local web server. When you develop an API you have a local server running it so the client application can talk to it so you can develop and test, this is the same idea.
Head over to your terminal and run tthe following command.
$ npm run hardhat nodeThis will ramp up a local ethreum network using hardhat and will show in the console a set of accounts and private keys that can be used for testing pruposes.
These are 20 test accounts and addresses created for your. Each account is also rich! are loaded with 10000 ETH. (fake eth btw).
Right now this is just an empty blockchain. There are no blocks nor smart contracts on it. But you want to create a new block and put the smart contract that you just created there, for that you need to create a little script that will use ethers.js power to compile and deploy.
Deploying to local Ethereum network
Now you need a way to keep a local network alive, deploy the smart contract there and then interact with it. You can think on this like a local web server. When you develop an API you have a local server running it so the client application can talk to it so you can develop and test, this is the same idea.
Head over to your terminal and run tthe following command.
$ npm run hardhat nodeThis will ramp up a local ethreum network using hardhat and will show in the console a set of accounts and private keys that can be used for testing pruposes.
These are 20 test accounts and addresses created for your. Each account is also rich! are loaded with 10000 ETH. (fake eth btw).
Right now this is just an empty blockchain. There are no blocks nor smart contracts on it. But you want to create a new block and put the smart contract that you just created there, for that you need to create a little script that will use ethers.js power to compile and deploy.
Open your editor to create the file scripts/deploy.js.
const main = async () => {
const [deployer] = await hre.ethers.getSigners();
const accountBalance = await deployer.getBalance();
console.log('Deploying contracts with account: ', deployer.address);
console.log('Account balance: ', accountBalance.toString());
const Token = await hre.ethers.getContractFactory('TipJar');
const portal = await Token.deploy();
await portal.deployed();
console.log('TipJar address: ', portal.address);
};
const runMain = async () => {
try {
await main();
process.exit(0);
} catch (error) {
console.error(error);
process.exit(1);
}
};
runMain();With this in had you're ready to deploy to the local network.
Deploying
Go to a new terminal window and run this command
$ npm run hardhat:deployMake sure to run this command at the root of the project folder and without stopping the local network.
After that is is done, the smart contract will be deployed to the local network and will be ready to interact with it.
When the contract is deployed it use the first account that was created by the local network.
If you check the output of the console, you'll see a message that contains the address of the contract, somethig like this:
Deploying contracts with account: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Account balance: 10000000000000000000000
TipJar address: 0x5FbDB2315678afecb367f032d93F642f64180aa3The address under TipJar address is what you'll use in the client application to "talk" to the smart contract. You can think in this address like your API url.
Now the contract is deployed and you have the blockchain address of it. Keep it safe to use it later in the web site.
In the terminal that is running the local network, you'll see a message like this:
Contract deployment: TipJar
Contract address: 0x5fbdb2315678afecb367f032d93f642f64180aa3
Transaction: 0xe1f30eea14dd4ec3250a23fe26ee396d46689260cfa416301ec24c14a625d4d4
From: 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266
Value: 0 ETH
Gas used: 745919 of 745919
Block #1: 0x1e9b1e2c715cda8a4cf21f029b0ee92df9c902e9af78ac6ab1207d10b794a49f
This shows you some information, like the contract name and the addressm but also the trasancation identifier that can be used to validate the trasnaction and also the account that perform the deployment (the owner).
Is time to work on the Dapp!