4

I have a solidity contract that allows users to register a name to their address. This is done with the register function. I also have a function to allow users to check the availability of a name; checkAvailability.

The problem is that checkAvailability never returns false. It's as if the users mapping never saves any values. Does anyone have any suggestions?

pragma solidity ^0.4.4;

contract Users {
    mapping (bytes32 => address) public users;

    function register(bytes32 name) {
        if(users[name] == 0 && name != ""){
            users[name] = msg.sender;
        }
    }

    function checkAvailability(bytes32 name) returns (bool) {
      if(users[name] == 0) {
        return true;
      }
      return false;
    }

}

NOTES: Checking against 0x00 doesn't seem to fix the issue. I'm using truffle test file to test this contract:

 it.only("should user", function() {
    return Users.deployed().then(function(instance) {
      return instance.register.call('test')
        .then(() => {
          return instance.checkAvailability.call('test');
        });
    })
    .then((register) => {
      console.log(register);
    });
  });
1

Solution was to call register with instance.register('foo', { from: acccounts[0] }) then instance.checkAvailability.call('hi') will return false.

... no idea why.

EDIT: Found out why! There is a difference between transactions and local calls: What is the difference between a transaction and a call?

0

there is the solution : i suggest to compare the address with 0X0 not with 0

 pragma solidity ^0.4.4;

contract Users {
    mapping (bytes32 => address) public users;

    function register(bytes32 name) {
        if(users[name] == 0x0 && name != ""){
            users[name] = msg.sender;

        }
    }

    function checkAvailability(bytes32 name) returns (bool) {
      if(users[name] == 0x0) {
        return true;
      }
      return false;
    }

}

enter image description here

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.