0

I have this contract called Registry

pragma solidity ^0.4.11;

contract Registry {
    mapping(bytes32 => address) public ID;
    mapping(address => address) public Contract;

    function register(bytes32 _id) {
        ID[_id] = tx.origin;
        Contract[tx.origin] = msg.sender;
    }

    function getPublicAddress(bytes32 _id) constant returns (address) {
        return (ID[_id]);
    }

    function getContractAddress(bytes32 _id) constant returns (address) { 
        return Contract[ID[_id]];
    } 
}

and another called User

contract User { 
    address owner;
    address reg;
    Registry registry = Registry(reg);
    // Register in the system
    function register(bytes32 _id) {
        registry.register(_id);
    }
    function getContractAddress(bytes32 _id) {
        registry.getContractAddress(_id);
    }
    function getPublicAddress(bytes32 _id) {
        registry.getPublicAddress(_id);
    }
}

The registry(), getPublicAddress(), and getContractAddress() are not working and I can't understand why.

I tried testing with Truffle but ran into a known bug which is unsolved as of now. I asked about it here, the discussion on GitHub is here.

I tried running it in my private blockchain, where I have the following code

register: function(publicAddress, contractAddress, _id) {
    // Getting that user's instance of the contract
    const contract = contractInstance("User", contractAddress);
    // Interaction with the contract
    contract.register(web3.fromAscii(_id), {from: publicAddress}, (err, res) => {
        // Log transaction to explore
        if (err) {
            console.log(err);
        } else {
            console.log('tx: ' + res);
            helpers.addTransaction(publicAddress, res);
        }
    });
},
getContractAddress: function(contractAddress, _id) {
    const contract = contractInstance("User", contractAddress);
    contract.getContractAddress.call(web3.fromAscii(_id), (err, res) => {
        if (err) {
            console.log(err);
        } else {
            console.log(res);
        }
    })
},
getPublicAddress: function(contractAddress, _id) {
    const contract = contractInstance("User", contractAddress);
    contract.getPublicAddress.call(web3.fromAscii(_id), (err, res) => {
        if (err) {
            console.log(err);
        } else {
            console.log(res);
        }
    })
}

After registering (and mining) I simply get back

[]

when calling getPublicAddress()or getContractAddress().

Can anyone see where the problem is?

1
  • Do you have to pass bytes32 parameters getPublicAddress and getContractAddress functions?
    – atomh33ls
    Aug 22 '17 at 22:42
2

Your contract has many bugs, as follows:

  • reg params, there is no way to set it. Default value of reg is zero, so Register contract wouldn't be called when you call getPublicAddress method.
  • There is no return values of getPublicAddress and getContractAddress methods.

So i modified your contract and test everything is ok in remix, contract as follows:

pragma solidity ^0.4.11;
contract Registry {

    mapping(bytes32 => address) public ID;
    mapping(address => address) public Contract;

    event log(bytes32,address);
    function register(bytes32 _id) {
        ID[_id] = tx.origin;
        Contract[tx.origin] = msg.sender;
    }

    function getPublicAddress(bytes32 _id) constant returns (address) {
        log(_id,ID[_id]);
        return (ID[_id]);
    }

    function getContractAddress(bytes32 _id) constant returns (address) { 
        log(_id,Contract[ID[_id]]);
        return Contract[ID[_id]];
    } 
}

contract User { 
    address owner;
    Registry registry;
    // Register in the system
    function register(bytes32 _id) {
        registry.register(_id);
    }
    function getContractAddress(bytes32 _id) constant returns (address){
        return registry.getContractAddress(_id);
    }
    function getPublicAddress(bytes32 _id) constant returns (address) {
        return registry.getPublicAddress(_id);
    }
    function setRegister(address _addr){
        registry = Registry(_addr);
    }
}

At first, you should deploy Register contract and then User contract, now copy Register contract address to call setRegister method, mined and test. The result is

enter image description here

Hope it helps~

0

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.