Solidity__合约调用合约

合约调用已经部署的合约

先部署下面的合约,复制合约地址。往合约转入以太坊待测试,getBalance()返回该合约的以太坊数量

pragma solidity ^0.4.22;
/*
+------------------------------------------------------------------------------+
|                                                                              |
|      XX               XXXXX XXXXXX          XXXXXXXXXXX        XXXXXXXXXX    |
|     XX XX           XXX   XXX   XXX         X         X        X        X    |
|    XX   XX          X      X      X         XXXXXXXXXXX        X        X    |
|   XXXXXXXXX         X             X         X                  X        X    |
|  XX       XX        X             X         X                  X        X    |
| XX          X       X             X         XXXXXXXXXXX        X        X    |
|                                                                              |
|                                                                              |
+------------------------------------------------------------------------------+
*/
contract  Balance{
   //存入一些ether用于后面的测试
  function deposit() payable{
  }

  //查询当前的余额
  function getBalance() constant returns(uint){
      return this.balance;
  }
}

再部署下面的合约

pragma solidity ^ 0.4.0;
/*
+------------------------------------------------------------------------------+
|                                                                              |
|      XX               XXXXX XXXXXX          XXXXXXXXXXX        XXXXXXXXXX    |
|     XX XX           XXX   XXX   XXX         X         X        X        X    |
|    XX   XX          X      X      X         XXXXXXXXXXX        X        X    |
|   XXXXXXXXX         X             X         X                  X        X    |
|  XX       XX        X             X         X                  X        X    |
| XX          X       X             X         XXXXXXXXXXX        X        X    |
|                                                                              |
|                                                                              |
+------------------------------------------------------------------------------+
*/
contract Balance {      //interface也可以,目前还不知道其中区别,后期更新
    function getBalance()public constant returns(uint);//你要调用的合约里的函数
}
contract A {
    Balance public balance = Balance(0x0e48a7ec9d78d0ef015a453098c3be6b6a796f0d);//初始化该合约
    uint public a;//第一个合约里的以太坊数量
function getbalance() public  returns(uint)
    {
    a=balance.getBalance();//第一个合约里的以太坊数量赋值给a
    }
}

你可能感兴趣的:(Solidity__合约调用合约)