#๐Ÿ”’ need help building a swap

6 messages ยท Page 1 of 1 (latest)

lone plover
#

from solders.message import Message

from solders.keypair import Keypair

from solders.instruction import Instruction

from solders.hash import Hash

from solders.transaction import Transaction

from solders.pubkey import Pubkey

program_id = Pubkey.default()

arbitrary_instruction_data = bytes([1])

accounts = []

instruction = Instruction(program_id, arbitrary_instruction_data, accounts)

payer = Keypair()

message = Message([instruction], payer.pubkey())

blockhash = Hash.default()  # replace with a real blockhash

tx = Transaction([payer], message, blockhash)

print(tx)```

already have all the bits and pieces

```py

def get_route(input_mint, output_mint, amount, slippage_bps):
    url = f"https://quote-api.jup.ag/v6/quote?inputMint={input_mint}&outputMint={output_mint}&amount={amount}&slippageBps={slippage_bps}"
    response = requests.get(url)
    return response.json()

# Example usage
route = get_route(input_mint, output_mint, amount, slippage_bps)
print(route)



def parse_route(route):
    swap_instructions = []
    for step in route['routePlan']:
        swap_instructions.append(step['swapInfo'])
    return swap_instructions

# Example usage
swap_instructions = parse_route(route)
print(swap_instructions)

just not sure how to put it all together

hard mantleBOT
#

@lone plover

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

lone plover
#

import requests
import timeit

def get_latest_blockhash():
    url = "https://api.mainnet-beta.solana.com"
    headers = {"Content-Type": "application/json"}
    payload = {
        "jsonrpc": "2.0",
        "id": 1,
        "method": "getRecentBlockhash"
    }

    response = requests.post(url, json=payload, headers=headers)
    if response.status_code == 200:
        result = response.json().get('result')
        if result:
            blockhash = result.get('value', {}).get('blockhash')
            return blockhash
    else:
        raise Exception(f"Error fetching blockhash: {response.text}")

# Time the get_latest_blockhash function
execution_time = timeit.timeit(get_latest_blockhash, number=1)
print(f"Execution time: {execution_time} seconds")

# Example usage
blockhash = get_latest_blockhash()
print(f"Latest blockhash: {blockhash}")


#

anyone with some experience, if you could just show me a way to combine this I would really appreciate it

hard mantleBOT
#

@lone plover

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.