This commit is contained in:
Arihant Bansal
2025-01-05 00:40:27 +05:30
6 changed files with 146 additions and 147 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "solana-agent-kit",
"version": "1.3.5",
"version": "1.3.6",
"description": "connect any ai agents to solana protocols",
"main": "dist/index.js",
"types": "dist/index.d.ts",

View File

@@ -30,10 +30,10 @@ const pythFetchPriceAction: Action = {
],
],
schema: z.object({
priceFeedId: z
tokenSymbol: z
.string()
.min(1)
.describe("The Pyth price feed ID to fetch the price from"),
.describe("The token symbol to fetch the price for"),
}),
handler: async (_agent: SolanaAgentKit, input: Record<string, any>) => {
try {

View File

@@ -23,8 +23,8 @@ import {
registerDomain,
request_faucet_funds,
trade,
limitOrder,
batchOrder,
// limitOrder,
// batchOrder,
cancelAllOrders,
withdrawAll,
closePerpTradeShort,
@@ -69,7 +69,7 @@ import {
MintCollectionNFTResponse,
PumpfunLaunchResponse,
PumpFunTokenOptions,
OrderParams,
// OrderParams,
} from "../types";
/**
@@ -190,21 +190,21 @@ export class SolanaAgentKit {
return trade(this, outputMint, inputAmount, inputMint, slippageBps);
}
async limitOrder(
marketId: PublicKey,
quantity: number,
side: string,
price: number,
): Promise<string> {
return limitOrder(this, marketId, quantity, side, price);
}
// async limitOrder(
// marketId: PublicKey,
// quantity: number,
// side: string,
// price: number,
// ): Promise<string> {
// return limitOrder(this, marketId, quantity, side, price);
// }
async batchOrder(
marketId: PublicKey,
orders: OrderParams[],
): Promise<string> {
return batchOrder(this, marketId, orders);
}
// async batchOrder(
// marketId: PublicKey,
// orders: OrderParams[],
// ): Promise<string> {
// return batchOrder(this, marketId, orders);
// }
async cancelAllOrders(marketId: PublicKey): Promise<string> {
return cancelAllOrders(this, marketId);

View File

@@ -4,11 +4,10 @@ import Decimal from "decimal.js";
import { Tool } from "langchain/tools";
import {
GibworkCreateTaskReponse,
OrderParams,
PythFetchPriceResponse,
SolanaAgentKit,
} from "../index";
import { create_image, FEE_TIERS, generateOrdersfromPattern } from "../tools";
import { create_image, FEE_TIERS } from "../tools";
export class SolanaBalanceTool extends Tool {
name = "solana_balance";
@@ -414,143 +413,143 @@ export class SolanaTradeTool extends Tool {
}
}
export class SolanaLimitOrderTool extends Tool {
name = "solana_limit_order";
description = `This tool can be used to place limit orders using Manifest.
// export class SolanaLimitOrderTool extends Tool {
// name = "solana_limit_order";
// description = `This tool can be used to place limit orders using Manifest.
Do not allow users to place multiple orders with this instruction, use solana_batch_order instead.
// Do not allow users to place multiple orders with this instruction, use solana_batch_order instead.
Inputs ( input is a JSON string ):
marketId: PublicKey, eg "ENhU8LsaR7vDD2G1CsWcsuSGNrih9Cv5WZEk7q9kPapQ" for SOL/USDC (required)
quantity: number, eg 1 or 0.01 (required)
side: string, eg "Buy" or "Sell" (required)
price: number, in tokens eg 200 for SOL/USDC (required)`;
// Inputs ( input is a JSON string ):
// marketId: PublicKey, eg "ENhU8LsaR7vDD2G1CsWcsuSGNrih9Cv5WZEk7q9kPapQ" for SOL/USDC (required)
// quantity: number, eg 1 or 0.01 (required)
// side: string, eg "Buy" or "Sell" (required)
// price: number, in tokens eg 200 for SOL/USDC (required)`;
constructor(private solanaKit: SolanaAgentKit) {
super();
}
// constructor(private solanaKit: SolanaAgentKit) {
// super();
// }
protected async _call(input: string): Promise<string> {
try {
const parsedInput = JSON.parse(input);
// protected async _call(input: string): Promise<string> {
// try {
// const parsedInput = JSON.parse(input);
const tx = await this.solanaKit.limitOrder(
new PublicKey(parsedInput.marketId),
parsedInput.quantity,
parsedInput.side,
parsedInput.price,
);
// const tx = await this.solanaKit.limitOrder(
// new PublicKey(parsedInput.marketId),
// parsedInput.quantity,
// parsedInput.side,
// parsedInput.price,
// );
return JSON.stringify({
status: "success",
message: "Trade executed successfully",
transaction: tx,
marketId: parsedInput.marketId,
quantity: parsedInput.quantity,
side: parsedInput.side,
price: parsedInput.price,
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "UNKNOWN_ERROR",
});
}
}
}
// return JSON.stringify({
// status: "success",
// message: "Trade executed successfully",
// transaction: tx,
// marketId: parsedInput.marketId,
// quantity: parsedInput.quantity,
// side: parsedInput.side,
// price: parsedInput.price,
// });
// } catch (error: any) {
// return JSON.stringify({
// status: "error",
// message: error.message,
// code: error.code || "UNKNOWN_ERROR",
// });
// }
// }
// }
export class SolanaBatchOrderTool extends Tool {
name = "solana_batch_order";
description = `Places multiple limit orders in one transaction using Manifest. Submit orders either as a list or pattern:
// export class SolanaBatchOrderTool extends Tool {
// name = "solana_batch_order";
// description = `Places multiple limit orders in one transaction using Manifest. Submit orders either as a list or pattern:
1. List format:
{
"marketId": "ENhU8LsaR7vDD2G1CsWcsuSGNrih9Cv5WZEk7q9kPapQ",
"orders": [
{ "quantity": 1, "side": "Buy", "price": 200 },
{ "quantity": 0.5, "side": "Sell", "price": 205 }
]
}
// 1. List format:
// {
// "marketId": "ENhU8LsaR7vDD2G1CsWcsuSGNrih9Cv5WZEk7q9kPapQ",
// "orders": [
// { "quantity": 1, "side": "Buy", "price": 200 },
// { "quantity": 0.5, "side": "Sell", "price": 205 }
// ]
// }
2. Pattern format:
{
"marketId": "ENhU8LsaR7vDD2G1CsWcsuSGNrih9Cv5WZEk7q9kPapQ",
"pattern": {
"side": "Buy",
"totalQuantity": 100,
"priceRange": { "max": 1.0 },
"spacing": { "type": "percentage", "value": 1 },
"numberOfOrders": 5
}
}
// 2. Pattern format:
// {
// "marketId": "ENhU8LsaR7vDD2G1CsWcsuSGNrih9Cv5WZEk7q9kPapQ",
// "pattern": {
// "side": "Buy",
// "totalQuantity": 100,
// "priceRange": { "max": 1.0 },
// "spacing": { "type": "percentage", "value": 1 },
// "numberOfOrders": 5
// }
// }
Examples:
- "Place 5 buy orders totaling 100 tokens, 1% apart below $1"
- "Create 3 sell orders of 10 tokens each between $50-$55"
- "Place buy orders worth 50 tokens, $0.10 spacing from $0.80"
// Examples:
// - "Place 5 buy orders totaling 100 tokens, 1% apart below $1"
// - "Create 3 sell orders of 10 tokens each between $50-$55"
// - "Place buy orders worth 50 tokens, $0.10 spacing from $0.80"
Important: All orders must be in one transaction. Combine buy and sell orders into a single pattern or list. Never break the orders down to individual buy or sell orders.`;
// Important: All orders must be in one transaction. Combine buy and sell orders into a single pattern or list. Never break the orders down to individual buy or sell orders.`;
constructor(private solanaKit: SolanaAgentKit) {
super();
}
// constructor(private solanaKit: SolanaAgentKit) {
// super();
// }
protected async _call(input: string): Promise<string> {
try {
const parsedInput = JSON.parse(input);
let ordersToPlace: OrderParams[] = [];
// protected async _call(input: string): Promise<string> {
// try {
// const parsedInput = JSON.parse(input);
// let ordersToPlace: OrderParams[] = [];
if (!parsedInput.marketId) {
throw new Error("Market ID is required");
}
// if (!parsedInput.marketId) {
// throw new Error("Market ID is required");
// }
if (parsedInput.pattern) {
ordersToPlace = generateOrdersfromPattern(parsedInput.pattern);
} else if (Array.isArray(parsedInput.orders)) {
ordersToPlace = parsedInput.orders;
} else {
throw new Error("Either pattern or orders array is required");
}
// if (parsedInput.pattern) {
// ordersToPlace = generateOrdersfromPattern(parsedInput.pattern);
// } else if (Array.isArray(parsedInput.orders)) {
// ordersToPlace = parsedInput.orders;
// } else {
// throw new Error("Either pattern or orders array is required");
// }
if (ordersToPlace.length === 0) {
throw new Error("No orders generated or provided");
}
// if (ordersToPlace.length === 0) {
// throw new Error("No orders generated or provided");
// }
ordersToPlace.forEach((order: OrderParams, index: number) => {
if (!order.quantity || !order.side || !order.price) {
throw new Error(
`Invalid order at index ${index}: quantity, side, and price are required`,
);
}
if (order.side !== "Buy" && order.side !== "Sell") {
throw new Error(
`Invalid side at index ${index}: must be "Buy" or "Sell"`,
);
}
});
// ordersToPlace.forEach((order: OrderParams, index: number) => {
// if (!order.quantity || !order.side || !order.price) {
// throw new Error(
// `Invalid order at index ${index}: quantity, side, and price are required`,
// );
// }
// if (order.side !== "Buy" && order.side !== "Sell") {
// throw new Error(
// `Invalid side at index ${index}: must be "Buy" or "Sell"`,
// );
// }
// });
const tx = await this.solanaKit.batchOrder(
new PublicKey(parsedInput.marketId),
parsedInput.orders,
);
// const tx = await this.solanaKit.batchOrder(
// new PublicKey(parsedInput.marketId),
// parsedInput.orders,
// );
return JSON.stringify({
status: "success",
message: "Batch order executed successfully",
transaction: tx,
marketId: parsedInput.marketId,
orders: parsedInput.orders,
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "UNKNOWN_ERROR",
});
}
}
}
// return JSON.stringify({
// status: "success",
// message: "Batch order executed successfully",
// transaction: tx,
// marketId: parsedInput.marketId,
// orders: parsedInput.orders,
// });
// } catch (error: any) {
// return JSON.stringify({
// status: "error",
// message: error.message,
// code: error.code || "UNKNOWN_ERROR",
// });
// }
// }
// }
export class SolanaCancelAllOrdersTool extends Tool {
name = "solana_cancel_all_orders";
@@ -2153,8 +2152,8 @@ export function createSolanaTools(solanaKit: SolanaAgentKit) {
new SolanaRaydiumCreateCpmm(solanaKit),
new SolanaOpenbookCreateMarket(solanaKit),
new SolanaManifestCreateMarket(solanaKit),
new SolanaLimitOrderTool(solanaKit),
new SolanaBatchOrderTool(solanaKit),
// new SolanaLimitOrderTool(solanaKit),
// new SolanaBatchOrderTool(solanaKit),
new SolanaCancelAllOrdersTool(solanaKit),
new SolanaWithdrawAllTool(solanaKit),
new SolanaClosePosition(solanaKit),

View File

@@ -1,5 +1,5 @@
export * from "./adrena_perp_trading";
export * from "./batch_order";
// export * from "./batch_order";
export * from "./cancel_all_orders";
export * from "./create_gibwork_task";
export * from "./create_image";
@@ -20,7 +20,7 @@ export * from "./get_tps";
export * from "./get_wallet_address";
export * from "./launch_pumpfun_token";
export * from "./lend";
export * from "./limit_order";
// export * from "./limit_order";
export * from "./manifest_create_market";
export * from "./mint_nft";
export * from "./openbook_create_market";

View File

@@ -11,9 +11,9 @@ export async function fetchPythPriceFeedID(
): Promise<string> {
try {
const stableHermesServiceUrl: string = "https://hermes.pyth.network";
const response = await fetch(
`${stableHermesServiceUrl}/v2/price_feeds/?query=${tokenSymbol}&asset_type=crypto`,
`${stableHermesServiceUrl}/v2/price_feeds?query=${tokenSymbol}&asset_type=crypto`,
);
if (!response.ok) {
@@ -60,7 +60,7 @@ export async function fetchPythPrice(feedID: string): Promise<string> {
const stableHermesServiceUrl: string = "https://hermes.pyth.network";
const response = await fetch(
`${stableHermesServiceUrl}/v2/updates/price/latest/?ids[]=${feedID}`,
`${stableHermesServiceUrl}/v2/updates/price/latest?ids[]=${feedID}`,
);
const data = await response.json();