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

准备:
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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,172评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,346评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 159,788评论 0 349
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,299评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,409评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,467评论 1 292
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,476评论 3 412
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,262评论 0 269
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,699评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,994评论 2 328
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,167评论 1 343
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,827评论 4 337
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,499评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,149评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,387评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,028评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,055评论 2 352