[Ethereum] Solidity Smart Contract – Split and Send Contract ETH Balance Between Two Accounts

remix-testssmart-contract-walletssolidity

Want to start off by saying thanks in advance. I'm very new to solidity, so really appreciate your time.

I've built a basic smart contract for testing purposes, and I'm running it on Remix.

I want to define a function (withdraw_equal) that allows the balance of the smart contract (say 4 eth) to be split in half and sent out to the two accounts evenly) (i.e. 2 eth to account_one as well as 2 eth to account_2.

Here's my code:

pragma solidity ^0.5.0;

contract JointSavings {
  address payable account_one = 0xc3879B456DAA348a16B6524CBC558d2CC784722b;
  address payable account_two = 0xA29f7E79ECEA4cE30DD78cfeb9605D9aFt5143a3;

  uint public balanceContract;
  
  function withdraw(uint amount, address payable recipient) public {
    recipient.transfer(amount);
    balanceContract = address(this).balance;
  }

  function withdraw_equal() public {
    uint amount = balanceContract / 2;
    account_one.transfer(amount);
    account_two.transfer(amount);
    balanceContract = address(this).balance;
  }

  function deposit() public payable {
    balanceContract = address(this).balance;
  }

  function() external payable {}

}

I'm conscious that the 'withdraw' function works because 'address payable' is included as an input parameter. However, as the 'withdraw_even" function doesn't take any parameters, I'm not quite sure how to adapt my code.

P.S. I'm just messing around and experimenting here, so feel free to send through any fun tweaks / suggestions / additions you might have to fix / update /enhance the code. Thanks in advance!

Best Answer

seems pretty explanatory to me.... in case you missed it he wants to create a function and then call that function from the pay address function. i interpret his question as asking, "can i hand off the payment to a subfunction". look at payment splitter under open zeppelin. you should be able to fathom your own code from that. https://forum.openzeppelin.com/t/using-paymentsplitter-with-remix/3007

Related Topic