Created
October 8, 2020 08:04
-
-
Save ImanMousavi/045a103f3a7e73939f04daeda19d8850 to your computer and use it in GitHub Desktop.
Donation with smart contract (solidity) [You can use ETH, TRX, ...]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.5.1 <0.6.0; | |
library SafeMath { | |
function mul(uint256 a, uint256 b) internal pure returns (uint256) { | |
if (a == 0) { | |
return 0; | |
} | |
uint256 c = a * b; | |
assert(c / a == b); | |
return c; | |
} | |
function div(uint256 a, uint256 b) internal pure returns (uint256) { | |
// assert(b > 0); // Solidity automatically throws when dividing by 0 | |
uint256 c = a / b; | |
// assert(a == b * c + a % b); // There is no case in which this doesn't hold | |
return c; | |
} | |
function sub(uint256 a, uint256 b) internal pure returns (uint256) { | |
assert(b <= a); | |
return a - b; | |
} | |
function add(uint256 a, uint256 b) internal pure returns (uint256) { | |
uint256 c = a + b; | |
assert(c >= a); | |
return c; | |
} | |
} | |
library Objects { | |
struct Investment { | |
uint256 planId; | |
uint256 investmentDate; | |
uint256 investment; | |
uint256 lastWithdrawalDate; | |
uint256 currentDividends; | |
bool isExpired; | |
} | |
struct Plan { | |
uint256 dailyInterest; | |
uint256 term; //0 means unlimited | |
uint256 maxDailyInterest; | |
} | |
struct Investor { | |
address addr; | |
uint256 referrerEarnings; | |
uint256 availableReferrerEarnings; | |
uint256 referrer; | |
uint256 planCount; | |
mapping(uint256 => Investment) plans; | |
uint256 level1RefCount; | |
uint256 level2RefCount; | |
} | |
} | |
contract Ownable { | |
address public owner; | |
event onOwnershipTransferred(address indexed previousOwner, address indexed newOwner); | |
/** | |
* @dev The Ownable constructor sets the original `owner` of the contract to the sender | |
* account. | |
*/ | |
constructor() public { | |
owner = msg.sender; | |
} | |
/** | |
* @dev Throws if called by any account other than the owner. | |
*/ | |
modifier onlyOwner() { | |
require(msg.sender == owner); | |
_; | |
} | |
/** | |
* @dev Allows the current owner to transfer control of the contract to a newOwner. | |
* @param _newOwner The address to transfer ownership to. | |
*/ | |
function transferOwnership(address _newOwner) public onlyOwner { | |
require(_newOwner != address(0)); | |
emit onOwnershipTransferred(owner, _newOwner); | |
owner = _newOwner; | |
} | |
} | |
contract Donation is Ownable { | |
using SafeMath for uint256; | |
address owner; | |
event fundMoved(address _to, uint256 _amount); | |
modifier onlyowner { if (msg.sender == owner) _; } | |
address[] _giver; | |
uint[] _values; | |
uint256 totalDonations_; | |
constructor() public { | |
owner = msg.sender; | |
} | |
function donate() payable public { | |
addGiver(msg.value); | |
} | |
function moveFund(address payable _to, uint256 _amount) onlyowner public { | |
uint256 balance = address(this).balance; | |
//uint amount = _amount; | |
if (_amount <= balance) { | |
if (_to.send(balance)) { | |
emit fundMoved(_to, _amount); | |
} else { | |
revert(); | |
} | |
} else { | |
revert(); | |
} | |
} | |
function getBalance() public view returns (uint256){ | |
return address(this).balance; | |
} | |
function getAllDonations() public view returns (uint256){ | |
return totalDonations_; | |
} | |
function addGiver(uint256 _amount) internal { | |
_giver.push(msg.sender); | |
_values.push(_amount); | |
totalDonations_ = totalDonations_.add(_amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment