diff --git a/openbb_terminal/cryptocurrency/onchain/ethplorer_model.py b/openbb_terminal/cryptocurrency/onchain/ethplorer_model.py index 51ffe4a68fe..3ee0ba612e8 100644 --- a/openbb_terminal/cryptocurrency/onchain/ethplorer_model.py +++ b/openbb_terminal/cryptocurrency/onchain/ethplorer_model.py @@ -209,10 +209,9 @@ def get_address_info(address: str) -> pd.DataFrame: """ response = make_request("getAddressInfo", address) - + tokens = [] if "tokens" in response: tokens = response.pop("tokens") - for token in tokens: token_info = token.pop("tokenInfo") token.update( @@ -224,7 +223,7 @@ def get_address_info(address: str) -> pd.DataFrame: / (10 ** int(token_info.get("decimals"))), } ) - else: + elif "token_info" in response: token_info = response.get("tokenInfo") or {} tokens = [ { @@ -236,7 +235,7 @@ def get_address_info(address: str) -> pd.DataFrame: } ] - eth = response["ETH"] or {} + eth = response.get("ETH") or {} eth_balance = eth.get("balance") eth_row = [ "Ethereum", @@ -244,17 +243,16 @@ def get_address_info(address: str) -> pd.DataFrame: "0x0000000000000000000000000000000000000000", eth_balance, ] - cols = [ "tokenName", "tokenSymbol", "tokenAddress", "balance", ] - df = pd.DataFrame(tokens)[cols] + df = pd.DataFrame(tokens) eth_row_df = pd.DataFrame([eth_row], columns=cols) df = pd.concat([eth_row_df, df], ignore_index=True) - df = df[df["tokenName"].notna()] + df = df[df["tokenName"].notna()][cols] create_df_index(df, "index") return df diff --git a/tests/openbb_terminal/cryptocurrency/onchain/cassettes/test_ethplorer_model/test_get_address_info_no_token_scenario.yaml b/tests/openbb_terminal/cryptocurrency/onchain/cassettes/test_ethplorer_model/test_get_address_info_no_token_scenario.yaml new file mode 100644 index 00000000000..7fe75f42da9 --- /dev/null +++ b/tests/openbb_terminal/cryptocurrency/onchain/cassettes/test_ethplorer_model/test_get_address_info_no_token_scenario.yaml @@ -0,0 +1,34 @@ +interactions: +- request: + body: null + headers: + Accept: + - "*/*" + Accept-Encoding: + - gzip, deflate + Connection: + - keep-alive + User-Agent: + - python-requests/2.28.1 + method: GET + uri: https://api.ethplorer.io/getAddressInfo/0xb274827BCbB6c06527DDe24c1BC7147715b49415?apiKey=MOCK_API_KEY + response: + body: + string: '{"address": "0xb274827BCbB6c06527DDe24c1BC7147715b49415", "ETH": {"balance": 1}, "countTxs": 1}' + headers: + Access-Control-Allow-Credentials: + - "true" + Access-Control-Allow-Origin: + - "*" + Connection: + - keep-alive + Content-Type: + - application/json + Date: + - Sun, 17 Jul 2022 03:04:58 GMT + Transfer-Encoding: + - chunked + status: + code: 200 + message: OK +version: 1 diff --git a/tests/openbb_terminal/cryptocurrency/onchain/test_ethplorer_model.py b/tests/openbb_terminal/cryptocurrency/onchain/test_ethplorer_model.py index 27621bac0f0..8296825dd4e 100644 --- a/tests/openbb_terminal/cryptocurrency/onchain/test_ethplorer_model.py +++ b/tests/openbb_terminal/cryptocurrency/onchain/test_ethplorer_model.py @@ -67,3 +67,13 @@ def test_get_tx_info(): assert isinstance(df, pd.DataFrame) assert not df.empty + + +@pytest.mark.vcr +def test_get_address_info_no_token_scenario(): + """Test the get_address_info scenario where the address returns no token data and only ETH balance.""" + df = ethplorer_model.get_address_info( + address="0xb274827BCbB6c06527DDe24c1BC7147715b49415", + ) + assert isinstance(df, pd.DataFrame) + assert not df.empty