Add cancel and withdraw functionality

This commit is contained in:
DonDuala
2024-12-30 17:59:32 -04:00
parent 2246367a86
commit 0dea94c5b5
5 changed files with 152 additions and 0 deletions

View File

@@ -22,6 +22,8 @@ import {
request_faucet_funds,
trade,
limitOrder,
cancelAllOrders,
withdrawAll,
transfer,
getTokenDataByAddress,
getTokenDataByTicker,
@@ -162,6 +164,14 @@ export class SolanaAgentKit {
return limitOrder(this, marketId, quantity, side, price);
}
async cancelAllOrders(marketId: PublicKey): Promise<string> {
return cancelAllOrders(this, marketId);
}
async withdrawAll(marketId: PublicKey): Promise<string> {
return withdrawAll(this, marketId);
}
async lendAssets(amount: number): Promise<string> {
return lendAsset(this, amount);
}

View File

@@ -350,6 +350,70 @@ export class SolanaLimitOrderTool extends Tool {
}
}
export class SolanaCancelAllOrdersTool extends Tool {
name = "solana_cancel_all_orders";
description = `This tool can be used to cancel all orders from a Manifest market.
Input ( input is a JSON string ):
marketId: string, eg "ENhU8LsaR7vDD2G1CsWcsuSGNrih9Cv5WZEk7q9kPapQ" for SOL/USDC (required)`;
constructor(private solanaKit: SolanaAgentKit) {
super();
}
protected async _call(input: string): Promise<string> {
try {
const marketId = new PublicKey(input.trim());
const tx = await this.solanaKit.cancelAllOrders(marketId);
return JSON.stringify({
status: "success",
message: "Cancel orders successfully",
transaction: tx,
marketId,
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "UNKNOWN_ERROR",
});
}
}
}
export class SolanaWithdrawAllTool extends Tool {
name = "solana_withdraw_all";
description = `This tool can be used to withdraw all funds from a Manifest market.
Input ( input is a JSON string ):
marketId: string, eg "ENhU8LsaR7vDD2G1CsWcsuSGNrih9Cv5WZEk7q9kPapQ" for SOL/USDC (required)`;
constructor(private solanaKit: SolanaAgentKit) {
super();
}
protected async _call(input: string): Promise<string> {
try {
const marketId = new PublicKey(input.trim());
const tx = await this.solanaKit.withdrawAll(marketId);
return JSON.stringify({
status: "success",
message: "Withdrew successfully",
transaction: tx,
marketId,
});
} catch (error: any) {
return JSON.stringify({
status: "error",
message: error.message,
code: error.code || "UNKNOWN_ERROR",
});
}
}
}
export class SolanaRequestFundsTool extends Tool {
name = "solana_request_funds";
description = "Request SOL from Solana faucet (devnet/testnet only)";
@@ -1700,6 +1764,8 @@ export function createSolanaTools(solanaKit: SolanaAgentKit) {
new SolanaOpenbookCreateMarket(solanaKit),
new SolanaManifestCreateMarket(solanaKit),
new SolanaLimitOrderTool(solanaKit),
new SolanaCancelAllOrdersTool(solanaKit),
new SolanaWithdrawAllTool(solanaKit),
new SolanaClosePostition(solanaKit),
new SolanaOrcaCreateCLMM(solanaKit),
new SolanaOrcaCreateSingleSideLiquidityPool(solanaKit),

View File

@@ -0,0 +1,37 @@
import {
PublicKey,
sendAndConfirmTransaction,
Transaction,
} from "@solana/web3.js";
import { SolanaAgentKit } from "../index";
import { ManifestClient } from "@cks-systems/manifest-sdk";
/**
* Cancels all orders from Manifest
* @param agent SolanaAgentKit instance
* @param marketId Public key for the manifest market
* @returns Transaction signature
*/
export async function cancelAllOrders(
agent: SolanaAgentKit,
marketId: PublicKey,
): Promise<string> {
try {
const mfxClient = await ManifestClient.getClientForMarket(
agent.connection,
marketId,
agent.wallet,
);
const cancelAllOrdersIx = await mfxClient.cancelAllIx();
const signature = await sendAndConfirmTransaction(
agent.connection,
new Transaction().add(cancelAllOrdersIx),
[agent.wallet],
);
return signature;
} catch (error: any) {
throw new Error(`Cancel all orders failed: ${error.message}`);
}
}

View File

@@ -7,6 +7,8 @@ export * from "./mint_nft";
export * from "./transfer";
export * from "./trade";
export * from "./limit_order";
export * from "./cancel_all_orders";
export * from "./withdraw_all";
export * from "./register_domain";
export * from "./resolve_sol_domain";
export * from "./get_primary_domain";

37
src/tools/withdraw_all.ts Normal file
View File

@@ -0,0 +1,37 @@
import {
PublicKey,
sendAndConfirmTransaction,
Transaction,
} from "@solana/web3.js";
import { SolanaAgentKit } from "../index";
import { ManifestClient } from "@cks-systems/manifest-sdk";
/**
* Withdraws all funds from Manifest
* @param agent SolanaAgentKit instance
* @param marketId Public key for the manifest market
* @returns Transaction signature
*/
export async function withdrawAll(
agent: SolanaAgentKit,
marketId: PublicKey,
): Promise<string> {
try {
const mfxClient = await ManifestClient.getClientForMarket(
agent.connection,
marketId,
agent.wallet,
);
const withdrawAllIx = await mfxClient.withdrawAllIx();
const signature = await sendAndConfirmTransaction(
agent.connection,
new Transaction().add(...withdrawAllIx),
[agent.wallet],
);
return signature;
} catch (error: any) {
throw new Error(`Withdraw all failed: ${error.message}`);
}
}