Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add source tag with order message link #520

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bot/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
getUserAge,
getStars,
} = require('../util');
const OrderEvents = require('./modules/events/orders');
const { logger } = require('../logger');
import { MainContext } from './start';
import { UserDocument } from '../models/user'
Expand Down Expand Up @@ -601,6 +602,8 @@ const publishBuyOrderMessage = async (
message1 && (message1.message_id).toString() ? (message1.message_id).toString() : null;

await order.save();
// We can publish the order on nostr now becase we have the message id
OrderEvents.orderUpdated(order);
if (messageToUser) {
// Message to user let know the order was published
await pendingBuyMessage(bot, user, order, channel, i18n);
Expand Down Expand Up @@ -634,6 +637,8 @@ const publishSellOrderMessage = async (
message1 && (message1.message_id).toString() ? (message1.message_id).toString() : null;

await order.save();
// We can publish the order on nostr now becase we have the message id
OrderEvents.orderUpdated(order);
// Message to user let know the order was published
if (messageToUser)
await pendingSellMessage(ctx, user, order, channel, i18n);
Expand Down
22 changes: 15 additions & 7 deletions bot/modules/nostr/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ const orderToTags = async order => {
Math.floor(Date.now() / 1000) +
parseInt(process.env.ORDER_PUBLISHED_EXPIRATION_WINDOW);
let fiat_amount = order.fiat_amount;
if (order.amount === 0) {
if (order.fiat_amount === undefined) {
fiat_amount = `${order.min_amount}-${order.max_amount}`;
}
const channel =
process.env.CHANNEL[0] === '@'
? process.env.CHANNEL.slice(1)
: process.env.CHANNEL;
let source = `https://t.me/${channel}/${order.tg_channel_message1}`;
const tags = [];
tags.push(['d', order.id]);
tags.push(['k', order.type]);
Expand All @@ -25,21 +30,24 @@ const orderToTags = async order => {
tags.push(['fa', fiat_amount.toString()]);
tags.push(['pm', order.payment_method]);
tags.push(['premium', order.price_margin.toString()]);
tags.push(['y', 'lnp2pbot']);
tags.push(['z', 'order']);
tags.push(['expiration', expiration.toString()]);
if (order.community_id) {
const community = await Community.findById(order.community_id);
if (community.public) {
tags.push(['community_id', order.community_id]);
}
source = `https://t.me/${community.group}/${order.tg_channel_message1}`;
tags.push(['community_id', order.community_id]);
}
tags.push(['source', source]);
tags.push(['y', 'lnp2pbot']);
tags.push(['z', 'order']);
tags.push(['expiration', expiration.toString()]);

return tags;
};

exports.createOrderEvent = async order => {
const myPrivKey = Config.getPrivateKey();
if (order.is_public === false) {
return;
}

const created_at = Math.floor(Date.now() / 1000);
const tags = await orderToTags(order);
Expand Down
4 changes: 3 additions & 1 deletion bot/ordersActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ const createOrder = async (
}
await order.save();

OrderEvents.orderUpdated(order);
if (order.status !== 'PENDING') {
OrderEvents.orderUpdated(order);
}

return order;
} catch (error) {
Expand Down
Loading