0

I'm trying to figure out how to store data on the "calldata" storage location on $eth?

is there a simple few liner examples on how can i do that?

Say, I want to store the ip addresses for 100 different users on the calldata, how can I store those data on chain and later retrieve them?

i've done some searches on this via https://ethereum.stackexchange.com/search?q=store+data+on+calldata but to no avail. The closest one was : When should I use calldata and when should I use memory?

For example you can see optimistic rollup is making use of this to store data cheaply

enter image description here

2 Answers 2

1

The calldata storage is just the trick for writing down some information on the blockchain. Usually, you use the calldata to pass some arguments to a contract method, but in some cases, you want just the fact of providing the data in arguments to be enough, for example - the layer 2 can pass continuously its checkpoint information to provide their verifiable state. To read this data offchain, you query the last transaction to the contract's method, and the input data is the information you are looking for. By fetching all that transactions, you get the history/full data.

Some -/+ of this method

  • - you can't store information in the calldata if you generate this information within the contracts method.
  • - difficult to fetch or filter. You fetch the transactions first and then process the data.
  • - not accessible later within the contracts method in other transactions
  • + the cheapest way to "store" the information.

How to store the Data? Simply submit a method-call transaction. Similar as Danijel has shown, but you don't process the values:

contract IPStorage {
    function storeIPAddresses(string[] calldata _ips) public {
        
    }
}

To access the data off-chain you will need a 3rd party service, which can give you all transactions by to and method signature filters, or you index the transactions on your own.

Another way to "store" the data is by using the Logs, which costs more gas:

  • + you can store information in event-parameters created within the contracts method.
  • + easier to fetch or filter by using eth_getLogs RPC call
  • - not accessible later within the contracts method in other transactions

Real storage solves all the minuses but costs more gas.

You can mix all of this "storage" types to save on tx fees and to solve your usecase.

1
  • this makes sense. thanks! Mar 6 at 12:31
1

Calldata is a read-only space on Ethereum that contains the arguments passed to a function call. It cannot be modified by the contract itself. Therefore, it is not possible to store data on the calldata storage location directly.

However, you can use calldata to pass arguments to a function that then stores the data on another storage location, such as storage or memory. Here's an example of how you can store IP addresses for 100 different users on the Ethereum blockchain:

contract IPStorage {
    mapping(uint => string) private ipAddresses;
    
    function storeIPAddresses(string[] calldata _ips) public {
        require(_ips.length <= 100, "Cannot store more than 100 IP addresses");
        for (uint i = 0; i < _ips.length; i++) {
            ipAddresses[i] = _ips[i];
        }
    }
    
    function getIPAddress(uint _index) public view returns (string memory) {
        return ipAddresses[_index];
    }
}

In this example, the contract defines a private mapping called ipAddresses that maps an index to a string representing an IP address. The storeIPAddresses function takes an array of IP addresses as input and stores them in the ipAddresses mapping. The getIPAddress function takes an index as input and returns the corresponding IP address from the ipAddresses mapping.

1
  • thanks for sharing! but when i read about how optimistic roll up works, it seems that they do not store data in Storage / Memory but they mention storing in calldata memory Mar 2 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.