# Developing smart contracts

In this guide we will be going over the following:

* Setting up a Solidity Project
* Compiling Solidity Source Code
* Adding More Contracts
* Using OpenZeppelin Contracts

### About Solidity <a href="#about_solidity" id="about_solidity"></a>

We won’t be covering language concepts such as syntax or keywords in this guide. For that, you’ll need to check out the following curated content, which features great learning resources for both newcomers and experienced developers:

* For a general overview of how Ethereum and smart contracts work, the official website hosts a [Learn about Ethereum](https://ethereum.org/learn/) section with lots of beginner-friendly content.
* If you’re new to the language, the [official Solidity documentation](https://solidity.readthedocs.io/en/latest/introduction-to-smart-contracts.html) is a good resource to have handy. Take a look at their [security recommendations](https://solidity.readthedocs.io/en/latest/security-considerations.html), which nicely go over the differences between blockchains and traditional software platforms.
* Consensys' [best practices](https://consensys.github.io/smart-contract-best-practices/) are quite extensive, and include both [proven patterns](https://consensys.github.io/smart-contract-best-practices/development-recommendations/) to learn from and [known pitfalls](https://consensys.github.io/smart-contract-best-practices/attacks/) to avoid.
* The [Ethernaut](https://ethernaut.openzeppelin.com/) web-based game will have you look for subtle vulnerabilities in smart contracts as you advance through levels of increasing difficulty.

With that out of the way, let’s get started!

### Setting up a Project <a href="#setting-up-a-solidity-project" id="setting-up-a-solidity-project"></a>

The first step is to install a development tool.

The most popular development framework for Ethereum is [Hardhat](https://hardhat.org/), and we cover its most common use with [ethers.js](https://docs.ethers.io/). The next most popular is [Truffle](https://www.trufflesuite.com/truffle) which uses [web3.js](https://web3js.readthedocs.io/). Each has its strengths and it is useful to be comfortable using all of them.

In this guide, we will show how to develop, test and deploy smart contracts using hardhat

To get started with Hardhat we will install it in our project directory.

```bash
npm install --save-dev hardhat
```

Once installed, we can run `npx hardhat`. This will create a Hardhat config file (`hardhat.config.js`) in our project directory.

```bash
npx hardhat
```

```
888    888                      888 888               888
888    888                      888 888               888
888    888                      888 888               888
8888888888  8888b.  888d888 .d88888 88888b.   8888b.  888888
888    888     "88b 888P"  d88" 888 888 "88b     "88b 888
888    888 .d888888 888    888  888 888  888 .d888888 888
888    888 888  888 888    Y88b 888 888  888 888  888 Y88b.
888    888 "Y888888 888     "Y88888 888  888 "Y888888  "Y888

Welcome to Hardhat v2.2.1

✔ What do you want to do? · Create an empty hardhat.config.js
Config file created
```

### First contract <a href="#first-contract" id="first-contract"></a>

We store our Solidity source files (`.sol`) in a `contracts` directory. This is equivalent to the `src` directory you may be familiar with from other languages.

We can now write our first simple smart contract, called `Box`: it will let people store a value that can be later retrieved.

We will save this file as `contracts/Box.sol`. Each `.sol` file should have the code for a single contract, and be named after it.

{% code title="Box.sol" %}

```solidity
// 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;
    }
}
```

{% endcode %}

### Compiling Solidity <a href="#compiling-solidity-source-code" id="compiling-solidity-source-code"></a>

The Ethereum Virtual Machine (EVM) cannot execute Solidity code directly: we first need to compile it into EVM bytecode.

Our `Box.sol` contract uses Solidity 0.8 so we need to first [configure Hardhat to use an appropriate solc version](https://hardhat.org/config/#solidity-configuration).

We specify a Solidity 0.8 solc version in our `hardhat.config.js`

```
/**
 * @type import('hardhat/config').HardhatUserConfig
 */
 module.exports = {
  solidity: "0.8.4",
};
```
