Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. contract DecypherCoin {
  2.  
  3. // ERC20 State
  4. mapping (address => uint256) public balances;
  5. mapping (address => mapping (address => uint256)) public allowances;
  6. uint256 public totalSupply;
  7.  
  8. // Human State
  9. string public name;
  10. uint8 public decimals;
  11. string public symbol;
  12. string public version;
  13.  
  14. // Minter State
  15. address public centralMinter;
  16.  
  17. // Backed By Ether State
  18. uint256 public buyPrice;
  19. uint256 public sellPrice;
  20.  
  21. // Modifiers
  22. modifier onlyMinter {
  23. if (msg.sender != centralMinter) throw;
  24. _;
  25. }
  26.  
  27. // ERC20 Events
  28. event Transfer(address indexed _from, address indexed _to, uint256 _value);
  29. event Approval(address indexed _owner, address indexed _spender, uint256 _value);
  30.  
  31. // Constructor
  32. function DecypherCoin(uint256 _initialAmount) {
  33. balances[msg.sender] = _initialAmount;
  34. totalSupply = _initialAmount;
  35. name = "DecypherCoin";
  36. decimals = 18;
  37. symbol = "DCY";
  38. version = "0.1";
  39. }
  40.  
  41. // ERC20 Methods
  42. function balanceOf(address _address) constant returns (uint256 balance) {
  43. return balances[_address];
  44. }
  45.  
  46. function allowance(address _owner, address _spender) constant returns (uint256 remaining) {
  47. return allowances[_owner][_spender];
  48. }
  49.  
  50. function transfer(address _to, uint256 _value) returns (bool success) {
  51. if(balances[msg.sender] < _value) throw;
  52. if(balances[_to] + _value < balances[_to]) throw;
  53. balances[msg.sender] -= _value;
  54. balances[_to] += _value;
  55. Transfer(msg.sender, _to, _value);
  56. return true;
  57. }
  58.  
  59. function approve(address _spender, uint256 _value) returns (bool success) {
  60. allowances[msg.sender][_spender] = _value;
  61. Approval(msg.sender, _spender, _value);
  62. return true;
  63. }
  64.  
  65. function transferFrom(address _owner, address _to, uint256 _value) returns (bool success) {
  66. if(balances[_owner] < _value) throw;
  67. if(balances[_to] + _value < balances[_to]) throw;
  68. if(allowances[_owner][msg.sender] < _value) throw;
  69. balances[_owner] -= _value;
  70. balances[_to] += _value;
  71. allowances[_owner][msg.sender] -= _value;
  72. Transfer(_owner, _to, _value);
  73. return true;
  74. }
  75.  
  76. // Minter Functions
  77. function mint(uint256 _amountToMint) onlyMinter {
  78. balances[centralMinter] += _amountToMint;
  79. totalSupply += _amountToMint;
  80. Transfer(this, centralMinter, _amountToMint);
  81. }
  82.  
  83. function transferMinter(address _newMinter) onlyMinter {
  84. centralMinter = _newMinter;
  85. }
  86.  
  87. // Backed By Ether Methods
  88. // Must create the contract so that it has enough Ether to buy back ALL tokens on the market, or else the contract will be insolvent and users won't be able to sell their tokens
  89. function setPrices(uint256 _newSellPrice, uint256 _newBuyPrice) onlyMinter {
  90. sellPrice = _newSellPrice;
  91. buyPrice = _newBuyPrice;
  92. }
  93.  
  94. function buy() payable returns (uint amount) {
  95. amount = msg.value / buyPrice;
  96. if(balances[centralMinter] < amount) throw; // Validate there are enough tokens minted
  97. balances[centralMinter] -= amount;
  98. balances[msg.sender] += amount;
  99. Transfer(centralMinter, msg.sender, amount);
  100. return amount;
  101. }
  102.  
  103. function sell(uint _amount) returns (uint revenue) {
  104. if (balances[msg.sender] < _amount) throw; // Validate sender has enough tokens to sell
  105. balances[centralMinter] += _amount;
  106. balances[msg.sender] -= _amount;
  107. revenue = _amount * sellPrice;
  108. if (!msg.sender.send(revenue)) {
  109. throw;
  110. } else {
  111. Transfer(msg.sender, centralMinter, _amount);
  112. return revenue;
  113. }
  114. }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement