-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbasic_txn.py
141 lines (111 loc) · 5.62 KB
/
basic_txn.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import sys
import os
import json
import asyncio
import base58
from solders.keypair import Keypair
from solders.pubkey import Pubkey
from solders.system_program import TransferParams, transfer
from solders.instruction import Instruction
from solders.transaction import Transaction
from solders.compute_budget import set_compute_unit_limit, set_compute_unit_price
from solders.transaction_status import TransactionConfirmationStatus
from solders.signature import Signature
from solana.rpc.async_api import AsyncClient
from solana.exceptions import SolanaRpcException
from jito_py_rpc import JitoJsonRpcSDK
async def check_transaction_status(client: AsyncClient, signature_str: str):
print("Checking transaction status...")
max_attempts = 60 # 60 seconds
attempt = 0
signature = Signature.from_string(signature_str)
while attempt < max_attempts:
try:
response = await client.get_signature_statuses([signature])
if response.value[0] is not None:
status = response.value[0]
slot = status.slot
confirmations = status.confirmations
err = status.err
confirmation_status = status.confirmation_status
print(f"Slot: {slot}")
print(f"Confirmations: {confirmations}")
print(f"Confirmation status: {confirmation_status}")
if err:
print(f"Transaction failed with error: {err}")
return False
elif confirmation_status == TransactionConfirmationStatus.Finalized:
print("Transaction is finalized.")
return True
elif confirmation_status == TransactionConfirmationStatus.Confirmed:
print("Transaction is confirmed but not yet finalized.")
elif confirmation_status == TransactionConfirmationStatus.Processed:
print("Transaction is processed but not yet confirmed or finalized.")
else:
print("Transaction status not available yet.")
await asyncio.sleep(1)
attempt += 1
except Exception as e:
print(f"Error checking transaction status: {e}")
await asyncio.sleep(1)
attempt += 1
print(f"Transaction not finalized after {max_attempts} attempts.")
return False
async def send_transaction_with_priority_fee(sdk, solana_client, sender, receiver, amount, jito_tip_amount, priority_fee, compute_unit_limit=100_000):
try:
recent_blockhash = await solana_client.get_latest_blockhash()
# Transfer to the known receiver
transfer_ix = transfer(TransferParams(from_pubkey=sender.pubkey(), to_pubkey=receiver, lamports=amount))
# Jito tip transfer
jito_tip_account = Pubkey.from_string(sdk.get_random_tip_account())
jito_tip_ix = transfer(TransferParams(from_pubkey=sender.pubkey(), to_pubkey=jito_tip_account, lamports=jito_tip_amount))
# Priority Fee
priority_fee_ix = set_compute_unit_price(priority_fee)
transaction = Transaction.new_signed_with_payer(
[priority_fee_ix, transfer_ix, jito_tip_ix],
sender.pubkey(),
[sender],
recent_blockhash.value.blockhash
)
serialized_transaction = base58.b58encode(bytes(transaction)).decode('ascii')
print(f"Sending transaction with priority fee: {priority_fee} micro-lamports per compute unit")
print(f"Transfer amount: {amount} lamports to {receiver}")
print(f"Jito tip amount: {jito_tip_amount} lamports to {jito_tip_account}")
print(f"Serialized transaction: {serialized_transaction}")
response = sdk.send_txn(params=serialized_transaction, bundleOnly=False)
if response['success']:
print(f"Full Jito SDK response: {response}")
signature_str = response['data']['result']
print(f"Transaction signature: {signature_str}")
finalized = await check_transaction_status(solana_client, signature_str)
if finalized:
print("Transaction has been finalized.")
solscan_url = f"https://solscan.io/tx/{signature_str}"
print(f"View transaction details on Solscan: {solscan_url}")
else:
print("Transaction was not finalized within the expected time.")
return signature_str
else:
print(f"Error sending transaction: {response['error']}")
return None
except Exception as e:
print(f"Exception occurred: {str(e)}")
return None
async def main():
solana_client = AsyncClient("https://api.mainnet-beta.solana.com")
sdk = JitoJsonRpcSDK(url="https://mainnet.block-engine.jito.wtf/api/v1")
wallet_path = "/path/to/wallet.json"
with open(wallet_path, 'r') as file:
private_key = json.load(file)
sender = Keypair.from_bytes(bytes(private_key))
receiver = Pubkey.from_string("YOU_RECIEVER_KEY")
print(f"Sender public key: {sender.pubkey()}")
print(f"Receiver public key: {receiver}")
priority_fee = 1000 # Lamport for priority fee
amount = 1000 # Lamports to transfer to receiver
jito_tip_amount = 1000 # Lamports for Jito tip
signature = await send_transaction_with_priority_fee(sdk, solana_client, sender, receiver, amount, jito_tip_amount, priority_fee)
if signature:
print(f"Transaction process completed. Signature: {signature}")
await solana_client.close()
asyncio.run(main())