An open API service indexing awesome lists of open source software.

https://github.com/nawodyaishan/evm-events-hardhat

The SimpleStorage contract showcases a basic Solidity smart contract enabling the storage and retrieval of a single uint256 value representing a "favorite number.".
https://github.com/nawodyaishan/evm-events-hardhat

hardhat javascript smart-contracts

Last synced: 7 months ago
JSON representation

The SimpleStorage contract showcases a basic Solidity smart contract enabling the storage and retrieval of a single uint256 value representing a "favorite number.".

Awesome Lists containing this project

README

          

## SimpleStorage Contract

The `SimpleStorage` contract showcases a basic Solidity smart contract enabling the storage and retrieval of a single `uint256` value representing a "favorite number.".

**Contract Features**

* **Storing a Favorite Number:** The `store(uint256)` function allows a user to update the stored favorite number, emitting a `storedNumber` event to log the change.
* **Retrieving the Favorite Number:** The `retrieve()` function provides a `view` function to access the currently stored favorite number.

## Testing

The included test (`SimpleStore.test.js`) demonstrates using Chai and Hardhat's ethers.js library for smart contract testing.

**Key Concepts**

* **Events:** Events serve as a vital mechanism to log on-chain actions like the `storedNumber` event in this contract.

* **View Functions:** `retrieve()` is marked as a `view` function, indicating it reads contract state without making any modifications.

## Usage

Here's a general workflow for interacting with the contract:

1. **Deployment:**
* Compile the `SimpleStorage.sol` contract using a Solidity compiler.
* Deploy the contract to an Ethereum network (local development network, testnet, or mainnet).

2. **Interaction:**
* Using Web3.js or another compatible library, connect to the deployed contract instance.
* **Call `store(newFavoriteNumber)`** to update the stored favorite number.
* **Call `retrieve()`** to fetch the currently stored value.

3. **Event Monitoring:** Watch for `storedNumber` events emitted after successful `store()` calls.

## Installation and Testing

**Prerequisites**

* Node.js and npm (or yarn)
* Hardhat development environment ([https://hardhat.org/](https://hardhat.org/))

**Setup**

1. Clone or download this project.
2. Navigate to the project directory in your terminal.
3. Install dependencies: `npm install` or `yarn install`

**Running Tests**

Execute the tests:

```bash
npx hardhat test
```

## Example Deployment (Hardhat Local Development)

For testing and quick use, Hardhat includes a built-in local blockchain:

1. Make sure the Hardhat development environment is initialized: `npx hardhat`
2. `npx hardhat test` runs both compilation and tests against the local Hardhat Network.

## Considerations

* **Gas Costs:** While a basic contract, be mindful of gas fees associated with updating state in production environments.
* **Limitations:** In its current form, `SimpleStorage` only stores a single favorite number.