Skip to content

Commit a61c323

Browse files
committed
feat: add seq & number init in client
1 parent 3c1c81f commit a61c323

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

examples/chain_client/1_MsgSend.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ async def main() -> None:
3535
# load account
3636
priv_key = PrivateKey.from_hex("f9db9bf330e23cb7839039e944adef6e9df447b90b503d5b4464c90bea9022f3")
3737
pub_key = priv_key.to_public_key()
38-
address = await pub_key.to_address().async_init_num_seq(network.lcd_endpoint)
38+
address = pub_key.to_address()
39+
account = await client.get_account("inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r")
3940

4041
# prepare tx msg
4142
msg = composer.MsgSend(

pyinjective/async_client.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -69,36 +69,39 @@
6969
DEFAULT_SESSION_RENEWAL_OFFSET = 120 # seconds
7070
DEFAULT_BLOCK_TIME = 2 # seconds
7171

72-
7372
logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.INFO)
7473

7574

7675
class AsyncClient:
7776
def __init__(
78-
self,
79-
network: Network,
80-
insecure: bool = False,
81-
load_balancer: bool = False,
82-
credentials = grpc.ssl_channel_credentials(),
83-
chain_cookie_location = ".chain_cookie"
77+
self,
78+
network: Network,
79+
insecure: bool = False,
80+
load_balancer: bool = False,
81+
credentials=grpc.ssl_channel_credentials(),
82+
chain_cookie_location=".chain_cookie",
8483
):
8584

8685
# use append mode to create file if not exist
8786
self.chain_cookie_location = chain_cookie_location
8887
cookie_file = open(chain_cookie_location, "a+")
8988
cookie_file.close()
90-
89+
90+
self.addr = ""
91+
self.number = 0
92+
self.sequence = 0
93+
9194
self.cookie_type = None
9295
self.expiration_format = None
9396
self.load_balancer = load_balancer
9497

9598
if self.load_balancer is False:
96-
self.cookie_type = "grpc-cookie"
97-
self.expiration_format = "20{}"
99+
self.cookie_type = "grpc-cookie"
100+
self.expiration_format = "20{}"
98101

99102
else:
100-
self.cookie_type = "GCLB"
101-
self.expiration_format = "{}"
103+
self.cookie_type = "GCLB"
104+
self.expiration_format = "{}"
102105

103106
# chain stubs
104107
self.chain_channel = (
@@ -165,6 +168,14 @@ def __init__(
165168
start=True,
166169
)
167170

171+
def get_sequence(self):
172+
current_seq = self.sequence
173+
self.sequence += 1
174+
return current_seq
175+
176+
def get_number(self):
177+
return self.number
178+
168179
async def get_tx(self, tx_hash):
169180
return await self.stubTx.GetTx(tx_service.GetTxRequest(hash=tx_hash))
170181

@@ -203,9 +214,9 @@ async def renew_cookie(self, existing_cookie, type):
203214
# format cookie date into RFC1123 standard
204215
cookie = SimpleCookie()
205216
cookie.load(existing_cookie)
206-
217+
207218
expires_at = cookie.get(f"{self.cookie_type}").get("expires")
208-
expires_at = expires_at.replace("-"," ")
219+
expires_at = expires_at.replace("-", " ")
209220
yyyy = f"{self.expiration_format}".format(expires_at[12:14])
210221
expires_at = expires_at[:12] + yyyy + expires_at[14:]
211222

@@ -278,13 +289,15 @@ async def get_account(self, address: str) -> Optional[account_pb2.EthAccount]:
278289
try:
279290
metadata = await self.load_cookie(type="chain")
280291
account_any = (await self.stubAuth.Account(
281-
auth_query.QueryAccountRequest.__call__(address=address, metadata=metadata)
292+
auth_query.QueryAccountRequest.__call__(address=address), metadata=metadata
282293
)).account
283294
account = account_pb2.EthAccount()
284295
if account_any.Is(account.DESCRIPTOR):
285296
account_any.Unpack(account)
286-
return account
287-
except:
297+
self.number = int(account.base_account.account_number)
298+
self.sequence = int(account.base_account.sequence)
299+
except Exception as e:
300+
logging.debug("error while fetching sequence and number{}".format(e))
288301
return None
289302

290303
async def get_request_id_by_tx_hash(self, tx_hash: bytes) -> List[int]:
@@ -307,7 +320,7 @@ async def get_request_id_by_tx_hash(self, tx_hash: bytes) -> List[int]:
307320
return request_ids
308321

309322
async def simulate_tx(
310-
self, tx_byte: bytes
323+
self, tx_byte: bytes
311324
) -> Tuple[Union[abci_type.SimulationResponse, grpc.RpcError], bool]:
312325
try:
313326
req = tx_service.SimulateRequest(tx_bytes=tx_byte)
@@ -543,19 +556,19 @@ async def get_rewards(self, **kwargs):
543556
# OracleRPC
544557

545558
async def stream_oracle_prices(
546-
self, base_symbol: str, quote_symbol: str, oracle_type: str
559+
self, base_symbol: str, quote_symbol: str, oracle_type: str
547560
):
548561
req = oracle_rpc_pb.StreamPricesRequest(
549562
base_symbol=base_symbol, quote_symbol=quote_symbol, oracle_type=oracle_type
550563
)
551564
return self.stubOracle.StreamPrices(req)
552565

553566
async def get_oracle_prices(
554-
self,
555-
base_symbol: str,
556-
quote_symbol: str,
557-
oracle_type: str,
558-
oracle_scale_factor: int,
567+
self,
568+
base_symbol: str,
569+
quote_symbol: str,
570+
oracle_type: str,
571+
oracle_scale_factor: int,
559572
):
560573
req = oracle_rpc_pb.PriceRequest(
561574
base_symbol=base_symbol,

0 commit comments

Comments
 (0)