@agentix/plugin-solana-openbook
Version:
157 lines (151 loc) • 5.34 kB
JavaScript
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
default: () => index_default
});
module.exports = __toCommonJS(index_exports);
var import_agentix2 = require("agentix");
// src/actions/createOpenbookMarket.ts
var import_web3 = require("@solana/web3.js");
var import_zod = require("zod");
// src/tools/openbook_create_market.ts
var import_raydium_sdk_v2 = require("@raydium-io/raydium-sdk-v2");
var import_spl_token = require("@solana/spl-token");
var import_agentix = require("agentix");
async function openbookCreateMarket(agent, baseMint, quoteMint, lotSize = 1, tickSize = 0.01) {
const raydium = await import_raydium_sdk_v2.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() !== import_spl_token.TOKEN_PROGRAM_ID.toBase58() || quoteMintInfo?.owner.toString() !== import_spl_token.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: import_spl_token.MintLayout.decode(baseMintInfo.data).decimals
},
quoteInfo: {
mint: quoteMint,
decimals: import_spl_token.MintLayout.decode(quoteMintInfo.data).decimals
},
lotSize,
tickSize,
dexProgramId: import_raydium_sdk_v2.OPEN_BOOK_PROGRAM,
txVersion: import_raydium_sdk_v2.TxVersion.V0
});
const txs = await Promise.all(
transactions.map(async (tx) => {
tx.message.recentBlockhash = (await agent.wallet.getConnection().getLatestBlockhash()).blockhash;
return await (0, import_agentix.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: import_zod.z.object({
baseMint: import_zod.z.string().min(1).describe("The base token's mint address"),
quoteMint: import_zod.z.string().min(1).describe("The quote token's mint address"),
lotSize: import_zod.z.number().positive().default(1).describe("The minimum order size (lot size)"),
tickSize: import_zod.z.number().positive().default(0.01).describe("The minimum price increment (tick size)")
}),
handler: async (agent, input) => {
try {
const baseMint = new import_web3.PublicKey(input.baseMint);
const quoteMint = new import_web3.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 import_agentix2.PluginBase {
constructor() {
const methods = {
openbookCreateMarket
};
const actions = [
createOpenbookMarket_default
];
const supportedChains = [
{
type: "solana"
}
];
super("openbook", methods, actions, supportedChains);
}
supportsWallet(wallet) {
return wallet instanceof import_agentix2.SolanaWalletBase;
}
};
var index_default = OpenbookPlugin;