mirror of
https://github.com/d0zingcat/solana-agent-kit.git
synced 2026-05-25 15:10:53 +00:00
62 lines
1.6 KiB
TypeScript
62 lines
1.6 KiB
TypeScript
import {
|
|
PublicKey,
|
|
Transaction,
|
|
sendAndConfirmTransaction,
|
|
TransactionInstruction,
|
|
} from "@solana/web3.js";
|
|
import { SolanaAgentKit } from "../index";
|
|
import {
|
|
ManifestClient,
|
|
WrapperPlaceOrderParamsExternal,
|
|
} from "@cks-systems/manifest-sdk";
|
|
import { OrderType } from "@cks-systems/manifest-sdk/client/ts/src/wrapper/types/OrderType";
|
|
|
|
/**
|
|
* Place limit orders using Manifest
|
|
* @param agent SolanaAgentKit instance
|
|
* @param marketId Public key for the manifest market
|
|
* @param quantity Amount to trade in tokens
|
|
* @param side Buy or Sell
|
|
* @param price Price in tokens ie. SOL/USDC
|
|
* @returns Transaction signature
|
|
*/
|
|
export async function limitOrder(
|
|
agent: SolanaAgentKit,
|
|
marketId: PublicKey,
|
|
quantity: number,
|
|
side: string,
|
|
price: number,
|
|
): Promise<string> {
|
|
try {
|
|
const mfxClient = await ManifestClient.getClientForMarket(
|
|
agent.connection,
|
|
marketId,
|
|
agent.wallet,
|
|
);
|
|
|
|
const orderParams: WrapperPlaceOrderParamsExternal = {
|
|
numBaseTokens: quantity,
|
|
tokenPrice: price,
|
|
isBid: side === "Buy",
|
|
lastValidSlot: 0,
|
|
orderType: OrderType.Limit,
|
|
clientOrderId: Number(Math.random() * 1000),
|
|
};
|
|
|
|
const depositPlaceOrderIx: TransactionInstruction[] =
|
|
await mfxClient.placeOrderWithRequiredDepositIx(
|
|
agent.wallet.publicKey,
|
|
orderParams,
|
|
);
|
|
const signature = await sendAndConfirmTransaction(
|
|
agent.connection,
|
|
new Transaction().add(...depositPlaceOrderIx),
|
|
[agent.wallet],
|
|
);
|
|
|
|
return signature;
|
|
} catch (error: any) {
|
|
throw new Error(`Limit Order failed: ${error.message}`);
|
|
}
|
|
}
|