[Python] Solana swap transation

Discussão em 'Python' iniciado por Stack, Janeiro 28, 2025.

  1. Stack

    Stack Membro Participativo

    Please help me to create swap transaction using solana and solders, there are not that many docs for Python and many legacy examples

    using solana 0.36 and solders 0.23

    # 1) Get a single quote object
    quote_url = (
    "https://quote-api.jup.ag/v6/quote"
    f"?inputMint=So11111111111111111111111111111111111111112"
    f"&outputMint={token_addr}"
    f"&amount={lamports_in}"
    "&slippageBps=50"
    )
    quote_resp = requests.get(quote_url, timeout=10).json()
    if "routePlan" not in quote_resp or not quote_resp["routePlan"]:
    print(f"[WARN] No routePlan for {token_addr}")
    return None

    # 2) Construct the swap request
    swap_req = {
    "quoteResponse": quote_resp, # Pass the entire quote here
    "userPublicKey": str(WALLET_PUBKEY),
    "destinationWallet": str(WALLET_PUBKEY), # optional, can be same as userPublicKey
    "wrapAndUnwrapSol": True
    }

    swap_url = "https://quote-api.jup.ag/v6/swap"
    swap_resp = requests.post(swap_url, json=swap_req, timeout=10).json()
    if "swapTransaction" not in swap_resp:
    print(f"[ERROR] No 'swapTransaction' in Jupiter swap response: {swap_resp}")
    return None

    tx = # ???


    send_resp = client.send_transaction(tx)
    signature = send_resp["result"]
    print(f"[BUY] {token_addr}, signature={signature}")
    return signature

    Continue reading...

Compartilhe esta Página