1. pragma solidity ^0.4.24;
  2. contract owned {
  3. address public owner;
  4. constructor() public {
  5. owner = msg.sender;
  6. }
  7. modifier onlyOwner {
  8. require(msg.sender == owner);
  9. _;
  10. }
  11. function transferOwnership(address newOwner) onlyOwner public {
  12. owner = newOwner;
  13. }
  14. }
  15. interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) external; }
  16. contract TokenERC20 {
  17. // Public variables of the token
  18. string public name;
  19. string public symbol;
  20. uint8 public decimals = 18;
  21. // 18 decimals is the strongly suggested default, avoid changing it
  22. uint256 public totalSupply;
  23. // This creates an array with all balances
  24. mapping (address => uint256) public balanceOf;
  25. mapping (address => mapping (address => uint256)) public allowance;
  26. // This generates a public event on the blockchain that will notify clients
  27. event Transfer(address indexed from, address indexed to, uint256 value);
  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. // This notifies clients about the amount burnt
  31. event Burn(address indexed from, uint256 value);
  32. /**
  33. * Constrctor function
  34. *
  35. * Initializes contract with initial supply tokens to the creator of the contract
  36. */
  37. constructor(
  38. uint256 initialSupply,
  39. string tokenName,
  40. string tokenSymbol
  41. ) public {
  42. totalSupply = initialSupply * 10 ** uint256(decimals); // Update total supply with the decimal amount
  43. balanceOf[msg.sender] = totalSupply; // Give the creator all initial tokens
  44. name = tokenName; // Set the name for display purposes
  45. symbol = tokenSymbol; // Set the symbol for display purposes
  46. }
  47. /**
  48. * Internal transfer, only can be called by this contract
  49. */
  50. function _transfer(address _from, address _to, uint _value) internal {
  51. // Prevent transfer to 0x0 address. Use burn() instead
  52. require(_to != 0x0);
  53. // Check if the sender has enough
  54. require(balanceOf[_from] >= _value);
  55. // Check for overflows
  56. require(balanceOf[_to] + _value > balanceOf[_to]);
  57. // Save this for an assertion in the future
  58. uint previousBalances = balanceOf[_from] + balanceOf[_to];
  59. // Subtract from the sender
  60. balanceOf[_from] -= _value;
  61. // Add the same to the recipient
  62. balanceOf[_to] += _value;
  63. emit Transfer(_from, _to, _value);
  64. // Asserts are used to use static analysis to find bugs in your code. They should never fail
  65. assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
  66. }
  67. /**
  68. * Transfer tokens
  69. *
  70. * Send `_value` tokens to `_to` from your account
  71. *
  72. * @param _to The address of the recipient
  73. * @param _value the amount to send
  74. */
  75. function transfer(address _to, uint256 _value) public returns (bool success) {
  76. _transfer(msg.sender, _to, _value);
  77. return true;
  78. }
  79. /**
  80. * Transfer tokens from other address
  81. *
  82. * Send `_value` tokens to `_to` in behalf of `_from`
  83. *
  84. * @param _from The address of the sender
  85. * @param _to The address of the recipient
  86. * @param _value the amount to send
  87. */
  88. function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
  89. require(_value <= allowance[_from][msg.sender]); // Check allowance
  90. allowance[_from][msg.sender] -= _value;
  91. _transfer(_from, _to, _value);
  92. return true;
  93. }
  94. /**
  95. * Set allowance for other address
  96. *
  97. * Allows `_spender` to spend no more than `_value` tokens in your behalf
  98. *
  99. * @param _spender The address authorized to spend
  100. * @param _value the max amount they can spend
  101. */
  102. function approve(address _spender, uint256 _value) public
  103. returns (bool success) {
  104. allowance[msg.sender][_spender] = _value;
  105. emit Approval(msg.sender, _spender, _value);
  106. return true;
  107. }
  108. /**
  109. * Set allowance for other address and notify
  110. *
  111. * Allows `_spender` to spend no more than `_value` tokens in your behalf, and then ping the contract about it
  112. *
  113. * @param _spender The address authorized to spend
  114. * @param _value the max amount they can spend
  115. * @param _extraData some extra information to send to the approved contract
  116. */
  117. function approveAndCall(address _spender, uint256 _value, bytes _extraData)
  118. public
  119. returns (bool success) {
  120. tokenRecipient spender = tokenRecipient(_spender);
  121. if (approve(_spender, _value)) {
  122. spender.receiveApproval(msg.sender, _value, this, _extraData);
  123. return true;
  124. }
  125. }
  126. /**
  127. * Destroy tokens
  128. *
  129. * Remove `_value` tokens from the system irreversibly
  130. *
  131. * @param _value the amount of money to burn
  132. */
  133. function burn(uint256 _value) public returns (bool success) {
  134. require(balanceOf[msg.sender] >= _value); // Check if the sender has enough
  135. balanceOf[msg.sender] -= _value; // Subtract from the sender
  136. totalSupply -= _value; // Updates totalSupply
  137. emit Burn(msg.sender, _value);
  138. return true;
  139. }
  140. /**
  141. * Destroy tokens from other account
  142. *
  143. * Remove `_value` tokens from the system irreversibly on behalf of `_from`.
  144. *
  145. * @param _from the address of the sender
  146. * @param _value the amount of money to burn
  147. */
  148. function burnFrom(address _from, uint256 _value) public returns (bool success) {
  149. require(balanceOf[_from] >= _value); // Check if the targeted balance is enough
  150. require(_value <= allowance[_from][msg.sender]); // Check allowance
  151. balanceOf[_from] -= _value; // Subtract from the targeted balance
  152. allowance[_from][msg.sender] -= _value; // Subtract from the sender's allowance
  153. totalSupply -= _value; // Update totalSupply
  154. emit Burn(_from, _value);
  155. return true;
  156. }
  157. }
  158. /******************************************/
  159. /* ADVANCED TOKEN STARTS HERE */
  160. /******************************************/
  161. contract MyAdvancedToken is owned, TokenERC20 {
  162. uint256 public sellPrice;
  163. uint256 public buyPrice;
  164. mapping (address => bool) public frozenAccount;
  165. /* This generates a public event on the blockchain that will notify clients */
  166. event FrozenFunds(address target, bool frozen);
  167. /* Initializes contract with initial supply tokens to the creator of the contract */
  168. constructor(
  169. uint256 initialSupply,
  170. string tokenName,
  171. string tokenSymbol
  172. ) TokenERC20(initialSupply, tokenName, tokenSymbol) public {}
  173. /* Internal transfer, only can be called by this contract */
  174. function _transfer(address _from, address _to, uint _value) internal {
  175. require (_to != 0x0); // Prevent transfer to 0x0 address. Use burn() instead
  176. require (balanceOf[_from] >= _value); // Check if the sender has enough
  177. require (balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
  178. require(!frozenAccount[_from]); // Check if sender is frozen
  179. require(!frozenAccount[_to]); // Check if recipient is frozen
  180. balanceOf[_from] -= _value; // Subtract from the sender
  181. balanceOf[_to] += _value; // Add the same to the recipient
  182. emit Transfer(_from, _to, _value);
  183. }
  184. /// @notice Create `mintedAmount` tokens and send it to `target`
  185. /// @param target Address to receive the tokens
  186. /// @param mintedAmount the amount of tokens it will receive
  187. function mintToken(address target, uint256 mintedAmount) onlyOwner public {
  188. balanceOf[target] += mintedAmount;
  189. totalSupply += mintedAmount;
  190. emit Transfer(0, this, mintedAmount);
  191. emit Transfer(this, target, mintedAmount);
  192. }
  193. /// @notice `freeze? Prevent | Allow` `target` from sending & receiving tokens
  194. /// @param target Address to be frozen
  195. /// @param freeze either to freeze it or not
  196. function freezeAccount(address target, bool freeze) onlyOwner public {
  197. frozenAccount[target] = freeze;
  198. emit FrozenFunds(target, freeze);
  199. }
  200. /// @notice Allow users to buy tokens for `newBuyPrice` eth and sell tokens for `newSellPrice` eth
  201. /// @param newSellPrice Price the users can sell to the contract
  202. /// @param newBuyPrice Price users can buy from the contract
  203. function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner public {
  204. sellPrice = newSellPrice;
  205. buyPrice = newBuyPrice;
  206. }
  207. /// @notice Buy tokens from contract by sending ether
  208. function buy() payable public {
  209. uint amount = msg.value / buyPrice; // calculates the amount
  210. _transfer(this, msg.sender, amount); // makes the transfers
  211. }
  212. /// @notice Sell `amount` tokens to contract
  213. /// @param amount amount of tokens to be sold
  214. function sell(uint256 amount) public {
  215. address myAddress = this;
  216. require(myAddress.balance >= amount * sellPrice); // checks if the contract has enough ether to buy
  217. _transfer(msg.sender, this, amount); // makes the transfers
  218. msg.sender.transfer(amount * sellPrice); // sends ether to the seller. It's important to do this last to avoid recursion attacks
  219. }
  220. }

You need to correctly ABI-encode your constructor parameters. You can easily do this with online tool https://abi.hashex.org. Just paste in your abi to auto-parse constructor parameters or manually add them and enter values. ABI-encoded constructor parameters would be automatically calculated. Just copy them and paste in etherscan.io constructor parameters input.

来自于:
https://ethereum.org/token#full-coin-code
abi中的初始值提取工具:https://abi.hashex.org/

分类: web

标签:   ethereum