0

I don't know to describe my question. So I will try to explain it here. I'm working with truffle. I have a Contract A that deploys a Contract B. A passes it's address to B. B's owner is set to the address of A. Inside B there is a function foo() that requires the caller to be the owner.

My question is this: Can I call foo() from truffle like this ?

B.foo( {from: A.address} ) 

Or is it the case that the foo() function is now only callable through the contract itself, e.g. with an A's fooA() function that calls B.foo() ?

I provide an example code below.

contract B {
  address private immutable ownerOfB;

  modifier ownerOnly  {
    require (msg.sender == ownerOfB, 
      "ONLY OWNER(S) ALLOWED TO DO THAT.");
    _;
  }
  constructor(address _owner) {
    ownerOfB = _owner;
  }
  function fooB() ownerOnly public {
    // do something
  }
}

contract A {
  
  address private immutable ownerOfA;
  B b;

  contructor() {
    ownerOfA = msg.sender;
    b = new B( address(this) );
  }
  
  function fooA() {
    fooB()
  }
}

Now if I use this code in truffle with the default account settings , A's owner will be accounts[0] and B's owner will be A.address. The function fooB() can be called through fooA() but cannot be called from truffle directly like this: B.foo( {from: A.address} ); even if we were to get an instance of the B contract in truffle with something like

aContr = await A.deployed();
bContr = await B.at( a.b() );

and then to call B.foo:

bContr.fooB( {from: aContr.address} )  --> throws Error "sender address not recognized"

Is this correct behaviour or is it relevant to truffle only?

PS. In my migration file I only deploy the A contract.

1 Answer 1

0

My question is totally wrong, I have made the assumption that a contract may be the initiator of a transaction (tx.origin being a contract) which obviously can't be true. I don't know how to delete the question, so I posted an answer.

1
  • You are correct. To send a transaction you need to sign it, since contracts do not have private keys then they can't sign a transaction.
    – Ismael
    Jun 21, 2022 at 4:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.