Skip to content

Commit b043666

Browse files
committed
Code formatting
1 parent ce2b64d commit b043666

File tree

11 files changed

+148
-64
lines changed

11 files changed

+148
-64
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,20 @@ jobs:
1717
- name: Set up Python
1818
uses: actions/setup-python@v4
1919
with:
20-
python-version: "3.10"
20+
python-version: "3.9"
2121

2222
- name: Install Poetry
2323
run: pip install poetry
2424

2525
- name: Install Dependencies
2626
run: poetry install --with dev
2727

28+
- name: Set up Environment Variables
29+
env:
30+
PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}
31+
run: echo "PRIVATE_KEY retrieved"
32+
2833
- name: Run Tests
34+
env:
35+
PRIVATE_KEY: ${{ secrets.PRIVATE_KEY }}
2936
run: poetry run pytest --cov=jup_ag_sdk

check_code.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
black jup_ag_sdk tests
1+
black jup_ag_sdk tests --line-length 79
22
isort jup_ag_sdk tests
33
flake8 jup_ag_sdk tests
44
mypy jup_ag_sdk tests

jup_ag_sdk/clients/jupiter_client.py

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,47 @@
11
import base64
22
import os
3+
from typing import Dict, Optional
34

45
import base58
56
import httpx
67
from solana.rpc.api import Client
7-
from solders.solders import Keypair, VersionedTransaction
8+
from solders.solders import Keypair, SendTransactionResp, VersionedTransaction
89

910

1011
class 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")

jup_ag_sdk/clients/swap_api_client.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1+
from typing import Any, Dict, Optional
2+
3+
from solders.solders import SendTransactionResp
4+
15
from jup_ag_sdk.clients.jupiter_client import JupiterClient
26
from jup_ag_sdk.models.swap_api.quote_request_model import QuoteRequest
37
from jup_ag_sdk.models.swap_api.swap_request_model import SwapRequest
48

59

610
class SwapApiClient(JupiterClient):
711
"""
8-
A client for interacting with the Jupiter Swap API. Inherits from JupiterClient.
12+
A client for interacting with the Jupiter Swap API.
13+
Inherits from JupiterClient.
914
"""
1015

1116
def __init__(
12-
self, api_key=None, rpc_url="https://api.mainnet-beta.solana.com", private_key_env_var="PRIVATE_KEY", timeout=10
17+
self,
18+
api_key: Optional[str] = None,
19+
rpc_url: str = "https://api.mainnet-beta.solana.com",
20+
private_key_env_var: str = "PRIVATE_KEY",
21+
timeout: int = 10,
1322
):
1423
super().__init__(
1524
api_key=api_key,
@@ -18,7 +27,7 @@ def __init__(
1827
timeout=timeout,
1928
)
2029

21-
def quote(self, request: QuoteRequest) -> dict:
30+
def quote(self, request: QuoteRequest) -> Dict[str, Any]:
2231
"""
2332
Get a swap quote from the Jupiter API.
2433
@@ -31,38 +40,44 @@ def quote(self, request: QuoteRequest) -> dict:
3140
params = request.to_dict()
3241

3342
url = f"{self.base_url}/swap/v1/quote"
34-
response = self.client.get(url, params=params, headers=self._get_headers())
43+
response = self.client.get(
44+
url, params=params, headers=self._get_headers()
45+
)
3546
response.raise_for_status()
3647

37-
return response.json()
48+
return response.json() # type: ignore
3849

39-
def swap(self, request: SwapRequest) -> dict:
50+
def swap(self, request: SwapRequest) -> Dict[str, Any]:
4051
"""
4152
Submit a swap request to the Jupiter API.
4253
4354
Args:
4455
request (SwapRequest): The swap request parameters.
4556
4657
Returns:
47-
dict: The parsed response containing transaction details and metadata.
58+
dict: The parsed response containing transaction details
59+
and metadata.
4860
"""
4961
payload = request.to_dict()
5062

5163
url = f"{self.base_url}/swap/v1/swap"
52-
response = self.client.post(url, json=payload, headers=self._get_headers())
64+
response = self.client.post(
65+
url, json=payload, headers=self._get_headers()
66+
)
5367
response.raise_for_status()
5468

55-
return response.json()
69+
return response.json() # type: ignore
5670

57-
def swap_and_execute(self, request: SwapRequest) -> dict:
71+
def swap_and_execute(self, request: SwapRequest) -> SendTransactionResp:
5872
"""
5973
Submit a swap request to the Jupiter API and execute it immediately.
6074
6175
Args:
6276
request (SwapRequest): The swap request parameters.
6377
6478
Returns:
65-
dict: The raw RPC response containing the result of the transaction execution.
79+
dict: The raw RPC response containing the result of the transaction
80+
execution.
6681
"""
6782
swap_response = self.swap(request)
6883
signed_transaction = self._sign_base64_transaction(

jup_ag_sdk/clients/ultra_api_client.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1+
from typing import Any, Dict, Optional
2+
13
from jup_ag_sdk.clients.jupiter_client import JupiterClient
2-
from jup_ag_sdk.models.ultra_api.ultra_execute_request_model import UltraExecuteRequest
3-
from jup_ag_sdk.models.ultra_api.ultra_order_request_model import UltraOrderRequest
4+
from jup_ag_sdk.models.ultra_api.ultra_execute_request_model import (
5+
UltraExecuteRequest,
6+
)
7+
from jup_ag_sdk.models.ultra_api.ultra_order_request_model import (
8+
UltraOrderRequest,
9+
)
410

511

612
class UltraApiClient(JupiterClient):
713
"""
8-
A client for interacting with the Jupiter Swap API. Inherits from JupiterClient.
14+
A client for interacting with the Jupiter Swap API.
15+
Inherits from JupiterClient.
916
"""
1017

1118
def __init__(
12-
self, api_key=None, private_key_env_var="PRIVATE_KEY", timeout=10
19+
self,
20+
api_key: Optional[str] = None,
21+
private_key_env_var: str = "PRIVATE_KEY",
22+
timeout: int = 10,
1323
):
1424
super().__init__(
1525
api_key=api_key,
@@ -18,7 +28,7 @@ def __init__(
1828
timeout=timeout,
1929
)
2030

21-
def order(self, request: UltraOrderRequest) -> dict:
31+
def order(self, request: UltraOrderRequest) -> Dict[str, Any]:
2232
"""
2333
Get an order from the Jupiter Ultra API.
2434
@@ -31,12 +41,14 @@ def order(self, request: UltraOrderRequest) -> dict:
3141
params = request.to_dict()
3242

3343
url = f"{self.base_url}/ultra/v1/order"
34-
response = self.client.get(url, params=params, headers=self._get_headers())
44+
response = self.client.get(
45+
url, params=params, headers=self._get_headers()
46+
)
3547
response.raise_for_status()
3648

37-
return response.json()
49+
return response.json() # type: ignore
3850

39-
def execute(self, request: UltraExecuteRequest) -> dict:
51+
def execute(self, request: UltraExecuteRequest) -> Dict[str, Any]:
4052
"""
4153
Execute the order with the Jupiter Ultra API.
4254
@@ -49,12 +61,14 @@ def execute(self, request: UltraExecuteRequest) -> dict:
4961
payload = request.to_dict()
5062

5163
url = f"{self.base_url}/ultra/v1/execute"
52-
response = self.client.post(url, json=payload, headers=self._get_headers())
64+
response = self.client.post(
65+
url, json=payload, headers=self._get_headers()
66+
)
5367
response.raise_for_status()
5468

55-
return response.json()
69+
return response.json() # type: ignore
5670

57-
def order_and_execute(self, request: UltraOrderRequest) -> dict:
71+
def order_and_execute(self, request: UltraOrderRequest) -> Dict[str, Any]:
5872
"""
5973
Get an order from the Jupiter Ultra API and execute it immediately.
6074
@@ -73,7 +87,9 @@ def order_and_execute(self, request: UltraOrderRequest) -> dict:
7387

7488
execute_request = UltraExecuteRequest(
7589
request_id=request_id,
76-
signed_transaction=self._serialize_versioned_transaction(signed_transaction),
90+
signed_transaction=self._serialize_versioned_transaction(
91+
signed_transaction
92+
),
7793
)
7894

7995
return self.execute(execute_request)

jup_ag_sdk/models/swap_api/quote_request_model.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import List, Optional
1+
from typing import Any, Dict, List, Optional
22

33
from pydantic import BaseModel
44
from pydantic.alias_generators import to_camel
@@ -21,10 +21,12 @@ class QuoteRequest(BaseModel):
2121
platform_fee_bps: Optional[int] = None
2222
dynamic_slippage: Optional[bool] = None
2323

24-
def to_dict(self):
24+
def to_dict(self) -> Dict[str, Any]:
2525
params = self.model_dump(exclude_none=True)
2626

27-
camel_case_params = {to_camel(key): value for key, value in params.items()}
27+
camel_case_params = {
28+
to_camel(key): value for key, value in params.items()
29+
}
2830

2931
if "dexes" in camel_case_params:
3032
camel_case_params["dexes"] = ",".join(

jup_ag_sdk/models/swap_api/swap_request_model.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Any, Dict, Optional
22

33
from pydantic import BaseModel
44
from pydantic.alias_generators import to_camel
@@ -10,13 +10,15 @@ class PriorityLevelWithMaxLamports(BaseModel):
1010

1111

1212
class PrioritizationFeeLamports(BaseModel):
13-
priority_level_with_max_lamports: Optional[PriorityLevelWithMaxLamports] = None
13+
priority_level_with_max_lamports: Optional[
14+
PriorityLevelWithMaxLamports
15+
] = None
1416
jito_tip_lamports: Optional[int] = None
1517

1618

1719
class SwapRequest(BaseModel):
1820
user_public_key: str
19-
quote_response: dict
21+
quote_response: Dict[str, Any]
2022

2123
wrap_and_unwrap_sol: Optional[bool] = None
2224
use_shared_accounts: Optional[bool] = None
@@ -35,9 +37,11 @@ class Config:
3537
alias_generator = to_camel
3638
populate_by_name = True
3739

38-
def to_dict(self):
40+
def to_dict(self) -> Dict[str, Any]:
3941
params = self.model_dump(exclude_none=True)
4042

41-
camel_case_params = {to_camel(key): value for key, value in params.items()}
43+
camel_case_params = {
44+
to_camel(key): value for key, value in params.items()
45+
}
4246

4347
return camel_case_params
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional
1+
from typing import Any, Dict
22

33
from pydantic import BaseModel
44
from pydantic.alias_generators import to_camel
@@ -8,9 +8,11 @@ class UltraExecuteRequest(BaseModel):
88
signed_transaction: str
99
request_id: str
1010

11-
def to_dict(self):
11+
def to_dict(self) -> Dict[str, Any]:
1212
params = self.model_dump(exclude_none=True)
1313

14-
camel_case_params = {to_camel(key): value for key, value in params.items()}
14+
camel_case_params = {
15+
to_camel(key): value for key, value in params.items()
16+
}
1517

1618
return camel_case_params

0 commit comments

Comments
 (0)