0

I'm trying to store records in a contract that are supposed to have an arbitrary number of fields. For example, one record would have "bitrate", another "ISBN" etc., in addition to a number of fixed fields. It is natural to store these fields as a string => string mapping.

Now, the question is, how do I pass these fields to the contract, and how do I get them back? Even the latest version of Solidity, v0.8, doesn't permit using mappings in public contract functions. I'm thinking of sending strings and parsing them inside the contract, but surely there is a more "native" Solidity way using low-level functions?

6
  • 1. What are you going to do with this data in the contract? 2. The examples that you gave fit well into bytes32 and they can be transferred to the contract as an array, which can be split into pairs inside – Mad Jackal Jan 12 at 10:32
  • Create a setter and getting function setField(itemId, fieldName, value) and getField(itemId, fieldName) public string. However blockchain is not designed to be a storage of string data and it is quite inefficient, so unless this is a theoretical exercise it does not make much sense. – Mikko Ohtamaa Jan 12 at 11:04
  • @MadJackal great idea, can you add it as an answer? – ulu Jan 12 at 11:34
  • @MikkoOhtamaa good idea, but it's not atomic, I can end up with a half-filled record. And it's not my choice, unfortunately, it's my employer's idea. – ulu Jan 12 at 11:37
  • 1
    You can also pass an array of keys and values if you want to write multiple fields once. – Mikko Ohtamaa Jan 12 at 12:04
2

The examples that you gave fit well into bytes32 and they can be transferred to the contract as an array, which can be split into pairs inside.

If you just want to transfer big data through a contract without processing it in it, then it is probably better to use a decentralized file system (IPFS, Ethereum Swarm) or cloud storage, and put a link to the data file in the blockchain.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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