diff --git a/README.md b/README.md index 56ed75b4..01815dc7 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,8 @@ Note that the [sync client](https://github.com/InjectiveLabs/sdk-python/blob/mas **0.5.7.4** * Refactor fetch_metada script to use K8S * Refactor testnet network config to K8S +* Removed print logs and standardized logging to info +* Added support for custom cookie in client initialization **0.5.7.3** * Add multi-subaccount and multi-market support in TradesRequest diff --git a/examples/chain_client/1_MsgSend.py b/examples/chain_client/1_MsgSend.py index db83f64d..60ec976b 100644 --- a/examples/chain_client/1_MsgSend.py +++ b/examples/chain_client/1_MsgSend.py @@ -28,7 +28,8 @@ async def main() -> None: composer = ProtoMsgComposer(network=network.string()) # initialize grpc client - client = AsyncClient(network, insecure=False) + # set custom cookie location (optional) - defaults to current dir + client = AsyncClient(network, insecure=False, chain_cookie_location="/tmp/.chain_cookie") await client.sync_timeout_height() # load account diff --git a/pyinjective/async_client.py b/pyinjective/async_client.py index d00f9621..c7388885 100644 --- a/pyinjective/async_client.py +++ b/pyinjective/async_client.py @@ -2,6 +2,7 @@ import time import grpc import aiocron +import logging import datetime from http.cookies import SimpleCookie from typing import List, Optional, Tuple, Union @@ -63,11 +64,9 @@ DEFAULT_TIMEOUTHEIGHT = 20 # blocks DEFAULT_SESSION_RENEWAL_OFFSET = 120 # seconds DEFAULT_BLOCK_TIME = 3 # seconds -DEFAULT_CHAIN_COOKIE_NAME = '.chain_cookie' -# use append mode to create file if not exist -cookie_file = open(DEFAULT_CHAIN_COOKIE_NAME, "a+") -cookie_file.close() + +logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO) class AsyncClient: def __init__( @@ -75,8 +74,14 @@ def __init__( network: Network, insecure: bool = False, credentials: grpc.ChannelCredentials = None, + chain_cookie_location = ".chain_cookie" ): + # use append mode to create file if not exist + self.chain_cookie_location = chain_cookie_location + cookie_file = open(chain_cookie_location, "a+") + cookie_file.close() + # load root CA cert if not insecure: if network.env == 'testnet': @@ -101,10 +106,10 @@ def __init__( self.stubTx = tx_service_grpc.ServiceStub(self.chain_channel) # attempt to load from disk - cookie_file = open(DEFAULT_CHAIN_COOKIE_NAME, "r+") + cookie_file = open(chain_cookie_location, "r+") self.chain_cookie = cookie_file.read() cookie_file.close() - print("chain session cookie loaded from disk:", self.chain_cookie) + logging.info("chain session cookie loaded from disk:{}".format(self.chain_cookie)) self.exchange_cookie = "" self.timeout_height = 1 @@ -231,10 +236,10 @@ def set_cookie(self, metadata, type): # write to client instance self.chain_cookie = new_cookie # write to disk - cookie_file = open(DEFAULT_CHAIN_COOKIE_NAME, "w") + cookie_file = open(self.chain_cookie_location, "w") cookie_file.write(new_cookie) cookie_file.close() - print("chain session cookie saved to disk") + logging.info("chain session cookie saved to disk") if type == "exchange": self.exchange_cookie = new_cookie diff --git a/pyinjective/composer.py b/pyinjective/composer.py index 7e7b8635..8864ef48 100644 --- a/pyinjective/composer.py +++ b/pyinjective/composer.py @@ -1,5 +1,6 @@ from time import time import json +import logging from google.protobuf import any_pb2, message, timestamp_pb2 @@ -29,6 +30,8 @@ from .utils import * from typing import List +logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO) + class Composer: def __init__(self, network: str): self.network = network @@ -52,7 +55,7 @@ def SpotOrder( ): # load denom metadata denom = Denom.load_market(self.network, market_id) - print("Loaded market metadata for", denom.description) + logging.info("Loaded market metadata for:{}".format(denom.description)) # prepare values quantity = spot_quantity_to_backend(quantity, denom) @@ -94,7 +97,7 @@ def DerivativeOrder( ): # load denom metadata denom = Denom.load_market(self.network, market_id) - print("Loaded market metadata for", denom.description) + logging.info("Loaded market metadata for:{}".format(denom.description)) if kwargs.get("is_reduce_only") is None: margin = derivative_margin_to_backend( @@ -153,7 +156,7 @@ def BinaryOptionsOrder( denom = Denom.load_market(self.network, market_id) # load denom metadata - print("Loaded market metadata for", denom.description) + logging.info("Loaded market metadata for:{}".format(denom.description)) if kwargs.get("is_reduce_only") is None and kwargs.get("is_buy"): margin = binary_options_buy_margin_to_backend( @@ -207,7 +210,7 @@ def BinaryOptionsOrder( def MsgSend(self, from_address: str, to_address: str, amount: float, denom: str): peggy_denom, decimals = Denom.load_peggy_denom(self.network, denom) be_amount = amount_to_backend(amount, decimals) - print( + logging.info( "Loaded send symbol {} ({}) with decimals = {}".format( denom, peggy_denom, decimals ) @@ -222,7 +225,7 @@ def MsgSend(self, from_address: str, to_address: str, amount: float, denom: str) def MsgDeposit(self, sender: str, subaccount_id: str, amount: float, denom: str): peggy_denom, decimals = Denom.load_peggy_denom(self.network, denom) be_amount = amount_to_backend(amount, decimals) - print( + logging.info( "Loaded deposit symbol {} ({}) with decimals = {}".format( denom, peggy_denom, decimals ) @@ -580,7 +583,7 @@ def MsgSubaccountTransfer( def MsgWithdraw(self, sender: str, subaccount_id: str, amount: float, denom: str): peggy_denom, decimals = Denom.load_peggy_denom(self.network, denom) be_amount = amount_to_backend(amount, decimals) - print( + logging.info( "Loaded withdrawal symbol {} ({}) with decimals = {}".format( denom, peggy_denom, decimals ) @@ -732,7 +735,7 @@ def MsgSendToEth( peggy_denom, decimals = Denom.load_peggy_denom(self.network, denom) be_amount = amount_to_backend(amount, decimals) be_bridge_fee = amount_to_backend(bridge_fee, decimals) - print("Loaded withdrawal symbol {} ({}) with decimals = {}".format(denom, peggy_denom, decimals)) + logging.info("Loaded withdrawal symbol {} ({}) with decimals = {}".format(denom, peggy_denom, decimals)) return injective_peggy_tx_pb.MsgSendToEth( sender=sender,