Are there null like thing in solidity
Asked Answered
S

5

42
struct buyer {
    uint amount;
    Status status;
}

mapping(address=>buyer) public buyers;
mapping(uint=>address) buyerIndex;
uint public buyerNum;

// Order a product.
function() {
    uint doubleValue=value*2;
    uint amount=msg.value/doubleValue;
    if(buyers[msg.sender]==null) { // Error in this line
        buyer abuyer=buyer({amount:amount,status:Status.Created}); // Error in this line
        buyerNum++;
        buyerIndex[buyerNum]=msg.sender;
        buyers[msg.sender]=abuyer;
    }
    else {
        buyers[msg.sender].amount+=amount;
    }
    Order(msg.sender,amount*doubleValue,amount);
}

If a buyer is not recorded in the buyer mapping, then buyerNum++; but I don't know how to tell whether a buyer is in the mapping

Schellens answered 16/6, 2016 at 7:34 Comment(1)
J
22

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)
Jacket answered 19/11, 2017 at 2:34 Comment(1)
You can also just say: if (buyers[msg.sender].amount) { ... }Gregarious
G
13

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'

Gregarious answered 21/10, 2016 at 12:56 Comment(0)
A
5

As Viktor said, default value for all possible values in mapping is zero. So if a buyer has not already inserted in mapping, the amount value for that address will be zero. But this approach has a flaw, if a buyer does exists but its balance became zero after some operations, you will treat it as it does not exist.

I think the best approach is to add a exists member to the buyer struct with bool type. Default value for this member is false and when the buyer get created, you initialize it with true value. So you can check exactly if a buyer exist or not via this member.

Buyer struct:

struct buyer{
   uint amount;
   Status status;
   bool exists;
}

Initialize buyer:

buyer memory b = buyer(0, status, true);

Check if buyer exists:

if(buyers[msg.sender].exists) {
  //so can buy
}
Attenborough answered 27/1, 2019 at 8:23 Comment(0)
E
4

There is nothing like null in solidity.

Just check for the length of the address:

if(buyers[msg.sender].length == 0){
    // do your thing
}

See also this answer on ethereum stack exchange.

Encephalogram answered 26/6, 2016 at 8:19 Comment(1)
Structs does not have length member.Attenborough
H
2

Instead of using one of the values or creating an extra boolean, you can check for the byte size of the structure.

if (bytes(buyers[msg.sender]).length > 0) {
    // buyer exists.
}
Harneen answered 4/3, 2019 at 21:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.