0

I want to call another contract's function when I call a function in my contract and send my msg.value to the other function. Here is an example of what I am trying to achieve:

function execute(address _target, bytes[] memory _payload) {
    _target.call(_payload, value = msg.value); 

How do I send the msg.value of the execute() function to _target in the .call() function?

1 Answer 1

2

You can use that contract interface to execute the function easily. To send ETH the function must be payable. Eg

interface Target {
  function someFunction() external payable;
}

contract MainContract {
  Target public target;

  constructor(address _tragetAddress) {
    target = Target(_targetAddress);
  }
  
  function execute() public {
    uint256 ethAmountToSend = address(this).balance;
    target.someFunction{value:ethAmountToSend}();
  }

}

Here target.someFunction{value:ethAmountToSend}() will send ETH to the target contract.

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.