2

im trying to buy a token in solana dex using jupiter dex, i do the next steps:

1- Get Quote 2- With the quote i get the swapTransaction

How do i send this "swapTransaction" variable and create the swap in the live solana network?

I have this code:

import asyncio
import base64
import solders
from solana.transaction import Transaction
from solathon import PublicKey, Keypair
from solana.rpc.types import TxOpts
from solana.rpc.api import Client
from solders import message, transaction

def send_transaction(swap_transaction, opts):

    client = Client("https://api.mainnet-beta.solana.com")
    sender = Keypair().from_private_key("XXX")

    raw_tx = solders.transaction.VersionedTransaction.from_bytes(base64.b64decode(swap_transaction))
    signed_txn = sender.sign(bytes(raw_tx))
    s_signed_txn = solders.transaction.VersionedTransaction.populate(raw_tx.message, [signed_txn])
    encoded_tx = base64.b64encode(bytes(s_signed_txn)).decode('utf-8')

    result = client.send_transaction(s_signed_txn, opts)
    txid = result.value

    print("TxID: {txid}")

async def perform_swap(sent_amount, input_token_mint, output_token_mint):

    tmpSwap = "swapTransaction"

    opts = TxOpts(skip_preflight=True, max_retries=3)
    send_transaction(tmpSwap, opts)

asyncio.run(perform_swap("1000", "So11111111111111111111111111111111111111112", "5HkMzQzo5wiXyNgsWJSm3F8jQYMSaUZ6z5Vd7Z1yCsC9"))
1
  • My code is running and giving the txnid but when opening in explorer it is not showing anything. what can be the issue? Commented Feb 6 at 4:44

1 Answer 1

1

I would suggest you try to use the solders package more as it incoperates a lot of the code present in the solana-py package and is updated more frequently and is more relevant.

Try using sender.sign_message(message.to_bytes_versioned(raw_tx.message)) instead when signing the message. When sending the transaction to the blockchain to be executed I prefer to send the transaction to the blockchain using the httpx package. So instead of using result = client.send_transaction(x, y) you would post it to the blockchain as shown below.

import asyncio
import httpx
import base64
from solders.transaction import VersionedTransaction  # type: ignore
from solders.keypair import Keypair  # type: ignore
from solders import message
import solders


async def send_transaction(swap_transaction):
    sender = Keypair().from_private_key("XXX")

    raw_tx = solders.transaction.VersionedTransaction.from_bytes(
        base64.b64decode(swap_transaction)
    )
    signed_txn = sender.sign_message(message.to_bytes_versioned(raw_tx.message))
    s_signed_txn = solders.transaction.VersionedTransaction.populate(
        raw_tx.message, [signed_txn]
    )
    encoded_tx = base64.b64encode(bytes(s_signed_txn)).decode("utf-8")

    headers = {"Content-Type": "application/json"}
    data = {
        "jsonrpc": "2.0",
        "id": 1,
        "method": "sendTransaction",
        "params": [
            encoded_tx,
            {
                "skipPreflight": True,
                "preflightCommitment": "finalized",
                "encoding": "base64",
                "maxRetries": None,
                "minContextSlot": None,
            },
        ],
    }

    async with httpx.AsyncClient() as client:
        response = await client.post(
            "https://api.mainnet-beta.solana.com",
            headers=headers,
            json=data,
        )

    return response.json()


async def main():
    result = await send_transaction(swap_transaction)
    txid = result["result"]
    print(f"https://explorer.solana.com/tx/{txid}")
    return result


if __name__ == "__main__":
    asyncio.run(main())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.