1
votes

I want to create a function in solidity that receive an amount from person A, double the received amount using the contract account balance, then transfer the doubled amount to a person B. Up to now, I create the function which receive an external payable amount from only the person A, I note that the amount placed should be > 2 ether, but for doubling and transfer the amount, I'm little confused about the method.

    contract MyContract {
    address payable public personA;
    address payable public personB;
    mapping(address => uint) balances;
    modifier onlyonlypersonA() {
        require(msg.sender == onlypersonA, "Only onlypersonA can call this method");
        _;}       
   function Send() onlypersonA external payable {
        if(msg.value < 2 ether) {revert();} 
        balances[msg.sender] += msg.value;}
}

1

1 Answers

1
votes

Try this:


  contract MyContract {
    address payable public personA;
    address payable public personB;
    mapping(address => uint) balances;
    modifier onlyonlypersonA() {
        require(msg.sender == onlypersonA, "Only onlypersonA can call this method");
        _;}       
   function Send() onlypersonA external payable {
        if(msg.value < 2 ether) {revert();} 
        balances[msg.sender] += msg.value;

        // Try doubling
        require(msg.value * 2 <= address(this).balance, "Insufficient funds to doubling");
        personB.transfer(msg.value * 2);
   }
}