Table of Contents
“`soliditypragma solidity ^0.8.0;
contract Note {
// Mapping to store notesmapping(uint => NoteData) public notes;// Structure to represent a notestruct NoteData { address author; string content; uint timestamp;}// Function to create a new notefunction createNote(string memory _content) public { // Generate a unique ID for the note uint id = notes.length++; // Create a new note NoteData memory noteData = NoteData({ author: msg.sender, content: _content, timestamp: block.timestamp }); // Store the note notes[id] = noteData;}// Function to get a note by its IDfunction getNote(uint _id) public view returns (NoteData memory) { return notes[_id];}
}“`
Explanation:
Note contract stores notes in a mapping called notes, where the key is the ID of the note and the value is a NoteData structure.NoteData structure contains the author’s address, the note content, and the timestamp.createNote function allows you to create a new note and store it in the notes mapping.getNote function allows you to retrieve a note by its ID.Example Usage:
“`// Create a new noteawait note.createNote(“This is a new note.”);
// Get the note by its IDNoteData memory noteData = await note.getNote(0);
// Print the note contentconsole.log(noteData.content); // Output: This is a new note.“`
Note:
NoteData struct to include additional data fields.Table of Contents
Categories