mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-11-02 07:23:25 +00:00
* implement user and change is guest * replace User methods * move remaining functions to user * fix bug * portfolio test * fix tests * fix tests * uncomment skip * ruff * start credentials model implementation * change deault structure * refacto quandl * change default struct and file names * bug * start using global user * add comments * move update flair * change cfg references in keys model * reddit & fmp * refactore additional keys * remaing config keys * remove unsude imports and fix hub keys * ruff * fix line too long * unblock unittests * fix keys model tests * remove os patch * fix account controller tests * fix account controller tests * fix account model * account fixed * fix user tests * User Model * User Model * User Model * Move session folder * User Model : grouping the models * User Model : fix bugs * Config Plot + bug fix * User Model : Refactoring plots * Feature flags * Remove dup * clean comments a fix some vars * fix feature flags controller * refactor obbff from settings controller * refactor a bunch of obbff variables * fix bug blocking terminal * fix integration tests * refactor controllers with obbff * refactor remaining ffs * remove comments * log not started * mispelled variable * set current user on local model * User Model : moving files + updating * User Preferences * User : Refactor is_guest function + bug fix * User : refactor is_guest function * Fix conftest.py * remove lost obbffs * typo on fileoverwrite * Plot config: move variables and start plot dpi * User : tests * refactor plot dpi references * remove plot dpi imports * refactor some config terminal leftover * /account/sync : fix bug * remove configs reload * start path refactor * installation paths vs user paths * remove cfg_plot * Tests : User Refactoring * user prefix only for preferences * user prefix only for preferences * move user paths * remove comment * update apply remote configs * fix tz not updating * save some copys * small fix * create interface for credentials and preferences * wrong docstrings fixed * ruff core * fix some pref setting * login is better * ruff * ruff * remove Literal * add small comment * Tests : User Refactoring * Credentials Model : add DataBento * Fix helper_funcs * Fixing code * Tests : fixing * User Model : freezing the models * Fixed Income + Databento * Tests : fixing keys * black * ruff * Tests : user model * Tests : user model * Fix linting * Fix linting * Linting * Linting * Fixing code and tests * Fix tests * mypy * SDK * Clean ruff cache * Fix syntax issue * Update fred_view * Black * Linting * Update code * Update tests * Linting * Linting * Tests * Black * Linting * Linting * Mock os.listdir to avoid looking for file that doesnt exist * Mock options paths * lint * Tests : fix * Code : update * Tests + Linting * Tests * Ruff * Code : update * Tests * Code : update * Fix workflow * Code : update --------- Co-authored-by: Chavithra PARANA <chavithra@gmail.com> Co-authored-by: luqazino@gmail.com <luqazino@gmail.com> Co-authored-by: James Maslek <jmaslek11@gmail.com>
183 lines
5.2 KiB
Python
183 lines
5.2 KiB
Python
"""Coinbase helpers model"""
|
|
__docformat__ = "numpy"
|
|
|
|
import argparse
|
|
import base64
|
|
import binascii
|
|
import hashlib
|
|
import hmac
|
|
import logging
|
|
import time
|
|
from typing import Any, Optional, Union
|
|
|
|
from requests.auth import AuthBase
|
|
|
|
from openbb_terminal.core.session.current_user import get_current_user
|
|
from openbb_terminal.helper_funcs import request
|
|
from openbb_terminal.rich_config import console
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class CoinbaseProAuth(AuthBase):
|
|
"""Authorize CoinbasePro requests. Source: https://docs.pro.coinbase.com/?python#signing-a-message"""
|
|
|
|
def __init__(self, api_key, secret_key, passphrase):
|
|
self.api_key = api_key
|
|
self.secret_key = secret_key
|
|
self.passphrase = passphrase
|
|
|
|
def __call__(self, request_coinbase):
|
|
timestamp = str(time.time())
|
|
message = (
|
|
timestamp
|
|
+ request_coinbase.method
|
|
+ request_coinbase.path_url
|
|
+ (request_coinbase.body or "")
|
|
)
|
|
message = message.encode("ascii")
|
|
|
|
try:
|
|
hmac_key = base64.b64decode(self.secret_key)
|
|
signature = hmac.new(hmac_key, message, hashlib.sha256)
|
|
signature_b64 = base64.b64encode(signature.digest())
|
|
except binascii.Error as e:
|
|
logger.exception(str(e))
|
|
signature_b64 = ""
|
|
|
|
request_coinbase.headers.update(
|
|
{
|
|
"CB-ACCESS-SIGN": signature_b64,
|
|
"CB-ACCESS-TIMESTAMP": timestamp,
|
|
"CB-ACCESS-KEY": self.api_key,
|
|
"CB-ACCESS-PASSPHRASE": self.passphrase,
|
|
"Content-Type": "application/json",
|
|
}
|
|
)
|
|
return request_coinbase
|
|
|
|
|
|
class CoinbaseRequestException(Exception):
|
|
"""Coinbase Request Exception object"""
|
|
|
|
def __init__(self, message: str):
|
|
super().__init__(message)
|
|
self.message = message
|
|
|
|
def __str__(self) -> str:
|
|
return f"CoinbaseRequestException: {self.message}"
|
|
|
|
|
|
class CoinbaseApiException(Exception):
|
|
"""Coinbase API Exception object"""
|
|
|
|
def __init__(self, message: str):
|
|
super().__init__(message)
|
|
self.message = message
|
|
|
|
def __str__(self) -> str:
|
|
return f"CoinbaseApiException: {self.message}"
|
|
|
|
|
|
def check_validity_of_product(product_id: str) -> str:
|
|
"""Helper method that checks if provided product_id exists. It's a pair of coins in format COIN-COIN.
|
|
If product exists it return it, in other case it raise an error. [Source: Coinbase]
|
|
|
|
Parameters
|
|
----------
|
|
product_id: str
|
|
Trading pair of coins on Coinbase e.g ETH-USDT or UNI-ETH
|
|
|
|
Returns
|
|
-------
|
|
str
|
|
pair of coins in format COIN-COIN
|
|
"""
|
|
|
|
products = [pair["id"] for pair in make_coinbase_request("/products")]
|
|
if product_id.upper() not in products:
|
|
raise argparse.ArgumentTypeError(
|
|
f"You provided wrong pair of coins {product_id}. "
|
|
f"It should be provided as a pair in format COIN-COIN e.g UNI-USD"
|
|
)
|
|
return product_id.upper()
|
|
|
|
|
|
def make_coinbase_request(
|
|
endpoint, params: Optional[dict] = None, auth: Optional[Any] = None
|
|
) -> dict:
|
|
"""Request handler for Coinbase Pro Api. Prepare a request url, params and payload and call endpoint.
|
|
[Source: Coinbase]
|
|
|
|
Parameters
|
|
----------
|
|
endpoint: str
|
|
Endpoint path e.g /products
|
|
params: dict
|
|
Parameter dedicated for given endpoint
|
|
auth: any
|
|
Api credentials for purpose of using endpoints that needs authentication
|
|
|
|
Returns
|
|
-------
|
|
dict
|
|
response from Coinbase Pro Api
|
|
"""
|
|
|
|
url = "https://api.pro.coinbase.com"
|
|
response = request(url + endpoint, params=params, auth=auth)
|
|
|
|
if not 200 <= response.status_code < 300:
|
|
raise CoinbaseApiException(f"Invalid Authentication: {response.text}")
|
|
try:
|
|
return response.json()
|
|
except ValueError as e:
|
|
logger.exception(str(e))
|
|
raise CoinbaseRequestException(f"Invalid Response: {response.text}") from e
|
|
|
|
|
|
def _get_account_coin_dict() -> dict:
|
|
"""Helper method that returns dictionary with all symbols and account ids in dictionary format. [Source: Coinbase]
|
|
|
|
Returns
|
|
-------
|
|
dict:
|
|
Your accounts in coinbase
|
|
{'1INCH': '0c29b708-d73b-4e1c-a58c-9c261cb4bedb', 'AAVE': '0712af66-c069-45b5-84ae-7b2347c2fd24', ..}
|
|
|
|
"""
|
|
current_user = get_current_user()
|
|
auth = CoinbaseProAuth(
|
|
current_user.credentials.API_COINBASE_KEY,
|
|
current_user.credentials.API_COINBASE_SECRET,
|
|
current_user.credentials.API_COINBASE_PASS_PHRASE,
|
|
)
|
|
accounts = make_coinbase_request("/accounts", auth=auth)
|
|
return {acc["currency"]: acc["id"] for acc in accounts}
|
|
|
|
|
|
def _check_account_validity(account: str) -> Union[str, Any]:
|
|
"""Helper methods that checks if given account exists. [Source: Coinbase]
|
|
|
|
Parameters
|
|
----------
|
|
account: str
|
|
coin or account id
|
|
|
|
Returns
|
|
-------
|
|
Union[str, Any]
|
|
Your account id or None
|
|
"""
|
|
|
|
accounts = _get_account_coin_dict()
|
|
|
|
if account in list(accounts.keys()):
|
|
return accounts[account]
|
|
|
|
if account in list(accounts.values()):
|
|
return account
|
|
|
|
console.print("Wrong account id or coin symbol")
|
|
return None
|