[Bugfix#2094] Fix handling of balances when user has no tokens. (#2095)

* Add edge case handlers for crypto balance. Resolves OpenBB-finance/OpenBBTerminal#2094

* Clear print statement.

* Run previous df generation.

* Run pure row merge.

* Fixing spacing.

* Adding test and fixing previous test failures.

* Clear comment.

* Adding vcr test.

* revert to old poetry.lock

Co-authored-by: DidierRLopes <dro.lopes@campus.fct.unl.pt>
This commit is contained in:
Jalil F
2022-07-17 08:33:16 -04:00
committed by GitHub
parent c5c2f3f8d4
commit 96ed08daaa
3 changed files with 49 additions and 7 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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