Developing Smart Contracts
To begin working with Mazze blockchain, which is compatible with the Ethereum Virtual Machine (EVM), you'll first need to install Hardhat in your project directory.
Execute the following command to install Hardhat:
npm install --save-dev hardhatAfter installation, initiate Hardhat by running npx hardhat. This action will generate a Hardhat configuration file (hardhat.config.js) in your project directory:
npx hardhatYou'll be greeted by the Hardhat interface:
npx hardhat
Welcome to Hardhat v2.2.1
โ What do you want to do? ยท Create an empty hardhat.config.js
Config file createdThe next step is to create your first smart contract. In the contracts directory, store your Solidity source files (.sol). Let's start with a basic contract named Box, which allows storing and retrieving a value.
Create the contract in the file contracts/Box.sol:
// contracts/Box.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Box {
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;
}
}For compiling Solidity code to EVM bytecode, configure Hardhat to use a suitable Solidity compiler version, matching your contract's requirements. In hardhat.config.js, specify Solidity 0.8 for our Box.sol contract:
To compile, run:
Hardhat will compile all contracts in the contracts directory. The compiled artifacts (bytecode and metadata) will be stored in the artifacts directory.
As your project expands, you might create more contracts. For instance, let's add an access control system to our Box contract. We'll create an Auth contract that stores an administrator address. This contract will be placed in a subdirectory, like contracts/access-control/Auth.sol.
To integrate Auth with Box, use an import statement in Box.sol:
For advanced modularization, consider using inheritance in Solidity. A great resource for reusable modules and libraries is the OpenZeppelin Contracts library. It's thoroughly audited for security and correctness.
To use OpenZeppelin Contracts, install the library:
Then, import the necessary contracts from OpenZeppelin. For example, to add access control to Box, replace the Auth contract with Ownable from OpenZeppelin:
For more information on developing secure smart contract systems, refer to the OpenZeppelin Contracts documentation, including their Access Control guide.
Last updated
Was this helpful?