{"size": 2019, "ext": "js", "max_stars_repo_path": "utils/manageDeals.js", "max_stars_repo_name": "crypto-trading-solutions/RWI_strategy", "max_stars_repo_stars_event_min_datetime": "2020-06-09T20:49:19.000Z", "max_stars_repo_stars_event_max_datetime": "2020-06-09T20:54:25.000Z", "max_issues_repo_path": "utils/manageDeals.js", "max_issues_repo_name": "crypto-trading-solutions/RWI_strategy", "max_issues_repo_issues_event_min_datetime": "2020-06-09T18:06:39.000Z", "max_issues_repo_issues_event_max_datetime": "2020-07-14T17:35:08.000Z", "max_forks_repo_path": "utils/manageDeals.js", "max_forks_repo_name": "crypto-trading-solutions/RWI_strategy", "max_forks_repo_forks_event_min_datetime": "2022-03-15T12:53:05.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-15T12:53:05.000Z"}
{"max_stars_count": 2, "max_issues_count": 9, "max_forks_count": 1, "avg_line_length": 34.2203389831, "max_line_length": 86, "alphanum_fraction": 0.583457157}
const to = require('await-to-js').default;
const Binance = require("../serializers/BinanceRequestProvider");
const binance = new Binance(process.env.APIKEY, process.env.APISECRET);

module.exports = async function (symbol, price, action) {
    const [openOrdersError, openOrders] = await to(
        binance.getOpenOrders(symbol)
    )
    if (openOrdersError) return { error: openOrdersError };
 
    if(openOrders.length > 0){
        return {error: 'We have opened orders!', orders: openOrders};
    }

    // for (let i = 0; i < openOrders.length; i++) {
    //     const dealErrors = await checkDealErrors(openOrders[i], action, symbol);
    //     console.log('dealErrors');
    //     console.log(dealErrors);
    //     console.log('dealErrors');
    //     if (dealErrors.error) return dealErrors;

    //     const manageDealsResult = await manageDeals(openOrders[i], action, symbol);

    //     return manageDealsResult;
    // }

    return { code: 'open' };
}

const checkDealErrors = (openOrder, newAction, newSymbol) => {
    if (newAction === 'sell') {
        if (openOrder.symbol === newSymbol && openOrder.side === newAction) {
            return { error: 'This BUY deal is already open', order: openOrder };
        }
    } else if (newAction === 'sell') {
        if (openOrder.symbol === newSymbol && openOrder.side === newAction) {
            return { error: 'This SELL deal is already open', order: openOrder };
        }
    }

    return { success: 'success' };
}

const manageDeals = (openOrder, newAction, newSymbol) => {
    switch(newAction) {
        case 'buy':
            if(openOrder.symbol === newSymbol && openOrder.side === 'sell'){
                return {code: 'closeDeal', order: openOrder};
            }
            break;
        case 'sell':
            if(openOrder.symbol === newSymbol && openOrder.side === 'buy'){
                return {code: 'closeDeal', order: openOrder};
            }
            break;
        default:
            return {code: 'open'};
    }
}