lifemathmoney

MatchingDonations.sol

May 8th, 2021 (edited)
486
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //SPDX-License-Identifier: AFL-3.0 (Academic Free License v3.0) https://opensource.org/licenses/afl-3.0
  2. //code by Harsh Strongman from lifemathmoney.com, the #1 self-improvement website for men
  3.  
  4. //solidity version specifier
  5. pragma solidity 0.8.1;
  6.  
  7. //A philanthropist has agreed to send matching contributions for all amounts received from the public
  8. //Given: the address of the philanthropist
  9. //philanthropist sends his maximum donation first, and only then money is accepted from the public
  10. //contract can be ended after 30 days, where any remaining balance of the philanthropist is refunded to him
  11.  
  12. contract donation {
  13.    
  14.     //setting up global variables
  15.     // DO NOT SEND ETH TO THESE ADDRESS - THE PRIVATE KEYS HAVE BEEN BURNED AND YOUR ETH WILL BE LOST FOREVER
  16.     // if you deploy this code, make sure you change these addresses
  17.     address payable addressOfPhilanthropist = payable(0x6ed0B157827d1F04EcE69e4BB19FBEB652f23A0d);
  18.     address payable addressOfCharity = payable(0x92893c3fEb851bc5D75f976bfA7713BEdD1E0Eb3);
  19.     uint receiptFromPhilanthropist;
  20.     uint receiptFromOthers;
  21.     uint startTime;
  22.    
  23.     //constructor to set the start time when the contract is created
  24.     constructor() {
  25.         startTime = block.timestamp;
  26.     }
  27.    
  28.    
  29.     //receive ether and maintain separate totals for receipts from the philanthropist and from everyone else
  30.     receive() external payable {
  31.         if (msg.sender == addressOfPhilanthropist) {
  32.             receiptFromPhilanthropist += msg.value;
  33.         }
  34.         else {
  35.             require((receiptFromOthers + msg.value) > receiptFromOthers); //protecting against overflow
  36.             receiptFromOthers += msg.value;
  37.             require((receiptFromOthers * 2) > receiptFromOthers); //protecting against overflow
  38.         }
  39.     }
  40.    
  41.     function withdraw() public {
  42.        
  43.         //check if 30 days have passed
  44.         require((block.timestamp - startTime) > 30 days, "30 days have not yet passed");
  45.        
  46.         //send matching donation and refund the balance to the philanthropist
  47.         if (receiptFromOthers < receiptFromPhilanthropist) {
  48.             addressOfCharity.transfer((receiptFromOthers * 2));
  49.             addressOfPhilanthropist.transfer(address(this).balance);
  50.         }
  51.         else {
  52.             addressOfCharity.transfer(address(this).balance);
  53.         }
  54.     }
  55. }
RAW Paste Data