Created
April 25, 2018 15:35
-
-
Save shd101wyy/0a81c456baa58c1038865425afcd5e0c to your computer and use it in GitHub Desktop.
Ribbit project experiments ground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this file is an example of calling other contracts | |
pragma solidity ^0.4.0; | |
contract Test { | |
mapping (string => uint) data; | |
address public previousContractAddress; | |
Test previousContract; | |
constructor(address previousContractAddress_) public { | |
previousContractAddress = previousContractAddress_; | |
previousContract = Test(previousContractAddress); | |
} | |
function getData(string key) external constant returns (uint) { | |
uint result = data[key]; | |
if (previousContractAddress != address(0)) { | |
result += previousContract.getData(key); | |
} | |
return result; | |
} | |
function incrementData(string key, uint delta) external { | |
data[key] = data[key] + delta; | |
} | |
} | |
/* | |
Suppose I firstly created an smart contract Test() and get an address 0xa6673deb97897b9e3e6f2cd69826141c50cd31f8 | |
.incrementData("x", 1) | |
.getData("x") => {"x": 1} | |
.incrementData("x", 1) | |
.getData("x") => {"x": 2} | |
Then I create another contract Test(0xa6673deb97897b9e3e6f2cd69826141c50cd31f8) | |
and get an address 0x6b0a8e29a932eaa75a1069b1915ceadf1fd3c5cb | |
.getData("x") => {"x": 2} | |
So you see it calls the previous contract. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment