michaelhly / solana-py

Solana Python SDK

Home Page:https://michaelhly.github.io/solana-py

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to sign and send a transaction after calling the swap endpoint of jupiter api?

marcoAcquati03 opened this issue · comments

Hi, i can't find a way to sign and send a transaction after calling the quote and swap endpoint of jup.ag API. Someon can help?
Code:

async def coin_swap(token_address):
    global amount_swap
    max_retries = 3
    async with aiohttp.ClientSession() as session:
        try:
            for retry in range(max_retries):
                quote_params = {
                    "inputMint": "So11111111111111111111111111111111111111112",
                    "outputMint": token_address,
                    "amount": amount_swap,
                    "slippageBps": 50
                }

                async with session.get('https://quote-api.jup.ag/v6/quote', params=quote_params) as quote_response:
                    data_quote_response = await quote_response.json()
                    coin_swapped = False

                    if 'error' not in data_quote_response:
                        print(f"Quote successful. Retry number: {retry + 1}")

                        swap_params = json.dumps({
                            "quoteResponse": data_quote_response,
                            "userPublicKey": str(Pubkey(pubKey_decoded))
                        })

                        async with session.post('https://quote-api.jup.ag/v6/swap', data=swap_params) as swap_response:
                            data_swap_response = await swap_response.json()

                            if 'swapTransaction' in data_swap_response:
                                swap_transaction = data_swap_response['swapTransaction']
                                #code to sign and send a transaction
                            else:
                                coin_swapped = False
                                print("Swap failed.")
                    else:
                        coin_swapped = False
                        print(f"Quote failed. Retry number: {retry + 1}")
                        break  
                await asyncio.sleep(5)
commented

There's a library that does this using solana.py library
Here's the github repo

@spacesudo thanks, it helped