Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2190

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T03:08:07+00:00 2024-11-26T03:08:07+00:00

Types of Solidity Storage

  • 61k

After taking Patrick Collins's 32-hour course on Learn Blockchain, I am putting out my note on some of the things I learnt. I hope you learn something too from them.

Solidity is a contract-oriented, high-level programming language for writing smart contracts on the Ethereum blockchain.

One of the critical features of Solidity is its ability to handle different types of data storage, including stack, memory, storage, calldata, code, and logs.

This article will explore these storage types and provide examples of how to use them in Solidity.

Stack

The stack is a temporary storage area used for holding data during the execution of a contract.

It is a last-in, first-out (LIFO) data structure, meaning that the last item added to the stack is the first item to be removed. In Solidity, the stack stores basic data types such as integers, booleans, and addresses.

Example:

pragma solidity ^0.8.0;  contract StackExample {   function addToStack(uint256 _number) public {     uint256 storedNumber = _number;   } } 
Enter fullscreen mode Exit fullscreen mode

In this example, we have a contract called StackExample with a function called addToStack.

This function takes an input of type uint256 and stores it in the stack by assigning it to the variable storedNumber.

Memory

Memory is similar to the stack in that it is a temporary storage area used during the execution of a contract.

However, unlike the stack, memory is not limited in size and is used for storing more complex data structures such as arrays and structs.

Example:

pragma solidity ^0.8.0;  contract MemoryExample { struct Person {   string name;   uint age; }   function addToMemory(string memory _name, uint _age) public {      Person memory person = Person(_name, _age);   } } 
Enter fullscreen mode Exit fullscreen mode

In this example, we have a contract called MemoryExample with a struct called Person. This struct has two fields, name and age, of type string and uint, respectively.

The contract also has a function called addToMemory which takes two inputs, a string and an uint, and stores them in memory by creating a new instance of the struct Person and assigning the inputs to the name and age fields, respectively.

Storage

Storage is a permanent storage area used to store data that should persist beyond the execution of a contract.

This is the storage area that is accessible to all functions of the contract and is also visible to external contracts. In Solidity, the storage area is used for storing data such as contract state variables and mapping.

Example:

pragma solidity ^0.8.0;    contract StorageExample {     mapping(address => uint) public balances;       function addToStorage(address _address, uint _amount) public {         balances[_address] = _amount;     } } 
Enter fullscreen mode Exit fullscreen mode

In this example, we have a contract called StorageExample that has a mapping called balances.

This mapping is of the type address to uint, meaning that it maps addresses to uint values. 

The contract also has a function called addToStorage which takes two inputs, an address and an uint, and stores them in storage by adding a new key-value pair to the mapping where the key is the address input and the value is the uint input.

Calldata

Calldata is the data passed to a contract as an input during a function call. It is read-only and used to pass information to a contract without modifying its storage.

Example:

pragma solidity ^0.8.0;  contract CalldataExample {   function setNumber(uint _number) public {     require(msg.data.length == 32, "Invalid input data");     uint storedNumber = _number;   } } 
Enter fullscreen mode Exit fullscreen mode

In this example, we have a contract called CalldataExample with a function called setNumber. This function takes an input of type uint and stores it in the contract by assigning it to the variable storedNumber.

The function also uses the “msg.data” keyword to check the input data length passed in the function call. If the length is not equal to 32, the function will revert with an error message “Invalid input data”.

Code

Code is the compiled bytecode of a contract deployed on the Ethereum blockchain. It is read-only and cannot be modified after deployment. In Solidity, a contract's code can be accessed using the “this” keyword.

Example:

pragma solidity ^0.8.0;  contract CodeExample {   function getCode() public view returns (bytes memory) {     return this.code;   } } 
Enter fullscreen mode Exit fullscreen mode

In this example, we have a contract called CodeExample with a function called getCode. This function uses the “this.code” keyword to access the contract's bytecode and return it as an output of type bytes.

Logs

Logs are a way for a contract to record events on the Ethereum blockchain.

They are stored in the blockchain's transaction receipt and can be used for various purposes, such as tracking contract execution or providing proof of certain events.

In Solidity, logs can be emitted using the “emit” keyword.

Example:

pragma solidity ^0.8.0; contract LogsExample {   event LogNumber(uint _number);    function logNumber(uint _number) public {     emit LogNumber(_number);   } } 
Enter fullscreen mode Exit fullscreen mode

In this example, we have a contract called LogsExample that has an event called LogNumber. This event takes an input of type uint and is emitted using the “emit” keyword in the function logNumber. Any contract or external user can listen to this event and get the logged number.

Conclusion

Solidity provides various storage options for handling different data types, including stack, memory, storage, calldata, code, and logs.

Each storage type has unique properties and use cases, and understanding how to use them correctly is crucial for writing efficient and secure smart contracts on the Ethereum blockchain.

Resources

Solidity documentation
Mastering Ethereum

If you like the articles and excellent write-up, please support me by getting me coffee or subscribing to my blog, so you get the latest content in your mail, and many more I don't post here.

blockchainjavascriptsmartcontractswebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.