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.".
- Host: GitHub
- URL: https://github.com/nawodyaishan/evm-events-hardhat
- Owner: nawodyaishan
- Created: 2024-01-07T19:40:52.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-02-19T01:47:05.000Z (over 1 year ago)
- Last Synced: 2025-01-21T20:49:24.693Z (9 months ago)
- Topics: hardhat, javascript, smart-contracts
- Language: JavaScript
- Homepage:
- Size: 259 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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.