get_swap

Installation

pip install karrotcapital.spiderswap

Usage

Initialization

First, initialize the 'Spiderswap' class with your API key.

from karrotcapital.spiderswap import Spiderswap

spiderswap = Spiderswap(ApiKey);

Method: get_swap

Description

The 'get_swap' method facilitates token swaps by fetching swap data from the SpiderSwap API, converting the amount to lamports and the slippage to basis points (bps) before making the API call.

Parameters

  • 'owner' (str): The wallet address of the owner initiating the swap.

  • 'from_mint' (str): The mint address of the token being swapped from.

  • 'to_mint' (str): The mint address of the token being swapped to.

  • 'amount' (float): The amount of the 'from_mint' token to swap.

  • 'slippage' (float): The acceptable slippage percentage for the swap.

  • 'provider' (str): The liquidity provider.

  • 'pool' (str | null, optional): The specific pool for the swap (default is None).

  • 'from_mint_decimals'(float, optional): The decimal places for the 'from_mint' token (default is 9).

Returns

'SwapResponse': A promise that resolves to a base64-encoded string containing the raw swap data.

'SwapResponse' Class

class SwapResponse(TypedDict):
    base64Transaction: str
    signers: List[str]

Example

async def perform_swap():
    try:
        swap_result = await spiderswap.swap.get_swap(
            'owner_address',
            'from_mint_address',
            'to_mint_address',
            1000000,      # Amount in tokens
            50,           # Slippage percentage
            'provider',
            'pool',
            9             # From Mint Decimals
        );
        print(f"Swap Data: {swap_result}");
    except Exception as e:
        print(f"An error occurred: {e}")

perform_swap();

Error Handling

The method relies on 'get_swap_raw' for error handling, throwing an error if the swap data cannot be fetched and logging the error details for debugging.

Last updated