Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. pragma solidity >=0.4.22 <0.6.0;
  2.  
  3. interface tokenRecipient {
  4. function receiveApproval(address _from, uint256 _value, address _token, bytes calldata _extraData) external;
  5. }
  6.  
  7. contract TokenERC20 {
  8. // Public variables of the token
  9. string public name;
  10. string public symbol;
  11. uint8 public decimals = 2;
  12. // 18 decimals is the strongly suggested default, avoid changing it
  13. uint256 public totalSupply;
  14. address public owner;
  15.  
  16. modifier onlyowner {
  17. require(owner == msg.sender);
  18. _;
  19. }
  20.  
  21. // This creates an array with all balances
  22. mapping (address => uint256) public balanceOf;
  23. mapping (address => mapping (address => uint256)) public allowance;
  24.  
  25. // This generates a public event on the blockchain that will notify clients
  26. event Transfer(address indexed from, address indexed to, uint256 value);
  27.  
  28. // This generates a public event on the blockchain that will notify clients
  29. event Approval(address indexed _owner, address indexed _spender, uint256 _value);
  30.  
  31. // This notifies clients about the amount burnt
  32. event Burn(address indexed from, uint256 value);
  33.  
  34. /**
  35. * Constructor function
  36. *
  37. * Initializes contract with initial supply tokens to the creator of the contract
  38. */
  39. constructor(
  40. uint256 initialSupply,
  41. string memory tokenName,
  42. string memory tokenSymbol
  43. ) public {
  44. totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount
  45. balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens
  46. name = tokenName; // Set the name for display purposes
  47. symbol = tokenSymbol; // Set the symbol for display purposes
  48. owner = msg.sender; // Set the contract owner
  49. }
  50.  
  51. /**
  52. * Internal transfer, only can be called by this contract
  53. */
  54. function _transfer(address _from, address _to, uint _value) internal {
  55. // Prevent transfer to 0x0 address. Use burn() instead
  56. require(_to != address(0x0));
  57. // Check if the sender has enough
  58. require(balanceOf[_from] >= _value);
  59. // Check for overflows
  60. require(balanceOf[_to] + _value >= balanceOf[_to]);
  61. // Save this for an assertion in the future
  62. uint previousBalances = balanceOf[_from] + balanceOf[_to];
  63. // Subtract from the sender
  64. balanceOf[_from] -= _value;
  65. // Add the same to the recipient
  66. balanceOf[_to] += _value;
  67. emit Transfer(_from, _to, _value);
  68. // Asserts are used to use static analysis to find bugs in your code. They should never fail
  69. assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
  70. }
  71.  
  72. /**
  73. * Transfer tokens
  74. *
  75. * Send `_value` tokens to `_to` from your account
  76. *
  77. * @param _to The address of the recipient
  78. * @param _value the amount to send
  79. */
  80. function transfer(address _to, uint256 _value) public returns (bool success) {
  81. _transfer(msg.sender, _to, _value);
  82. return true;
  83. }
  84.  
  85. /**
  86. * Add supply
  87. *
  88. * Give the creator more tokens
  89. *
  90. * @param _value the amount to send
  91. */
  92. function addSupply(uint256 _value) public returns (uint256 success) {
  93. totalSupply += _value;
  94. balanceOf[msg.sender] += _value;
  95. return totalSupply;
  96. }
  97.  
  98. /**
  99. * Transfer tokens from other address
  100. *
  101. * Send `_value` tokens to `_to` on behalf of `_from`
  102. *
  103. * @param _from The address of the sender
  104. * @param _to The address of the recipient
  105. * @param _value the amount to send
  106. */
  107. function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
  108. require(_value <= allowance[_from][msg.sender]); // Check allowance
  109. allowance[_from][msg.sender] -= _value;
  110. _transfer(_from, _to, _value);
  111. return true;
  112. }
  113.  
  114. /**
  115. * Set allowance for other address
  116. *
  117. * Allows `_spender` to spend no more than `_value` tokens on your behalf
  118. *
  119. * @param _spender The address authorized to spend
  120. * @param _value the max amount they can spend
  121. */
  122. function approve(address _spender, uint256 _value) public
  123. returns (bool success) {
  124. allowance[msg.sender][_spender] = _value;
  125. emit Approval(msg.sender, _spender, _value);
  126. return true;
  127. }
  128.  
  129. /**
  130. * Set allowance for other address and notify
  131. *
  132. * Allows `_spender` to spend no more than `_value` tokens on your behalf, and then ping the contract about it
  133. *
  134. * @param _spender The address authorized to spend
  135. * @param _value the max amount they can spend
  136. * @param _extraData some extra information to send to the approved contract
  137. */
  138. function approveAndCall(address _spender, uint256 _value, bytes memory _extraData)
  139. public
  140. returns (bool success) {
  141. tokenRecipient spender = tokenRecipient(_spender);
  142. if (approve(_spender, _value)) {
  143. spender.receiveApproval(msg.sender, _value, address(this), _extraData);
  144. return true;
  145. }
  146. }
  147.  
  148. /**
  149. * Destroy tokens
  150. *
  151. * Remove `_value` tokens from the system irreversibly
  152. *
  153. * @param _value the amount of money to burn
  154. */
  155. function burn(uint256 _value) public onlyowner returns (bool success) {
  156. require(balanceOf[msg.sender] >= _value); // Check if the sender has enough
  157. balanceOf[msg.sender] -= _value; // Subtract from the sender
  158. totalSupply -= _value; // Updates totalSupply
  159. emit Burn(msg.sender, _value);
  160. return true;
  161. }
  162.  
  163. /**
  164. * Destroy tokens from other account
  165. *
  166. * Remove `_value` tokens from the system irreversibly on behalf of `_from`.
  167. *
  168. * @param _from the address of the sender
  169. * @param _value the amount of money to burn
  170. */
  171. function burnFrom(address _from, uint256 _value) public onlyowner returns (bool success) {
  172. require(balanceOf[_from] >= _value); // Check if the targeted balance is enough
  173. require(_value <= allowance[_from][msg.sender]); // Check allowance
  174. balanceOf[_from] -= _value; // Subtract from the targeted balance
  175. allowance[_from][msg.sender] -= _value; // Subtract from the sender's allowance
  176. totalSupply -= _value; // Update totalSupply
  177. emit Burn(_from, _value);
  178. return true;
  179. }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement