Ejemplo n.º 1
0
def dojo_setup():
    at = dojo_auth()
    try:
        status = dojo_status().json()
    except AttributeError:
        status = dojo_status()

    user_info = User.query.filter_by(username=current_user.username).first()
    form = DojoForm()
    if form.validate_on_submit():
        api_keys_json = api_keys_class.loader()
        api_keys_json['dojo']['onion'] = form.dojo_onion.data
        api_keys_json['dojo']['api_key'] = form.dojo_apikey.data
        api_keys_json['dojo']['token'] = form.dojo_token.data
        api_keys_class.saver(api_keys_json)
        at = dojo_auth()
    elif request.method == "GET":
        at = dojo_auth()
        api_keys_json = api_keys_class.loader()
        form.dojo_onion.data = api_keys_json['dojo']['onion']
        form.dojo_apikey.data = api_keys_json['dojo']['api_key']
        try:
            form.dojo_token.data = at["authorizations"]["access_token"]
        except (KeyError, TypeError):
            form.dojo_token.data = "Error getting token"

    last_block = tor_request("https://api.oxt.me/lastblock")
    if last_block == "ConnectionError":
        last_block = " - "
        progress = "unknown"
    else:
        try:
            if status["blocks"]:
                last_block = last_block.json()
                progress = float(status["blocks"]) / float(
                    last_block["data"][0]["height"])
            else:
                progress = "unknown"
        except (KeyError, TypeError):
            progress = "unknown"

    return render_template(
        "dojo.html",
        title="Dojo Config and Check",
        form=form,
        at=at,
        user_info=user_info,
        status=status,
        last_block=last_block,
        progress=progress,
    )
Ejemplo n.º 2
0
def get_started():
    from thewarden.pricing_engine.pricing import api_keys_class
    api_keys_json = api_keys_class.loader()
    aa_apikey = api_keys_json['alphavantage']['api_key']
    return render_template("welcome.html",
                           title="Welcome",
                           aa_apikey=aa_apikey)
Ejemplo n.º 3
0
def apikeys_management():
    from thewarden.pricing_engine.pricing import api_keys_class
    api_keys_json = api_keys_class.loader()
    form = ApiKeysForm()

    if request.method == "GET":
        form.dojo_key.data = api_keys_json['dojo']['api_key']
        form.dojo_onion.data = api_keys_json['dojo']['onion']
        form.bitmex_key.data = api_keys_json['bitmex']['api_key']
        form.bitmex_secret.data = api_keys_json['bitmex']['api_secret']
        form.aa_key.data = api_keys_json['alphavantage']['api_key']

        return render_template("apikeys_management.html",
                               title="API Keys Management",
                               form=form)

    if request.method == "POST":
        api_keys_json['dojo']['api_key'] = form.dojo_key.data
        api_keys_json['dojo']['onion'] = form.dojo_onion.data
        api_keys_json['bitmex']['api_key'] = form.bitmex_key.data
        api_keys_json['bitmex']['api_secret'] = form.bitmex_secret.data
        api_keys_json['alphavantage']['api_key'] = form.aa_key.data
        api_keys_class.saver(api_keys_json)

        flash("Keys Updated Successfully", "success")
        return render_template("apikeys_management.html",
                               title="API Keys Management",
                               form=form)
Ejemplo n.º 4
0
def bitmex_transactions():
    logging.info(f"Started Bitmex Transaction method")
    meta = {}
    testnet = False
    transactions = {}
    transactions["error"] = ""
    meta["success"] = False
    meta["n_txs"] = 0
    from thewarden.pricing_engine.pricing import api_keys_class
    api_keys_json = api_keys_class.loader()
    bitmex_credentials = api_keys_json['bitmex']
    if ("api_key" in bitmex_credentials) and ("api_secret"
                                              in bitmex_credentials):
        data = bitmex_orders(bitmex_credentials['api_key'],
                             bitmex_credentials['api_secret'], testnet)
        try:
            # Create a DataFrame to return
            data_df = pd.DataFrame.from_dict(data[0])
            data_df['fiat_fee'] = data_df['execComm'] * data_df[
                'lastPx'] / 100000000
            # Check if the transactions are included in the database already
            data_df['exists'] = data_df['execID'].apply(check_trade_included)
            transactions["data"] = data_df
            meta["success"] = "success"
        except ValueError:
            meta["success"] = "error"

    return render_template("bitmex_transactions.html",
                           title="Bitmex Transactions",
                           meta=meta,
                           transactions=transactions,
                           bitmex_credentials=bitmex_credentials)
Ejemplo n.º 5
0
def dojo_get_settings(force=False):
    # Get and test settings. If not working get a new at
    logging.info("Getting Dojo settings")
    # check if dojo_settings are already stored
    from thewarden.pricing_engine.pricing import api_keys_class
    api_keys_json = api_keys_class.loader()
    onion_address = api_keys_json['dojo']['onion']
    api_key = api_keys_json['dojo']['api_key']
    at = dojo_auth()
    try:
        logging.info("Trying to get token")
        logging.info(f"Received back from auth: {at}")
        token = at["authorizations"]["access_token"]
        api_keys_json['dojo']['token'] = token
        api_keys_class.saver(api_keys_json)
    except (KeyError, TypeError) as e:
        logging.warn(f"Unable to get Dojo Token: {e}")
        logging.warn(f"Received back from auth: {at}, setting token to error.")
        token = "error"
        api_keys_json['dojo']['token'] = token
        api_keys_class.saver(api_keys_json)
        logging.error(f"Error while getting token.")

    logging.info(f"Current token: {token}")

    return (api_keys_json['dojo'])
Ejemplo n.º 6
0
def bitmex_gethistory(ticker):
    # Gets historical prices from bitmex
    # Saves to folder
    from bitmex import bitmex
    testnet = False
    logging.info(f"[Bitmex] Trying Bitmex for {ticker}")
    from thewarden.pricing_engine.pricing import api_keys_class
    api_keys_json = api_keys_class.loader()
    bitmex_credentials = api_keys_json['bitmex']

    if ("api_key" in bitmex_credentials) and ("api_secret"
                                              in bitmex_credentials):
        try:
            mex = bitmex(test=testnet,
                         api_key=bitmex_credentials['api_key'],
                         api_secret=bitmex_credentials['api_secret'])
            # Need to paginate results here to get all the history
            # Bitmex API end point limits 750 results per call
            start_bin = 0
            resp = (mex.Trade.Trade_getBucketed(symbol=ticker,
                                                binSize="1d",
                                                count=750,
                                                start=start_bin).result())[0]
            df = pd.DataFrame(resp)
            last_update = df['timestamp'].iloc[-1]

            # If last_update is older than 3 days ago, keep building.
            while last_update < (datetime.now(timezone.utc) -
                                 timedelta(days=3)):
                start_bin += 750
                resp = (mex.Trade.Trade_getBucketed(
                    symbol=ticker, binSize="1d", count=750,
                    start=start_bin).result())[0]
                df = df.append(resp)
                last_update = df['timestamp'].iloc[-1]
                # To avoid an infinite loop, check if start_bin
                # is higher than 10000 and stop (i.e. 30+yrs of data)
                if start_bin > 10000:
                    logging.error(
                        "[Bitmex] Something went wrong on price grab loop. Forced quit of loop."
                    )
                    break

            logging.info(f"[Bitmex] Success. Downloaded data for {ticker}")
            return (df)

        except Exception as e:
            logging.error(f"[Bitmex] error: {e}")
            return ("error")

    else:
        logging.warning(f"[Bitmex] No Credentials Found")
        return ('error')
Ejemplo n.º 7
0
def before_request():
    # Before any request at main, check if API Keys are set
    # But only if user is logged in.
    exclude_list = ["main.get_started", "main.importcsv", "main.csvtemplate"]
    if request.endpoint not in exclude_list:
        if current_user.is_authenticated:
            from thewarden.pricing_engine.pricing import api_keys_class
            api_keys_json = api_keys_class.loader()
            aa_apikey = api_keys_json['alphavantage']['api_key']
            if aa_apikey is None:
                logging.error("NO AA API KEY FOUND!")
                return render_template("welcome.html", title="Welcome")
            transactions = Trades.query.filter_by(
                user_id=current_user.username)
            if transactions.count() == 0:
                return redirect(url_for("main.get_started"))
Ejemplo n.º 8
0
def dojo_auth():
    # Receives authentication token back from Dojo
    # https://github.com/Samourai-Wallet/samourai-dojo/blob/develop/doc/POST_auth_login.md
    # POST /v2/auth/login

    # On Success, returns:                  On Invalid token:
    # {                                     {'status': 'error',
    #   "authorizations": {                 'error': 'Invalid JSON Web Token'}
    #     "access_token": <token>,
    #     "refresh_token": <token>
    #   }
    # }

    # On API Key error, returns:            On Connection error, returns:
    # {                                     {
    #   "status": "error",                      "status": "error",
    #   "error": "Invalid API key"              "error": "Connection Error"
    # }                                     }
    # SET Default timeout to get a token. Too low timeouts could be a problem

    TIME_OUT = 20
    logging.info("Starting DOJO Auth")
    # Check if variables are saved
    from thewarden.pricing_engine.pricing import api_keys_class
    api_keys_json = api_keys_class.loader()
    onion_address = api_keys_json['dojo']['onion']
    APIKey = api_keys_json['dojo']['api_key']
    token = api_keys_json['dojo']['token']
    if (onion_address is None) or (APIKey is None):
        logging.info("DOJO Auth: No Onion Address or API Key")
        auth_response = {"status": "error", "error": "missing config"}
        try:
            api_keys_json['dojo']['token'] = 'error'
        except TypeError:
            logging.info("Dojo_AUTH: Dojo Settings not found at database")
        return auth_response

    # Try to get the token
    url = "http://" + onion_address + "/v2/auth/login"
    session = requests.session()
    session.proxies = {
        "http": "socks5h://127.0.0.1:9150",
        "https": "socks5h://127.0.0.1:9150",
    }
    post_fields = {"apikey": APIKey}
    try:
        logging.info("DOJO AUTH: Trying to get authorization")
        auth_response = session.post(url, post_fields, timeout=TIME_OUT).json()
        token = auth_response['authorizations']['access_token']
    except (requests.exceptions.ConnectionError,
            requests.exceptions.InvalidURL, KeyError,
            requests.exceptions.ReadTimeout, requests.exceptions.InvalidSchema,
            UnicodeError, requests.exceptions.InvalidSchema) as e:
        logging.info(f"DOJO AUTH: Error: {e}")
        auth_response = {"status": "error", "error": f"Error: {e}"}
        token = 'error'
    # Store for this session in Global Variable
    api_keys_json['dojo']['token'] = token
    api_keys_class.saver(api_keys_json)
    logging.info(f"DOJO Settings updated")
    return (auth_response)