{"size": 3542, "ext": "js", "max_stars_repo_path": "src/dapp/contract.js", "max_stars_repo_name": "jensendarren/FlightSurety", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/dapp/contract.js", "max_issues_repo_name": "jensendarren/FlightSurety", "max_issues_repo_issues_event_min_datetime": "2021-05-11T16:14:40.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-19T03:09:30.000Z", "max_forks_repo_path": "src/dapp/contract.js", "max_forks_repo_name": "jensendarren/FlightSurety", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 5, "max_forks_count": null, "avg_line_length": 35.0693069307, "max_line_length": 105, "alphanum_fraction": 0.54743083}
import FlightSuretyApp from '../../build/contracts/FlightSuretyApp.json';
import FlightSuretyData from '../../build/contracts/FlightSuretyData.json';
import Config from './config.json';
import Web3 from 'web3';

export default class Contract {
    constructor(network, callback) {

        let config = Config[network];
        // this.web3 = new Web3(new Web3.providers.HttpProvider(config.url));
        this.web3 = new Web3(new Web3.providers.WebsocketProvider(config.url.replace('http', 'ws')));
        this.flightSuretyApp = new this.web3.eth.Contract(FlightSuretyApp.abi, config.appAddress);
        this.flightSuretyData = new this.web3.eth.Contract(FlightSuretyData.abi, config.dataAddress);
        this.initialize(callback, config.appAddress);
        this.registerEvents(callback);
        this.owner = null;
        this.airlines = [];
        this.passengers = [];
    }

    initialize(callback, address) {
        this.web3.eth.getAccounts((error, accts) => {

            this.owner = accts[0];

            let counter = 1;

            while(this.airlines.length < 5) {
                this.airlines.push(accts[counter++]);
            }

            while(this.passengers.length < 5) {
                this.passengers.push(accts[counter++]);
            }

            this.flightSuretyData.methods
                .setAuthorizedCaller(address)
                .send({from: this.owner}, (error, result) => {
                    console.log(error);
                });

            callback();
        });
    }

    async registerEvents(callback) {
        await this.flightSuretyApp.events.OracleReport({ fromBlock: 'latest' })
                .on('data', (e) => {
                    let event = new CustomEvent("OracleReportEvent", { "detail": e.returnValues.status })
                    document.dispatchEvent(event);
                })
                .on('changed', console.log)
                .on('error', console.log);

        await this.flightSuretyApp.events.FlightStatusInfo({ fromBlock: 'latest' })
                .on('data', (e) => {
                    let event = new CustomEvent("FlightStatusInfo", { "detail": e.returnValues.status })
                    document.dispatchEvent(event);
                })
                .on('changed', console.log)
                .on('error', console.log);
    }

    isOperational(callback) {
       let self = this;
       self.flightSuretyApp.methods
            .isOperational()
            .call({ from: self.owner}, callback);
    }

    fetchFlightStatus(flight, timestamp, callback) {
        let self = this;
        let payload = {
            airline: self.airlines[0],
            flight: flight,
            timestamp: timestamp,
        }
        self.flightSuretyApp.methods
            .fetchFlightStatus(payload.airline, payload.flight, payload.timestamp)
            .send({from: self.owner}, (error, result) => {
                callback(error, payload);
            });
    }

    buyInsurance(flight, timestamp, value,  callback) {
        let self = this;
        let payload = {
            airline: self.airlines[0],
            flight: flight,
            timestamp: timestamp
        }
        self.flightSuretyApp.methods
            .buy(payload.airline, payload.flight, payload.timestamp)
            .send({ from: self.passengers[0],
                    value: this.web3.utils.toWei(value, 'Ether'),
                    gas: 300000
                }, (error, result) => {
                callback(error, payload)
            });
    }
}