in the first case I would check the string length
require(bytes(hash).length > 0);
in the second case just check against the void address
require(hashes[hash] == address(0x0));
Answer from qbsp on Stack Exchangein the first case I would check the string length
require(bytes(hash).length > 0);
in the second case just check against the void address
require(hashes[hash] == address(0x0));
Just to add to @mirg's valid answer.
As you know, in Solidity variables are never null. Instead, if they are not initialized, they contain their default value. For example for uint this is 0. So, whatever type you use, if it's not initialized its value is its default value.
This is a bit unclear when it comes to strings, but checking the bytes length seems to be the way to go. More answers for example here: How can you check if a `string` is empty in Solidity
In solidity every variable is set to 0 by default.
You should think of mappings as all possible combinations are set to 0 by default.
In your specific case I would use the following:
if (buyers[msg.sender].amount == 0)
For integers:
You could create none variable to use it as a NULL:
uint256 constant NULL = 0;
Example code for check:
function isNULL(uint256 variable) internal returns (bool) {
return variable == NULL;
}
For bytes32:
You can follow different approach for bytes:
bytes32 constant NULL = "";
Example code piece:
pragma solidity ^0.6.0;
mapping(address => bytes32) public Countries;
function isCountriesInitialized(address _user) external view returns (bool)
{
if (Countries[_user] == NULL) // Returns true if `Countries[_user]` is not initialized
return false;
return true;
}
I observe that on solidity >= v0.6.0 it may return 32 for length even though it is not mapped.
Example of its returned value:
b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
You can check for the length of its bytes like this:
require(bytes(orderName).length != 0)
Doing much stuff with string is not allowed yet in solidity, I will attach 2 examples which will help you:
How to validate if string is empty by converting the string to bytes and validating the length:
contract testContract {
function isStringEmpty(string memory _test) public view returns(bool) {
bytes memory checkString = bytes(_test);
if (checkString.length > 0) {
return false;
} else {
return true;
}
}
}
How to compare string by converting the string to bytes and keccak256:
pragma solidity ^0.6.0;
contract testContract {
function stringComparison(string memory _test) public view returns(bool) {
require(keccak256(bytes(_test)) != keccak256(bytes("null")), "Invalid Name");
}
}
You can wrap this logic into modifier and call it on whatever method you need.
The entire storage space is virtually initialized to 0 (there is no undefined). So you have to compare the value to the 0 value for your type. For example, mapping[key] == address(0x0) or mapping[key] = bytes4(0x0).
There is no direct method to check whether the mapping has particular key. But you can check if mapping property has value or not. The following example considered that the Ticket is the struct with some property.
pragma solidity >=0.4.21 <0.6.0;
contract Test {
struct Ticket {
uint seatNumber;
}
mapping (string => Ticket) myMapping;
function isExists(string memory key) public view returns (bool) {
if(myMapping[key].seatNumber != 0){
return true;
}
return false;
}
function add(string memory key, uint seatNumber) public returns (bool){
myMapping[key].seatNumber = seatNumber;
return true;
}
}