This commit is contained in:
Dobromir Popov
2024-11-12 13:07:11 +02:00
parent cd3e9e79ec
commit d9395a030c
3 changed files with 146 additions and 76 deletions

View File

@ -770,7 +770,9 @@ class SolanaAPI:
logging.info(f"Initiating move. Transaction data:\n {transaction_data}")
raw_transaction = VersionedTransaction.from_bytes(base64.b64decode(transaction_data))
priority_fee = await async_client.get_fee_for_message(raw_transaction.message)
fee = await async_client.get_fee_for_message(raw_transaction.message)
priority_fee = fee.value or 100_000
# fee = await async_client.get_fee_for_message(transaction_data)
# priority_fee = 0
@ -785,16 +787,70 @@ class SolanaAPI:
# message = raw_transaction.message
# Add compute budget instruction to set priority fee
from solders.compute_budget import set_compute_unit_price
compute_budget_instruction = set_compute_unit_price(priority_fee)
# from solders.compute_budget import set_compute_unit_price
# compute_budget_instruction = set_compute_unit_price(priority_fee)
# Add compute budget instruction to the transaction
raw_transaction.message.add_instruction(compute_budget_instruction)
# raw_transaction.message.add_instruction(compute_budget_instruction)
# Create new instructions list with compute budget instruction first
# new_instructions = [compute_budget_instruction] + list(raw_transaction.message.instructions)
# # Create a new message with the updated instructions
# from solders.message import MessageV0
# new_message = MessageV0(
# instructions=new_instructions,
# address_table_lookups=raw_transaction.message.address_table_lookups,
# recent_blockhash=raw_transaction.message.recent_blockhash,
# payer=raw_transaction.message.payer
# )
# signature = private_key.sign_message( bytes(message) )
signature = private_key.sign_message(message.to_bytes_versioned(raw_transaction.message))
signed_txn = VersionedTransaction.populate(raw_transaction.message, [signature])
# working - no priority fee
# signature = private_key.sign_message(message.to_bytes_versioned(raw_transaction.message))
# signed_txn = VersionedTransaction.populate(raw_transaction.message, [signature])
# new - not working
# signature = private_key.sign_message(new_message.to_bytes_versioned())
# signed_txn = VersionedTransaction.populate(new_message, [signature])
from solders.compute_budget import set_compute_unit_price, ID as COMPUTE_BUDGET_ID
priority_fee_ix = set_compute_unit_price(priority_fee)
# Get the current message
msg = raw_transaction.message
new_account_keys = msg.account_keys
program_id_index = 0
if COMPUTE_BUDGET_ID not in msg.account_keys:
new_account_keys = msg.account_keys + [COMPUTE_BUDGET_ID]
program_id_index = len(msg.account_keys) # Index of the newly added program ID
else:
new_account_keys = msg.account_keys
program_id_index = msg.account_keys.index(COMPUTE_BUDGET_ID)
# Compile the priority fee instruction
compiled_priority_fee_ix = CompiledInstruction(
program_id_index=program_id_index,
accounts=bytes([]),
data=priority_fee_ix.data
)
# Add priority fee instruction at the beginning
new_instructions = [compiled_priority_fee_ix] + msg.instructions
# Create new message with updated instructions
new_message = Message.new_with_compiled_instructions(
num_required_signatures=msg.header.num_required_signatures,
num_readonly_signed_accounts=msg.header.num_readonly_signed_accounts,
num_readonly_unsigned_accounts=msg.header.num_readonly_unsigned_accounts,
account_keys=new_account_keys,
recent_blockhash=msg.recent_blockhash,
instructions=new_instructions
)
#signature = private_key.sign_message(new_message.to_bytes_versioned())
signature = private_key.sign_message(message.to_bytes_versioned(new_message))
signed_txn = VersionedTransaction.populate(new_message, [signature])
opts = TxOpts(
skip_preflight=False,
preflight_commitment=Processed,

View File

@ -308,7 +308,7 @@ def init_app(tr_handler=None):
else:
await SolanaAPI.SAPI.follow_move(tr)
# Store the successful copytrade transaction
await storage.store_transaction(wallet, "SWAP", tr['symbol_in'] , tr['amount_in'], tr['value_in_USD'], tr['symbol_out'], tr['amount_out'], tr['value_out_USD'],tx_signature)
# await storage.store_transaction(wallet, "SWAP", tr['symbol_in'] , tr['amount_in'], tr['value_in_USD'], tr['symbol_out'], tr['amount_out'], tr['value_out_USD'],tx_signature)
except Exception as e:
# Store the failed copytrade transaction
# await storage.store_transaction(wallet, "SWAP_FAIL", tr['symbol_in'] , tr['amount_in'], tr['value_in_USD'], tr['symbol_out'], tr['amount_out'], tr['value_out_USD'],tx_signature)