SHARE
TWEET

Untitled

a guest Aug 19th, 2019 86 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. pragma solidity ^0.4.25;
  3.  
  4. /**
  5.  * @dev Wrappers over Solidity's arithmetic operations with added overflow
  6.  * checks.
  7.  *
  8.  * Arithmetic operations in Solidity wrap on overflow. This can easily result
  9.  * in bugs, because programmers usually assume that an overflow raises an
  10.  * error, which is the standard behavior in high level programming languages.
  11.  * `SafeMath` restores this intuition by reverting the transaction when an
  12.  * operation overflows.
  13.  *
  14.  * Using this library instead of the unchecked operations eliminates an entire
  15.  * class of bugs, so it's recommended to use it always.
  16.  */
  17. library SafeMath {
  18.     /**
  19.      * @dev Returns the addition of two unsigned integers, reverting on
  20.      * overflow.
  21.      *
  22.      * Counterpart to Solidity's `+` operator.
  23.      *
  24.      * Requirements:
  25.      * - Addition cannot overflow.
  26.      */
  27.     function add(uint256 a, uint256 b) internal pure returns (uint256) {
  28.         uint256 c = a + b;
  29.         require(c >= a, "SafeMath: addition overflow");
  30.  
  31.         return c;
  32.     }
  33.  
  34.     /**
  35.      * @dev Returns the subtraction of two unsigned integers, reverting on
  36.      * overflow (when the result is negative).
  37.      *
  38.      * Counterpart to Solidity's `-` operator.
  39.      *
  40.      * Requirements:
  41.      * - Subtraction cannot overflow.
  42.      */
  43.     function sub(uint256 a, uint256 b) internal pure returns (uint256) {
  44.         return sub(a, b, "SafeMath: subtraction overflow");
  45.     }
  46.  
  47.     /**
  48.      * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
  49.      * overflow (when the result is negative).
  50.      *
  51.      * Counterpart to Solidity's `-` operator.
  52.      *
  53.      * Requirements:
  54.      * - Subtraction cannot overflow.
  55.      */
  56.     function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
  57.         require(b <= a, errorMessage);
  58.         uint256 c = a - b;
  59.  
  60.         return c;
  61.     }
  62.  
  63.     /**
  64.      * @dev Returns the multiplication of two unsigned integers, reverting on
  65.      * overflow.
  66.      *
  67.      * Counterpart to Solidity's `*` operator.
  68.      *
  69.      * Requirements:
  70.      * - Multiplication cannot overflow.
  71.      */
  72.     function mul(uint256 a, uint256 b) internal pure returns (uint256) {
  73.         // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
  74.         // benefit is lost if 'b' is also tested.
  75.         // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
  76.         if (a == 0) {
  77.             return 0;
  78.         }
  79.  
  80.         uint256 c = a * b;
  81.         require(c / a == b, "SafeMath: multiplication overflow");
  82.  
  83.         return c;
  84.     }
  85.  
  86.     /**
  87.      * @dev Returns the integer division of two unsigned integers. Reverts on
  88.      * division by zero. The result is rounded towards zero.
  89.      *
  90.      * Counterpart to Solidity's `/` operator. Note: this function uses a
  91.      * `revert` opcode (which leaves remaining gas untouched) while Solidity
  92.      * uses an invalid opcode to revert (consuming all remaining gas).
  93.      *
  94.      * Requirements:
  95.      * - The divisor cannot be zero.
  96.      */
  97.     function div(uint256 a, uint256 b) internal pure returns (uint256) {
  98.         return div(a, b, "SafeMath: division by zero");
  99.     }
  100.  
  101.     /**
  102.      * @dev Returns the integer division of two unsigned integers. Reverts with custom message on
  103.      * division by zero. The result is rounded towards zero.
  104.      *
  105.      * Counterpart to Solidity's `/` operator. Note: this function uses a
  106.      * `revert` opcode (which leaves remaining gas untouched) while Solidity
  107.      * uses an invalid opcode to revert (consuming all remaining gas).
  108.      *
  109.      * Requirements:
  110.      * - The divisor cannot be zero.
  111.      */
  112.     function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
  113.         // Solidity only automatically asserts when dividing by 0
  114.         require(b > 0, errorMessage);
  115.         uint256 c = a / b;
  116.         // assert(a == b * c + a % b); // There is no case in which this doesn't hold
  117.  
  118.         return c;
  119.     }
  120.  
  121.     /**
  122.      * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
  123.      * Reverts when dividing by zero.
  124.      *
  125.      * Counterpart to Solidity's `%` operator. This function uses a `revert`
  126.      * opcode (which leaves remaining gas untouched) while Solidity uses an
  127.      * invalid opcode to revert (consuming all remaining gas).
  128.      *
  129.      * Requirements:
  130.      * - The divisor cannot be zero.
  131.      */
  132.     function mod(uint256 a, uint256 b) internal pure returns (uint256) {
  133.         return mod(a, b, "SafeMath: modulo by zero");
  134.     }
  135.  
  136.     /**
  137.      * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
  138.      * Reverts with custom message when dividing by zero.
  139.      *
  140.      * Counterpart to Solidity's `%` operator. This function uses a `revert`
  141.      * opcode (which leaves remaining gas untouched) while Solidity uses an
  142.      * invalid opcode to revert (consuming all remaining gas).
  143.      *
  144.      * Requirements:
  145.      * - The divisor cannot be zero.
  146.      */
  147.     function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
  148.         require(b != 0, errorMessage);
  149.         return a % b;
  150.     }
  151. }
  152.  
  153. contract TokenSales {
  154.     /* Define variable owner of the type address */
  155.     using SafeMath for uint256;
  156.  
  157.     address owner;
  158.  
  159.     mapping (uint8 => uint256) slotTokenIds;
  160.     mapping (uint8 => uint256) slotTokenPrices;
  161.     mapping (uint8 => bool) slotEnabled;
  162.     mapping (uint8 => uint8) slotPrecision;
  163.     // upto 10 tokens for sale.
  164.     uint8 nSlots=10;
  165.     /* This constructor is executed at initialization and sets the owner of the contract */
  166.     constructor() public {
  167.         owner = msg.sender;
  168.     }
  169.  
  170.     modifier onlyOwner() {
  171.         require(msg.sender == owner);
  172.         _;
  173.     }
  174.  
  175.     event Sale(uint256 tokenId,uint256 units,uint256 raw,uint256 decimals,uint256 pricePerUnitSun);
  176.     function() external payable {
  177.         uint256 sentTokenId = uint256(msg.tokenid);
  178.         for(uint8 slotId=0;slotId<nSlots;slotId++){
  179.             uint256 slotTokenId=slotTokenIds[slotId];
  180.             bool tokenEnabled=slotEnabled[slotId];
  181.             if(slotTokenId == sentTokenId && tokenEnabled){
  182.                 return;
  183.             }
  184.         }
  185.         require(false);
  186.     }
  187.  
  188.     function buy(uint8 slotId) public payable {
  189.         // 0 TRX transaction not valid;
  190.         require(msg.value>0);
  191.         // check token is enabled for sale and price set
  192.  
  193.         require(slotEnabled[slotId] && slotTokenPrices[slotId]>0);
  194.         // calc amount of tokens to send
  195.         uint256 tokenPrice=slotTokenPrices[slotId];
  196.         uint256 amountTRXSent=msg.value;
  197.         uint256 amountToBuy=amountTRXSent.div(tokenPrice);
  198.         uint256 decimals = slotPrecision[slotId];
  199.         uint256 rawPerUnit= 10 ** decimals;
  200.         uint256 amountRaw=amountTRXSent.mul(rawPerUnit).div(tokenPrice);
  201.  
  202.          // check there is enough balance
  203.         uint256 tokenId = slotTokenIds[slotId];
  204.         address self=address(this);
  205.         require(self.tokenBalance(tokenId)>=amountToBuy);
  206.         // send
  207.         msg.sender.transferToken(amountRaw,tokenId);
  208.         emit Sale(tokenId,amountToBuy,amountRaw,decimals,tokenPrice);
  209.     }
  210.  
  211.     function updatePrice(uint8 slotId,uint256 priceSun) public onlyOwner{
  212.        slotTokenPrices[slotId]=priceSun;
  213.     }
  214.  
  215.     function getPrice(uint8 slotId) public  view  returns(uint256 priceSun){
  216.        return slotTokenPrices[slotId];
  217.     }
  218.  
  219.     function getTokenId(uint8 slotId) public  view returns(uint256 tokenId){
  220.        return slotTokenIds[slotId];
  221.     }
  222.  
  223.     function setToken(uint8 slotId,uint256 tokenID,uint8 precision,uint256 priceSun) public onlyOwner{
  224.       slotTokenIds[slotId]=tokenID;
  225.       slotPrecision[slotId]=precision;
  226.       slotTokenPrices[slotId]=priceSun;
  227.       slotEnabled[slotId]=true;
  228.     }
  229.  
  230.     function disableSales(uint8 slotId) public onlyOwner{
  231.        slotEnabled[slotId]=false;
  232.     }
  233.  
  234.     function enableSales(uint8 slotId) public onlyOwner{
  235.        slotEnabled[slotId]=true;
  236.     }
  237.  
  238.  
  239.  
  240.  
  241.     /* Function to recover the funds on the contract */
  242.     function kill() public { if (msg.sender == owner) selfdestruct(msg.sender); }
  243.  function withdrawToken(uint8 slotId,uin256 units) public onlyOwner{
  244.    uint8 decimals=slotPrecision[slotId];
  245.    uint256 rawPerUnit= 10 ** decimals;
  246.    uint256 amountRaw=rawPerUnit*units;
  247.    // check there is enough balance
  248.    uint256 tokenId = slotTokenIds[slotId];
  249.    address self=address(this);
  250.    require(self.tokenBalance(tokenId)>=amountRaw);
  251.    //send
  252.    msg.sender.transferToken(amountRaw,tokenId);
  253.  }
  254.  
  255.   function withdrawTrx(uint256 amountSun) public onlyOwner {
  256.     msg.sender.send(amountSun);
  257.   }
  258. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top