|
| 1 | +import json |
| 2 | +import os |
| 3 | +from typing import Optional |
| 4 | + |
| 5 | +import httpx |
| 6 | +from dotenv import load_dotenv |
| 7 | + |
| 8 | +from swarms import Agent |
| 9 | + |
| 10 | +load_dotenv() |
| 11 | + |
| 12 | +BASE_URL = "https://swarms.world" |
| 13 | + |
| 14 | +PRIVATE_KEY = os.getenv("PRIVATE_KEY") |
| 15 | +SWARMS_API_KEY = os.getenv("SWARMS_API_KEY") |
| 16 | + |
| 17 | +# System prompt: specialize the agent as a meme-coin launch expert |
| 18 | +MEME_COIN_LAUNCH_SYSTEM_PROMPT = """ |
| 19 | +You are an expert at launching meme coins. Your specialty is: |
| 20 | +
|
| 21 | +- **Naming & branding**: Create catchy, memorable token names and tickers (short, punchy, often 3–5 chars) that fit the meme or theme. |
| 22 | +- **Copy & narrative**: Write sharp, viral-ready descriptions and one-liners that explain the joke or community without sounding corporate. |
| 23 | +- **Creative direction**: Suggest or interpret image/logo ideas (URLs or base64) that match the meme aesthetic—bold, simple, recognizable. |
| 24 | +- **Launch strategy**: When asked, advise on timing, community hooks, and how to describe the token so it resonates with degens and normies alike. |
| 25 | +
|
| 26 | +You have tools: `launch_token(name, description, ticker, image)` to create a token on the Swarms launchpad, and `claim_fees_httpx(contract_address)` to claim fees. Use them when the user wants to launch a token or manage fees. Always confirm key details (name, ticker, description, image) with the user before calling launch_token unless they have already provided everything. Be concise, creative, and on-brand for meme coins. |
| 27 | +""" |
| 28 | + |
| 29 | + |
| 30 | +def launch_token( |
| 31 | + name: str, |
| 32 | + description: str, |
| 33 | + ticker: str, |
| 34 | + image: str, |
| 35 | +): |
| 36 | + """ |
| 37 | + Launches a new token on the Swarms platform via the Launchpad API. |
| 38 | +
|
| 39 | + This function sends a POST request to the Swarms API to create a new token with the specified parameters. |
| 40 | + It uses the API key and the configured private key for authentication and authorization. |
| 41 | +
|
| 42 | + Args: |
| 43 | + name (str): The name of the token to be launched (e.g., "My Cool Token"). |
| 44 | + description (str): A brief description of the token's purpose or use-case. |
| 45 | + ticker (str): The ticker symbol for the token (e.g., "MCT"). |
| 46 | + image (str): A URL or base64-encoded string representing the token image/logo. |
| 47 | +
|
| 48 | + Returns: |
| 49 | + dict: The parsed JSON response from the API containing token creation details |
| 50 | + or error information. Expected successful response keys may include: |
| 51 | + - "success" (bool): Whether the token was created successfully. |
| 52 | + - "token_id" (str or int): The ID of the created token. |
| 53 | + - "message" (str): Additional info or status messages. |
| 54 | +
|
| 55 | + Raises: |
| 56 | + httpx.RequestError: If there is a network problem or the API is unreachable. |
| 57 | + httpx.HTTPStatusError: If the server returns an error status code. |
| 58 | + (Note: these errors will not be caught here; callers should handle as needed.) |
| 59 | +
|
| 60 | + Example: |
| 61 | + >>> result = launch_token( |
| 62 | + ... name="Test Token", |
| 63 | + ... description="Token for demo purposes.", |
| 64 | + ... ticker="TT", |
| 65 | + ... image="https://example.com/img.png" |
| 66 | + ... ) |
| 67 | + >>> print(result["success"]) |
| 68 | + True |
| 69 | +
|
| 70 | + Security Notes: |
| 71 | + - The `PRIVATE_KEY` is sent as part of the payload. Keep your keys secure and |
| 72 | + be careful not to expose them. |
| 73 | + - Ensure that SWARMS_API_KEY and PRIVATE_KEY are set in your environment variables. |
| 74 | +
|
| 75 | + """ |
| 76 | + url = f"{BASE_URL}/api/token/launch" |
| 77 | + headers = { |
| 78 | + "Authorization": f"Bearer {SWARMS_API_KEY}", |
| 79 | + "Content-Type": "application/json", |
| 80 | + } |
| 81 | + data = { |
| 82 | + "name": name, |
| 83 | + "description": description, |
| 84 | + "ticker": ticker, |
| 85 | + "image": image, |
| 86 | + "private_key": PRIVATE_KEY, |
| 87 | + } |
| 88 | + response = httpx.post(url, headers=headers, json=data) |
| 89 | + output = response.json() |
| 90 | + |
| 91 | + return json.dumps(output, indent=4) |
| 92 | + |
| 93 | + |
| 94 | +def claim_fees_httpx( |
| 95 | + contract_address: Optional[str] = None, |
| 96 | +) -> str: |
| 97 | + """ |
| 98 | + Claims fees from the Swarms API using httpx. |
| 99 | +
|
| 100 | + Args: |
| 101 | + contract_address (Optional[str]): The contract address ("ca") to claim from. |
| 102 | + Defaults to a preset address if not provided. |
| 103 | + private_key (Optional[str]): The base58 private key for authorization. |
| 104 | + Must be provided by the caller for security. |
| 105 | +
|
| 106 | + Returns: |
| 107 | + dict: The parsed JSON response from the API containing signature, amount claimed, |
| 108 | + fees, or error information. |
| 109 | + """ |
| 110 | + url = "https://swarms.world/api/product/claimfees" |
| 111 | + private_key = PRIVATE_KEY |
| 112 | + |
| 113 | + payload = {"ca": contract_address, "privateKey": private_key} |
| 114 | + |
| 115 | + try: |
| 116 | + response = httpx.post(url, json=payload, timeout=10) |
| 117 | + response.raise_for_status() |
| 118 | + data = response.json() |
| 119 | + return json.dumps(data, indent=4) |
| 120 | + except httpx.HTTPStatusError as exc: |
| 121 | + # If error response is JSON, attempt to extract "error" or fall back to content string |
| 122 | + try: |
| 123 | + error_data = exc.response.json() |
| 124 | + print( |
| 125 | + "Error:", error_data.get("error", exc.response.text) |
| 126 | + ) |
| 127 | + return error_data |
| 128 | + except Exception: |
| 129 | + print("Error:", exc.response.text) |
| 130 | + return {"error": exc.response.text} |
| 131 | + except httpx.RequestError as exc: |
| 132 | + print( |
| 133 | + f"Network error while requesting {exc.request.url!r}: {exc}" |
| 134 | + ) |
| 135 | + return {"error": str(exc)} |
| 136 | + |
| 137 | + |
| 138 | +# Initialize the agent |
| 139 | +agent = Agent( |
| 140 | + agent_name="Meme-Coin-Launch-Pro", |
| 141 | + agent_description="Expert agent for launching and branding meme coins on the Swarms launchpad; specializes in naming, tickers, viral copy, and token creation.", |
| 142 | + system_prompt=MEME_COIN_LAUNCH_SYSTEM_PROMPT, |
| 143 | + model_name="anthropic/claude-sonnet-4-5", |
| 144 | + dynamic_temperature_enabled=True, |
| 145 | + max_loops=1, |
| 146 | + dynamic_context_window=True, |
| 147 | + streaming_on=True, |
| 148 | + interactive=False, |
| 149 | + top_p=None, |
| 150 | + tools=[claim_fees_httpx, launch_token], |
| 151 | +) |
| 152 | + |
| 153 | +out = agent.run( |
| 154 | + task="I want to launch a meme coin about a cat that thinks it's a CEO. Suggest a name, ticker, short description, and what kind of image would work. If I say 'launch it', use the launch_token tool with those details (you can use a placeholder image URL if I don't provide one).", |
| 155 | +) |
| 156 | + |
| 157 | +print(out) |
0 commit comments