Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.4.25;
- /**
- * @dev Wrappers over Solidity's arithmetic operations with added overflow
- * checks.
- *
- * Arithmetic operations in Solidity wrap on overflow. This can easily result
- * in bugs, because programmers usually assume that an overflow raises an
- * error, which is the standard behavior in high level programming languages.
- * `SafeMath` restores this intuition by reverting the transaction when an
- * operation overflows.
- *
- * Using this library instead of the unchecked operations eliminates an entire
- * class of bugs, so it's recommended to use it always.
- */
- 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.
- */
- 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.
- */
- 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.
- */
- function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
- require(b != 0, errorMessage);
- return a % b;
- }
- }
- contract TokenSales {
- /* Define variable owner of the type address */
- using SafeMath for uint256;
- address owner;
- mapping (uint8 => uint256) slotTokenIds;
- mapping (uint8 => uint256) slotTokenPrices;
- mapping (uint8 => bool) slotEnabled;
- mapping (uint8 => uint8) slotPrecision;
- // upto 10 tokens for sale.
- uint8 nSlots=10;
- /* This constructor is executed at initialization and sets the owner of the contract */
- constructor() public {
- owner = msg.sender;
- }
- modifier onlyOwner() {
- require(msg.sender == owner);
- _;
- }
- event Sale(uint256 tokenId,uint256 units,uint256 raw,uint256 decimals,uint256 pricePerUnitSun);
- function() external payable {
- uint256 sentTokenId = uint256(msg.tokenid);
- for(uint8 slotId=0;slotId<nSlots;slotId++){
- uint256 slotTokenId=slotTokenIds[slotId];
- bool tokenEnabled=slotEnabled[slotId];
- if(slotTokenId == sentTokenId && tokenEnabled){
- return;
- }
- }
- require(false);
- }
- function buy(uint8 slotId) public payable {
- // 0 TRX transaction not valid;
- require(msg.value>0);
- // check token is enabled for sale and price set
- require(slotEnabled[slotId] && slotTokenPrices[slotId]>0);
- // calc amount of tokens to send
- uint256 tokenPrice=slotTokenPrices[slotId];
- uint256 amountTRXSent=msg.value;
- uint256 amountToBuy=amountTRXSent.div(tokenPrice);
- uint256 decimals = slotPrecision[slotId];
- uint256 rawPerUnit= 10 ** decimals;
- uint256 amountRaw=amountTRXSent.mul(rawPerUnit).div(tokenPrice);
- // check there is enough balance
- uint256 tokenId = slotTokenIds[slotId];
- address self=address(this);
- require(self.tokenBalance(tokenId)>=amountToBuy);
- // send
- msg.sender.transferToken(amountRaw,tokenId);
- emit Sale(tokenId,amountToBuy,amountRaw,decimals,tokenPrice);
- }
- function updatePrice(uint8 slotId,uint256 priceSun) public onlyOwner{
- slotTokenPrices[slotId]=priceSun;
- }
- function getPrice(uint8 slotId) public view returns(uint256 priceSun){
- return slotTokenPrices[slotId];
- }
- function getTokenId(uint8 slotId) public view returns(uint256 tokenId){
- return slotTokenIds[slotId];
- }
- function setToken(uint8 slotId,uint256 tokenID,uint8 precision,uint256 priceSun) public onlyOwner{
- slotTokenIds[slotId]=tokenID;
- slotPrecision[slotId]=precision;
- slotTokenPrices[slotId]=priceSun;
- slotEnabled[slotId]=true;
- }
- function disableSales(uint8 slotId) public onlyOwner{
- slotEnabled[slotId]=false;
- }
- function enableSales(uint8 slotId) public onlyOwner{
- slotEnabled[slotId]=true;
- }
- /* Function to recover the funds on the contract */
- function kill() public { if (msg.sender == owner) selfdestruct(msg.sender); }
- function withdrawToken(uint8 slotId,uin256 units) public onlyOwner{
- uint8 decimals=slotPrecision[slotId];
- uint256 rawPerUnit= 10 ** decimals;
- uint256 amountRaw=rawPerUnit*units;
- // check there is enough balance
- uint256 tokenId = slotTokenIds[slotId];
- address self=address(this);
- require(self.tokenBalance(tokenId)>=amountRaw);
- //send
- msg.sender.transferToken(amountRaw,tokenId);
- }
- function withdrawTrx(uint256 amountSun) public onlyOwner {
- msg.sender.send(amountSun);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement