From 3bf9221135751a0490c48f99b7bf03fab9091904 Mon Sep 17 00:00:00 2001 From: "[Luis Rodrigues]" Date: Sun, 26 May 2024 00:50:07 +0100 Subject: [PATCH] adicionada funcao 'consultar_preco_token()' --- coingecko_api.py | 70 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/coingecko_api.py b/coingecko_api.py index 87edfe3..968b78b 100644 --- a/coingecko_api.py +++ b/coingecko_api.py @@ -49,7 +49,7 @@ def consultar_preco(id_criptomoeda: str = '', if not isinstance(vs_currency, str): raise TypeError('\'vs_currency\' não é do tipo \'str\'') if vs_currency == '': - raise ValueError('') + raise ValueError('\'vs_currency\' tem de ser definido') if not isinstance(flag_include_market_cap, bool): flag_include_market_cap = False if not isinstance(flag_include_24hr_vol, bool): @@ -92,9 +92,71 @@ def obter_lista_moedas_vs_currencies() -> tuple: resposta = requests.get(url_pedido, headers=headers) return resposta.status_code, resposta.json() # debug (decomentar linhas seguintes para testar funcao) -codigo, dados = obter_lista_moedas_vs_currencies() -for item in dados: - print(item) +# codigo, dados = obter_lista_moedas_vs_currencies() +# for item in dados: +# print(item) + +# https://docs.coingecko.com/v3.0.1/reference/simple-token-price +# consultar preco de token usando o endereco de contrato do token +def consultar_preco_token( + id_platform: str = '', + contract_address: str = '', + vs_currency: str = '', + flag_include_market_cap: bool = False, + flag_include_24hr_vol: bool = False, + flag_include_24hr_change: bool = False, + flag_include_last_updated_at: bool = False, + precisao: int | str = 'full' +) -> tuple: + # validacao de parametros + if not isinstance(id_platform, str): + raise TypeError('\'id_platform\' tem de ser do tipo \'str\'') + if id_platform == '': + raise ValueError('\'id_platform\' tem de ser definido') + if not isinstance(contract_address, str): + raise TypeError('\'contract_address\' tem de ser do tipo \'str\'') + if contract_address == '': + raise ValueError('\'contract_address\' tem de ser definido') + if not isinstance(vs_currency, str): + raise TypeError('\'vs_currency\' tem de ser do tipo \'str\'') + if vs_currency == '': + raise ValueError('\'vs_currency\' tem de ser definido') + if not isinstance(flag_include_market_cap, bool): + flag_include_market_cap = False + if not isinstance(flag_include_24hr_vol, bool): + flag_include_24hr_vol = False + if not isinstance(flag_include_24hr_change, bool): + flag_include_24hr_change = False + if not isinstance(flag_include_last_updated_at, bool): + flag_include_last_updated_at = False + if (not isinstance(precisao, int)) and (not isinstance(precisao, str)): + raise TypeError('\'precisao\' tem de ser do tipo \'int\' ou \'str\'') + if isinstance(precisao, int): + if precisao < 0: + precisao = 0 + if precisao > 18: + precisao = 18 + precisao = str(precisao) + + endpoint_api = 'simple/token_price/' + id_platform + \ + '?contract_addresses=' + contract_address + \ + '&vs_currencies=' + vs_currency + \ + '&include_market_cap=' + str(flag_include_market_cap).lower() + \ + '&include_24hr_vol=' + str(flag_include_24hr_vol).lower() + \ + '&include_24hr_change=' + str(flag_include_24hr_change).lower() + \ + '&include_last_updated_at=' + str(flag_include_last_updated_at).lower() + \ + '&precision=' + precisao + url_pedido = url_raiz_API + endpoint_api + resposta = requests.get(url_pedido, headers=headers) + return resposta.status_code, resposta.json() +# debug (decomentar linhas seguintes para testar funcao) +# id_plataforma = 'ethereum' +# endereco_contracto = '0x2260fac5e5542a773aa44fbcfedf7c193bc2c599' +# codigo, dados = consultar_preco_token(id_plataforma, endereco_contracto, 'eur') +# print(f'codigo: {codigo}') +# if codigo == 200: +# for item in dados: +# print(f'{dados[item]}') # fazer consulta de dados OHLC def consulta_ohcl(criptomoeda: str = '',