Home > Article > Web Front-end > Building and deploying a smart contract with OpenZepplin and Solidity in less than minutes
I've been developer for over 10 years now. I've been lucky enough to become an Apache committer and PPMC, speak at Google, write a book for Manning Publications, and a few other things. As the job market is not great and people are struggling to find good work, I'm starting to see business opportunities in blockchain, more specifically - I see some great opportunities to help others build their own businesses. I'd like to share some technical things I've learned in the last few weeks.
I’ve been working through understanding the pros and cons of distributed applications (DApps). There are many tools out there for you to choose from to get started in building them. In this article I’m giving you an opinionated approach to building, deploying and interacting with smart contracts locally. No web based tools, only command line.
Prerequisites:
I'm using Node 18.17, however, this should work with a later version of node.
$ nvm install 18.17
$ mkdir hello-world && cd hello-world
$ npm init -y
$ npm install --save-dev hardhat
npx is used to run executables installed locally in your project. It’s recommended to install Hardhat locally in each project so that you can control the version on a project by project basis.
$ npx hardhat init Need to install the following packages: hardhat@2.22.15 Ok to proceed? (y) You should see the option show up. Select “ Create an empty hardhat.config.js”
You'll see this upon successful creation.
✔ What do you want to do? · Create an empty hardhat.config.js ✨ Config file created ✨
To verify everything executed as expected you should now see two field in your current directory.
$ ls -lta package.json hardhat.config.js
When using Hardhat you can store Solidity source files (.sol) in a contracts directory. We will write our first simple smart contract, called Storage: it will let people store a value that can be later retrieved. It's a variation of another starter contract with Hardhat. I'm working through the process manually so we understand all of the moving pieces.
$ mkdir contracts && vim contracts/Storage.sol
// contracts/Storage.sol // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract Storage { uint256 private _value; // Emitted when the stored value changes event ValueChanged(uint256 value); // Stores a new value in the contract function store(uint256 value) public { _value = value; emit ValueChanged(value); } // Reads the last stored value function retrieve() public view returns (uint256) { return _value; } }
After writing the above to the file close vim with :wq or :x
The Ethereum Virtual Machine (EVM) cannot execute Solidity code directly: we first need to compile it into EVM bytecode. Our Storage.sol contract uses Solidity 0.8 so we need to first configure Hardhat to use an appropriate solc version. We specify a Solidity 0.8 solc version in our hardhat.config.js.
$ nvm install 18.17
$ mkdir hello-world && cd hello-world
We will create a script to deploy our Storage contract. We will save this file as scripts/deploy.js.
$ npm init -y
$ npm install --save-dev hardhat
We use ethers in our script, so we need to install it and the @nomicfoundation/hardhat-ethers plugin.
$ npm install --save-dev @nomicfoundation/hardhat-ethers ethers
We need to add in our configuration that we are using the @nomicfoundation/hardhat-ethers plugin.
Our hard hat config should now look like this;
$ npx hardhat init Need to install the following packages: hardhat@2.22.15 Ok to proceed? (y) You should see the option show up. Select “ Create an empty hardhat.config.js”
We need an environment where we can deploy our contracts. The Ethereum blockchain (often called "mainnet", for "main network") requires spending real money to use it, in the form of Ether (its native currency). This makes it a poor choice when trying out new ideas or tools.
To solve this, a number of "testnets" (for "test networks") exist: However, you will still need to deal with private key management, blocktimes of 12 seconds or more, and actually getting this free Ether.
During development, it is a better idea to instead use a local blockchain. It runs on your machine, provides you with all the Ether that you need, and mines blocks instantly.
✔ What do you want to do? · Create an empty hardhat.config.js ✨ Config file created ✨
Deploy your smart contract
$ ls -lta package.json hardhat.config.js
With our Storage contract deployed, we can start using it right away.
We will use the Hardhat console to interact with our deployed Storage contract on our localhost network.
We need to specify the address of our Storage contract we displayed in our deploy script.
It’s important that we explicitly set the network for Hardhat to connect our console session to. If we don’t, Hardhat will default to using a new ephemeral network, which our Storage contract wouldn’t be deployed to.
$ mkdir contracts && vim contracts/Storage.sol
The first function, store, receives an integer value and stores it in the contract storage. Because this function modifies the blockchain state, we need to send a transaction to the contract to execute it.
We will send a transaction to call the store function with a numeric value:
$ nvm install 18.17
The other function is called retrieve, and it returns the integer value stored in the contract. This is a query of blockchain state, so we don’t need to send a transaction:
$ mkdir hello-world && cd hello-world
Because queries only read state and don’t send a transaction, there is no transaction hash to report. This also means that using queries doesn’t cost any Ether, and can be used for free on any network.
We've created a minimal smart contract and deployed it to a local blockchain instance to demonstrate how we can write and read values from the blockchain. If you found this article helpful, please give it a like and/or a share.
Please feel free to comment with suggestions or corrections you see fit. I write these articles before and after work, I get them out as quickly as I can.
Thanks!
Reference:
Hardhat docs
The above is the detailed content of Building and deploying a smart contract with OpenZepplin and Solidity in less than minutes. For more information, please follow other related articles on the PHP Chinese website!