0

Assuming that I don’t know the contract A, I just know its address, and that it has a setNumber method that I want to call it from contract B.

I want to call the function setNumber in contract A, using the method callsetNumbercontractA in contract B.

How to reproduce:

  • Deploy both contracts
  • Copy contract A address and use the function setcontract in contract B
  • Now try to use the function callsetNumbercontractA in contract B

It will fail and I can't get why.

pragma solidity 0.5.12;
contract A {
    event lmao (string);
    uint public x;
    function hello () public {
        emit lmao ('YES');
    }
    function setNumber (uint n) public {
        x=n;
    }
} 

pragma solidity 0.5.12;
contract B{
    address public to;    
    function callcontractA () public {
       to.call(abi.encodeWithSignature("hello()"));
    }   
    function setcontract (address x) public{
        to = x;
    }   
    function callsetNumbercontractA () public {
        // I wanna set var x (in A) to 10
       to.call(abi.encodeWithSignature("setNumber(uint)",10));
    }
}

Ropsten transactions:

4
  • ? What is missing
    – Dani69654
    Mar 31, 2020 at 16:51
  • What is Looks like I can use callcontractA() but not callsetNumbercontractA () supposed to mean??? Have you looked at the code formatting??? Mar 31, 2020 at 16:52
  • Sorry for that I edited my post. Hope it's more readable.
    – Dani69654
    Mar 31, 2020 at 17:02
  • I've fixed it a little bit, guess you could probably fix it further. Mar 31, 2020 at 17:18

1 Answer 1

1

There is no reasoning in your question for not using this:

contract IA {
    function setNumber(uint n) public;
}

contract B {
    IA public a;    
    function setContract(IA _a) public {
        a = _a;
    }   
    function setNumber() public {
        a.setNumber(10);
    }
}
2
  • Thanks for editing my question (I am not practical at all excuse me). As far as I understand, in order to follow you guide, I should have both contracts on the same solidity file. Is it right? In my case, the contracts are in totally different solidity files.
    – Dani69654
    Mar 31, 2020 at 17:22
  • @Dani69654: I should have both contracts on the same solidity file - no you don't (do you see both contracts in my example above?). Mar 31, 2020 at 17:25

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.