|
| 1 | +from cache.token_info import TokenInfoCache |
| 2 | +from common.constants import SOL_DECIMAL, WSOL |
| 3 | +from common.utils.jupiter import JupiterAPI |
| 4 | +from solana.rpc.async_api import AsyncClient |
| 5 | +from solders.keypair import Keypair # type: ignore |
| 6 | +from solders.transaction import VersionedTransaction # type: ignore |
| 7 | + |
| 8 | +from trading.swap import SwapDirection, SwapInType |
| 9 | +from trading.tx import sign_transaction_from_raw |
| 10 | + |
| 11 | +from .base import TransactionBuilder |
| 12 | + |
| 13 | + |
| 14 | +class JupiterTransactionBuilder(TransactionBuilder): |
| 15 | + """Jupiter 交易构建器""" |
| 16 | + |
| 17 | + def __init__(self, rpc_client: AsyncClient) -> None: |
| 18 | + super().__init__(rpc_client=rpc_client) |
| 19 | + self.token_info_cache = TokenInfoCache() |
| 20 | + self.jupiter_client = JupiterAPI() |
| 21 | + |
| 22 | + async def build_swap_transaction( |
| 23 | + self, |
| 24 | + keypair: Keypair, |
| 25 | + token_address: str, |
| 26 | + ui_amount: float, |
| 27 | + swap_direction: SwapDirection, |
| 28 | + slippage_bps: int, |
| 29 | + in_type: SwapInType | None = None, |
| 30 | + use_jito: bool = False, |
| 31 | + priority_fee: float | None = None, |
| 32 | + ) -> VersionedTransaction: |
| 33 | + """Build swap transaction with GMGN API. |
| 34 | +
|
| 35 | + Args: |
| 36 | + token_address (str): token address |
| 37 | + amount_in (float): amount in |
| 38 | + swap_direction (SwapDirection): swap direction |
| 39 | + slippage (int): slippage, percentage |
| 40 | + in_type (SwapInType | None, optional): in type. Defaults to None. |
| 41 | + use_jto (bool, optional): use jto. Defaults to False. |
| 42 | + priority_fee (float | None, optional): priority fee. Defaults to None. |
| 43 | +
|
| 44 | + Returns: |
| 45 | + VersionedTransaction: The built transaction ready to be signed and sent |
| 46 | + """ |
| 47 | + if swap_direction == "sell" and in_type is None: |
| 48 | + raise ValueError("in_type must be specified when selling") |
| 49 | + |
| 50 | + if swap_direction == SwapDirection.Buy: |
| 51 | + token_in = str(WSOL) |
| 52 | + token_out = token_address |
| 53 | + amount = int(ui_amount * SOL_DECIMAL) |
| 54 | + elif swap_direction == SwapDirection.Sell: |
| 55 | + token_info = await self.token_info_cache.get(token_address) |
| 56 | + if token_info is None: |
| 57 | + raise ValueError("Token info not found") |
| 58 | + decimals = token_info.decimals |
| 59 | + token_in = token_address |
| 60 | + token_out = str(WSOL) |
| 61 | + amount = int(ui_amount * 10**decimals) |
| 62 | + else: |
| 63 | + raise ValueError("swap_direction must be buy or sell") |
| 64 | + |
| 65 | + if use_jito and priority_fee is None: |
| 66 | + raise ValueError("priority_fee must be specified when using jito") |
| 67 | + |
| 68 | + swap_tx_response = await self.jupiter_client.get_swap_transaction( |
| 69 | + input_mint=token_in, |
| 70 | + output_mint=token_out, |
| 71 | + user_publickey=str(keypair.pubkey()), |
| 72 | + amount=amount, |
| 73 | + slippage_bps=slippage_bps, |
| 74 | + use_jito=use_jito, |
| 75 | + jito_tip_lamports=int(priority_fee * SOL_DECIMAL) if priority_fee else None, |
| 76 | + ) |
| 77 | + swap_tx = swap_tx_response["swapTransaction"] |
| 78 | + signed_tx = await sign_transaction_from_raw(swap_tx, keypair) |
| 79 | + return signed_tx |
0 commit comments