11import base64
22import os
3+ from typing import Dict , Optional
34
45import base58
56import httpx
67from solana .rpc .api import Client
7- from solders .solders import Keypair , VersionedTransaction
8+ from solders .solders import Keypair , SendTransactionResp , VersionedTransaction
89
910
1011class JupiterClient :
1112 """
12- Base client for interacting with Jupiter API. Also acts as a parent class for all sub-clients.
13+ Base client for interacting with Jupiter API.
14+ Also acts as a parent class for all sub-clients.
1315 """
1416
15- def __init__ (self , api_key , rpc_url , private_key_env_var , timeout ):
17+ def __init__ (
18+ self ,
19+ api_key : Optional [str ],
20+ rpc_url : Optional [str ],
21+ private_key_env_var : str ,
22+ timeout : int ,
23+ ):
1624 self .api_key = api_key
1725 self .rpc = Client (rpc_url ) if rpc_url else None
18- self .base_url = "https://api.jup.ag" if api_key else "https://lite-api.jup.ag"
26+ self .base_url = (
27+ "https://api.jup.ag" if api_key else "https://lite-api.jup.ag"
28+ )
1929 self .private_key_env_var = private_key_env_var
2030 self .timeout = timeout
2131 self .client = httpx .Client (timeout = self .timeout )
2232
23- def close (self ):
33+ def close (self ) -> None :
2434 self .client .close ()
2535
26- def _get_headers (self ):
36+ def _get_headers (self ) -> Dict [ str , str ] :
2737 headers = {
2838 "Accept" : "application/json" ,
2939 }
3040 if self .api_key :
3141 headers ["x-api-key" ] = self .api_key
3242 return headers
3343
34- def _post_headers (self ):
44+ def _post_headers (self ) -> Dict [ str , str ] :
3545 headers = {
3646 "Accept" : "application/json" ,
3747 "Content-Type" : "application/json" ,
@@ -40,31 +50,45 @@ def _post_headers(self):
4050 headers ["x-api-key" ] = self .api_key
4151 return headers
4252
43- def _get_public_key (self ):
53+ def _get_public_key (self ) -> str :
4454 wallet = Keypair .from_bytes (
45- base58 .b58decode (os .getenv (self .private_key_env_var ))
55+ base58 .b58decode (os .getenv (self .private_key_env_var , "" ))
4656 )
4757 return str (wallet .pubkey ())
4858
49- def _send_transaction (self , transaction ):
59+ def _send_transaction (
60+ self , transaction : VersionedTransaction
61+ ) -> SendTransactionResp :
62+ if not self .rpc :
63+ raise ValueError ("Client was initialized without RPC URL." )
5064 return self .rpc .send_transaction (transaction )
5165
52- def _sign_base64_transaction (self , transaction_base64 : str ):
66+ def _sign_base64_transaction (
67+ self , transaction_base64 : str
68+ ) -> VersionedTransaction :
5369 transaction_bytes = base64 .b64decode (transaction_base64 )
54- versioned_transaction = VersionedTransaction .from_bytes (transaction_bytes )
70+ versioned_transaction = VersionedTransaction .from_bytes (
71+ transaction_bytes
72+ )
5573 return self ._sign_versioned_transaction (versioned_transaction )
5674
57- def _sign_versioned_transaction (self , versioned_transaction : VersionedTransaction ):
75+ def _sign_versioned_transaction (
76+ self , versioned_transaction : VersionedTransaction
77+ ) -> VersionedTransaction :
5878 wallet = Keypair .from_bytes (
59- base58 .b58decode (os .getenv (self .private_key_env_var ))
79+ base58 .b58decode (os .getenv (self .private_key_env_var , "" ))
6080 )
6181 account_keys = versioned_transaction .message .account_keys
6282 wallet_index = account_keys .index (wallet .pubkey ())
6383
6484 signers = list (versioned_transaction .signatures )
65- signers [wallet_index ] = wallet
85+ signers [wallet_index ] = wallet # type: ignore
6686
67- return VersionedTransaction (versioned_transaction .message , signers )
87+ return VersionedTransaction (
88+ versioned_transaction .message , signers # type: ignore
89+ )
6890
69- def _serialize_versioned_transaction (self , versioned_transaction : VersionedTransaction ):
91+ def _serialize_versioned_transaction (
92+ self , versioned_transaction : VersionedTransaction
93+ ) -> str :
7094 return base64 .b64encode (bytes (versioned_transaction )).decode ("utf-8" )
0 commit comments