From 534d88b47ad1596ff6af15b304e364cac9392203 Mon Sep 17 00:00:00 2001 From: DonDuala Date: Sat, 4 Jan 2025 18:14:28 -0400 Subject: [PATCH] Combine manifest tool files --- src/tools/cancel_all_orders.ts | 37 ---- src/tools/index.ts | 6 +- src/tools/limit_order.ts | 61 ------- src/tools/manifest_create_market.ts | 43 ----- .../{batch_order.ts => manifest_trade.ts} | 161 +++++++++++++++++- src/tools/withdraw_all.ts | 37 ---- 6 files changed, 153 insertions(+), 192 deletions(-) delete mode 100644 src/tools/cancel_all_orders.ts delete mode 100644 src/tools/limit_order.ts delete mode 100644 src/tools/manifest_create_market.ts rename src/tools/{batch_order.ts => manifest_trade.ts} (50%) delete mode 100644 src/tools/withdraw_all.ts diff --git a/src/tools/cancel_all_orders.ts b/src/tools/cancel_all_orders.ts deleted file mode 100644 index d8e3639..0000000 --- a/src/tools/cancel_all_orders.ts +++ /dev/null @@ -1,37 +0,0 @@ -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 { - 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}`); - } -} diff --git a/src/tools/index.ts b/src/tools/index.ts index 2544fed..2bead34 100644 --- a/src/tools/index.ts +++ b/src/tools/index.ts @@ -1,6 +1,4 @@ export * from "./adrena_perp_trading"; -export * from "./batch_order"; -export * from "./cancel_all_orders"; export * from "./create_gibwork_task"; export * from "./create_image"; export * from "./create_tiplinks"; @@ -20,8 +18,7 @@ export * from "./get_tps"; export * from "./get_wallet_address"; export * from "./launch_pumpfun_token"; export * from "./lend"; -export * from "./limit_order"; -export * from "./manifest_create_market"; +export * from "./manifest_trade"; export * from "./mint_nft"; export * from "./openbook_create_market"; export * from "./orca_close_position"; @@ -46,4 +43,3 @@ export * from "./stake_with_solayer"; export * from "./tensor_trade"; export * from "./trade"; export * from "./transfer"; -export * from "./withdraw_all"; diff --git a/src/tools/limit_order.ts b/src/tools/limit_order.ts deleted file mode 100644 index bdbb63d..0000000 --- a/src/tools/limit_order.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { - PublicKey, - Transaction, - sendAndConfirmTransaction, - TransactionInstruction, -} from "@solana/web3.js"; -import { SolanaAgentKit } from "../index"; -import { - ManifestClient, - WrapperPlaceOrderParamsExternal, - OrderType, -} from "@cks-systems/manifest-sdk"; - -/** - * 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 { - 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}`); - } -} diff --git a/src/tools/manifest_create_market.ts b/src/tools/manifest_create_market.ts deleted file mode 100644 index e0960f4..0000000 --- a/src/tools/manifest_create_market.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { ManifestClient } from "@cks-systems/manifest-sdk"; -import { - Keypair, - PublicKey, - sendAndConfirmTransaction, - SystemProgram, - Transaction, - TransactionInstruction, -} from "@solana/web3.js"; -import { SolanaAgentKit } from "../index"; - -export async function manifestCreateMarket( - agent: SolanaAgentKit, - baseMint: PublicKey, - quoteMint: PublicKey, -): Promise { - const marketKeypair: Keypair = Keypair.generate(); - const FIXED_MANIFEST_HEADER_SIZE: number = 256; - const createAccountIx: TransactionInstruction = SystemProgram.createAccount({ - fromPubkey: agent.wallet.publicKey, - newAccountPubkey: marketKeypair.publicKey, - space: FIXED_MANIFEST_HEADER_SIZE, - lamports: await agent.connection.getMinimumBalanceForRentExemption( - FIXED_MANIFEST_HEADER_SIZE, - ), - programId: new PublicKey("MNFSTqtC93rEfYHB6hF82sKdZpUDFWkViLByLd1k1Ms"), - }); - const createMarketIx = ManifestClient["createMarketIx"]( - agent.wallet.publicKey, - baseMint, - quoteMint, - marketKeypair.publicKey, - ); - - const tx: Transaction = new Transaction(); - tx.add(createAccountIx); - tx.add(createMarketIx); - const signature = await sendAndConfirmTransaction(agent.connection, tx, [ - agent.wallet, - marketKeypair, - ]); - return [signature, marketKeypair.publicKey.toBase58()]; -} diff --git a/src/tools/batch_order.ts b/src/tools/manifest_trade.ts similarity index 50% rename from src/tools/batch_order.ts rename to src/tools/manifest_trade.ts index 83080fe..e65aef7 100644 --- a/src/tools/batch_order.ts +++ b/src/tools/manifest_trade.ts @@ -1,16 +1,159 @@ -import { - PublicKey, - Transaction, - sendAndConfirmTransaction, - TransactionInstruction, -} from "@solana/web3.js"; import { ManifestClient, - WrapperPlaceOrderParamsExternal, OrderType, + WrapperPlaceOrderParamsExternal, } from "@cks-systems/manifest-sdk"; -import { SolanaAgentKit } from "../index"; -import { BatchOrderPattern, OrderParams } from "../types"; +import { + Keypair, + PublicKey, + sendAndConfirmTransaction, + SystemProgram, + Transaction, + TransactionInstruction, +} from "@solana/web3.js"; +import { BatchOrderPattern, OrderParams, SolanaAgentKit } from "../index"; + +export async function manifestCreateMarket( + agent: SolanaAgentKit, + baseMint: PublicKey, + quoteMint: PublicKey, +): Promise { + const marketKeypair: Keypair = Keypair.generate(); + const FIXED_MANIFEST_HEADER_SIZE: number = 256; + const createAccountIx: TransactionInstruction = SystemProgram.createAccount({ + fromPubkey: agent.wallet.publicKey, + newAccountPubkey: marketKeypair.publicKey, + space: FIXED_MANIFEST_HEADER_SIZE, + lamports: await agent.connection.getMinimumBalanceForRentExemption( + FIXED_MANIFEST_HEADER_SIZE, + ), + programId: new PublicKey("MNFSTqtC93rEfYHB6hF82sKdZpUDFWkViLByLd1k1Ms"), + }); + const createMarketIx = ManifestClient["createMarketIx"]( + agent.wallet.publicKey, + baseMint, + quoteMint, + marketKeypair.publicKey, + ); + + const tx: Transaction = new Transaction(); + tx.add(createAccountIx); + tx.add(createMarketIx); + const signature = await sendAndConfirmTransaction(agent.connection, tx, [ + agent.wallet, + marketKeypair, + ]); + return [signature, marketKeypair.publicKey.toBase58()]; +} + +/** + * 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 { + 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}`); + } +} + +/** + * 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 { + 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}`); + } +} + +/** + * 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 { + 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}`); + } +} /** * Generates an array of orders based on the specified pattern diff --git a/src/tools/withdraw_all.ts b/src/tools/withdraw_all.ts deleted file mode 100644 index 99e3bc1..0000000 --- a/src/tools/withdraw_all.ts +++ /dev/null @@ -1,37 +0,0 @@ -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 { - 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}`); - } -}