This repository contains a smart contract written in Solidity that functions as a simple digital notebook. The contract allows users to create, store, and share short notes. Users can manage the visibility of their notes by keeping them private, sharing them with specific users, or making them public for everyone to read.
The project also includes a test suite ensuring a minimum of 90% test coverage and optimizations for gas efficiency and security.
- Contract Name: BlockNotesk
- Version: Solidity ^0.8.26
- Network: Ethereum Sepolia Testnet
- Deployed Address: Link to Etherscan (Replace with actual link to verified contract)
Before running or deploying the contract, make sure you have:
- Node.js and npm installed
- Truffle or Hardhat framework
- Solidity ^0.8.26
- Ganache for local testing or any Ethereum-compatible network.
-
Private:
Only the owner can read the note. -
Shared:
The owner can share the note with specific users. -
Public:
Anyone can read the note.
-
Mapping:
Notes are stored in a mapping of noteId to Note struct. -
User's Notes:
Each user has a list of their note IDs stored in a mapping of address to uint256[].
-
createNote(string memory content, Visibility visibility):
Creates a new note with specified visibility. -
getNote(uint256 _noteId):
Reads a note, respecting the visibility settings. -
shareWith(uint256 noteId, address user):
Shares a note with a specific address. -
deleteNote(uint256 noteId):
Deletes a note owned by the user. -
changeVisibility(uint256 noteId, Visibility newVisibility):
Allows the owner to change the visibility of a note. -
receive():
Returns ether to users whom mistakenly sends funds to contract.
-
NoteCreated(uint256 noteId, address owner)
-
NoteShared(uint256 noteId, address sharedWith)
-
NoteVisibilityChanged(uint256 noteId, Visibility newVisibility)
-
NoteDeleted(uint256 noteId)
-
Custom Errors:
Reduces gas costs by replacing string-based require messages with custom errors. -
Efficient Storage Access:
Reduces gas by minimizing redundant storage reads in modifiers. -
Mappings Instead of Arrays:
Optimizes user access control with O(1) lookups rather than more expensive dynamic array operations.
The contract includes a receive() function to handle any ETH sent to the contract. However, ETH handling is not a primary function of this contract.
Clone the repository:
git https://github.com/nativealien/solidity-assignment.git
cd solidity-assignmentInstall dependencies:
npm installCompile the contract:
npx hardhat compileRun the tests:
npm testRun the test suite to ensure the contract works as expected:
npx hardhat testEnsure that test coverage is above 90% by running:
npx hardhat coverage