Skip to content

Instantly share code, notes, and snippets.

@taarushv
Created September 6, 2019 00:10
Show Gist options
  • Save taarushv/62f0e73705b38fc3a5c3e6a9ba75bced to your computer and use it in GitHub Desktop.
Save taarushv/62f0e73705b38fc3a5c3e6a9ba75bced to your computer and use it in GitHub Desktop.
pragma solidity ^0.5.0;
library SafeMath {
/**
* @dev Returns the addition of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `+` operator.
*
* Requirements:
* - Addition cannot overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
* - Subtraction cannot overflow.
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}
/**
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on
* overflow (when the result is negative).
*
* Counterpart to Solidity's `-` operator.
*
* Requirements:
* - Subtraction cannot overflow.
*
* NOTE: This is a feature of the next version of OpenZeppelin Contracts.
* @dev Get it via `npm install @openzeppelin/contracts@next`.
*/
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
/**
* @dev Returns the multiplication of two unsigned integers, reverting on
* overflow.
*
* Counterpart to Solidity's `*` operator.
*
* Requirements:
* - Multiplication cannot overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
}
/**
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on
* division by zero. The result is rounded towards zero.
*
* Counterpart to Solidity's `/` operator. Note: this function uses a
* `revert` opcode (which leaves remaining gas untouched) while Solidity
* uses an invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
* NOTE: This is a feature of the next version of OpenZeppelin Contracts.
* @dev Get it via `npm install @openzeppelin/contracts@next`.
*/
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
// Solidity only automatically asserts when dividing by 0
require(b > 0, errorMessage);
uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return c;
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*/
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}
/**
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
* Reverts with custom message when dividing by zero.
*
* Counterpart to Solidity's `%` operator. This function uses a `revert`
* opcode (which leaves remaining gas untouched) while Solidity uses an
* invalid opcode to revert (consuming all remaining gas).
*
* Requirements:
* - The divisor cannot be zero.
*
* NOTE: This is a feature of the next version of OpenZeppelin Contracts.
* @dev Get it via `npm install @openzeppelin/contracts@next`.
*/
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}
contract CrowdSale{
using SafeMath for uint256;
mapping (address=> uint256) public contributions;
uint256 public totalRaised;
uint256 public maxAmount;
uint256 public saleEndTimeStamp;
bool public saleActive;
address public admin;
// constructor to enable basic parameters of the crowdsale
constructor(uint256 _maxAmount, bool _saleActive, uint256 _saleEndTimeStamp) public {
admin = msg.sender;
// Max amount limit
maxAmount = _maxAmount;
// Sale status on launch
saleActive = _saleActive;
// Sale end time
saleEndTimeStamp = _saleEndTimeStamp;
}
// Function to deposit ETH for the crowdsale
function contribute() public payable returns (bool){
require(msg.value > 0, "Deposit ETH along with the function");
require(msg.value + totalRaised < maxAmount, "Crowdsale limit reached");
require(saleActive, "Sale hasn't started or has ended");
require(now < saleEndTimeStamp, "Sale has ended");
contributions[msg.sender] = msg.value;
totalRaised = totalRaised.add(msg.value);
return true;
}
// Function contributers can use to withdraw their money at all times
function withdraw() public returns (bool){
require(contributions[msg.sender] > 0, "No deposits made");
// refund their funds
msg.sender.transfer(contributions[msg.sender]);
// remove their contributions from the log
totalRaised = totalRaised.sub(contributions[msg.sender]);
contributions[msg.sender]= 0;
return true;
}
// Function to end crowdsale and distribute funds to admin
function endCrowdSale() public onlyAdmin{
saleActive = false;
msg.sender.transfer(totalRaised);
}
// Function to resume/pause crowdsale
function changeCrowdSaleStatus(bool _status) public onlyAdmin{
saleActive = _status;
}
// Function to check total amount raised
function totalAmountRaised() public view returns (uint256){
return totalRaised;
}
// Sets permissions for certain function
modifier onlyAdmin() {
require(msg.sender == admin);
_;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment