Skip to content

Instantly share code, notes, and snippets.

@shd101wyy
Created April 25, 2018 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shd101wyy/0a81c456baa58c1038865425afcd5e0c to your computer and use it in GitHub Desktop.
Save shd101wyy/0a81c456baa58c1038865425afcd5e0c to your computer and use it in GitHub Desktop.
Ribbit project experiments ground
// 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