Guest User

Untitled

a guest
Jul 20th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. pragma solidity ^0.4.0;
  2.  
  3. contract CorretoraOpcoes {
  4.  
  5. struct DateTime {
  6. uint16 year;
  7. uint8 month;
  8. uint8 day;
  9. }
  10.  
  11. uint constant DAY_IN_SECONDS = 86400;
  12. uint constant YEAR_IN_SECONDS = 31536000;
  13. uint constant LEAP_YEAR_IN_SECONDS = 31622400;
  14. uint constant HOUR_IN_SECONDS = 3600;
  15. uint constant MINUTE_IN_SECONDS = 60;
  16. uint16 constant ORIGIN_YEAR = 1970;
  17.  
  18. function isLeapYear(uint16 year) public pure returns (bool) {
  19. if (year % 4 != 0) {
  20. return false;
  21. }
  22. if (year % 100 != 0) {
  23. return true;
  24. }
  25. if (year % 400 != 0) {
  26. return false;
  27. }
  28. return true;
  29. }
  30.  
  31. function toTimestamp(uint16 year, uint8 month, uint8 day, uint8 hour, uint8 minute, uint8 second) public pure returns (uint timestamp) {
  32. uint16 i;
  33.  
  34. // Year
  35. for (i = ORIGIN_YEAR; i < year; i++) {
  36. if (isLeapYear(i)) {
  37. timestamp += LEAP_YEAR_IN_SECONDS;
  38. }
  39. else {
  40. timestamp += YEAR_IN_SECONDS;
  41. }
  42. }
  43.  
  44. // Month
  45. uint8[12] memory monthDayCounts;
  46. monthDayCounts[0] = 31;
  47. if (isLeapYear(year)) {
  48. monthDayCounts[1] = 29;
  49. }
  50. else {
  51. monthDayCounts[1] = 28;
  52. }
  53. monthDayCounts[2] = 31;
  54. monthDayCounts[3] = 30;
  55. monthDayCounts[4] = 31;
  56. monthDayCounts[5] = 30;
  57. monthDayCounts[6] = 31;
  58. monthDayCounts[7] = 31;
  59. monthDayCounts[8] = 30;
  60. monthDayCounts[9] = 31;
  61. monthDayCounts[10] = 30;
  62. monthDayCounts[11] = 31;
  63.  
  64. for (i = 1; i < month; i++) {
  65. timestamp += DAY_IN_SECONDS * monthDayCounts[i - 1];
  66. }
  67.  
  68. // Day
  69. timestamp += DAY_IN_SECONDS * (day - 1);
  70.  
  71. // Hour
  72. timestamp += HOUR_IN_SECONDS * (hour);
  73.  
  74. // Minute
  75. timestamp += MINUTE_IN_SECONDS * (minute);
  76.  
  77. // Second
  78. timestamp += second;
  79.  
  80. return timestamp;
  81. }
  82.  
  83. struct Quote {
  84. uint256 today;
  85. uint256 yesterday;
  86. }
  87.  
  88. struct Option {
  89. bool buy; // true se for de compra, false se for de venda
  90. bool usa; // true se for americana, false se for europeia
  91. uint8 answer; // 0 = waiting | 1 = accepted | 2 = refused
  92.  
  93. address owner;
  94. address holder;
  95.  
  96. uint256 strike;
  97. uint256 quote;
  98.  
  99. uint256 premium;
  100.  
  101. DateTime date;
  102. }
  103.  
  104. // Endereço Público do dono do contrato
  105. address public owner;
  106.  
  107. mapping(address => int8) registered; // Guarda quem esta registrado
  108. mapping(address => int256) balances; // Guarda os saldos <address><saldo> em centavos
  109. mapping(uint256 => mapping (uint256 => uint256[])) optionsId; // "2020" -> "timestamp do dia" -> "[id1, id2]"
  110.  
  111. Option[] public options;
  112. uint256 public counter;
  113.  
  114. Quote public quote;
  115.  
  116.  
  117. // Evento anuncia pra rede que o dono do contrato mudou
  118. event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
  119.  
  120. // Auto executavel
  121. constructor() public {
  122. owner = msg.sender;
  123. counter = 0;
  124. }
  125.  
  126. // So para o dono
  127. modifier onlyOwner() {
  128. require(msg.sender == owner);
  129. _;
  130. }
  131.  
  132. function transferOwnership(address _newOwner) public onlyOwner {
  133. _transferOwnership(_newOwner);
  134. }
  135. function _transferOwnership(address _newOwner) internal {
  136. require(_newOwner != address(0));
  137. emit OwnershipTransferred(owner, _newOwner);
  138. owner = _newOwner;
  139. }
  140.  
  141. function registerAddress(address user) public onlyOwner {
  142. registered[user] = 1;
  143. balances[user] = 0;
  144. }
  145.  
  146. // quote = Quantos BRL's uma (1) unidade de dólar compra, em centavos
  147. // Exemplo:
  148. // U$1 <> R$3.20: quote = 320
  149. // U$1 <> R$17.20: quote = 1720
  150. function updateQuote(uint256 _quote) public onlyOwner{
  151. quote.yesterday = quote.today;
  152. quote.today = _quote;
  153. }
  154.  
  155. function createOption(bool _buy, bool _usa, uint256 _strike, uint256 _quote,
  156. uint16 _year, uint8 _month, uint8 _day, address _caller, uint256 _premium) public returns (uint256 identifier) {
  157.  
  158. require(registered[msg.sender] == 1);
  159. require(registered[_caller] == 1);
  160.  
  161. DateTime _date;
  162. _date.year = _year;
  163. _date.month = _month;
  164. _date.day = _day;
  165.  
  166. uint256 dayYear = toTimestamp(_year, _month, _day, 0, 0, 0);
  167.  
  168. Option _option;
  169. _option.buy = _buy;
  170. _option.usa = _usa;
  171. _option.answer = 0;
  172. _option.owner = msg.sender;
  173. _option.holder = _caller;
  174. _option.strike = _strike;
  175. _option.quote = _quote;
  176. _option.premium = _premium;
  177. _option.date = _date;
  178.  
  179. options[counter] = _option;
  180. optionsId[_year][dayYear].push(counter);
  181.  
  182. counter++;
  183.  
  184. // TODO: Emitir evento
  185.  
  186. return counter--;
  187. }
  188.  
  189. function acceptOptionById(uint256 identifier, uint8 answer) public {
  190. require(options[identifier].holder == msg.sender);
  191. options[identifier].answer = answer;
  192.  
  193. // TODO: Emitir evento
  194. }
  195. }
Add Comment
Please, Sign In to add comment