Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't send a transaction through Jito using Python. #718

Open
MrLux0r opened this issue Nov 11, 2024 · 0 comments
Open

I can't send a transaction through Jito using Python. #718

MrLux0r opened this issue Nov 11, 2024 · 0 comments

Comments

@MrLux0r
Copy link

MrLux0r commented Nov 11, 2024

Problem

Proposed Solution

I have a problem—I can't send a transaction through Jito using Python. I'm unable to figure out how to add tips to the transaction. Could you please help me with this?

async def main():
url = "https://mainnet.helius-rpc.com/?api-key=MY_KEY"
solana_client = AsyncClient(url, commitment=Confirmed)
sdk = JitoJsonRpcSDK(url="https://mainnet.block-engine.jito.wtf/api/v1")
wallet_path = "keypair.json"

with open(wallet_path, 'r') as file:
    private_key = json.load(file)
    sender = Keypair.from_seed(bytes(private_key))

print(f"Sender public key: {sender.pubkey()}")

jupiter = Jupiter(
    async_client=solana_client,
    keypair=sender,
)

token_mint = "T1oYbAejEESrZLtSAjumAXhzFqZGNxQ4kVN9vPUoxMv"  # Mint адрес токена, который вы хотите купить
amount_in_sol = 0.01  # Количество SOL, которое вы хотите потратить на покупку токена
LAMPORTS_PER_SOL = 1_000_000_000  # 1 SOL = 1,000,000,000 lamports
amount_in_lamports = int(amount_in_sol * LAMPORTS_PER_SOL)

# Выполнение свопа через Jupiter
transaction_data = await jupiter.swap(
    input_mint="So11111111111111111111111111111111111111112",  # WSOL
    output_mint=token_mint,
    amount=amount_in_lamports,
    slippage_bps=50,  # 0.5% slippage
)

recent_blockhash = await solana_client.get_latest_blockhash()

versioned_tx = VersionedTransaction.from_bytes(base64.b64decode(transaction_data))

# print("Original instructions:")
# for inst in versioned_tx.message.instructions:
#     print(f"Program ID index: {inst.program_id_index}")
#     print(f"Accounts: {inst.accounts}")
#     print(f"Data: {inst.data}")
#     print("---")

# Получаем инструкции из versioned_tx
instructions = list(versioned_tx.message.instructions)
# instructions = [
#     Instruction.decode(bytes(inst.program_id_index) + bytes(inst.data))
#     for inst in versioned_tx.message.instructions
# ]

# print(instructions)

# Создаем Jito tip инструкцию
jito_tip_account = Pubkey.from_string(sdk.get_random_tip_account())

jito_tip = transfer(
    TransferParams(
        from_pubkey=sender.pubkey(),
        to_pubkey=jito_tip_account,
        lamports=1000
    )
)

# Добавляем Jito tip к инструкциям
instructions.append(jito_tip)

msg = MessageV0.try_compile(
    payer=sender.pubkey(),
    instructions=instructions,
    address_lookup_table_accounts=[],
    recent_blockhash=recent_blockhash.value.blockhash,
)

tx = VersionedTransaction(msg, [sender])

# print(tx)

# serialized_transaction = tx.serialize()

# Отправка транзакции через Jito
serialized_transaction = base58.b58encode(bytes(tx)).decode('ascii')
response = sdk.send_txn(params=serialized_transaction, bundleOnly=True)

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.")
else:
    print(f"Error sending transaction: {response['error']}")

await solana_client.close()


ERROR

TypeError: argument 'instructions': 'CompiledInstruction' object cannot be converted to 'Instruction'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant