Skip to content

Commit

Permalink
Merge pull request #23 from NibiruChain/jc/pf
Browse files Browse the repository at this point in the history
Added pricefeed module
  • Loading branch information
onikonychev authored Jul 25, 2022
2 parents 9c8210f + 930dd3e commit b5ae936
Show file tree
Hide file tree
Showing 17 changed files with 262 additions and 85 deletions.
4 changes: 2 additions & 2 deletions nibiru/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from .clients import Dex as DexClient
from .clients import Perp as PerpClient
from .clients import Pricefeed as PricefeedClient
from .clients import VPool as VPoolClient
from .exceptions import NotFoundError
from .network import Network
Expand All @@ -28,7 +29,6 @@
from .proto.epochs import query_pb2_grpc as epochs_query
from .proto.incentivization.v1 import incentivization_pb2_grpc as incentivization_query
from .proto.lockup.v1 import query_pb2_grpc as lockup_query
from .proto.pricefeed import query_pb2_grpc as pricefeed_query
from .proto.stablecoin import query_pb2_grpc as stablecoin_query

DEFAULT_TIMEOUTHEIGHT_SYNC_INTERVAL = 10 # seconds
Expand Down Expand Up @@ -91,7 +91,7 @@ def __init__(
)
# Query services
self.dex = DexClient(self.exchange_channel)
self.stubPricefeed = pricefeed_query.QueryStub(self.exchange_channel)
self.pricefeed = PricefeedClient(self.exchange_channel)
self.perp = PerpClient(self.exchange_channel)
self.stubLockup = lockup_query.QueryStub(self.exchange_channel)
self.stubIncentivization = incentivization_query.QueryStub(self.exchange_channel)
Expand Down
1 change: 1 addition & 0 deletions nibiru/clients/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .dex import Dex # noqa
from .perp import Perp # noqa
from .pricefeed import Pricefeed # noqa
from .vpool import VPool # noqa
45 changes: 45 additions & 0 deletions nibiru/clients/pricefeed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from grpc import Channel

from nibiru.proto.pricefeed import query_pb2 as pf_type
from nibiru.proto.pricefeed import query_pb2_grpc as pf_query

from .util import deserialize


class Pricefeed:
"""
Pricefeed allows to query the endpoints made available by the Nibiru Chain's Pricefeed module.
"""

def __init__(self, channel: Channel):
self.api = pf_query.QueryStub(channel)

def params(self):
req = pf_type.QueryParamsRequest()
return deserialize(self.api.QueryParams(req))

def price(self, pair_id: str):
req = pf_type.QueryPriceRequest(
pair_id=pair_id,
)
return deserialize(self.api.QueryPrice(req))

def prices(self):
req = pf_type.QueryPricesRequest()
return deserialize(self.api.QueryPrices(req))

def raw_prices(self, pair_id: str):
req = pf_type.QueryRawPricesRequest(
pair_id=pair_id,
)
return deserialize(self.api.QueryRawPrices(req))

def oracles(self, pair_id: str):
req = pf_type.QueryOraclesRequest(
pair_id=pair_id,
)
return deserialize(self.api.QueryOracles(req))

def markets(self):
req = pf_type.QueryMarketsRequest()
return deserialize(self.api.QueryMarkets(req))
2 changes: 1 addition & 1 deletion nibiru/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Direction(Enum):
@dataclass
class PoolAsset:
token: coin_pb.Coin
weight: str
weight: int


class TxConfig:
Expand Down
3 changes: 2 additions & 1 deletion nibiru/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from google.protobuf import any_pb2

from .composers import Dex, Perp
from .composers import Dex, Perp, Pricefeed
from .proto.cosmos.authz.v1beta1 import tx_pb2 as cosmos_authz_tx_pb
from .proto.cosmos.bank.v1beta1 import tx_pb2 as cosmos_bank_tx_pb
from .proto.cosmos.base.v1beta1 import coin_pb2 as cosmos_base_coin_pb
Expand All @@ -13,6 +13,7 @@
class Composer:
dex = Dex
perp = Perp
pricefeed = Pricefeed

@staticmethod
def coin(amount: float, denom: str):
Expand Down
1 change: 1 addition & 0 deletions nibiru/composers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from .dex import Dex # noqa
from .perp import Perp # noqa
from .pricefeed import Pricefeed # noqa
4 changes: 2 additions & 2 deletions nibiru/composers/dex.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

class Dex:
@staticmethod
def create_pool(creator: str, swap_fee: float, exit_fee: float, assets: List[PoolAsset]):
pool_assets = [pool_tx_pb.PoolAsset(token=a.token, weight=a.weight) for a in assets]
def create_pool(creator: str, assets: List[PoolAsset], swap_fee: float = 0, exit_fee: float = 0):
pool_assets = [pool_tx_pb.PoolAsset(token=a.token, weight=str(a.weight)) for a in assets]
swap_fee_dec = float_to_sdkdec(swap_fee)
exit_fee_dec = float_to_sdkdec(exit_fee)
return dex_tx_pb.MsgCreatePool(
Expand Down
17 changes: 17 additions & 0 deletions nibiru/composers/pricefeed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from datetime import datetime

from nibiru import utils
from nibiru.proto.pricefeed import tx_pb2 as pb


class Pricefeed:
@staticmethod
def post_price(oracle: str, token0: str, token1: str, price: float, expiry: datetime):
price_dec = utils.float_to_sdkdec(price)
return pb.MsgPostPrice(
oracle=oracle,
token0=token0,
token1=token1,
price=price_dec,
expiry=utils.toTsPb(expiry),
)
60 changes: 40 additions & 20 deletions nibiru/proto/perp/v1/tx_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 40 additions & 3 deletions nibiru/proto/perp/v1/tx_pb2.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,26 @@ class MsgRemoveMarginResponse(google.protobuf.message.Message):
DESCRIPTOR: google.protobuf.descriptor.Descriptor
MARGIN_OUT_FIELD_NUMBER: builtins.int
FUNDING_PAYMENT_FIELD_NUMBER: builtins.int
POSITION_FIELD_NUMBER: builtins.int
@property
def margin_out(self) -> cosmos.base.v1beta1.coin_pb2.Coin:
"""MarginOut: tokens transferred back to the trader"""
"""tokens transferred back to the trader"""
pass
funding_payment: typing.Text
"""the funding payment applied on this position interaction"""

@property
def position(self) -> perp.v1.state_pb2.Position:
"""The resulting position"""
pass
def __init__(self,
*,
margin_out: typing.Optional[cosmos.base.v1beta1.coin_pb2.Coin] = ...,
funding_payment: typing.Text = ...,
position: typing.Optional[perp.v1.state_pb2.Position] = ...,
) -> None: ...
def HasField(self, field_name: typing_extensions.Literal["margin_out",b"margin_out"]) -> builtins.bool: ...
def ClearField(self, field_name: typing_extensions.Literal["funding_payment",b"funding_payment","margin_out",b"margin_out"]) -> None: ...
def HasField(self, field_name: typing_extensions.Literal["margin_out",b"margin_out","position",b"position"]) -> builtins.bool: ...
def ClearField(self, field_name: typing_extensions.Literal["funding_payment",b"funding_payment","margin_out",b"margin_out","position",b"position"]) -> None: ...
global___MsgRemoveMarginResponse = MsgRemoveMarginResponse

class MsgAddMargin(google.protobuf.message.Message):
Expand Down Expand Up @@ -233,6 +241,35 @@ global___MsgClosePosition = MsgClosePosition

class MsgClosePositionResponse(google.protobuf.message.Message):
DESCRIPTOR: google.protobuf.descriptor.Descriptor
EXCHANGED_NOTIONAL_VALUE_FIELD_NUMBER: builtins.int
EXCHANGED_POSITION_SIZE_FIELD_NUMBER: builtins.int
FUNDING_PAYMENT_FIELD_NUMBER: builtins.int
REALIZED_PNL_FIELD_NUMBER: builtins.int
MARGIN_TO_TRADER_FIELD_NUMBER: builtins.int
exchanged_notional_value: typing.Text
"""The amount of quote assets exchanged."""

exchanged_position_size: typing.Text
"""The amount of base assets exchanged."""

funding_payment: typing.Text
"""The funding payment applied on this position change, measured in quote units."""

realized_pnl: typing.Text
"""The amount of PnL realized on this position changed, measured in quote units."""

margin_to_trader: typing.Text
"""The amount of margin the trader receives after closing the position, from the vault.
Should never be negative.
"""

def __init__(self,
*,
exchanged_notional_value: typing.Text = ...,
exchanged_position_size: typing.Text = ...,
funding_payment: typing.Text = ...,
realized_pnl: typing.Text = ...,
margin_to_trader: typing.Text = ...,
) -> None: ...
def ClearField(self, field_name: typing_extensions.Literal["exchanged_notional_value",b"exchanged_notional_value","exchanged_position_size",b"exchanged_position_size","funding_payment",b"funding_payment","margin_to_trader",b"margin_to_trader","realized_pnl",b"realized_pnl"]) -> None: ...
global___MsgClosePositionResponse = MsgClosePositionResponse
Loading

0 comments on commit b5ae936

Please sign in to comment.