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 ?
Asked
Active
Viewed 3,535 times
1
ashwin
- 389
- 6
- 14
-
Well... Does it? – goodvibration Jul 07 '20 at 08:32
-
this suggested link s contract level payable handling, I am looking for function level payable handling and via interface – ashwin Jul 07 '20 at 08:33
1 Answers
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