Skip to content

Commit

Permalink
refactor: got rid of sender parameter in messages
Browse files Browse the repository at this point in the history
  • Loading branch information
onikonychev committed Sep 26, 2023
1 parent 7c6deab commit 505f71c
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 155 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ print(client.query.get_bank_balances(client.address))
```python
output = client.tx.execute_msgs(
Msg.perp.open_position(
sender=client.address,
pair=pair,
is_long=True,
quote_asset_amount=10,
Expand Down
1 change: 0 additions & 1 deletion examples/2- My first transaction.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@
"source": [
"output = client.tx.execute_msgs(\n",
" Msg.perp.open_position(\n",
" sender=client.address,\n",
" pair=pair,\n",
" is_long=True,\n",
" quote_asset_amount=10,\n",
Expand Down
7 changes: 0 additions & 7 deletions examples/colab_notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
"source": [
"output = client.tx.execute_msgs(\n",
" Msg.perp.open_position(\n",
" sender=client.address,\n",
" pair=pair,\n",
" is_long=True,\n",
" quote_asset_amount=10,\n",
Expand Down Expand Up @@ -144,7 +143,6 @@
"source": [
"output = client.tx.execute_msgs(\n",
" Msg.perp.add_margin(\n",
" sender=client.address,\n",
" pair=pair,\n",
" margin=Coin(1, \"unusd\"),\n",
" )\n",
Expand All @@ -167,7 +165,6 @@
"source": [
"output = client.tx.execute_msgs(\n",
" Msg.perp.remove_margin(\n",
" sender=client.address,\n",
" pair=pair,\n",
" margin=Coin(1, \"unusd\")\n",
" )\n",
Expand All @@ -190,7 +187,6 @@
"source": [
"output = client.tx.execute_msgs(\n",
" Msg.perp.close_position(\n",
" sender=client.address,\n",
" pair=pair,\n",
" )\n",
")\n",
Expand Down Expand Up @@ -232,20 +228,17 @@
"output = client.tx.execute_msgs(\n",
" [\n",
" Msg.perp.open_position(\n",
" sender=client.address,\n",
" pair=pair,\n",
" is_long=True,\n",
" quote_asset_amount=10,\n",
" leverage=1,\n",
" base_asset_amount_limit=0,\n",
" ),\n",
" Msg.perp.add_margin(\n",
" sender=client.address,\n",
" pair=pair,\n",
" margin=Coin(1, \"unusd\"),\n",
" ),\n",
" Msg.perp.close_position(\n",
" sender=client.address,\n",
" pair=pair,\n",
" ),\n",
" ]\n",
Expand Down
12 changes: 4 additions & 8 deletions nibiru/msg/bank.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@ class MsgsBank:

@staticmethod
def send(
from_address: str,
to_address: str,
coins: Union[Coin, List[Coin]],
) -> 'MsgSend':
"""
Send tokens from one account to another
Attributes:
from_address (str): The address of the sender
to_address (str): The address of the receiver
coins (List[Coin]): The list of coins to send
Returns:
MsgSend: PythonMsg corresponding to the 'cosmos.bank.v1beta1.MsgSend' message
"""
return MsgSend(from_address=from_address, to_address=to_address, coins=coins)
return MsgSend(to_address=to_address, coins=coins)


@dataclasses.dataclass
Expand All @@ -40,16 +38,14 @@ class MsgSend(PythonMsg):
'cosmos.bank.v1beta1.MsgSend' message.
Attributes:
from_address (str): The address of the sender
to_address (str): The address of the receiver
coins (Union[Coin, List[Coin]]): The list of coins to send
"""

from_address: str
to_address: str
coins: Union[Coin, List[Coin]]

def to_pb(self) -> pb.MsgSend:
def to_pb(self, sender: str) -> pb.MsgSend:
"""
Returns the Message as protobuf object.
Expand All @@ -62,7 +58,7 @@ def to_pb(self) -> pb.MsgSend:
coins = [self.coins]

return pb.MsgSend(
from_address=self.from_address,
from_address=sender,
to_address=self.to_address,
amount=[coin._generate_proto_object() for coin in coins],
amount=[coin.to_pb() for coin in coins],
)
79 changes: 23 additions & 56 deletions nibiru/msg/perp.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ class MsgsPerp:

@staticmethod
def open_position(
sender: str,
pair: str,
is_long: bool,
quote_asset_amount: float,
Expand All @@ -41,7 +40,6 @@ def open_position(
Open a posiiton using the specified parameters.
Attributes:
sender (str): The sender address
pair (str): The token pair
is_long (bool): Determines whether to open with long or short exposure.
quote_asset_amount (float): The quote amount you want to use to buy base
Expand All @@ -51,7 +49,6 @@ def open_position(
quote.
"""
return MsgMarketOrder(
sender=sender,
pair=pair,
dir=Direction.LONG if is_long else Direction.SHORT,
quote_asset_amount=quote_asset_amount,
Expand All @@ -60,81 +57,60 @@ def open_position(
)

@staticmethod
def close_position(
sender: str,
pair: str,
) -> 'MsgClosePosition':
def close_position(pair: str) -> 'MsgClosePosition':
"""
Close the position.
Attributes:
sender (str): The sender address
pair (str): The token pair
"""
return MsgClosePosition(sender=sender, pair=pair)
return MsgClosePosition(pair=pair)

@staticmethod
def add_margin(
sender: str,
pair: str,
margin: Coin,
) -> 'MsgAddMargin':
"""
Add margin for the position (pair + trader)
Attributes:
sender (str): The trader address
pair (str): The token pair
margin (Coin): The margin to remove in a coin format
"""
return MsgAddMargin(sender=sender, pair=pair, margin=margin)
return MsgAddMargin(pair=pair, margin=margin)

@staticmethod
def remove_margin(
sender: str,
pair: str,
margin: Coin,
) -> 'MsgRemoveMargin':
def remove_margin(pair: str, margin: Coin) -> 'MsgRemoveMargin':
"""
Remove margin for the position (pair + trader)
Attributes:
sender (str): The trader address
pair (str): The token pair
margin (Coin): The margin to remove in a coin format
"""
return MsgRemoveMargin(sender=sender, pair=pair, margin=margin)
return MsgRemoveMargin(pair=pair, margin=margin)

@staticmethod
def liquidate(
sender: str,
pair: str,
trader: str,
) -> 'MsgMultiLiquidate':
def liquidate(pair: str, trader: str) -> 'MsgMultiLiquidate':
"""
Liquidates unhealthy position (pair + trader)
Attributes:
sender (str): The liquidator address
pair (str): The token pair
trader (str): The trader address
"""
return MsgMultiLiquidate(
sender=sender, liquidations=[Liquidation(pair=pair, trader=trader)]
)
return MsgMultiLiquidate(liquidations=[Liquidation(pair=pair, trader=trader)])

@staticmethod
def liquidate_multiple(
sender: str, liquidations: List[Liquidation]
) -> 'MsgMultiLiquidate':
def liquidate_multiple(liquidations: List[Liquidation]) -> 'MsgMultiLiquidate':
"""
Liquidates multiple unhealthy positions (pair + trader)
Attributes:
sender (str): The liquidator address
liquidations (List[Liquidation]): list of pair/traders to liquidate
"""
return MsgMultiLiquidate(sender=sender, liquidations=liquidations)
return MsgMultiLiquidate(liquidations=liquidations)


@dataclasses.dataclass
Expand All @@ -143,16 +119,14 @@ class MsgRemoveMargin(PythonMsg):
Remove margin for the position (pair + trader)
Attributes:
sender (str): The trader address
pair (str): The token pair
margin (Coin): The margin to remove in a coin format
"""

sender: str
pair: str
margin: Coin

def to_pb(self) -> pb.MsgRemoveMargin:
def to_pb(self, sender: str) -> pb.MsgRemoveMargin:
"""
Returns the Message as protobuf object.
Expand All @@ -161,9 +135,9 @@ def to_pb(self) -> pb.MsgRemoveMargin:
"""
return pb.MsgRemoveMargin(
sender=self.sender,
sender=sender,
pair=self.pair,
margin=self.margin._generate_proto_object(),
margin=self.margin.to_pb(),
)


Expand All @@ -173,16 +147,14 @@ class MsgAddMargin(PythonMsg):
Add margin for the position (pair + trader)
Attributes:
sender (str): The trader address
pair (str): The token pair
margin (Coin): The margin to remove in a coin format
"""

sender: str
pair: str
margin: Coin

def to_pb(self) -> pb.MsgAddMargin:
def to_pb(self, sender: str) -> pb.MsgAddMargin:
"""
Returns the Message as protobuf object.
Expand All @@ -191,9 +163,9 @@ def to_pb(self) -> pb.MsgAddMargin:
"""
return pb.MsgAddMargin(
sender=self.sender,
sender=sender,
pair=self.pair,
margin=self.margin._generate_proto_object(),
margin=self.margin.to_pb(),
)


Expand All @@ -203,7 +175,6 @@ class MsgMarketOrder(PythonMsg):
Open a position using the specified parameters.
Attributes:
sender (str): The sender address
pair (str): The token pair
side (Side): The side, either Side.BUY or Side.SELL
quote_asset_amount (float): The quote amount you want to use to buy base
Expand All @@ -213,14 +184,13 @@ class MsgMarketOrder(PythonMsg):
quote.
"""

sender: str
pair: str
dir: Direction
quote_asset_amount: float
leverage: float
base_asset_amount_limit: float

def to_pb(self) -> pb.MsgMarketOrder:
def to_pb(self, sender: str) -> pb.MsgMarketOrder:
"""
Returns the Message as protobuf object.
Expand All @@ -238,7 +208,7 @@ def to_pb(self) -> pb.MsgMarketOrder:
leverage_pb = to_sdk_dec(self.leverage)

return pb.MsgMarketOrder(
sender=self.sender,
sender=sender,
pair=self.pair,
side=pb_side,
quote_asset_amount=quote_asset_amount_pb,
Expand All @@ -253,14 +223,12 @@ class MsgClosePosition(PythonMsg):
Close the position.
Attributes:
sender (str): The sender address
pair (str): The token pair
"""

sender: str
pair: str

def to_pb(self) -> pb.MsgClosePosition:
def to_pb(self, sender: str) -> pb.MsgClosePosition:
"""
Returns the Message as protobuf object.
Expand All @@ -269,25 +237,24 @@ def to_pb(self) -> pb.MsgClosePosition:
"""
return pb.MsgClosePosition(
sender=self.sender,
sender=sender,
pair=self.pair,
)


@dataclasses.dataclass
class MsgMultiLiquidate(PythonMsg):
"""
Close the position.
Liquidate one or multiple unhealthy positions.
Unhealthy positions are positions with margin_ratio < maintenance_margin_ratio.
Attributes:
sender (str): The sender address
liquidations (Liquidation): The list of {pair, trader} pairs.
"""

sender: str
liquidations: List[Liquidation]

def to_pb(self) -> pb.MsgMultiLiquidate:
def to_pb(self, sender: str) -> pb.MsgMultiLiquidate:
"""
Returns the Message as protobuf object.
Expand All @@ -296,7 +263,7 @@ def to_pb(self) -> pb.MsgMultiLiquidate:
"""
return pb.MsgMultiLiquidate(
sender=self.sender,
sender=sender,
liquidations=[
pb.MsgMultiLiquidate.Liquidation(
pair=liquidation.pair,
Expand Down
Loading

0 comments on commit 505f71c

Please sign in to comment.