Skip to content

Instantly share code, notes, and snippets.

@ArpitxGit
Created June 20, 2022 22:31
Show Gist options
  • Save ArpitxGit/0bafb5fb1bf8887ee17f6a506ca5b838 to your computer and use it in GitHub Desktop.
Save ArpitxGit/0bafb5fb1bf8887ee17f6a506ca5b838 to your computer and use it in GitHub Desktop.
Smart Contract to Split Payments
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
//contract for splitting payments
contract PaymentSplitter {
//array for holding addresses, public, payable
address payable [] public recipients;
//lastly adding event for use in application
event TransferReceived(address _from, uint _amount);
//constructor, whenever deloy the contract, need this parameter
constructor(address payable [] memory _address) {
//iterating to save addresses in array
for(uint i=0; i < _address.length; i++) {
recipients.push(_address[i]);
}
}
//special function "receive",
//called whenever somebody just transfers the ether into your account
//basically for handling the money that is sent to our smart contract
receive() payable external {
uint256 share = msg.value / recipients.length; //dividing equally
//loop for transfer money
for(uint i=0; i < recipients.length; i++) {
recipients[i].transfer(share);
}
//firing the event
emit TransferReceived(msg.sender, msg.value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment