SWAP FOLLOW WORKS !!!

This commit is contained in:
Dobromir Popov 2024-10-07 13:28:23 +03:00
parent a6ca2e3886
commit e32901d228

View File

@ -119,6 +119,7 @@ async def send_telegram_message(message):
try: try:
await bot.send_message(chat_id=DEVELOPER_CHAT_ID, text=message, parse_mode=ParseMode.HTML) await bot.send_message(chat_id=DEVELOPER_CHAT_ID, text=message, parse_mode=ParseMode.HTML)
logging.info(f"Telegram message sent: {message}") logging.info(f"Telegram message sent: {message}")
# logging.info(f"Telegram message dummy sent: {message}")
except Exception as e: except Exception as e:
logging.error(f"Error sending Telegram message: {str(e)}") logging.error(f"Error sending Telegram message: {str(e)}")
@ -752,7 +753,7 @@ async def follow_move(move):
input_mint=move['token_in'], input_mint=move['token_in'],
output_mint=move['token_out'], output_mint=move['token_out'],
amount=int(amount_to_swap * 1e6), # Convert to lamports amount=int(amount_to_swap * 1e6), # Convert to lamports
slippage_bps=1, slippage_bps=50, # Increased to 0.5%
) )
@ -760,57 +761,29 @@ async def follow_move(move):
signature = private_key.sign_message(message.to_bytes_versioned(raw_transaction.message)) signature = private_key.sign_message(message.to_bytes_versioned(raw_transaction.message))
signed_txn = VersionedTransaction.populate(raw_transaction.message, [signature]) signed_txn = VersionedTransaction.populate(raw_transaction.message, [signature])
opts = TxOpts(skip_preflight=False, preflight_commitment=Processed) opts = TxOpts(skip_preflight=False, preflight_commitment=Processed)
# send the transaction
result = await async_client.send_raw_transaction(txn=bytes(signed_txn), opts=opts) result = await async_client.send_raw_transaction(txn=bytes(signed_txn), opts=opts)
transaction_id = json.loads(result.to_json())['result'] transaction_id = json.loads(result.to_json())['result']
print(f"Transaction sent: https://explorer.solana.com/tx/{transaction_id}") print(f"Transaction sent: https://explorer.solana.com/tx/{transaction_id}")
# wait for the transaction to be confirmed
# await async_client.wait_for_confirmation(Signature.from_string(transaction_id))
# # transaction_data is already a string, no need to json.loads() # wait 10 seconds
# raw_transaction = base64.b64decode(transaction_data) await asyncio.sleep(10)
# # Send the raw transaction
# opts = TxOpts(skip_preflight=False, preflight_commitment=Processed)
# result = await async_client.send_raw_transaction(txn=raw_transaction, opts=opts)
# transaction_id = result.value # This should be the transaction signature
# Fetch the transaction details to get the output amount # Fetch the transaction details to get the output amount
tx_details = await async_client.get_transaction(transaction_id) tx_details = await get_transaction_details_rpc(transaction_id)
output_amount = tx_details.value.meta.post_balances[1] - tx_details.value.meta.pre_balances[1] output_amount = tx_details.value.meta.post_balances[1] - tx_details.value.meta.pre_balances[1]
output_token_info = your_balances.get(move['token_out'], {'name': 'Unknown'}) output_token_info = your_balances.get(move['token_out'], {'name': 'Unknown'})
output_token_name = output_token_info['name'] output_token_name = output_token_info['name']
# transaction_data = json.loads(transaction_data)
# instructions = [transaction_data['instruction']] # Adjust as needed
# message = Message(instructions, private_key.pubkey())
# blockhash = await async_client.get_latest_blockhash()
# tx = Transaction([private_key], message, blockhash.value)
# result = await async_client.send_transaction(tx, private_key)
# transaction_id = result.value
# output_token_info = your_balances.get(move['token_out'], {'name': 'Unknown'})
# output_token_name = output_token_info['name']
# raw_transaction = VersionedTransaction.from_bytes(base64.b64decode(transaction_data))
# tx_message = raw_transaction.message
# signature = private_key.sign_message(tx_message.to_bytes_versioned())
# signed_txn = VersionedTransaction.populate(tx_message, [signature])
# opts = TxOpts(skip_preflight=False, preflight_commitment=Processed)
# result = await async_client.send_raw_transaction(txn=bytes(signed_txn), opts=opts)
# transaction_id = json.loads(result.to_json())['result']
# output_token_info = your_balances.get(move['token_out'], {'name': 'Unknown'})
# output_token_name = output_token_info['name']
notification = ( notification = (
f"<b>Move Followed:</b>\n" f"<b>Move Followed:</b>\n"
f"Swapped {amount_to_swap:.6f} {token_name} ({move['token_in']}) " f"Swapped {amount_to_swap:.6f} {token_name} ({move['token_in']}) "
f"(same {move['percentage_swapped']:.2f}% as followed wallet)\n" f"(same {move['percentage_swapped']:.2f}% as followed wallet)\n"
f"for {transaction_data['outputAmount'] / 1e6:.6f} {output_token_name} ({move['token_out']})" f"for {transaction_data['outputAmount'] / 1e6:.6f} {output_token_name} ({move['token_out']})"
f"\n\n<b>Transaction:</b> <a href='https://explorer.solana.com/tx/{transaction_id}'>{transaction_id}</a>"
) )
logging.info(notification) logging.info(notification)
await send_telegram_message(notification) await send_telegram_message(notification)
@ -848,16 +821,12 @@ async def subscribe_to_wallet():
reconnect_delay = 5 # Start with a 5-second delay reconnect_delay = 5 # Start with a 5-second delay
max_reconnect_delay = 60 # Maximum delay of 60 seconds max_reconnect_delay = 60 # Maximum delay of 60 seconds
await list_initial_wallet_states()
while True: while True:
try: try:
async with websockets.connect(uri) as websocket: async with websockets.connect(uri) as websocket:
logger.info("Connected to Solana websocket") logger.info("Connected to Solana websocket")
subscription_id = await load_subscription_id() subscription_id = await load_subscription_id()
request = { request = {
"jsonrpc": "2.0", "jsonrpc": "2.0",
@ -920,7 +889,8 @@ async def main():
# Initialize logging # Initialize logging
logging.basicConfig(level=logging.DEBUG) logging.basicConfig(level=logging.DEBUG)
await send_telegram_message("Solana Agent Started. Connecting to mainnet...") await send_telegram_message("Solana Agent Started. Connecting to mainnet...")
# await subscribe_to_wallet() # await list_initial_wallet_states()
await subscribe_to_wallet()
def run_flask(): def run_flask():
# Run Flask app without the reloader, so we can run the async main function # Run Flask app without the reloader, so we can run the async main function