1

I have a map such as:

mapping (address => bytes32) private myMap;

Later in the code, in a function, I want to ensure my sender already has an entry in that given map.

Can I do this?

require(!myMap[msg.sender], 'there is an entry in the map already!')
Tiago Bértolo
  • 203
  • 4
  • 8

1 Answers1

2

If the bytes32 value cannot be zero in your app, then:

require(myMap[msg.sender] != bytes32(0x0), "No data.");

To avoid reinventing the wheel, have a look at Are there well-solved and simple storage patterns for Solidity?

This explainer might unpack things in a digestible way: https://medium.com/robhitchens/solidity-crud-part-1-824ffa69509a

Also, while it is admittedly not a great starting point for learning solidity, a general-purpose and reasonably fresh utility over here: https://github.com/rob-Hitchens/UnorderedKeySet

Hope it helps.

Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145