function initiateClaim(string memory flightNumber) external payable returns (bytes32) {
IOracle.KeyValue[] memory queryParams = new IOracle.KeyValue[](2);
queryParams[0] = IOracle.KeyValue({
key: "access_key",
value: "BKYs...vbD9mAl",
encrypted: true
});
queryParams[1] = IOracle.KeyValue({
key: "flight_iata",
value: flightNumber,
encrypted: false
});
IOracle.Condition memory flightLanded = IOracle.Condition({
operator: "eq",
value: "landed",
encrypted: false
});
IOracle.Condition memory isDelayed = IOracle.Condition({
operator: "gt",
value: "0",
encrypted: false
});
IOracle.Condition memory noCondition = IOracle.Condition({
operator: "",
value: "",
encrypted: false
});
IOracle.ResponseField[] memory responseFields = new IOracle.ResponseField[](5);
responseFields[0] = IOracle.ResponseField({
path: "$.data[0].flight_status",
responseType: "string",
condition: flightLanded
});
responseFields[1] = IOracle.ResponseField({
path: "$.data[0].arrival.delay",
responseType: "uint256",
condition: isDelayed
});
responseFields[2] = IOracle.ResponseField({
path: "$.data[0].arrival.delay",
responseType: "uint256",
condition: noCondition
});
responseFields[3] = IOracle.ResponseField({
path: "$.data[0].flight.iata",
responseType: "string",
condition: noCondition
});
responseFields[4] = IOracle.ResponseField({
path: "$.data[0].flight_date",
responseType: "string",
condition: noCondition
});
bytes32 requestId = makeRequest({
method: IOracle.HttpMethod.GET,
url: "https://api.aviationstack.com/v1/flights",
urlEncrypted: false,
headers: new IOracle.KeyValue[](0),
queryParams: queryParams,
body: "",
bodyEncrypted: false,
responseFields: responseFields
});
claimers[requestId] = msg.sender;
flightIds[requestId] = FlightId({flightNumber: flightNumber, date: ""});
return requestId;
}
function fundContract() external payable onlyOwner {
}
function withdrawFunds() external onlyOwner {
SafeTransferLib.safeTransferAllETH(owner());
}
function getFlightInfo(string memory flightNumber, string memory date) external view returns (FlightInfo memory) {
return flightData[flightNumber][date];
}
function getFlightInfoById(bytes32 requestId) external view returns (FlightInfo memory) {
FlightId memory flightId = flightIds[requestId];
return flightData[flightId.flightNumber][flightId.date];
}
function _handleResponse(bytes32 requestId, bool success, bytes calldata data) internal override {
address claimer = claimers[requestId];
(bool isLanded, bool isDelayed, uint256 delayMinutes, string memory flightNumber, string memory date) =
abi.decode(data, (bool, bool, uint256, string, string));
FlightInfo memory flight = flightData[flightNumber][date];
if (flight.claimTimestamp != 0) {
return;
}
flight.delayed = isLanded && isDelayed;
flight.delayMinutes = delayMinutes;
emit FlightDelayVerified(flightNumber, date, flight.delayed, delayMinutes);
if (flight.delayed) {
flight.claimed = true;
flight.claimTimestamp = block.timestamp;
SafeTransferLib.safeTransferETH(claimer, INSURANCE_PAYOUT);
emit ClaimPaid(claimer, flightNumber, date, INSURANCE_PAYOUT);
}
flightData[flightNumber][date] = flight;
flightIds[requestId].date = date;
}