UNPKG

@agentix/plugin-solana-openbook

Version:
140 lines (135 loc) 4.25 kB
// src/index.ts import { PluginBase, SolanaWalletBase as SolanaWalletBase2 } from "agentix"; // src/actions/createOpenbookMarket.ts import { PublicKey } from "@solana/web3.js"; import { z } from "zod"; // src/tools/openbook_create_market.ts import { OPEN_BOOK_PROGRAM, Raydium, TxVersion } from "@raydium-io/raydium-sdk-v2"; import { MintLayout, TOKEN_PROGRAM_ID } from "@solana/spl-token"; import { signOrSendTX } from "agentix"; async function openbookCreateMarket(agent, baseMint, quoteMint, lotSize = 1, tickSize = 0.01) { const raydium = await Raydium.load({ connection: agent.wallet.getConnection() }); const baseMintInfo = await agent.wallet.getConnection().getAccountInfo(baseMint); const quoteMintInfo = await agent.wallet.getConnection().getAccountInfo(quoteMint); if (baseMintInfo?.owner.toString() !== TOKEN_PROGRAM_ID.toBase58() || quoteMintInfo?.owner.toString() !== TOKEN_PROGRAM_ID.toBase58()) { throw new Error( "openbook market only support TOKEN_PROGRAM_ID mints, if you want to create pool with token-2022, please create raydium cpmm pool instead" ); } const { transactions } = await raydium.marketV2.create({ baseInfo: { mint: baseMint, decimals: MintLayout.decode(baseMintInfo.data).decimals }, quoteInfo: { mint: quoteMint, decimals: MintLayout.decode(quoteMintInfo.data).decimals }, lotSize, tickSize, dexProgramId: OPEN_BOOK_PROGRAM, txVersion: TxVersion.V0 }); const txs = await Promise.all( transactions.map(async (tx) => { tx.message.recentBlockhash = (await agent.wallet.getConnection().getLatestBlockhash()).blockhash; return await signOrSendTX(agent, tx); }) ); return txs; } // src/actions/createOpenbookMarket.ts var createOpenbookMarketAction = { name: "CREATE_OPENBOOK_MARKET", similes: [ "create openbook market", "setup trading market", "new openbook market", "create trading pair", "setup dex market", "new trading market" ], description: "Create a new trading market on Openbook DEX", examples: [ [ { input: { baseMint: "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v", // USDC quoteMint: "So11111111111111111111111111111111111111112", // SOL lotSize: 1, tickSize: 0.01 }, output: { status: "success", signatures: ["2ZE7Rz...", "3YKpM1..."], message: "Successfully created Openbook market" }, explanation: "Create a new USDC/SOL market on Openbook with default lot and tick sizes" } ] ], schema: z.object({ baseMint: z.string().min(1).describe("The base token's mint address"), quoteMint: z.string().min(1).describe("The quote token's mint address"), lotSize: z.number().positive().default(1).describe("The minimum order size (lot size)"), tickSize: z.number().positive().default(0.01).describe("The minimum price increment (tick size)") }), handler: async (agent, input) => { try { const baseMint = new PublicKey(input.baseMint); const quoteMint = new PublicKey(input.quoteMint); const lotSize = input.lotSize || 1; const tickSize = input.tickSize || 0.01; const transactions = await openbookCreateMarket( agent, baseMint, quoteMint, lotSize, tickSize ); return { status: "success", transactions, message: "Successfully created Openbook market" }; } catch (error) { return { status: "error", message: `Failed to create Openbook market: ${error.message}` }; } } }; var createOpenbookMarket_default = createOpenbookMarketAction; // src/index.ts var OpenbookPlugin = class extends PluginBase { constructor() { const methods = { openbookCreateMarket }; const actions = [ createOpenbookMarket_default ]; const supportedChains = [ { type: "solana" } ]; super("openbook", methods, actions, supportedChains); } supportsWallet(wallet) { return wallet instanceof SolanaWalletBase2; } }; var index_default = OpenbookPlugin; export { index_default as default };