在传递给另一个合约作为参数时,msg.sender地址是否已更改?

时间:2018-07-14 07:46:39

标签: ethereum solidity

我在这里要问我关于统一代码的问题。

这是简单的示例代码。合同“地图”具有映射的数据。它具有每个地址的T / F值。我创建了一个帐户X,并将其添加到'simpleMap'

通过“地图”合同签订“询问”查找映射数据合同。当X称为askMe()时,我预期的结果是TRUE,但始终是FALSE

当我致电whoIsMsgSender时,它将返回确切的X的帐户。有什么问题吗?

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;
    }
}

1 个答案:

答案 0 :(得分:0)

我在Remix中尝试了您的合同,但是askMe()为我返回了true。您如何分开这两个合同?也许在Ask尝试在其上调用函数之前尚未部署Map。我分开了两个合同:

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;
    }
}