Skip to content

Commit

Permalink
cast str to float safely | v0.3 (#111)
Browse files Browse the repository at this point in the history
* test: all green w/ new binary

* increment pyproject version for release

* update changelog

* feat: safely cast string fields in vpool all_pools

* incremement minor version
  • Loading branch information
Unique-Divine authored Aug 25, 2022
1 parent 7d39001 commit 816f824
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
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)
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

0 comments on commit 816f824

Please sign in to comment.