forked from YZYLAB/solana-swap-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
31 lines (24 loc) · 1.11 KB
/
Copy pathexample.py
File metadata and controls
31 lines (24 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from solders.keypair import Keypair
from solanatracker import SolanaTracker
async def swap():
keypair = Keypair.from_base58_string("YOUR_SECRET_KEY_HERE")
solana_tracker = SolanaTracker(keypair, "https://rpc.solanatracker.io/public?advancedTx=true")
swap_response = await solana_tracker.get_swap_instructions(
"So11111111111111111111111111111111111111112", # From Token
"667w6y7eH5tQucYQXfJ2KmiuGBE8HfYnqqbjLNSw7yww", # To Token
0.0005, # Amount to swap
10, # Slippage
str(keypair.pubkey()), # Payer public key
0.00005, # Priority fee (Recommended while network is congested)
True, # Force legacy transaction for Jupiter
)
txid = await solana_tracker.perform_swap(swap_response)
if not txid:
# Add retries / handle error as needed
raise Exception("Swap failed")
# Returns txid when the swap is successful or raises an exception if the swap fails
print("Transaction ID:", txid)
print("Transaction URL:", f"https://explorer.solana.com/tx/{txid}")
if __name__ == "__main__":
import asyncio
asyncio.run(swap())