
[ad_1]
Do we want the data to persist between calling a smart contract or being saved locally when a function is executed?

This time, we’ll talk about storage locations in the Solidity programming language, specifically about storage
And memory
place. Not knowing what they represent and how they work can cause problems with our smart contracts.
Storage in smart contracts holds data between function calls. we can imagine that storage
In the form of hard drive in the computer. Even if we turn it off, the data remains and is not erased. What we write in storage on the blockchain gets stored.
state variables
By default, Solidity will store the state variables of the smart contract.
contract StorageContract {
struct LuckyNumber {
uint256 number;
string reason;
} mapping(address => LuckyNumber) luckyNumbers;
}
In this example the state variables of the smart contract luckyNumbers
is kept in storage, and the data will persist between function calls.
When we add up and get the lucky number, we have an approximate result.
function addLuckyNumber(LuckyNumber memory luckyNumber) external {
require(luckyNumber.number != 0, "Lucky number can't be 0!");
require(luckyNumbers[msg.sender].number == 0, "You already have set lucky number. Edit it if you have another one."); luckyNumbers[msg.sender] = luckyNumber;
}function getMyLuckyNumber() external view returns(uint256) {
require(luckyNumbers[msg.sender].number != 0, "You don't have a lucky number set yet."); LuckyNumber memory luckyNumber = luckyNumbers[msg.sender]; return luckyNumber.number;
}
local function variable
Local function variables of a struct, array, or mapping are saved to storage by default. This means that if we declare these values in our functions, they are kept in storage, which can lead to unexpected problems that are difficult to track.
If we add a function editLuckyNumber
to our code example and mark the local copy storage
This will edit the state variable we expect.
function editLuckyNumber(uint256 luckyNumber) external {
require(luckyNumber != 0, "Lucky number can't be 0!");
require(luckyNumbers[msg.sender].number != 0, "You don't have a lucky number set yet."); LuckyNumber storage _luckyNumber = luckyNumbers[msg.sender];
_luckyNumber.number = luckyNumber;
}
In memory, Solidity holds all locally defined value types, which can be uint, string, etc., but not an array, a struct, or a mapping. Function arguments are also kept in memory. remember that memory
Cannot be used at smart contract level, only in functions locally.
function multiplyByItself(uint256 number) external pure returns(uint256) {
uint256 result = number * number; return result;
}
In this example, the function argument number
What we pass to our function gets stored in memory. Also, locally defined variables result
is stored in memory and will be released as soon as the execution of the function ends.
One of the major disadvantages of incorrect use of storage
And memory
Keyword in solidity programming language is that we declare a variable storage
either memory
without thinking and understanding. First, keeping the data storage
Will consume more gas as we have to pay for block space. Second, we should ask ourselves whether we need to access the data we hold between function calls. By function call, there can also be two different functions.
if we define _luckyNumber
in function editLuckyNumber
use of memory
Keyword, it will only edit this function locally, and the changes will not be written to the blockchain.
function editLuckyNumber(uint256 luckyNumber) external {
require(luckyNumber != 0, "Lucky number can't be 0!");
require(luckyNumbers[msg.sender].number != 0, "You don't have a lucky number set yet."); LuckyNumber memory _luckyNumber = luckyNumbers[msg.sender];
_luckyNumber.number = luckyNumber;
}
This function result in lucky number not working as we only update it locally.
Storing data using the Solidity language is an important thing in our smart contracts. Life is easier with value types, but with arrays, structures, and mappings, it’s more difficult. So whenever we want to save these variables it is necessary to ask. Do we want the data to persist between calling a smart contract or being saved locally when a function is executed?
[ad_2]
Source link
#Storage #Memory #Secrets #Solidity #Kristaps #Grinbergs #June