Complete the code to declare a storage variable in Solidity.
contract Storage {
uint256 public [1];
}The variable storageValue is declared as a storage variable of type uint256.
Complete the code to read a storage variable inside a function.
contract Storage {
uint256 public storedData;
function getData() public view returns (uint256) {
return [1];
}
}To return the value of a storage variable, just use its name storedData.
Fix the error in the code to correctly update a storage variable.
contract Storage {
uint256 public count;
function increment() public {
count [1] 1;
}
}The operator += adds 1 to the current value of count.
Fill both blanks to declare and initialize a mapping in storage.
contract Storage {
mapping(address => [1]) public balances;
function setBalance(address user, uint256 amount) public {
balances[user] [2] amount;
}
}The mapping stores uint256 values for each address. To set a balance, use the assignment operator =.
Fill both blanks to create a struct, declare a storage variable, and update its field.
contract Storage {
struct User { {
uint256 id;
string name;
}
User public {BLANK_2}};
function setUser(uint256 _id, string memory _name) public {
[2].id = _id;
{{BLANK_3}}.name = _name;
}
}The struct body starts with a brace '{'. The storage variable is named user. Inside the function, we update user's fields.