eth 基础知识

eth 以太坊合约之间相互调用

2018-10-29  本文已影响0人  uestcAries

准备:
remix

说明: demo1.sol 和 demo02.sol 使用相同账户进行部署

demo1.sol

pragma solidity ^0.4.25;

/**
 * 有所有人的合约
 * 所有权限管理都在这里定义
 * event    OwnershipTransferred(address, address)
 * modifier onlyOwner()
 */
contract Ownable {
    
    address public owner;

    constructor () public {
        owner = msg.sender;
    }

    /**
     * 判断当前用户是否是合约所有人
     */
    modifier onlyOwner () {
        require(msg.sender == owner);
        _;
    }
}


contract demo1 is Ownable {
    
    event test(address a);
    
    function test01() public  onlyOwner returns(uint256)  {
        
        emit test(msg.sender);
        return 2;
    }
    
    function test02() public returns(uint256) {
        return 100;
    }
}

demo1.sol 编译

from:0xca3...a733c, to:demo1.(constructor), value:0 wei, data:0x608...00029, 0 logs, hash:0x26e...49872
 status     0x1 Transaction mined and execution succeed
 contractAddress    0xde6a66562c299052b1cfd24abc1dc639d429e1d6
 from   0xca35b7d915458ef540ade6068dfe2f44e8fa733c
 to     demo1.(constructor)
 gas    300000000 gas
        
 transaction cost   223805 gas 
 execution cost     131201 gas 
 hash   0x26ef317470455cf2d7896a368457d8c81616eb5b414a74bd06da31dd74c49872
 input  0x6080604052336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555061022a806100536000396000f300608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680631b18f9521461005c578063671c7d68146100875780638da5cb5b146100b2575b600080fd5b34801561006857600080fd5b50610071610109565b6040518082815260200191505060405180910390f35b34801561009357600080fd5b5061009c6101d0565b6040518082815260200191505060405180910390f35b3480156100be57600080fd5b506100c76101d9565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b60008060009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561016657600080fd5b7fbb29998e780fd657e5ebd4526fde6268a7318596399e4e76708e80a468c28fd633604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390a16002905090565b60006064905090565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff16815600a165627a7a72305820efa3ff9395f1d21b08cda2313eae24568f4bbf49fe1c1b7d5d9bb9deeddde5e00029
 decoded input  {}
 decoded output      - 
 logs   []
 value  0 wei

contractAddress 0xde6a66562c299052b1cfd24abc1dc639d429e1d6 这个是我们另一个合约需要的

demo2.sol

pragma solidity ^0.4.25;


/**
 * 有所有人的合约
 * 所有权限管理都在这里定义
 */
contract Ownable {
    address public owner;

    constructor () public {
        owner = msg.sender;
    }

    /**
     * 判断当前用户是否是合约所有人
     */
    modifier onlyOwner () {
        require(msg.sender == owner);
        _;
    }
}

contract FountainTokenInterface is Ownable {
    
    function test01() public  onlyOwner returns(uint256);
    function test02() public returns(uint256);
}


contract demo2 is Ownable {
   FountainTokenInterface fountain = FountainTokenInterface(0xde6a66562c299052b1cfd24abc1dc639d429e1d6);
   
   function test03()  public returns(uint256)  {
       uint256 tt = fountain.test01();
       return tt + 100;
   }
   
   function test04()  public returns(uint256)  {
       uint256 tt = fountain.test02();
       return tt +200;
   }
    
}

demo2.sol 编译

creation of demo2 pending...
[vm] from:0xca3...a733c, to:demo2.(constructor), value:0 wei, data:0x608...80029, 0 logs, hash:0x336...c26eb
 status     0x1 Transaction mined and execution succeed
 contractAddress    0x1526613135cbe54ee257c11dd17254328a774f4a
 from   0xca35b7d915458ef540ade6068dfe2f44e8fa733c
 to     demo2.(constructor)
 gas    300000000 gas
        
 transaction cost   301217 gas 
 execution cost     193105 gas 
 hash   0x336a770173b5ece478e595499cecc372d7a1f58065ce7b7c0cf0c9f1c31c26eb
 input  0x608060405273de6a66562c299052b1cfd24abc1dc639d429e1d6600160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff160217905550336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506102fa806100a86000396000f300608060405260043610610057576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680638da5cb5b1461005c578063a67d1a55146100b3578063bee82aae146100de575b600080fd5b34801561006857600080fd5b50610071610109565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b3480156100bf57600080fd5b506100c861012e565b6040518082815260200191505060405180910390f35b3480156100ea57600080fd5b506100f36101fe565b6040518082815260200191505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b600080600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1663671c7d686040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b1580156101b757600080fd5b505af11580156101cb573d6000803e3d6000fd5b505050506040513d60208110156101e157600080fd5b8101908080519060200190929190505050905060c8810191505090565b600080600160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16631b18f9526040518163ffffffff167c0100000000000000000000000000000000000000000000000000000000028152600401602060405180830381600087803b15801561028757600080fd5b505af115801561029b573d6000803e3d6000fd5b505050506040513d60208110156102b157600080fd5b8101908080519060200190929190505050905060648101915050905600a165627a7a72305820b72f21fe3b8a58099688a2bb6c125183097f076c2328e2cb87db4fe71b7cdeb80029
 decoded input  {}
 decoded output      - 
 logs   []
 value  0 wei
        

调用test04:
[vm] from:0xca3...a733c, to:demo2.test04() 0x152...74f4a, value:0 wei, data:0xa67...d1a55, 0 logs, hash:0xbab...4b92e

 status     0x1 Transaction mined and execution succeed
 from   0xca35b7d915458ef540ade6068dfe2f44e8fa733c
 to     demo2.test04() 0x1526613135cbe54ee257c11dd17254328a774f4a
 gas    300000000 gas
        
 transaction cost   23586 gas 
 execution cost     2314 gas 
 hash   0xbaba194b9d0bb19d1198ea010302fe4b8ba4b9a07f0683f59eaf1281d084b92e
 input  0xa67d1a55
 decoded input  {}
 decoded output     {
    "0": "uint256: 300"
}
 logs   []
 value  0 wei

调用test03
from:0xca3...a733c, to:demo2.test03() 0x152...74f4a, value:0 wei, data:0xbee...82aae, 0 logs, hash:0x6cf...03b5d

transact to demo2.test03 errored: VM error: revert.
revert  The transaction has been reverted to the initial state.
Note: The constructor should be payable if you send value.  Debug the transaction to get more information
上一篇下一篇

猜你喜欢

热点阅读