Guest User

Untitled

a guest
Feb 20th, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 KB | None | 0 0
  1. pragma solidity ^0.4.19;
  2.  
  3. uint256 public Id;
  4.  
  5. //Color default to white
  6. string public Colorhex = "#ffffff";
  7.  
  8. //Inital price
  9. uint256 public StartPrice = 5;
  10.  
  11. //Times pixel has been purchased
  12. uint256 public TimesPurchased = 0;
  13.  
  14. function Pixel(uint256 id, string colorhex, uint256 startprice, uint256 timespurchased) public {
  15. Id = id;
  16. Colorhex = colorhex;
  17. StartPrice = startprice;
  18. TimesPurchased = timespurchased;
  19. }
  20.  
  21. using SafeMath for uint256;
  22.  
  23. //Get the Cost
  24. function getCost(uint _startprice, uint256 _times) public pure returns(uint cost) {
  25. return _startprice*(_times+1);
  26. }
  27. //The current price of pixel
  28. uint256 public cost = getCost(StartPrice,TimesPurchased);
  29.  
  30. //The current pixel owner
  31. address public pixelowner;
  32.  
  33. //Safely increments counter
  34. function addpixelpurchasecounter() public view {
  35. TimesPurchased.add(1);
  36. }
  37.  
  38. //Returns hex color
  39. function setcolorhex(string _hex) public {
  40. Colorhex = _hex;
  41. }
  42.  
  43. //Address to which the funds will be sent
  44. address public owner;
  45.  
  46. mapping(address => uint256) balances;
  47. mapping(address => mapping(address =>uint256)) allowed;
  48.  
  49. using SafeMath for uint256;
  50.  
  51. address[] newContracts;
  52.  
  53. //Use these somehow?
  54. //This checks whether Ether has been paid to the address
  55. function () private payable {
  56. UpdateColor();
  57. }
  58. //Automatically send tokens.
  59. function UpdateColor() public payable {
  60. require(msg.value > 0);
  61. owner.transfer(msg.value);
  62. }
  63.  
  64. function balanceOf(address _owner) public constant returns (uint256 balance) {
  65. return balances[_owner];
  66. }
  67.  
  68. function approve(address _spender, uint256 _value) public returns (bool success) {
  69. allowed[msg.sender][_spender] = _value;
  70. Approval(msg.sender, _spender, _value);
  71. return true;
  72. }
  73.  
  74. //Spend from sender funds.
  75. //This recieves pixel.Cost at index i.
  76. function transfer(address _to, uint256 _value) public returns (bool success) {
  77. require(balances[msg.sender] >= _value && _value > 0);
  78. balances[msg.sender].sub(_value);
  79. balances[_to].add(_value);
  80. Transfer(msg.sender, _to, _value);
  81. return true;
  82. }
  83.  
  84. function getTransfer(address _to, uint256 _value, uint256 _id, string _hex) public returns (bool success) {
  85. require(transfer(_to,_value));
  86. setColor(_id,_hex);
  87. increaseCost(_id);
  88. return true;
  89. }
  90.  
  91. function increaseCost(uint256 i) public view {
  92. getContract(i).addpixelpurchasecounter();
  93. }
  94.  
  95. function allowance(address _owner, address _spender) public constant returns (uint256 remaining) {
  96. return allowed[_owner][_spender];
  97. }
  98.  
  99. uint256 constant PIXELAMOUNT = 10000;
  100.  
  101. //Add one empty pixel contract
  102. function AddEmptyPixel(uint256 id) private{
  103. address newContract = Pixel(id);
  104. //Add contract to list of contracts
  105. newContracts.push(newContract);
  106. }
  107.  
  108. //Populate pixel contract with PIXELAMOUNT
  109. function AddEmptyPixels(uint256 amount) private {
  110. for (uint i = 0; i < amount; i++) {
  111. AddEmptyPixels(i);
  112. }
  113. }
  114.  
  115. //Set Color to hex at index i
  116. function setColor(uint i, string _hex) public {
  117. getContract(i).setcolorhex(_hex);
  118. }
  119.  
  120. //Get cost of pixel at index i
  121. //This should be called from Front once the pixel is hovered and Metamask transaction started.
  122. function getCost(uint i) public view returns (uint256 cost) {
  123. return getContract(i).cost();
  124. }
  125.  
  126. function getContract(uint i) public view returns (Pixel chosenPixel) {
  127. return Pixel(newContracts[i]);
  128. }
  129.  
  130. //Add initial empty pixels on contract start.
  131. function Canvas() public {
  132. AddEmptyPixels(PIXELAMOUNT);
  133. }
  134.  
  135. event Transfer(address indexed _from, address indexed _to, uint256 _value);
  136. event Approval(address indexed _owner, address indexed _spender, uint256 _value);
  137.  
  138. Fallback function of contract Canvas requires too much gas (infinite). If the fallback function requires more than 2300 gas, the contract cannot receive Ether.
Add Comment
Please, Sign In to add comment