Skip to content

Commit

Permalink
Merge branch 'hummingbot:development' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
petioptrv authored Sep 22, 2023
2 parents 8cba8ad + 2aa845c commit 0db8e48
Show file tree
Hide file tree
Showing 124 changed files with 6,203 additions and 764 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ omit =
hummingbot/strategy/*/start.py
hummingbot/strategy/dev*
hummingbot/user/user_balances.py
hummingbot/smart_components/controllers/*
dynamic_context = test_function
branch = true

Expand Down
1 change: 1 addition & 0 deletions hummingbot/connector/connector_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
'plenty': 'bronze',
'woo_x': 'bronze',
'woo_x_testnet': 'bronze',
'kujira': 'bronze',
}

warning_messages = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ async def _place_order(
False,
amount
).result_price

price = self.quantize_order_price(trading_pair, price)
notional_amount = amount * price
if notional_amount not in self._order_notional_amounts.keys():
self._order_notional_amounts[notional_amount] = len(self._order_notional_amounts.keys())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,16 +378,17 @@ async def _create_order(
:param position_action: is the order opening or closing a position
"""
try:
if price is None:
if price is None or price.is_nan():
calculated_price = self.get_price_for_volume(
trading_pair=trading_pair,
is_buy=trade_type == TradeType.BUY,
volume=amount,
).result_price
calculated_price = self.quantize_order_price(trading_pair, calculated_price)
else:
calculated_price = price

calculated_price = self.quantize_order_price(trading_pair, calculated_price)

await super()._create_order(
trade_type=trade_type,
order_id=order_id,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import asyncio

from pyinjective.async_client import AsyncClient
from pyinjective.composer import Composer
from pyinjective.constant import Network
from pyinjective.core.network import Network
from pyinjective.transaction import Transaction
from pyinjective.wallet import PrivateKey

Expand All @@ -26,10 +25,9 @@


async def main() -> None:
composer = Composer(network=NETWORK.string())

# initialize grpc client
client = AsyncClient(NETWORK, insecure=False)
composer = await client.composer()
await client.sync_timeout_height()

# load account
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import asyncio
import logging
import os
import time
from abc import ABC, abstractmethod
from decimal import Decimal
from enum import Enum
from functools import partial
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

from google.protobuf import any_pb2
Expand Down Expand Up @@ -64,11 +62,6 @@ def publisher(self):
def query_executor(self):
raise NotImplementedError

@property
@abstractmethod
def composer(self) -> Composer:
raise NotImplementedError

@property
@abstractmethod
def order_creation_lock(self) -> asyncio.Lock:
Expand Down Expand Up @@ -114,6 +107,10 @@ def portfolio_account_subaccount_index(self) -> int:
def network_name(self) -> str:
raise NotImplementedError

@abstractmethod
async def composer(self) -> Composer:
raise NotImplementedError

@abstractmethod
async def timeout_height(self) -> int:
raise NotImplementedError
Expand Down Expand Up @@ -265,8 +262,6 @@ async def start(self, market_ids: List[str]):
async def stop(self):
for task in self.events_listening_tasks():
task.cancel()
cookie_file_path = Path(self._chain_cookie_file_path())
cookie_file_path.unlink(missing_ok=True)

def add_listener(self, event_tag: Enum, listener: EventListener):
self.publisher.add_listener(event_tag=event_tag, listener=listener)
Expand Down Expand Up @@ -486,7 +481,7 @@ async def cancel_orders(
))
else:
market_id = await self.market_id_for_spot_trading_pair(trading_pair=order.trading_pair)
order_data = self._generate_injective_order_data(order=order, market_id=market_id)
order_data = await self._generate_injective_order_data(order=order, market_id=market_id)
spot_orders_data.append(order_data)
orders_with_hash.append(order)

Expand All @@ -499,12 +494,12 @@ async def cancel_orders(
))
else:
market_id = await self.market_id_for_derivative_trading_pair(trading_pair=order.trading_pair)
order_data = self._generate_injective_order_data(order=order, market_id=market_id)
order_data = await self._generate_injective_order_data(order=order, market_id=market_id)
derivative_orders_data.append(order_data)
orders_with_hash.append(order)

if len(orders_with_hash) > 0:
delegated_message = self._order_cancel_message(
delegated_message = await self._order_cancel_message(
spot_orders_to_cancel=spot_orders_data,
derivative_orders_to_cancel=derivative_orders_data,
)
Expand Down Expand Up @@ -544,7 +539,7 @@ async def cancel_all_subaccount_orders(
spot_markets_ids = spot_markets_ids or []
perpetual_markets_ids = perpetual_markets_ids or []

delegated_message = self._all_subaccount_orders_cancel_message(
delegated_message = await self._all_subaccount_orders_cancel_message(
spot_markets_ids=spot_markets_ids,
derivative_markets_ids=perpetual_markets_ids,
)
Expand Down Expand Up @@ -763,23 +758,23 @@ async def _order_creation_messages(
raise NotImplementedError

@abstractmethod
def _order_cancel_message(
async def _order_cancel_message(
self,
spot_orders_to_cancel: List[injective_exchange_tx_pb.OrderData],
derivative_orders_to_cancel: List[injective_exchange_tx_pb.OrderData]
) -> any_pb2.Any:
raise NotImplementedError

@abstractmethod
def _all_subaccount_orders_cancel_message(
async def _all_subaccount_orders_cancel_message(
self,
spot_markets_ids: List[str],
derivative_markets_ids: List[str]
) -> any_pb2.Any:
raise NotImplementedError

@abstractmethod
def _generate_injective_order_data(self, order: GatewayInFlightOrder, market_id: str) -> injective_exchange_tx_pb.OrderData:
async def _generate_injective_order_data(self, order: GatewayInFlightOrder, market_id: str) -> injective_exchange_tx_pb.OrderData:
raise NotImplementedError

@abstractmethod
Expand All @@ -796,9 +791,6 @@ def _place_order_results(
) -> List[PlaceOrderResult]:
raise NotImplementedError

def _chain_cookie_file_path(self) -> str:
return f"{os.path.join(os.path.dirname(__file__), '../.injective_cookie')}"

async def _last_traded_price(self, market_id: str) -> Decimal:
price = Decimal("nan")
if market_id in await self.spot_market_and_trading_pair_map():
Expand Down Expand Up @@ -1041,8 +1033,9 @@ async def _send_in_transaction(self, messages: List[any_pb2.Any]) -> Dict[str, A
await self.initialize_trading_account()
raise

composer = await self.composer()
gas_limit = int(simulation_result["gasInfo"]["gasUsed"]) + CONSTANTS.EXTRA_TRANSACTION_GAS
fee = [self.composer.Coin(
fee = [composer.Coin(
amount=gas_limit * CONSTANTS.DEFAULT_GAS_PRICE,
denom=self.fee_denom,
)]
Expand Down Expand Up @@ -1299,8 +1292,9 @@ async def _process_transaction_update(self, transaction_event: Dict[str, Any]):
self.publisher.trigger_event(event_tag=InjectiveEvent.ChainTransactionEvent, message=transaction_event)

async def _create_spot_order_definition(self, order: GatewayInFlightOrder):
composer = await self.composer()
market_id = await self.market_id_for_spot_trading_pair(order.trading_pair)
definition = self.composer.SpotOrder(
definition = composer.SpotOrder(
market_id=market_id,
subaccount_id=self.portfolio_account_subaccount_id,
fee_recipient=self.portfolio_account_injective_address,
Expand All @@ -1312,8 +1306,9 @@ async def _create_spot_order_definition(self, order: GatewayInFlightOrder):
return definition

async def _create_derivative_order_definition(self, order: GatewayPerpetualInFlightOrder):
composer = await self.composer()
market_id = await self.market_id_for_derivative_trading_pair(order.trading_pair)
definition = self.composer.DerivativeOrder(
definition = composer.DerivativeOrder(
market_id=market_id,
subaccount_id=self.portfolio_account_subaccount_id,
fee_recipient=self.portfolio_account_injective_address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from pyinjective import Transaction
from pyinjective.async_client import AsyncClient
from pyinjective.composer import Composer, injective_exchange_tx_pb
from pyinjective.constant import Network
from pyinjective.core.network import Network
from pyinjective.orderhash import OrderHashManager
from pyinjective.wallet import Address, PrivateKey

Expand Down Expand Up @@ -50,9 +50,8 @@ def __init__(
self._client = AsyncClient(
network=self._network,
insecure=not use_secure_connection,
chain_cookie_location=self._chain_cookie_file_path(),
)
self._composer = Composer(network=self._network.string())
self._composer = None
self._query_executor = PythonSDKInjectiveQueryExecutor(sdk_client=self._client)

self._private_key = None
Expand Down Expand Up @@ -99,10 +98,6 @@ def publisher(self):
def query_executor(self):
return self._query_executor

@property
def composer(self) -> Composer:
return self._composer

@property
def order_creation_lock(self) -> asyncio.Lock:
return self._order_creation_lock
Expand Down Expand Up @@ -139,6 +134,11 @@ def portfolio_account_subaccount_index(self) -> int:
def network_name(self) -> str:
return self._network.string()

async def composer(self) -> Composer:
if self._composer is None:
self._composer = await self._client.composer()
return self._composer

def events_listening_tasks(self) -> List[asyncio.Task]:
return self._events_listening_tasks.copy()

Expand Down Expand Up @@ -509,7 +509,7 @@ async def _order_creation_messages(
spot_orders_to_create: List[GatewayInFlightOrder],
derivative_orders_to_create: List[GatewayPerpetualInFlightOrder],
) -> Tuple[List[any_pb2.Any], List[str], List[str]]:
composer = self.composer
composer = await self.composer()
spot_market_order_definitions = []
derivative_market_order_definitions = []
spot_order_definitions = []
Expand Down Expand Up @@ -580,12 +580,12 @@ async def _order_creation_messages(

return [delegated_message], spot_order_hashes, derivative_order_hashes

def _order_cancel_message(
async def _order_cancel_message(
self,
spot_orders_to_cancel: List[injective_exchange_tx_pb.OrderData],
derivative_orders_to_cancel: List[injective_exchange_tx_pb.OrderData]
) -> any_pb2.Any:
composer = self.composer
composer = await self.composer()

message = composer.MsgBatchUpdateOrders(
sender=self.portfolio_account_injective_address,
Expand All @@ -598,12 +598,12 @@ def _order_cancel_message(
)
return delegated_message

def _all_subaccount_orders_cancel_message(
async def _all_subaccount_orders_cancel_message(
self,
spot_markets_ids: List[str],
derivative_markets_ids: List[str]
) -> any_pb2.Any:
composer = self.composer
composer = await self.composer()

message = composer.MsgBatchUpdateOrders(
sender=self.portfolio_account_injective_address,
Expand All @@ -617,8 +617,9 @@ def _all_subaccount_orders_cancel_message(
)
return delegated_message

def _generate_injective_order_data(self, order: GatewayInFlightOrder, market_id: str) -> injective_exchange_tx_pb.OrderData:
order_data = self.composer.OrderData(
async def _generate_injective_order_data(self, order: GatewayInFlightOrder, market_id: str) -> injective_exchange_tx_pb.OrderData:
composer = await self.composer()
order_data = composer.OrderData(
market_id=market_id,
subaccount_id=self.portfolio_account_subaccount_id,
order_hash=order.exchange_order_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pyinjective import Transaction
from pyinjective.async_client import AsyncClient
from pyinjective.composer import Composer, injective_exchange_tx_pb
from pyinjective.constant import Network
from pyinjective.core.network import Network

from hummingbot.connector.exchange.injective_v2 import injective_constants as CONSTANTS
from hummingbot.connector.exchange.injective_v2.data_sources.injective_data_source import InjectiveDataSource
Expand Down Expand Up @@ -40,9 +40,8 @@ def __init__(
self._client = AsyncClient(
network=self._network,
insecure=not use_secure_connection,
chain_cookie_location=self._chain_cookie_file_path(),
)
self._composer = Composer(network=self._network.string())
self._composer = None
self._query_executor = PythonSDKInjectiveQueryExecutor(sdk_client=self._client)

self._publisher = PubSub()
Expand All @@ -67,10 +66,6 @@ def publisher(self):
def query_executor(self):
return self._query_executor

@property
def composer(self) -> Composer:
return self._composer

@property
def order_creation_lock(self) -> asyncio.Lock:
return None
Expand Down Expand Up @@ -107,6 +102,11 @@ def portfolio_account_subaccount_index(self) -> int:
def network_name(self) -> str:
return self._network.string()

async def composer(self) -> Composer:
if self._composer is None:
self._composer = await self._client.composer()
return self._composer

async def timeout_height(self) -> int:
raise NotImplementedError

Expand Down Expand Up @@ -337,22 +337,25 @@ async def _order_creation_messages(
) -> Tuple[List[any_pb2.Any], List[str], List[str]]:
raise NotImplementedError

def _order_cancel_message(
async def _order_cancel_message(
self,
spot_orders_to_cancel: List[injective_exchange_tx_pb.OrderData],
derivative_orders_to_cancel: List[injective_exchange_tx_pb.OrderData]
) -> any_pb2.Any:
raise NotImplementedError

def _all_subaccount_orders_cancel_message(
async def _all_subaccount_orders_cancel_message(
self,
spot_orders_to_cancel: List[injective_exchange_tx_pb.OrderData],
derivative_orders_to_cancel: List[injective_exchange_tx_pb.OrderData]
) -> any_pb2.Any:
raise NotImplementedError

def _generate_injective_order_data(self, order: GatewayInFlightOrder,
market_id: str) -> injective_exchange_tx_pb.OrderData:
async def _generate_injective_order_data(
self,
order: GatewayInFlightOrder,
market_id: str,
) -> injective_exchange_tx_pb.OrderData:
raise NotImplementedError

async def _updated_derivative_market_info_for_id(self, market_id: str) -> InjectiveDerivativeMarket:
Expand Down
Loading

0 comments on commit 0db8e48

Please sign in to comment.