0
contract xyz {
    mapping(address => bool) Users;
    function add(address userAddress) {
            require(userAddress != 0x0 && !Users[userAddress]);             
            Users[userAddress] = true;    
    }
    function pass(address passAddress) returns (bool) {   
        return Users[passAddress];
    }
}

contract SaveData {
    address[] addrs;
    string[] hashSet;
    xyz asd = xyz();
    function Save(address PubAddress) {
        require(asd.pass(PubAddress)==true);
        addrs.push(PubAddress);    //saving public addresses

    }
//function to save hash
    function saveHash(string hashStr) {
        hashSet.push(hashStr);
    }
}

I am new to solidity, i am creating a simple user reg and checking and saving it using another contract. Contract xyz is working fine, but now i want to call it into SaveData Contract and check address using pass function that if address is whitelisted then only it will save the address.

thanks in advance

  • 1
    Possible duplicate of Call function on another contract – Michael Kohl Dec 19 '17 at 6:40
  • @MichaelKohl what i need is that i have deployed my xyz contract and it generating an address. Now i need to send the same address in savedata contract and save it using save function. – Sandeep Gupta Dec 19 '17 at 6:56
  • This looks fine, except you are never calling asd.add() to whitelist your addresses. What are you finding is not working? (also, no need for "==true" in your require.) – Andy McEwan Dec 19 '17 at 13:27
1

The main thing is you cast a variable as an instance of your contract, then you instantiate it at the address where you expect it to be.

Something like

MyContract myContract; 
myContract = MyContract(contractAddress);

I couldn't help myself and adjusted the names of some things.

pragma solidity 0.4.21;

contract UserReg {

    mapping(address => bool) public isUser;

    function add(address userAddress) public {
        require(userAddress != 0x0 && !isUser[userAddress]);             
        isUser[userAddress] = true;    
    }

    function pass(address passAddress) public view returns(bool) {   
        return isUser[passAddress];
    }

    // Added this for safety check near 31

    function isUserReg() public pure returns(bool isIndeed) {
        return true;
    }
}

contract SaveData {

    address[] public addrs;

    UserReg userReg;

    function instantiateXyz(address userRegAddr) public returns(bool success) {
        userReg = UserReg(userRegAddr);     // instantiate asd instance of Zyz
        require(userReg.isUserReg());       // should return true, right?
        return true;
    }

    function saveAddress(address pubAddress) public returns(bool success) {
        require(userReg.pass(pubAddress)==true);
        addrs.push(pubAddress);    //saving public addresses
        return true;
    }

}

Hope it helps.

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.