Here how can I find the address of A within Test() function?
contract A {
uint public target;
function setTarget(uint _target) public {
target = _target;
}
}
contract B {
A a = Test(0x123abc...); // address of deployed A
function editA() public {
a.setTarget(1);
}
}
You can get the address by casting the A type to type address.
contract B {
A a = Test(0x123abc...);
funciton getAddressA() public view returns (address) {
return address(a); // typecasting to `address`
}
}
Related
I want to get the value that is inside of struct in Solidity,
but I have no idea how to get it.
pragma solidity >=0.4.21 <0.6.0;
contract PlaceList {
struct hoge {
uint id;
address user;
}
hoge[] public hoges;
constructor() public {
admin = msg.sender;
}
function set(uint id) public {
hoges.push(hoge(id, msg.sender));
}
function getId() public view returns(uint) {
return (hoges[0].id);
}
}
When I call getId, console command say this,
ƒ () {
if (abiItemModel.isOfType('constructor')) {
return target.executeMethod(abiItemModel, arguments, 'contract-deployment');
}
return targe…
Could you give me any advise how to get id by using solidity function, please?
hoges[0][0] will work as it points to the first element, 'id' .
I'm here to ask my problem on solidity code.
Here is simple example code. Contract 'Map' has a mapped data. It has each address's T/F values. I created an account X and add it to 'simpleMap'.
Contract 'Ask' lookup mapping data through 'Map' contract. What I expected result when X called askMe() was TRUE but it was always FALSE.
When I called whoIsMsgSender it returns the exact X's account. What's the problem??
pragma solidity ^0.4.24;
contract Map {
mapping (address => bool) simpleMap;
function add(address _address) external {
simpleMap[_address] = true;
}
function isTrue(address _address) external view returns (bool _ret) {
return simpleMap[_address];
}
}
contract Ask {
Map public map = Map(Map's address like 0x~~ just for test);
function askMe() external view returns (bool _ret) {
return map.isTrue(msg.sender);
}
function whoIsMsgSender() external view returns (address _address) {
return msg.sender;
}
}
I tried your contracts in Remix, but askMe() returned true for me. How are you separating the two contracts? Maybe Map is not yet deployed before Ask is trying to call a function on it. I separated the two contracts:
pragma solidity ^0.4.24;
contract Map {
mapping (address => bool) simpleMap;
function add(address _address) external {
simpleMap[_address] = true;
}
function isTrue(address _address) external view returns (bool _ret) {
return simpleMap[_address];
}
}
pragma solidity ^0.4.24;
contract Map { function isTrue(address _address) external view returns (bool _ret); }
contract Ask {
Map public map = Map(MAP_ADDRESS);
function askMe() external view returns (bool _ret) {
return map.isTrue(msg.sender);
}
function whoIsMsgSender() external view returns (address _address) {
return msg.sender;
}
}
If a method in contract B can only be called by contract A, can contract A be disguised?
The code is as follows:
pragma solidity ^0.4.23;
contract A {
address public owner;
B public b;
function A(address _owner) {
owner = _owner;
b = B(msg.sender);
}
function callB() public view returns(string _greeting) {
require(msg.sender == owner);
return b.onlyForA();
}
}
contract B {
A public a;
function B() {
a = new A(msg.sender);
}
// only contract A can call this function
function onlyForA() public view returns(string _greeting) {
require(msg.sender == address(a));
return "Hello A!";
}
}
If attacker know the address of contract a, can he pretend contract A then call the method "onlyForA" of contract B?
I have created a contract where I am taking 2 Hashes from the user and trying to compare them both which in return would give a boolean value of true or false. It works good on remix but when I try to run the contract on Mist the compareString function just shows a message "NO". here is my code.
pragma solidity ^0.4.18;
contract Hash {
string fhash;
string comphash;
event Instructor(string _fhash);
event Instructors(string _comphash);
function setinstructor(string _fhash) public {
fhash = _fhash;
emit Instructor(_fhash);
}
function getinstructor() public constant returns(string){
return(fhash);
}
function setinstructors(string _comphash) public {
comphash = _comphash;
emit Instructors(_comphash);
}
function getinstructors() public constant returns(string){
return(comphash);
}
function compareStrings() public view returns (bool){
return sha256(fhash) == sha256(comphash)? true : false;
}
}
Image of Mist response
I have a deployed contract "greeter" in Ethereum
contract mortal {
address owner;
function mortal() { owner = msg.sender; }
function kill() { if (msg.sender == owner) selfdestruct(owner); }
}
contract greeter is mortal {
string greeting;
function greeter(string _greeting) public {
greeting = _greeting;
}
function greet() constant returns (string) {
return greeting;
}
}
And I want to create another contract, which will call function "kill" from my first contract. The main idea is that this 2 contracts is different. I publish contract A and then publish contract B, which calls A.
How I can do that?
Something like this, but from contract...
var contract = web3.eth.contract(contractAddress, ABI);
contract.call().kill();
Approximately like this, but there's a catch.
pragma solidity ^0.4.6;
contract Mortal {
address owner;
function Mortal() { owner = msg.sender; }
function kill() { if (msg.sender == owner) selfdestruct(owner); }
}
contract Greeter is Mortal {
string greeting;
function Greeter(string _greeting) public {
greeting = _greeting;
}
function greet() constant returns (string) {
return greeting;
}
}
contract Killer {
function destroyVictim(address victim) {
Greeter g = Greeter(victim);
g.kill();
}
}
That's a basic syntax. The ABI gets picked up by including Greeter in the source file along with Killer; that is, the compiler can "see it".
So far, so good.
The issue that arises is that Greeter is going to ignore the command, owing to if(msg.sender==owner). It won't be. It will be whatever address Killer got.
A possible solution to this before Greeter is deployed is to anticipate the need for a changeOwner() function, usually reserved for only the current owner.
Hope it helps.