LEARN SOLIDITY LESSON
Interface Solidity. Interacting with other contracts.
Interface Solidity.
Define an interface of the LuckyNumber contract:
contract LuckyNumber { mapping(address => uint) numbers; function setNum(uint _num) public { numbers[msg.sender] = _num; } function getNum(address _myAddress) public view returns (uint) { return numbers[_myAddress]; } }
contract NumberInterface { function getNum(address _myAddress) public view returns (uint); }
1. For one, we're only declaring the functions we want to interact with — in this case getNum — and we don't mention any of the other functions or state variables.2. Secondly, we're not defining the function bodies. Instead of curly braces ({ and }), we're simply ending the function declaration with a semi-colon (;).
Interacting with other contracts.
In this way, your contract can interact with any other contract on the Ethereum blockchain, as long they expose those functions as public or external.
contract NumberInterface { function getNum(address _myAddress) public view returns (uint); }
contract MyContract { address NumberInterfaceAddress = 0xab99... NumberInterface numberContract = NumberInterface(NumberInterfaceAddress); function someFunction() public { // Call `getNum` from contract: uint num = numberContract.getNum(msg.sender); } }
Comments