i have been Googling for this answer, but i haven`t succeeded.

The scenario is the following: I have two contracts interacting with each other, and I want to make sure that they only respond to each other. Meaning that if anyone else, or other contracts try to call their functions, the transaction must be reverted.

In order to achieve that, tried adding something like "require(msg.sender==addrContractA)" to the functions that i want to restrict but it does not workout.

Please look at the example below.

contract B {
     // Some code here...
     address addrA;
     receive() public payable {
        require(msg.sender ==addrA, "Error- Sender not allowed");
        // Some more code here...

    function setAddressA(address _addrA) public onlyOwner{ addrA = _addrA;}

contract A{
     // Some code here...
   address b;

   constructor(address payable _b) public { 
       b =_b; 
   receive() payable external{

The system is expected to behave like this:

  • A person sends eth to contract A, contract A passes the value along to contract B.
  • When the value hits contract B, it must verify if the sender is contract A. Any value that did not pass through contract A must be refused by reverting the transaction.

Problem here is that I do not know what variable I could inspect to get the address of the "internal sender". Something like msg.sender, but the variable "msg.sender" always returns the address of the wallet of the person who started the transaction ( who sent eth to contract A) rather than the address of contract A.

Basically, what i am asking is, if any contract in the ethereum network tries to interact with contract B, how do i find out it's address? I mean, i need to know the address of who is calling contract B in order to refuse it if it is not coming from A.

I read a little bit about internal transactions and i did not find any encouraging topics :(, so here i am looking for help.

Grateful to any reasonable suggestion!

  • How are you testing? msg.sender is the caller, tx.origin is EOA that signed the transaction.
    – Ismael
    Dec 31 '20 at 16:33
  • As suspected, cannot reproduce Problem here is that the variable "msg.sender" will always return the address of the wallet of the person who started the transaction (who sent eth to contract A) rather than the address of contract A. Dec 31 '20 at 16:36
  • @Ismael: Doesn't matter how he's testing. The problem that he's describing simply does not exist in the given code (and tx.origin is not a test-side element anyway, it's an onchain element, which is not present in the onchain code posted here). Dec 31 '20 at 16:39
  • 1
    @goodvibration You are correct the real problem is different and it is elsewhere so I'm asking him to provide the testing code.
    – Ismael
    Dec 31 '20 at 16:46
  • Hi everyone, thanks for trying to help. In fact the code itself is a little more complicated than this simple example.The example that I posted is only for better understanding the problem. In this example, inside the "receive" function, on the contract B, i am doing "require(msg.sender ==addrA)" but as i already it does not apply to the inspection that i really need, as the variable msg.sender does not contain the address of the intermediary contract that passed the value along.
    – DarkWinter
    Dec 31 '20 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.