Project Setup Using Hardhat & Truffle — part 3
Now we will write a simple smart contract and deploy it to our local blockchain
Writing the smart contract
We can ignore the existing contract for now but feel free to explore them
For both Truffle and Hardhat, we can create a new file inside the contract folder
Solidity is the language that will be used for our example. More information about it can be found HERE
- Name it
NumberChanger.sol
- Declare the SPDX-License with it being commented out
// SPDX-License-Identifier: MIT
- Define the version of Solidity the contract will be complied in, we will set it to the version to be the same as in our config file
This allows it to be compiled with version 0.8.6 or higher
pragma solidity ^0.8.6;
- Write our contract by first declaring it and giving it the same name as our file name
/ SPDX-License-Identifier: MITpragma solidity ^0.8.6;
contract NumberChanger {}
- Declare a state variable called number
// SPDX-License-Identifier: MITpragma solidity ^0.8.6;
contract NumberChanger {uint256 number;}
- Create a function that will retrieve the number stored in the state variable
// SPDX-License-Identifier: MITpragma solidity ^0.8.6;contract NumberChanger {uint256 number;function getNumber() public views returns(uint256) {return number; }}
- Create a function that updates the number in state variable
// SPDX-License-Identifier: MITpragma solidity ^0.8.6;contract NumberChanger {uint256 number;function getNumber() public views returns(uint256) {return number;}function setNumber(uint256 _number) public {number = _number; }}
We now have written a smart contract that allows the number to be stored and updated on the blockchain
Hardhat
Hardhat allows the option to console log in Solidity, in order to do so
- Import hardhat/console.sol after the version of Solidity has been declared
import "hardhat/console.sol"// SPDX-License-Identifier: MITpragma solidity ^0.8.6;import "hardhat/console.sol"
- Add it to where you would like to see an output
// SPDX-License-Identifier: MITpragma solidity ^0.8.6;import "hardhat/console.sol";contract NumberChanger {uint256 number;function getNumber() public view returns(uint256) {return number;}function setNumber(uint256 _number) public {number = _number;console.log(number) }}
The import of hardhat/console.sol and use of console.log is optional only if you are using Hardhat
The finished contract should look like this
// SPDX-License-Identifier: MITpragma solidity ^0.8.6;contract NumberChanger {uint256 number;function getNumber() public view returns(uint256) {return number;}function setNumber(uint256 _number) public {number = _number; }}
In part 4 we will deploy our contract to the local blockchain. Click here for part 4.
Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing
Also, Read
- Best Crypto Exchanges in Africa | Hoo Exchange Review
- eToro vs Robinhood | MoonXBT vs Bybit vs Bityard
- What are the Trading Signals? | Bitstamp vs Coinbase
- ProfitFarmers Review | How to use Cornix Trading Bot
- How to Buy Domain Name on Unstoppable Domains?
- Crypto Tax in India | altFINS Review | Prokey Review
- Blockfi vs Coinbase | BitKan Review | Bexplus Review
- Crypto Exchanges In South Africa | BitMEX Crypto Signals
- MoonXBT Copy Trading | Crypto Wallets in UAE
- Remitano Review | Guide to 1inch Protocol