1
1
import base64
2
2
import os
3
+ from typing import Dict , Optional
3
4
4
5
import base58
5
6
import httpx
6
7
from solana .rpc .api import Client
7
- from solders .solders import Keypair , VersionedTransaction
8
+ from solders .solders import Keypair , SendTransactionResp , VersionedTransaction
8
9
9
10
10
11
class JupiterClient :
11
12
"""
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.
13
15
"""
14
16
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
+ ):
16
24
self .api_key = api_key
17
25
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
+ )
19
29
self .private_key_env_var = private_key_env_var
20
30
self .timeout = timeout
21
31
self .client = httpx .Client (timeout = self .timeout )
22
32
23
- def close (self ):
33
+ def close (self ) -> None :
24
34
self .client .close ()
25
35
26
- def _get_headers (self ):
36
+ def _get_headers (self ) -> Dict [ str , str ] :
27
37
headers = {
28
38
"Accept" : "application/json" ,
29
39
}
30
40
if self .api_key :
31
41
headers ["x-api-key" ] = self .api_key
32
42
return headers
33
43
34
- def _post_headers (self ):
44
+ def _post_headers (self ) -> Dict [ str , str ] :
35
45
headers = {
36
46
"Accept" : "application/json" ,
37
47
"Content-Type" : "application/json" ,
@@ -40,31 +50,45 @@ def _post_headers(self):
40
50
headers ["x-api-key" ] = self .api_key
41
51
return headers
42
52
43
- def _get_public_key (self ):
53
+ def _get_public_key (self ) -> str :
44
54
wallet = Keypair .from_bytes (
45
- base58 .b58decode (os .getenv (self .private_key_env_var ))
55
+ base58 .b58decode (os .getenv (self .private_key_env_var , "" ))
46
56
)
47
57
return str (wallet .pubkey ())
48
58
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." )
50
64
return self .rpc .send_transaction (transaction )
51
65
52
- def _sign_base64_transaction (self , transaction_base64 : str ):
66
+ def _sign_base64_transaction (
67
+ self , transaction_base64 : str
68
+ ) -> VersionedTransaction :
53
69
transaction_bytes = base64 .b64decode (transaction_base64 )
54
- versioned_transaction = VersionedTransaction .from_bytes (transaction_bytes )
70
+ versioned_transaction = VersionedTransaction .from_bytes (
71
+ transaction_bytes
72
+ )
55
73
return self ._sign_versioned_transaction (versioned_transaction )
56
74
57
- def _sign_versioned_transaction (self , versioned_transaction : VersionedTransaction ):
75
+ def _sign_versioned_transaction (
76
+ self , versioned_transaction : VersionedTransaction
77
+ ) -> VersionedTransaction :
58
78
wallet = Keypair .from_bytes (
59
- base58 .b58decode (os .getenv (self .private_key_env_var ))
79
+ base58 .b58decode (os .getenv (self .private_key_env_var , "" ))
60
80
)
61
81
account_keys = versioned_transaction .message .account_keys
62
82
wallet_index = account_keys .index (wallet .pubkey ())
63
83
64
84
signers = list (versioned_transaction .signatures )
65
- signers [wallet_index ] = wallet
85
+ signers [wallet_index ] = wallet # type: ignore
66
86
67
- return VersionedTransaction (versioned_transaction .message , signers )
87
+ return VersionedTransaction (
88
+ versioned_transaction .message , signers # type: ignore
89
+ )
68
90
69
- def _serialize_versioned_transaction (self , versioned_transaction : VersionedTransaction ):
91
+ def _serialize_versioned_transaction (
92
+ self , versioned_transaction : VersionedTransaction
93
+ ) -> str :
70
94
return base64 .b64encode (bytes (versioned_transaction )).decode ("utf-8" )
0 commit comments