-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflightinsurnacecontract.sol
142 lines (120 loc) · 4.29 KB
/
flightinsurnacecontract.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
pragma solidity ^0.4.22;
import "github.com/provable-things/ethereum-api/provableAPI_0.4.25.sol";
import "github.com/Arachnid/solidity-stringutils/strings.sol";
contract Owned {
address public owner;
address public newOwner;
event OwnershipTransferred(address indexed _from, address indexed _to);
function Owned() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function transferOwnership(address _newOwner) public onlyOwner {
newOwner = _newOwner;
}
function acceptOwnership() public {
require(msg.sender == newOwner);
OwnershipTransferred(owner, newOwner);
owner = newOwner;
newOwner = address(0);
}
}
contract ExampleContract is usingProvable, Owned {
struct Customer {
string flightno;
string query;
bool boarded;
}
struct Flight {
bool late;
Customer[] customers;
}
//public variables
string public status;
bool public late;
uint public lateseconds;
uint flightprice;
mapping(address => Customer) customer;
mapping(string => address[]) flights;
mapping(string => string) flightstatuses;
mapping (bytes32 => string) requests;
using strings for *;
event LogConstructorInitiated(string nextStep);
event LogPriceUpdated(string price);
event LogNewProvableQuery(string description);
//
function parsestatus() {
lateseconds = parseString(status);
late = parselate(status);
//return(true);
}
function lookupflight(string flight)public view returns (string r){
return(flightstatuses[flight]);
}
//Call
function __callback(bytes32 myid, string result) {
if (msg.sender != provable_cbAddress()) revert();
status = result;
flightstatuses[requests[myid]] = result;
//late = late(result);
//lateseconds = parseRandomNumbers(result);
LogPriceUpdated(result);
}
//flight status query
function flightstatus(string flight) payable {
if (provable_getPrice("URL") > this.balance) {
LogNewProvableQuery("Provable query was NOT sent, please add some ETH to cover for the query fee");
} else {
LogNewProvableQuery("Provable query was sent, standing by for the answer..");
bytes32 queryId = provable_query("WolframAlpha", strConcat(flight," delay time in seconds "));
//bytes32 queryId = provable_query("URL", "json(https://www.wolframalpha.com/input/?i=JetBlue+Airways+flight+1723+delay).result");
//provable_query("URL", append("https://www.wolframalpha.com/input/?i=",flight));
requests[queryId] = flight;
}
}
//Get numbers
function parseString(string numbers) public view returns (uint number) {
strings.slice memory s = numbers.toSlice();
strings.slice memory delim = " ".toSlice();
uint[] memory parts = new uint[](s.count(delim) + 1);
for (uint i = 0; i < parts.length; i++) {
parts[i] = parseInt(s.split(delim).toString());
}
return parts[0];
}
//Late or not
function parselate(string numbers) public view returns(bool r ){
strings.slice memory s = numbers.toSlice();
late = s.contains("late".toSlice());
return(s.contains("late".toSlice()));
}
//Register customers. Turn into ownable?
function registerflight(string flightno) public payable{
require(msg.value >= flightprice);
flights[flightno].push(msg.sender);
customer[msg.sender].flightno = flightno;
}
function customerregistered(string flightno) public view returns(bool r){
address[] storage custs = flights[flightno];
address sender = msg.sender;
for (uint i = 0; i <custs.length; i++) {
if(sender == custs[i]){
return(true);
}
}
return(false);
}
function customerregistered2(string flightno) public view returns(address r){
address[] storage custs = flights[flightno];
return(custs[0]);
}
function refundcustomer(string flightno) public{
flightstatus(flightno);
if(late){
msg.sender.transfer(flightprice);
}
}
}