1

I have a function marked as payable in contract A and I want to call it from contract B so that msg.value in contract A can have its desired value and contract A must have balance of sent amount , how it is possible ?

ashwin
  • 389
  • 6
  • 14

1 Answers1

3

This is how you handle amount sending between contracts via interface:

ContractA.sol:

pragma solidity ^0.5.11;

contract A { uint256 public lastFundSentToContract = 0;

function updateLastFundSentToContract () public payable {
    lastFundSentToContract = msg.value;
}

function getContractBalance() public view returns(uint256) {
    return address(this).balance;
}

}

ContractB.sol:

pragma solidity ^0.5.11;

contract B { A a_contract_instance; constructor(address _a_contract_address) public { a_contract_instance = A(_a_contract_address); }

function callToContractA() public payable {
    a_contract_instance.updateLastFundSentToContract.value(msg.value)();
}

function getContractBalance() public view returns(uint256) {
    return address(this).balance;
}

}

interface A { function updateLastFundSentToContract () external payable; }

Miroslav Nedelchev
  • 3,519
  • 2
  • 10
  • 12