Python example for buy and sell

Here is a fully functional code for buying and selling using our API. The code naturally requires improvements, such as storing the latest valid JWT token in memory. However, by entering your JWT token from WL.BOT in the JWT_TOKEN field, you can perform buy and sell transactions.

import requests

API_BASE_URL = "https://api-prod.wl.bot/api"
JWT_TOKEN = "your_initial_jwt_token"  # Replace with your actual token


def get_headers(token: str = JWT_TOKEN) -> dict:
    return {
        "Authorization": f"Bearer {token}",
        "Content-Type": "application/json"
    }


def refresh_token(current_token: str = JWT_TOKEN) -> str:
    url = f"{API_BASE_URL}/auth/token/refresh"
    response = requests.get(url, headers=get_headers(current_token))

    if response.status_code == 200:
        new_token = response.json().get("token")
        print("✅ Token refreshed successfully.")
        return new_token
    else:
        raise Exception(f"❌ Failed to refresh token: {response.status_code} {response.text}")


def buy_token(token_address: str, amount_sol: float, token_pool: str = None, token: str = JWT_TOKEN):
    url = f"{API_BASE_URL}/portfolio/handle/execute"
    payload = {
        "token_address": token_address,
        "amount": amount_sol,
        "type": "BUY"
    }
    if token_pool:
        payload["token_pool"] = token_pool

    response = requests.post(url, headers=get_headers(token), json=payload)

    if response.ok:
        print("✅ Buy order executed successfully.")
        return response.json()
    elif response.status_code == 401:
        print("⚠️ Token expired. Please refresh your JWT.")
    else:
        print(f"❌ Buy error: {response.status_code} {response.text}")


def sell_token(token_address: str, percentage: int, token_pool: str = None, token: str = JWT_TOKEN):
    assert 1 <= percentage <= 100, "Percentage must be between 1 and 100"
    url = f"{API_BASE_URL}/portfolio/handle/execute"
    payload = {
        "token_address": token_address,
        "amount": percentage,
        "type": "SELL_PERCENTAGE"
    }
    if token_pool:
        payload["token_pool"] = token_pool

    response = requests.post(url, headers=get_headers(token), json=payload)

    if response.ok:
        print("✅ Sell order executed successfully.")
        return response.json()
    elif response.status_code == 401:
        print("⚠️ Token expired. Please refresh your JWT.")
    else:
        print(f"❌ Sell error: {response.status_code} {response.text}")


if __name__ == "__main__":
    token = JWT_TOKEN

    print("=== Manual Trading ===")
    token_address = input("Enter token address: ").strip()

    action = input("Choose action (buy / sell): ").strip().lower()

    if action == "buy":
        try:
            amount = float(input("Enter amount in SOL: "))
            buy_token(token_address, amount, token=token)
        except ValueError:
            print("❌ Invalid amount.")
    elif action == "sell":
        try:
            percent = int(input("Enter sell percentage (1–100): "))
            sell_token(token_address, percent, token=token)
        except ValueError:
            print("❌ Invalid percentage.")
    else:
        print("❌ Unknown command. Use 'buy' or 'sell'.")

It works

Last updated

Was this helpful?