Skip to content

Commit 4017766

Browse files
authored
Merge pull request #96 from niceberginc/use_switchboard
use_swithcboard
2 parents d436cdc + d9f7895 commit 4017766

File tree

5 files changed

+147
-1
lines changed

5 files changed

+147
-1
lines changed

agentipy/agent/__init__.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2728,4 +2728,26 @@ def transfer_from_multisig_treasury(
27282728
from agentipy.tools.use_squads import SquadsManager
27292729
return SquadsManager.transfer_from_multisig_treasury(self, amount, to, vault_index, mint)
27302730
except Exception as e:
2731-
raise SolanaAgentKitError(f"Failed to transfer from multisig treasury: {e}")
2731+
raise SolanaAgentKitError(f"Failed to transfer from multisig treasury: {e}")
2732+
2733+
def simulate_switchboard_feed(
2734+
self,
2735+
feed: str,
2736+
crossbar_url: Optional[str] = None,
2737+
) -> Optional[Dict[str, Any]]:
2738+
"""
2739+
Simulate a Switchboard feed.
2740+
2741+
Args:
2742+
agent (SolanaAgentKit): The Solana agent instance.
2743+
feed (str): The feed name.
2744+
crossbar_url (str): The Crossbar URL.
2745+
2746+
Returns:
2747+
dict: Simulation details.
2748+
"""
2749+
try:
2750+
from agentipy.tools.use_switchboard import SwitchboardManager
2751+
return SwitchboardManager.simulate_switchboard_feed(self, feed, crossbar_url)
2752+
except Exception as e:
2753+
raise SolanaAgentKitError(f"Failed to simulate Switchboard feed: {e}")

agentipy/langchain/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from agentipy.langchain.solutiofi import get_solutiofi_tools
3434
from agentipy.langchain.squads import get_squads_tools
3535
from agentipy.langchain.stork import get_stork_tools
36+
from agentipy.langchain.switchboard import get_switchboard_tools
3637

3738

3839
def create_solana_tools(solana_kit: SolanaAgentKit):
@@ -75,4 +76,5 @@ def create_solana_tools(solana_kit: SolanaAgentKit):
7576
*get_allora_tools(solana_kit=solana_kit),
7677
*get_solutiofi_tools(solana_kit=solana_kit),
7778
*get_squads_tools(solana_kit=solana_kit),
79+
*get_switchboard_tools(solana_kit=solana_kit),
7880
]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from agentipy.agent import SolanaAgentKit
2+
from agentipy.langchain.switchboard.simulate_switchboard_feed import \
3+
SwitchboardSimulateFeedTool
4+
5+
6+
def get_switchboard_tools(solana_kit: SolanaAgentKit):
7+
return [
8+
SwitchboardSimulateFeedTool(solana_kit=solana_kit)
9+
]
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import json
2+
3+
from langchain.tools import BaseTool
4+
5+
from agentipy.agent import SolanaAgentKit
6+
from agentipy.helpers import validate_input
7+
8+
9+
class SwitchboardSimulateFeedTool(BaseTool):
10+
name: str = "switchboard_simulate_feed"
11+
description: str = """
12+
Simulate a Switchboard feed.
13+
14+
Input: A JSON string with:
15+
{
16+
"feed": "string, the name of the Switchboard feed to simulate.",
17+
"crossbar_url": "string, optional, the Crossbar URL for additional configuration."
18+
}
19+
20+
Output:
21+
{
22+
"simulation_details": "dict, the details of the simulation",
23+
"message": "string, if an error occurs"
24+
}
25+
"""
26+
solana_kit: SolanaAgentKit
27+
28+
async def _arun(self, input: str):
29+
try:
30+
data = json.loads(input)
31+
schema = {
32+
"feed": {"type": str, "required": True},
33+
"crossbar_url": {"type": str, "required": False},
34+
}
35+
validate_input(data, schema)
36+
37+
feed = data["feed"]
38+
crossbar_url = data.get("crossbar_url")
39+
40+
result = await self.solana_kit.simulate_switchboard_feed(feed, crossbar_url)
41+
return {
42+
"status": "success",
43+
"simulation_details": result,
44+
}
45+
except Exception as e:
46+
return {
47+
"status": "error",
48+
"message": str(e),
49+
}
50+
51+
def _run(self, input: str):
52+
raise NotImplementedError("This tool only supports async execution via _arun. Please use the async interface.")

agentipy/tools/use_switchboard.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import logging
2+
from typing import Any, Dict, Optional
3+
4+
import requests
5+
6+
from agentipy.agent import SolanaAgentKit
7+
from agentipy.utils.agentipy_proxy.utils import encrypt_private_key
8+
9+
logger = logging.getLogger(__name__)
10+
11+
12+
class SwitchboardManager:
13+
"""
14+
Manager class to handle Switchboard functionalities such as simulating a Switchboard feed.
15+
"""
16+
17+
@staticmethod
18+
def simulate_switchboard_feed(
19+
agent: SolanaAgentKit,
20+
feed: str,
21+
crossbar_url: Optional[str] = None,
22+
) -> Optional[Dict[str, Any]]:
23+
"""
24+
Simulates a Switchboard feed.
25+
26+
:param agent: An instance of SolanaAgentKit.
27+
:param feed: The feed to be simulated.
28+
:param crossbar_url: The crossbar URL for Switchboard (default provided).
29+
:return: The result of the simulation or error details.
30+
"""
31+
try:
32+
if not feed:
33+
raise ValueError("Feed is required.")
34+
35+
encrypted_private_key = encrypt_private_key(agent.private_key)
36+
37+
payload = {
38+
"requestId": encrypted_private_key["requestId"],
39+
"encrypted_private_key": encrypted_private_key["encryptedPrivateKey"],
40+
"rpc_url": agent.rpc_url,
41+
"open_api_key": agent.openai_api_key,
42+
"feed": feed,
43+
"crossbarUrl": crossbar_url if crossbar_url else None
44+
}
45+
46+
response = requests.post(
47+
f"{agent.base_proxy_url}/{agent.api_version}/switchboard/simulate-switchboard-feed",
48+
json=payload,
49+
headers={"Content-Type": "application/json"},
50+
)
51+
response.raise_for_status()
52+
53+
data = response.json()
54+
return data if data.get("success") else {"success": False, "error": data.get("error", "Unknown error")}
55+
56+
except requests.exceptions.RequestException as http_error:
57+
logger.error(f"HTTP error during Switchboard feed simulation: {http_error}", exc_info=True)
58+
return {"success": False, "error": str(http_error)}
59+
except Exception as error:
60+
logger.error(f"Unexpected error during Switchboard feed simulation: {error}", exc_info=True)
61+
return {"success": False, "error": str(error)}

0 commit comments

Comments
 (0)