Project Setup Using Hardhat & Truffle — part 3

Shih-Yu Hwang
Coinmonks
Published in
3 min readDec 3, 2021

--

Laptop open on a desk

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

--

--