以太坊 智能合约 Solidity interface的使用

以太坊网络把在网络上读与写数据进行了区分,写数据被称做交易(transaction),读数据被称做调用(call)。网络

使用工具:remix IDE —browser-based

场景:两个合约分别部署在不一样的地址,相互间的调用 数据交互操做

第一个合约:Doug.sol代码以下:app

pragma solidity ^0.4.19;
contract Doug{
    mapping (bytes32 => uint) public contracts;
    function Doug() {
        contracts['hww'] = 1;
        contracts['brian'] = 2;
        contracts['zzy'] = 7;
    }
    
    function getDougName(string _name) public view returns(string) {
        return _name;
    }
    
     function getDougAge(uint _age) public pure returns(uint) {
        return 3 ** _age;
    }
}

第二个合约 myContract.sol 代码以下:工具

pragma solidity ^0.4.19;

contract DogInterface {
    function getDougAge(uint _age) returns (uint);
    function contracts(bytes32 name) returns (uint);
}

contract main{
    
    event FetchContract(address dogInterfaceAddress, address sender, bytes32 name);
    
    address DOUG;
    
    address dogInterfaceAddress = 0x3e6494333ae0e929ade0eb9a19fb02632b8e07cf;
    DogInterface dogContract = DogInterface(dogInterfaceAddress);
          
    function setDOUG(address _doug) {
        DOUG = _doug;
    }
    
    function dougOfage(uint _age) public view returns(uint) {
        
        uint num =  dogContract.getDougAge(_age);
        return _age+num;
        //   return num;
    }
    
    function uintOfName(bytes32 _name)  returns (uint) {
        
        dogContract.contracts(_name);
        FetchContract(dogInterfaceAddress, msg.sender, _name);
       
    }
    
    // function getTest(string _name) public view returns(string) {
    //     string memory newName = _name ;
    //     DogInterface(DOUG).getDougName(newName);
    //     return newName;
    // }
}

执行结果: 输入图片说明ui

相关文章
相关标签/搜索