Created
December 24, 2024 22:05
-
-
Save theishu4/a0b57cd27dd745868d43b003db56aa2b to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.24+commit.e11b9ed9.js&optimize=false&runs=200&gist=
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.18; | |
import {SimpleStorage} from "./SimpleStorage.sol"; | |
contract AddFiveStorage is SimpleStorage { | |
function sayBye() public pure returns (string memory) { | |
return "Bye Ishu, Nice to meet you!"; | |
} | |
// Overriding updateFavouriteNumber method | |
function updateFavouriteNumber(uint256 number) public override { | |
favouriteNumber = number + 5; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
/* | |
* Specifying Solidity Compiler Version: | |
* - `0.8.18`: Locks the compiler to this exact version (v0.8.18). | |
* | |
* - `^0.8.18`: Allows using this version or any newer minor/patch versions (e.g., 0.8.19, 0.8.20). | |
* Note: It will not include breaking changes from major versions like 0.9.0. | |
* | |
* - `>=0.8.0 <0.9.0`: Allows any version from 0.8.0 up to (but not including) 0.9.0. | |
*/ | |
pragma solidity 0.8.24; | |
contract SimpleStorage { | |
// If no access modifier is specified, the default visibility is `internal`. | |
uint256 favouriteNumber; | |
Person[] peopleList; // List of people with their favourite numbers | |
mapping(string => uint256) nameToFavouriteNumber; // Maps a person's name to their favourite number | |
struct Person { | |
string name; | |
uint256 favouriteNumber; | |
} | |
// virtual -> Allows this method to be overridden by derived (sub) contracts. | |
function updateFavouriteNumber(uint256 _favouriteNumber) public virtual { | |
favouriteNumber = _favouriteNumber; | |
} | |
// Returns the contract's favourite number (view -> read-only) | |
function getFavouriteNumber() public view returns (uint256) { | |
return favouriteNumber; | |
} | |
// Returns a fixed greeting message (pure -> does not read or modify contract state) | |
function sayHello() public pure returns (string memory) { | |
return "Hello, Ishu!"; | |
} | |
/* | |
* `memory`: Temporary variable that can be modified within the function. | |
* | |
* `calldata`: Temporary variable that cannot be modified within the function. | |
It cannot be used as the function return type. Only valid for function input | |
parameters. | |
* | |
* `storage`: Permanent variable that can be modified. | |
*/ | |
function addPerson(string memory _name, uint256 _favouriteNumber) public { | |
peopleList.push(Person({name: _name, favouriteNumber: _favouriteNumber})); | |
nameToFavouriteNumber[_name] = _favouriteNumber; | |
} | |
/* | |
* `memory`: Used for the return type since the struct is temporarily loaded from storage. | |
* Includes a check to prevent accessing an invalid index. | |
*/ | |
function getPersonAt(uint256 index) public view returns (Person memory) { | |
require(index < peopleList.length, "Index out of bounds"); | |
return peopleList[index]; | |
} | |
// If the name is not in the mapping, it returns 0 by default. | |
function getFavouriteNumberByName(string memory _name) public view returns (uint256) { | |
return nameToFavouriteNumber[_name]; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.18; | |
import {SimpleStorage} from "./SimpleStorage.sol"; | |
contract StorageFactory { | |
SimpleStorage[] public simpleStorageList; | |
function createSimpleStorageContract() public { | |
simpleStorageList.push(new SimpleStorage()); | |
} | |
function sfUpdateFavouriteNumber(uint256 simpleStorageIndex, uint256 favouriteNumber) public { | |
require(simpleStorageIndex < simpleStorageList.length, "Index Out of Bound"); | |
SimpleStorage currStorage = simpleStorageList[simpleStorageIndex]; | |
currStorage.updateFavouriteNumber(favouriteNumber); | |
} | |
function getFavouriteNumberOfStorage(uint256 index) public view returns (uint256) { | |
return simpleStorageList[index].getFavouriteNumber(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment