Skip to content

Commit

Permalink
Merge pull request #133 from InjectiveLabs/f/logging_and_custom_cookie
Browse files Browse the repository at this point in the history
F/logging and custom cookie
  • Loading branch information
achilleas-kal committed Jul 29, 2022
2 parents b66f924 + dbc99d2 commit 2328794
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion examples/chain_client/1_MsgSend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 13 additions & 8 deletions pyinjective/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,20 +64,24 @@
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__(
self,
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':
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
17 changes: 10 additions & 7 deletions pyinjective/composer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from time import time
import json
import logging

from google.protobuf import any_pb2, message, timestamp_pb2

Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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
)
Expand All @@ -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
)
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 2328794

Please sign in to comment.