I want to call the method getdata() and retrieve the returns value to use in another contract. But the call only returns bool on success or failure.
How do I get the return value from getdata() from caller() function in contract extra .
How do I modify the state:- send transaction to setdata(uint,bytes32) from contract extra and change the state of data1 and data2.
Above problem are based on following code.
pragma solidity 0.4.21;
contract Base{
uint public data1;
bytes32 public data2;
function setdata(uint a, bytes32 b){
data1 = a;
data2 = b;
}
function getdata() public view returns(uint){
return data1 ;
}
}
contract extra{
bool public retrive;
bool public retrive_setter;
address public baseaddress = 0xca598f876f79a5f8f479bfa1dcc8f4f2dffbd5c2;
uint a = 5;
bytes32 b ="Lina";
function caller(){
retrive = baseaddress.call.gas(10000)(bytes8(keccak256("getdata()")));
retrive_setter = baseaddress.call.gas(1000000)(bytes24(keccak256("setdata(uint, bytes32)")),a,b);
}
}
Either you can Modify the code and give your answer. I saw this problem on many devs who are newby. Every possible answers are acceptable.
simply on testing using Remix .
retrive 0:bool: true
retrive_setter 0:bool: false
Best Answer
Hope it helps.
UPDATE
The above example just creates a new
Base
with each deployment ofExtra
to show them talking in the simplest manner I can think of. Another way to go is to deploy aBase
separately. It's location can then be relayed toExtra
.You could pass it into the constructor:
It's not checking anything, merely assuming the deployer knows what they're doing. You can also make the address of the base something the owner can change.
Add an
owner
to the state and the constructor:Add a modifier (after state and before constructor, by convention) to restrict access to the sensitive function.
Add a restricted function to change the base.
You might want to know the address of the Base:
It would be good practice to add event emitters to the state-changing functions in both contracts.