Created
February 18, 2024 17:55
-
-
Save 0xriyadh/484e4b98d94aefd04aa13441ed5d1cef 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
{ | |
"overrides": [ | |
{ | |
"files": "*.sol", | |
"options": { | |
"printWidth": 80, | |
"tabWidth": 4, | |
"useTabs": false, | |
"singleQuote": false, | |
"bracketSpacing": false | |
} | |
}, | |
{ | |
"files": "*.yml", | |
"options": {} | |
}, | |
{ | |
"files": "*.yaml", | |
"options": {} | |
}, | |
{ | |
"files": "*.toml", | |
"options": {} | |
}, | |
{ | |
"files": "*.json", | |
"options": {} | |
}, | |
{ | |
"files": "*.js", | |
"options": {} | |
}, | |
{ | |
"files": "*.ts", | |
"options": {} | |
} | |
] | |
} |
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 | |
// telling our compiler which version of solidity we are using in this contract | |
// ^0.8.24 represents, 0.8.24 version or greater versions will work with this contract | |
// >=0.8.24 <0.9.0, this would tell the compiler any version between 0.8.24 and 0.9.0 is a valid version | |
pragma solidity ^0.8.24; | |
contract SimpleStorage { | |
// Basic Types: uint, int, boolean, address, bytes (lower level types) | |
// uint: unsigned integer, meaning no decimal, no fraction [0, infinity) | |
// int: signed integer (-infinity, infinity) | |
bool hasFavNum = true; | |
// we can also define how many bits we wanna use | |
// by default the bits is 256, so uint and uint256 are the same thing. | |
// but it's better to write the num of bits, explicitly. | |
uint256 favNum1 = 99; | |
int256 favNum2 = -55; | |
string favNumInText = "99"; | |
address myFirstAdd = 0xaD1C737896cd841766BED18dd052d8244331e1DB; | |
// NOTE: although uint256 and uint are same, bytes32 and bytes are not. | |
bytes32 favBytes32 = "hello world!"; | |
} |
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.24; | |
contract SimpleStorage { | |
// fabNum variable will get initialized with 0 if no value is given. | |
// but this will not be visible to the public, as by defaut a variable's state is set to internal | |
// so, uint256 internal favNum; and uint256 favNum; are the same thing | |
// to make it visible we need to set the state to public | |
/* | |
Functions & Variables in Solidity can have 4 kinds of visibility | |
1. public: visible externally and internally. Also creates a getter function for storage/state variables. | |
2. private: only visible in the current contract | |
3. external: only visible externally. Only for functions. - i.e. can only be message -called (via this.func) | |
4. internal (default): only visible internally, other people can't call. | |
*/ | |
uint256 public favNum; // 0 | |
function store(uint256 _favNum) public { | |
favNum = _favNum; | |
} | |
/* | |
We will see there will be three function button now when we deploy our contract | |
- orange color | |
- blue color: pure, view. These are two special keywords that notates functions that don't actually have to send a txn in order to call them. They do not mutate any state in the blockchain, they just read states from the blockchain. So if we use any of these two keywords we wouldn't be able to perform any state change operation inside that function. | |
- view: disallow updating state | |
- pure: disallow updating state + reading from state | |
Note: However, view and pure function does costs gas only when another function which is using gas for txn call a view or pure function. | |
*/ | |
function retrieve() public view returns (uint256) { | |
return favNum; | |
} | |
/* | |
We should also keep in mind, when a txn happens, we should care about the txn cost as this refers to the gas that has been used for the txn. Whereas, gas refers to the amount of gas we sent for the txn to happen. | |
*/ | |
} |
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.24; | |
contract SimpleStorage { | |
uint256 myFavNum; // 0 | |
// array | |
uint256[] listOfFavNums; // [] | |
// in solidity we can create custom types using struct | |
struct Person { | |
uint256 favNum; | |
string name; | |
} | |
// Person public myFriend = Person(33, "Shafin"); | |
/* | |
Person public shafin = Person({favNum: 33, name: "Shafin"}); | |
Person public maria = Person({favNum: 177, name: "Maria"}); | |
Person public faruk = Person({favNum: 235, name: "Faruk"}); | |
*/ | |
// dynamic array: size of the array can grow and shrink | |
Person[] public dynamicListOfPeople; // [] | |
// static array: size of the array is fixed | |
// Person[3] public staticListOfPeople; | |
function store(uint256 _favNum) public { | |
myFavNum = _favNum; | |
} | |
function retrieve() public view returns (uint256) { | |
return myFavNum; | |
} | |
function addPerson(string memory _name, uint256 _favNum) public { | |
dynamicListOfPeople.push( Person({name: _name, favNum: _favNum}) ); | |
} | |
} |
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.24; | |
contract SimpleStorage { | |
uint256 myFavNum; // 0 | |
uint256[] listOfFavNums; // [] | |
struct Person { | |
uint256 favNum; | |
string name; | |
} | |
Person[] public dynamicListOfPeople; // [] | |
function store(uint256 _favNum) public { | |
myFavNum = _favNum; | |
} | |
function retrieve() public view returns (uint256) { | |
return myFavNum; | |
} | |
// string is a special type in solidity, that is why we need to specify the data location | |
// data location can only be specified for special types, such as, array, struct or mapping types | |
// uint256 is a premitive type but string is an array of bytes in solidity | |
/* | |
Three (2) main places to store data in solidity: | |
1. Memory (Temporary) | |
2. Calldata (Temporary) | |
3. Storage (Permanent) | |
Memory is useful for storing variables that are only needed temporarily during the execution of a function. These variables may include function arguments, local variables, and dynamic arrays that are created during the execution of a function. | |
Calldata is useful for passing large amounts of data to a function without having to copy the data into memory, which can be expensive in terms of gas usage. By using calldata, you can avoid the overhead of copying data into memory and reduce the amount of gas needed to execute the function. | |
One key difference between memory and calldata is that memory can be modified by the function, while calldata cannot. This means that if you want to modify a function argument, you must first copy it into memory. Here is an example: | |
function addOne(uint[] calldata numbers) public pure returns (uint[] memory) { | |
uint[] memory newNumbers = new uint[](numbers.length); | |
for (uint i = 0; i < numbers.length; i++) { | |
newNumbers[i] = numbers[i] + 1; | |
} | |
return newNumbers; | |
} | |
In summary, memory and calldata are both temporary data storage locations in Solidity, but they have important differences. Memory is used to hold temporary variables during function execution, while Calldata is used to hold function arguments passed in from an external caller. Calldata is read-only and cannot be modified by the function, while Memory can be modified. If you need to modify function arguments that are stored in calldata, you must first copy them into memory. | |
*/ | |
function addPerson(string memory _name, uint256 _favNum) public { | |
dynamicListOfPeople.push( Person({name: _name, favNum: _favNum}) ); | |
} | |
/* | |
Won't Work ❌ | |
function addPerson(string calldata _name, uint256 _favNum) public { | |
_name = "Jakir"; | |
dynamicListOfPeople.push( Person({name: _name, favNum: _favNum}) ); | |
} | |
Will Work ✅ | |
function addPerson(string memory _name, uint256 _favNum) public { | |
_name = "Jakir"; | |
dynamicListOfPeople.push( Person({name: _name, favNum: _favNum}) ); | |
} | |
*/ | |
} |
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.24; | |
contract SimpleStorage { | |
uint256 myFavNum; // 0 | |
uint256[] listOfFavNums; // [] | |
struct Person { | |
uint256 favNum; | |
string name; | |
} | |
Person[] public dynamicListOfPeople; // [] | |
// mapping type (this is similar to python's dictionary / js's object) | |
// type visibility varName | |
// mapping(keyType => valueType) | |
mapping(string => uint256) public nameToFavNumber; | |
function store(uint256 _favNum) public { | |
myFavNum = _favNum; | |
} | |
function retrieve() public view returns (uint256) { | |
return myFavNum; | |
} | |
function addPerson(string memory _name, uint256 _favNum) public { | |
dynamicListOfPeople.push( Person({name: _name, favNum: _favNum}) ); | |
nameToFavNumber[_name] = _favNum; | |
} | |
} |
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; | |
contract BytesOrStrings { | |
string constant _string = "cryptopus.co Medium"; | |
bytes32 constant _bytes = "cryptopus.co Medium"; | |
function getAsString() public pure returns (string memory) { | |
return _string; | |
} | |
function getAsBytes() public pure returns (bytes32) { | |
return _bytes; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment