Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Jupiter Limit Order actions #324

Merged
merged 8 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ import getWormholeSupportedChainsAction from "./wormhole/getSupportedChains";
import cctpTransferAction from "./wormhole/cctpTransfer";
import createWrappedTokenAction from "./wormhole/createWrappedToken";
import tokenTransferAction from "./wormhole/tokenTransfer";
import getLimitOrderHistoryAction from "./jupiter/getLimitOrderHistory";
import createLimitOrderAction from "./jupiter/createLimitOrder";
import cancelLimitOrdersAction from "./jupiter/cancelLimitOrders";
import getOpenLimitOrdersAction from "./jupiter/getOpenLimitOrders";

export const ACTIONS = {
GET_INFO_ACTION: getInfoAction,
Expand Down Expand Up @@ -242,6 +246,10 @@ export const ACTIONS = {
CCTP_TRANSFER_ACTION: cctpTransferAction,
CREATE_WRAPPED_TOKEN_ACTION: createWrappedTokenAction,
TOKEN_TRANSFER_ACTION: tokenTransferAction,
CREATE_LIMIT_ORDER_ACTION: createLimitOrderAction,
CANCEL_LIMIT_ORDERS_ACTION: cancelLimitOrdersAction,
GET_LIMIT_ORDER_HISTORY_ACTION: getLimitOrderHistoryAction,
GET_OPEN_LIMIT_ORDERS_ACTION: getOpenLimitOrdersAction,
};

export type { Action, ActionExample, Handler } from "../types/action";
69 changes: 69 additions & 0 deletions src/actions/jupiter/cancelLimitOrders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Action } from "../../types/action";
import { SolanaAgentKit } from "../../agent";
import { z } from "zod";
import { cancelLimitOrders } from "../../tools/jupiter/cancel_limit_orders";

const cancelOrdersSchema = z.object({
orders: z.array(z.string()).describe("The order public keys to cancel"),
});

const cancelOrdersAction: Action = {
name: "CANCEL_LIMIT_ORDERS",
similes: [
"abort orders",
"cancel limit order",
"revoke orders",
"terminate orders",
],
description: "Cancels specified orders on the Solana blockchain.",
examples: [
[
{
input: {
orders: ["GgMvwcfMz...ienihZvTmyBZYM", "HhNvwcfMz...Qa8ihZvTmyBZYN"],
},
output: {
signatures: ["5K3N9...3J4", "6L4O0...4K5"],
success: true,
explanation: "Orders canceled successfully.",
},
explanation: "Successfully canceled the specified orders.",
},
{
input: {
orders: ["InvalidOrderKey"],
},
output: {
signatures: [],
success: false,
error: "Error canceling orders: Invalid order key",
explanation: "Failed to cancel orders due to invalid order key.",
},
explanation: "Failed to cancel orders due to an invalid order key.",
},
],
],
schema: cancelOrdersSchema,
handler: async (agent: SolanaAgentKit, input: Record<string, any>) => {
const params = cancelOrdersSchema.parse(input);

try {
const result = await cancelLimitOrders(agent, params);

return {
status: "success",
result,
message: "Orders canceled successfully.",
};
} catch (error) {
const errorMessage = `Error canceling orders: ${error}`;
console.error(errorMessage);
return {
status: "error",
message: errorMessage,
};
}
},
};

export default cancelOrdersAction;
73 changes: 73 additions & 0 deletions src/actions/jupiter/createLimitOrder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Action } from "../../types/action";
import { SolanaAgentKit } from "../../agent";
import { z } from "zod";
import { createLimitOrder } from "../../tools/jupiter/create_limit_order";

const createLimitOrderSchema = z.object({
inputMint: z.string(),
outputMint: z.string(),
params: z.object({
makingAmount: z.string(),
takingAmount: z.string(),
expiredAt: z.string().optional(),
}),
});

export const createLimitOrderAction: Action = {
name: "CREATE_LIMIT_ORDER",
similes: ["place limit order", "submit limit order", "create trading order"],
description: "Creates and sends a limit order on the Solana blockchain.",
examples: [
[
{
input: {
inputAmount: "5500000",
outputAmount: "50000000",
},
output: {
signature: "5K3N9...3J4",
order: "order123",
success: true,
explanation: "Order created and sent successfully.",
},
explanation:
"Successfully created a limit order with specified amounts.",
},
{
input: {
inputAmount: "1000000",
outputAmount: "20000000",
},
output: {
signature: "",
order: "",
success: false,
error: "Error creating and sending limit order: Network error",
explanation: "Failed to create and send the order.",
},
explanation: "Failed to create a limit order due to a network error.",
},
],
],
schema: createLimitOrderSchema,
handler: async (agent: SolanaAgentKit, input: Record<string, any>) => {
const params = createLimitOrderSchema.parse(input);

try {
const result = await createLimitOrder(agent, params);

return {
status: "success",
result,
message: `Order created and sent successfully.`,
};
} catch (error) {
return {
status: "error",
message: `Failed to create limit order: ${error}`,
};
}
},
};

export default createLimitOrderAction;
81 changes: 81 additions & 0 deletions src/actions/jupiter/getLimitOrderHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Action } from "../../types/action";
import { SolanaAgentKit } from "../../agent";
import { z } from "zod";
import { getLimitOrderHistory } from "../../tools/jupiter/get_limit_order_history";

const getLimitOrderHistoryAction: Action = {
name: "GET_LIMIT_ORDER_HISTORY",
similes: [
"fetch order history",
"get limit order history",
"retrieve order history",
"get past orders",
],
description: "Fetches the limit order history for a given wallet.",
examples: [
[
{
input: {
walletPublicKey: "CmwPTro4ogHPhuG9Dozx1X7KiATNudF1rkem3BQmuPn7",
page: 1,
},
output: {
history: {
orders: [
{
userPubkey: "CmwPTro4ogHPhuG9Dozx1X7KiATNudF1rkem3BQmuPn7",
orderKey: "GgMvwcfMzP9AmfwZuMzNienXGBhQa8dksihZvTmyBZYM",
inputMint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
outputMint: "So11111111111111111111111111111111111111112",
makingAmount: "10000000",
takingAmount: "50000000",
remainingMakingAmount: "50000000",
remainingTakingAmount: "10000000",
expiredAt: null,
createdAt: "2023-10-01T00:00:00Z",
updatedAt: "2023-10-02T00:00:00Z",
status: "Open",
openTx:
"https://solscan.io/tx/2431GhdanFFwWg...77BBCSk34SW2iFHwu17zQARjr",
closeTx: "",
programVersion: "1.0",
trades: [
{
amount: "10000000",
price: "50000000",
timestamp: "2023-10-01T01:00:00Z",
},
],
},
],
hasMoreData: false,
page: 1,
},
success: true,
},
explanation: "Successfully fetched order history for the given wallet.",
},
],
],
schema: z.object({}),
handler: async (agent: SolanaAgentKit) => {
try {
const history = await getLimitOrderHistory(agent);
return {
status: "success",
result: { history, success: true },
message: "Successfully fetched order history for the given wallet.",
};
} catch (error) {
const errorMessage = `Error fetching order history: ${error}`;
console.error(errorMessage);
return {
status: "error",
message: errorMessage,
result: { history: [], success: false },
};
}
},
};

export default getLimitOrderHistoryAction;
71 changes: 71 additions & 0 deletions src/actions/jupiter/getOpenLimitOrders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Action } from "../../types/action";
import { SolanaAgentKit } from "../../agent";
import { getOpenLimitOrders } from "../../tools/jupiter/get_open_limit_orders";
import { z } from "zod";

const getOpenLimitOrdersAction: Action = {
name: "GET_OPEN_LIMIT_ORDERS",
similes: ["fetch open orders", "get limit orders", "retrieve open orders"],
description: "Fetches the open limit orders for a given wallet.",
examples: [
[
{
input: {
walletPublicKey: "CmwPTro4ogHP...muPn7",
},
output: {
orders: [
{
userPubkey: "CmwPTro4ogHP...muPn7",
orderKey: "GgMvwcfM...vTmyBZYM",
inputMint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
outputMint: "So11111111111111111111111111111111111111112",
makingAmount: "10000000",
takingAmount: "50000000",
remainingMakingAmount: "50000000",
remainingTakingAmount: "10000000",
expiredAt: null,
createdAt: "2023-10-01T00:00:00Z",
updatedAt: "2023-10-02T00:00:00Z",
status: "Open",
openTx:
"https://solscan.io/tx/2431GhdanFFwWg...77BBCSk34SW2iFHwu17zQARjr",
closeTx: "",
programVersion: "1.0",
trades: [
{
amount: "10000000",
price: "50000000",
timestamp: "2023-10-01T01:00:00Z",
},
],
},
],
success: true,
},
explanation: "Successfully fetched open orders for the given wallet.",
},
],
],
schema: z.object({}),
handler: async (agent: SolanaAgentKit) => {
try {
const orders = await getOpenLimitOrders(agent);
return {
status: "success",
result: { orders, success: true },
message: "Successfully fetched open orders for the given wallet.",
};
} catch (error) {
const errorMessage = `Error fetching open orders: ${error}`;
console.error(errorMessage);
return {
status: "error",
message: errorMessage,
result: { orders: [], success: false },
};
}
},
};

export default getOpenLimitOrdersAction;
41 changes: 40 additions & 1 deletion src/agent/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ import {
CctpTransferInput,
TokenTransferInput,
CreateWrappedTokenInput,
CreateJupiterOrderRequest,
CancelJupiterOrderRequest,
} from "../types";
import {
DasApiAsset,
Expand All @@ -187,14 +189,17 @@ import {
getTrendingTokensUsingElfaAi,
getSmartTwitterAccountStats,
} from "../tools/elfa_ai";
import { Chain, TokenId } from "@wormhole-foundation/sdk/dist/cjs";
import {
getQuote as getOkxQuote,
executeSwap as executeOkxSwapTool,
getTokens,
getChainData,
getLiquidity,
} from "../tools/okx-dex";
import { createLimitOrder } from "../tools/jupiter/create_limit_order";
import { cancelLimitOrders } from "../tools/jupiter/cancel_limit_orders";
import { getOpenLimitOrders } from "../tools/jupiter/get_open_limit_orders";
import { getLimitOrderHistory } from "../tools/jupiter/get_limit_order_history";

/**
* Main class for interacting with Solana blockchain
Expand Down Expand Up @@ -1390,4 +1395,38 @@ export class SolanaAgentKit {
async getOkxChainData() {
return getChainData(this);
}

/**
* Create a limit order on Jupiter
* @param params Parameters for creating the limit order
* @returns Result of the limit order creation
*/
async createJupiterLimitOrder(params: CreateJupiterOrderRequest) {
return createLimitOrder(this, params);
}

/**
* Cancel limit orders on Jupiter
* @param params Parameters for canceling the orders
* @returns Result of the order cancellation
*/
async cancelJupiterLimitOrders(params: CancelJupiterOrderRequest) {
return cancelLimitOrders(this, params);
}

/**
* Get open limit orders on Jupiter
* @returns List of open limit orders
*/
async getOpenJupiterLimitOrders() {
return getOpenLimitOrders(this);
}

/**
* Get limit order history on Jupiter
* @returns Limit order history
*/
async getJupiterLimitOrderHistory() {
return getLimitOrderHistory(this);
}
}
Loading
Loading