Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.4.0;
- contract CorretoraOpcoes {
- struct DateTime {
- uint16 year;
- uint8 month;
- uint8 day;
- }
- uint constant DAY_IN_SECONDS = 86400;
- uint constant YEAR_IN_SECONDS = 31536000;
- uint constant LEAP_YEAR_IN_SECONDS = 31622400;
- uint constant HOUR_IN_SECONDS = 3600;
- uint constant MINUTE_IN_SECONDS = 60;
- uint16 constant ORIGIN_YEAR = 1970;
- function isLeapYear(uint16 year) public pure returns (bool) {
- if (year % 4 != 0) {
- return false;
- }
- if (year % 100 != 0) {
- return true;
- }
- if (year % 400 != 0) {
- return false;
- }
- return true;
- }
- function toTimestamp(uint16 year, uint8 month, uint8 day, uint8 hour, uint8 minute, uint8 second) public pure returns (uint timestamp) {
- uint16 i;
- // Year
- for (i = ORIGIN_YEAR; i < year; i++) {
- if (isLeapYear(i)) {
- timestamp += LEAP_YEAR_IN_SECONDS;
- }
- else {
- timestamp += YEAR_IN_SECONDS;
- }
- }
- // Month
- uint8[12] memory monthDayCounts;
- monthDayCounts[0] = 31;
- if (isLeapYear(year)) {
- monthDayCounts[1] = 29;
- }
- else {
- monthDayCounts[1] = 28;
- }
- monthDayCounts[2] = 31;
- monthDayCounts[3] = 30;
- monthDayCounts[4] = 31;
- monthDayCounts[5] = 30;
- monthDayCounts[6] = 31;
- monthDayCounts[7] = 31;
- monthDayCounts[8] = 30;
- monthDayCounts[9] = 31;
- monthDayCounts[10] = 30;
- monthDayCounts[11] = 31;
- for (i = 1; i < month; i++) {
- timestamp += DAY_IN_SECONDS * monthDayCounts[i - 1];
- }
- // Day
- timestamp += DAY_IN_SECONDS * (day - 1);
- // Hour
- timestamp += HOUR_IN_SECONDS * (hour);
- // Minute
- timestamp += MINUTE_IN_SECONDS * (minute);
- // Second
- timestamp += second;
- return timestamp;
- }
- struct Quote {
- uint256 today;
- uint256 yesterday;
- }
- struct Option {
- bool buy; // true se for de compra, false se for de venda
- bool usa; // true se for americana, false se for europeia
- uint8 answer; // 0 = waiting | 1 = accepted | 2 = refused
- address owner;
- address holder;
- uint256 strike;
- uint256 quote;
- uint256 premium;
- DateTime date;
- }
- // Endereço Público do dono do contrato
- address public owner;
- mapping(address => int8) registered; // Guarda quem esta registrado
- mapping(address => int256) balances; // Guarda os saldos <address><saldo> em centavos
- mapping(uint256 => mapping (uint256 => uint256[])) optionsId; // "2020" -> "timestamp do dia" -> "[id1, id2]"
- Option[] public options;
- uint256 public counter;
- Quote public quote;
- // Evento anuncia pra rede que o dono do contrato mudou
- event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
- // Auto executavel
- constructor() public {
- owner = msg.sender;
- counter = 0;
- }
- // So para o dono
- modifier onlyOwner() {
- require(msg.sender == owner);
- _;
- }
- function transferOwnership(address _newOwner) public onlyOwner {
- _transferOwnership(_newOwner);
- }
- function _transferOwnership(address _newOwner) internal {
- require(_newOwner != address(0));
- emit OwnershipTransferred(owner, _newOwner);
- owner = _newOwner;
- }
- function registerAddress(address user) public onlyOwner {
- registered[user] = 1;
- balances[user] = 0;
- }
- // quote = Quantos BRL's uma (1) unidade de dólar compra, em centavos
- // Exemplo:
- // U$1 <> R$3.20: quote = 320
- // U$1 <> R$17.20: quote = 1720
- function updateQuote(uint256 _quote) public onlyOwner{
- quote.yesterday = quote.today;
- quote.today = _quote;
- }
- function createOption(bool _buy, bool _usa, uint256 _strike, uint256 _quote,
- uint16 _year, uint8 _month, uint8 _day, address _caller, uint256 _premium) public returns (uint256 identifier) {
- require(registered[msg.sender] == 1);
- require(registered[_caller] == 1);
- DateTime _date;
- _date.year = _year;
- _date.month = _month;
- _date.day = _day;
- uint256 dayYear = toTimestamp(_year, _month, _day, 0, 0, 0);
- Option _option;
- _option.buy = _buy;
- _option.usa = _usa;
- _option.answer = 0;
- _option.owner = msg.sender;
- _option.holder = _caller;
- _option.strike = _strike;
- _option.quote = _quote;
- _option.premium = _premium;
- _option.date = _date;
- options[counter] = _option;
- optionsId[_year][dayYear].push(counter);
- counter++;
- // TODO: Emitir evento
- return counter--;
- }
- function acceptOptionById(uint256 identifier, uint8 answer) public {
- require(options[identifier].holder == msg.sender);
- options[identifier].answer = answer;
- // TODO: Emitir evento
- }
- }
Add Comment
Please, Sign In to add comment