Skip to main content

command

case

💡交易命令介绍:

/buy: 立即买入代币 /sell: 立即卖出代币 /create: 创建买/卖限价单

示例命令: /buy ukHH6c7mMyiWCf1b9pnWe25TSpkDDt3H5pQZgZ74J82 0.5 表示立即买入 0.5 SOL BOME代币

/sell ukHH6c7mMyiWCf1b9pnWe25TSpkDDt3H5pQZgZ74J82 50 /sell ukHH6c7mMyiWCf1b9pnWe25TSpkDDt3H5pQZgZ74J82 50% 表示立即卖出 50% BOME代币持仓

/create limitbuy ukHH6c7mMyiWCf1b9pnWe25TSpkDDt3H5pQZgZ74J82 0.5@0.1 -exp 3600 表示创建BOME代币限价买单,金额 0.5 SOL,触发价 $0.1,过期时间1小时(3600秒)

/create limitsell ukHH6c7mMyiWCf1b9pnWe25TSpkDDt3H5pQZgZ74J82 50@0.1 -exp 3600 表示创建BOME代币限价卖单,卖出50%持仓,触发价 $0.1,过期时间1小时(3600秒)

如果'-exp'参数未填写,则限价单的默认有效期是3天

code

要实现这些交易命令的功能,你可以使用 grammy 处理命令,并根据不同的命令参数执行相应的逻辑。以下是如何实现这些功能的示例:

代码示例

import { Bot } from "grammy";

const bot = new Bot("<your-bot-token>");

// 处理 /buy 命令
bot.command("buy", async (ctx) => {
const [token, amount] = ctx.match.split(" ");
if (!token || !amount) {
return ctx.reply("Usage: /buy <token> <amount>");
}
// 调用买入逻辑
await buyToken(token, parseFloat(amount));
ctx.reply(`Bought ${amount} of token ${token}`);
});

// 处理 /sell 命令
bot.command("sell", async (ctx) => {
const [token, amount] = ctx.match.split(" ");
if (!token || !amount) {
return ctx.reply("Usage: /sell <token> <amount>");
}
// 调用卖出逻辑
await sellToken(token, amount);
ctx.reply(`Sold ${amount} of token ${token}`);
});

// 处理 /create 命令
bot.command("create", async (ctx) => {
const [type, token, amountPrice, exp] = ctx.match.split(" ");
if (!type || !token || !amountPrice) {
return ctx.reply("Usage: /create <type> <token> <amount@price> [-exp <seconds>]");
}

const [amount, price] = amountPrice.split("@");
const expiration = exp ? parseInt(exp.split(" ")[1]) : 259200; // 默认3天

if (type === "limitbuy") {
// 调用限价买入逻辑
await createLimitBuy(token, parseFloat(amount), parseFloat(price), expiration);
ctx.reply(`Created limit buy order for ${amount} of ${token} at $${price} with expiration ${expiration} seconds`);
} else if (type === "limitsell") {
// 调用限价卖出逻辑
await createLimitSell(token, parseFloat(amount), parseFloat(price), expiration);
ctx.reply(`Created limit sell order for ${amount} of ${token} at $${price} with expiration ${expiration} seconds`);
} else {
ctx.reply("Invalid order type. Use 'limitbuy' or 'limitsell'.");
}
});

// 模拟买入逻辑
async function buyToken(token: string, amount: number) {
console.log(`Buying ${amount} of ${token}`);
}

// 模拟卖出逻辑
async function sellToken(token: string, amount: string) {
console.log(`Selling ${amount} of ${token}`);
}

// 模拟创建限价买单逻辑
async function createLimitBuy(token: string, amount: number, price: number, expiration: number) {
console.log(`Creating limit buy order for ${amount} of ${token} at $${price} with expiration ${expiration}`);
}

// 模拟创建限价卖单逻辑
async function createLimitSell(token: string, amount: number, price: number, expiration: number) {
console.log(`Creating limit sell order for ${amount} of ${token} at $${price} with expiration ${expiration}`);
}

// 启动机器人
bot.start();

说明

  1. 命令解析:使用 ctx.match.split(" ") 分割命令参数。
  2. 参数验证:检查必要参数是否提供,并给出使用说明。
  3. 逻辑处理:根据命令类型调用相应的逻辑函数,如 buyTokensellTokencreateLimitBuycreateLimitSell
  4. 默认值处理:如果没有提供 -exp 参数,则使用默认值(3天)。

确保在实际应用中替换模拟函数为实际的交易逻辑,并处理可能的错误情况。