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

Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145
Sandeep Gupta
  • 71
  • 2
  • 7

1 Answers1

2

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.

Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145