forked from sendaifun/solana-agent-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add switchboard feed simulation tool (sendaifun#246)
# Pull Request Description [Switchboard](https://switchboard.xyz/) is a decentralized oracle network that provides reliable and tamper-proof data feeds to smart contracts on blockchain platforms. It allows developers to access real-world data, such as price feeds, weather information, and other external data sources, which can be used in decentralized applications (dApps). Switchboard aims to enhance the functionality of smart contracts by enabling them to interact with off-chain data securely and efficiently. ## Changes Made This PR adds the following changes: - This PR adds the tools and actions needed to enable agents to simulate switchboard feeds. You must provide the feed hash of a Switchboard feed (you can browse feeds [here](https://ondemand.switchboard.xyz/solana/mainnet)) and the agent will simulate it to get the current value. ## Implementation Details <!-- Provide technical details about the implementation --> - Followed `guides/add_your_own_tool.md` directions. - ## Transaction executed by agent N/A ## Prompt Used <!-- If relevant, include the prompt or configuration used --> ``` - simulate the following switchboard feed: DwYF1yveo8XTF1oqfsqykj332rjSxAd7bR6Gu6i4iUET - simulate the following switchboard feed: DwYF1yveo8XTF1oqfsqykj332rjSxAd7bR6Gu6i4iUET using this crossbar URL: https://crossbar.switchboard.xyz ``` ## Additional Notes Screenshot: <img width="1791" alt="image" src="https://github.com/user-attachments/assets/df84d6a5-3409-4f07-b002-9ec2d19d8490" /> ## Checklist - [x] I have tested these changes locally - [x] I have updated the documentation - [x] I have added a transaction link - [x] I have added the prompt used to test it
- Loading branch information
Showing
14 changed files
with
191 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Action } from "../../types/action"; | ||
import { SolanaAgentKit } from "../../agent"; | ||
import { z } from "zod"; | ||
import { simulate_switchboard_feed } from "../../tools"; | ||
import { SWITCHBOARD_DEFAULT_CROSSBAR } from "../../constants"; | ||
|
||
const switchboardSimulateFeedAction: Action = { | ||
name: "SWITCHBOARD_SIMULATE_FEED", | ||
similes: [ | ||
"simulate switchboard price feed", | ||
"simulate switchboard feed", | ||
"switchboard oracle feed", | ||
"get switchboard price", | ||
"check switchboard price", | ||
"switchboard price", | ||
"switchbaord feed", | ||
], | ||
description: | ||
"Simulates a given switchboard price feed and returns the value.", | ||
examples: [ | ||
[ | ||
{ | ||
input: { | ||
feed: "6qmsMwtMmeqMgZEhyLv1Pe4wcqT5iKwJAWnmzmnKjf83", // BTC/USDT price feed | ||
}, | ||
output: { | ||
status: "success", | ||
value: "104097.59", | ||
message: "Simulation result: 104097.59", | ||
}, | ||
explanation: | ||
"Get the current BTC/USDT price by simulating a Switchbaord feed", | ||
}, | ||
], | ||
], | ||
schema: z.object({ | ||
feed: z | ||
.string() | ||
.describe("The address of the Switchboard feed to simulate"), | ||
crossbarUrl: z | ||
.string() | ||
.default(SWITCHBOARD_DEFAULT_CROSSBAR) | ||
.describe("The url of the crossbar server to use"), | ||
}), | ||
handler: async (agent: SolanaAgentKit, input: Record<string, any>) => { | ||
try { | ||
const { feedAddress, crossbarUrl } = input; | ||
const result = await simulate_switchboard_feed(feedAddress, crossbarUrl); | ||
return { | ||
status: "success", | ||
feed: feedAddress, | ||
message: `Simulation result: ${result}`, | ||
}; | ||
} catch (error: any) { | ||
return { | ||
status: "error", | ||
message: `Failed to simulate Switchboard feed: ${error.message}`, | ||
}; | ||
} | ||
}, | ||
}; | ||
|
||
export default switchboardSimulateFeedAction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./switchboard_simulate_feed"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Tool } from "langchain/tools"; | ||
import { SolanaAgentKit } from "../../agent"; | ||
import { SwitchboardSimulateFeedResponse } from "../../index"; | ||
|
||
export class SolanaSwitchboardSimulateFeed extends Tool { | ||
name = "switchboard_simulate_feed"; | ||
description = `Simluates a Switchboard price feed given the feed's public key. | ||
Input should be a JSON string with the following format: | ||
{ | ||
"feed": string (required) - the public key (a.k.a. feed hash) of the feed to simulate | ||
"crossbarUrl": string (optional) - the url of the crossbar instance to use. Defaults to "https://crossbar.switchboard.xyz" | ||
} | ||
`; | ||
|
||
constructor(private solanaKit: SolanaAgentKit) { | ||
super(); | ||
} | ||
|
||
async _call(input: string): Promise<string> { | ||
try { | ||
const InputFormat = JSON.parse(input); | ||
const feed = InputFormat.feed; | ||
const crossbarUrl = InputFormat.crossbarUrl; | ||
|
||
const value = await this.solanaKit.simulateSwitchboardFeed( | ||
feed, | ||
crossbarUrl, | ||
); | ||
|
||
const response: SwitchboardSimulateFeedResponse = { | ||
status: "success", | ||
feed, | ||
value: Number.parseInt(value), | ||
}; | ||
|
||
return JSON.stringify(response); | ||
} catch (error: any) { | ||
const response: SwitchboardSimulateFeedResponse = { | ||
status: "error", | ||
message: error.message, | ||
code: error.code, | ||
}; | ||
return JSON.stringify(response); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from "./simulate_feed"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { SolanaAgentKit } from "../../index"; | ||
import { SWITCHBOARD_DEFAULT_CROSSBAR } from "../../constants"; | ||
import { CrossbarClient } from "@switchboard-xyz/common"; | ||
|
||
/** | ||
* Simulate a switchboard feed | ||
* @param agent SolanaAgentKit instance | ||
* @param feed Public key of the feed to simulate as base58 | ||
* @param crossbarUrl The url of the crossbar instance to use | ||
* @returns Result of the simulation | ||
*/ | ||
|
||
export async function simulate_switchboard_feed( | ||
agent: SolanaAgentKit, | ||
feed: string, | ||
crossbarUrl: string = SWITCHBOARD_DEFAULT_CROSSBAR, | ||
): Promise<string> { | ||
try { | ||
const crossbar = new CrossbarClient(crossbarUrl, true); | ||
const results = await crossbar.simulateSolanaFeeds("mainnet", [feed]); | ||
|
||
if (results.length === 0) { | ||
throw new Error( | ||
`Error simulating feed ${feed}. Did you provide the right mainnet feed hash?`, | ||
); | ||
} | ||
|
||
return results[0].results.toString(); | ||
} catch (error: any) { | ||
throw new Error(`Error: ${error.message}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters