eth 以太坊合約之間相互調(diào)用

準備:
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
        

調(diào)用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

調(diào)用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
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市楣颠,隨后出現(xiàn)的幾起案子巡蘸,更是在濱河造成了極大的恐慌,老刑警劉巖饵骨,帶你破解...
    沈念sama閱讀 206,378評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異茫打,居然都是意外死亡居触,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,356評論 2 382
  • 文/潘曉璐 我一進店門老赤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來轮洋,“玉大人,你說我怎么就攤上這事抬旺”子瑁” “怎么了?”我有些...
    開封第一講書人閱讀 152,702評論 0 342
  • 文/不壞的土叔 我叫張陵开财,是天一觀的道長汉柒。 經(jīng)常有香客問我,道長床未,這世上最難降的妖魔是什么竭翠? 我笑而不...
    開封第一講書人閱讀 55,259評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮薇搁,結(jié)果婚禮上斋扰,老公的妹妹穿的比我還像新娘。我一直安慰自己啃洋,他們只是感情好传货,可當我...
    茶點故事閱讀 64,263評論 5 371
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著宏娄,像睡著了一般问裕。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上孵坚,一...
    開封第一講書人閱讀 49,036評論 1 285
  • 那天粮宛,我揣著相機與錄音窥淆,去河邊找鬼。 笑死巍杈,一個胖子當著我的面吹牛忧饭,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播筷畦,決...
    沈念sama閱讀 38,349評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼词裤,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了鳖宾?” 一聲冷哼從身側(cè)響起吼砂,我...
    開封第一講書人閱讀 36,979評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎鼎文,沒想到半個月后渔肩,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,469評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡拇惋,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,938評論 2 323
  • 正文 我和宋清朗相戀三年赖瞒,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蚤假。...
    茶點故事閱讀 38,059評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖吧兔,靈堂內(nèi)的尸體忽然破棺而出磷仰,到底是詐尸還是另有隱情,我是刑警寧澤境蔼,帶...
    沈念sama閱讀 33,703評論 4 323
  • 正文 年R本政府宣布灶平,位于F島的核電站,受9級特大地震影響箍土,放射性物質(zhì)發(fā)生泄漏逢享。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,257評論 3 307
  • 文/蒙蒙 一吴藻、第九天 我趴在偏房一處隱蔽的房頂上張望瞒爬。 院中可真熱鬧,春花似錦沟堡、人聲如沸侧但。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,262評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽禀横。三九已至,卻和暖如春粥血,著一層夾襖步出監(jiān)牢的瞬間柏锄,已是汗流浹背酿箭。 一陣腳步聲響...
    開封第一講書人閱讀 31,485評論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留趾娃,地道東北人缭嫡。 一個月前我還...
    沈念sama閱讀 45,501評論 2 354
  • 正文 我出身青樓,卻偏偏與公主長得像茫舶,于是被迫代替她去往敵國和親械巡。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 42,792評論 2 345