Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cast str to float safely | v0.3 #111

Merged
merged 6 commits into from
Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions nibiru/query_clients/vpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def reserve_assets(self, pair: str):
def all_pools(self):
req = vpool_type.QueryAllPoolsRequest()
resp = self.query(self.api.AllPools, req)
for _, prices in enumerate(resp["prices"]):
prices["index_price"] = cast_str_to_float_safely(prices["index_price"])
prices["twap_mark"] = cast_str_to_float_safely(prices["twap_mark"])
return resp

def base_asset_price(self, pair: str, direction: Direction, base_asset_amount: str):
Expand All @@ -35,3 +38,11 @@ def base_asset_price(self, pair: str, direction: Direction, base_asset_amount: s
pair=pair, direction=dir_pb, base_asset_amount=base_asset_amount
)
return self.query(self.api.BaseAssetPrice, req)


def cast_str_to_float_safely(number_str: str) -> float:
try:
number = float(number_str)
except:
number = float(0)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need numpy np.nan there?

I was doing this just now:

        # The golang application is returning index price and twap price as string since v0.14.0
        # This is because those fields can be null, which leads to empty strings returned which cannot be easily
        # deserialized automatically by the deserialized function inside the self.query call above.
        # For this reason we do this manual update there

        for _, price in enumerate(resp["prices"]):

            for field in ["twap_mark", "index_price"]:
                value = price[field]
                if isinstance(value, str) and value == "":
                    value = np.nan
                else:
                    value = float(value)

                price[field] = value

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually it's better to not have all the numpy dependency and reqs for the docker build we make

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had the same idea. I wasn't sure if we should add the numpy dependency

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And both "" and None are invalid inputs to the float() function, so it handles both cases

return number
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nibiru"
version = "0.2.0"
version = "0.3.0"
description = "Python SDK for interacting with Nibiru."
authors = ["Nibiru Chain <[email protected]>"]
license = "MIT"
Expand Down
4 changes: 2 additions & 2 deletions tests/vpool_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ def test_query_vpool_all_pools(agent: nibiru.Sdk):

vpool_prices = all_vpool_prices[0]
assert isinstance(vpool_prices["block_number"], int), "block_number"
assert isinstance(vpool_prices["index_price"], str), "index_price"
assert isinstance(vpool_prices["index_price"], float), "index_price"
assert isinstance(vpool_prices["mark_price"], float), "mark_price"
assert isinstance(vpool_prices["swap_invariant"], int), "swap_invariant"
assert isinstance(vpool_prices["twap_mark"], str), "twap_mark"
assert isinstance(vpool_prices["twap_mark"], float), "twap_mark"
assert isinstance(vpool_prices["pair"], str), "pair"
tests.LOGGER.info(f"vpool_prices: {pprint.pformat(vpool_prices, indent=3)}")

Expand Down