Created
December 12, 2022 20:26
-
-
Save grunch/6977022d6ca79850d6d91a894ec60e93 to your computer and use it in GitHub Desktop.
lnp2pbot -> user response time PR fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/bot/commands.js b/bot/commands.js | |
index 0cbaed9..6e12be5 100644 | |
--- a/bot/commands.js | |
+++ b/bot/commands.js | |
@@ -150,7 +150,7 @@ const waitPayment = async (ctx, bot, buyer, seller, order, buyerInvoice) => { | |
const stars = getEmojiRate(buyer.total_rating); | |
const roundedRating = decimalRound(buyer.total_rating, -1); | |
const rate = `${roundedRating} ${stars} (${buyer.total_reviews})`; | |
- const buyerResponse = minutesCeil(buyer.avg_fiat_release_time); | |
+ const buyerResponse = minutesCeil(buyer.avg_fiat_sent_time); | |
// We send the hold invoice to the seller | |
await messages.invoicePaymentRequestMessage( | |
ctx, | |
@@ -709,9 +709,9 @@ const fiatSent = async (ctx, orderId, user) => { | |
order.status = 'FIAT_SENT'; | |
order.fiat_sent_at = new Date(); | |
- calculateBuyerResponse(order.buyer_id); | |
const seller = await User.findOne({ _id: order.seller_id }); | |
await order.save(); | |
+ calculateBuyerResponse(order.buyer_id); | |
// We sent messages to both parties | |
// We need to create i18n context for each user | |
const i18nCtxBuyer = await getUserI18nContext(user); | |
@@ -752,8 +752,8 @@ const release = async (ctx, orderId, user) => { | |
await settleHoldInvoice({ secret: order.secret }); | |
order.release_funds_at = new Date(); | |
- calculateSellerResponse(order.seller_id); | |
await order.save(); | |
+ calculateSellerResponse(order.seller_id); | |
} catch (error) { | |
logger.error(error); | |
} | |
diff --git a/bot/messages.js b/bot/messages.js | |
index 97f54be..601bf94 100644 | |
--- a/bot/messages.js | |
+++ b/bot/messages.js | |
@@ -61,12 +61,17 @@ const invoicePaymentRequestMessage = async ( | |
: order.fiat_code; | |
const expirationTime = | |
parseInt(process.env.HOLD_INVOICE_EXPIRATION_WINDOW) / 60; | |
+ const buyerResponseMessage = buyerResponse | |
+ ? i18n.t('buyer_response', { | |
+ buyerResponse, | |
+ }) | |
+ : ''; | |
const message = i18n.t('invoice_payment_request', { | |
currency, | |
order, | |
expirationTime, | |
rate, | |
- buyerResponse, | |
+ buyerResponse: buyerResponseMessage, | |
}); | |
await ctx.telegram.sendMessage(user.tg_id, message); | |
// Create QR code | |
@@ -560,12 +565,7 @@ const publishBuyOrderMessage = async ( | |
messageToUser | |
) => { | |
try { | |
- const buyerResponse = minutesCeil(user.avg_fiat_release_time); | |
- let publishMessage = `⚡️🍊⚡️\n${order.description}${ | |
- buyerResponse === 0 | |
- ? '' | |
- : i18n.t('buyer_time_response', { buyerResponse }) | |
- }\n`; | |
+ let publishMessage = `⚡️🍊⚡️\n${order.description}\n`; | |
publishMessage += `:${order._id}:`; | |
const channel = await getOrderChannel(order); | |
@@ -599,12 +599,7 @@ const publishSellOrderMessage = async ( | |
messageToUser | |
) => { | |
try { | |
- const sellerResponse = minutesCeil(user.avg_funds_release_time); | |
- let publishMessage = `⚡️🍊⚡️\n${order.description}${ | |
- sellerResponse === 0 | |
- ? '' | |
- : i18n.t('seller_time_response', { sellerResponse }) | |
- }\n`; | |
+ let publishMessage = `⚡️🍊⚡️\n${order.description}\n`; | |
publishMessage += `:${order._id}:`; | |
const channel = await getOrderChannel(order); | |
// We send the message to the channel | |
diff --git a/bot/ordersActions.js b/bot/ordersActions.js | |
index a9a7f5c..2c8a3ea 100644 | |
--- a/bot/ordersActions.js | |
+++ b/bot/ordersActions.js | |
@@ -8,6 +8,7 @@ const { | |
getEmojiRate, | |
decimalRound, | |
getFee, | |
+ minutesCeil, | |
} = require('../util'); | |
const logger = require('../logger'); | |
@@ -44,12 +45,19 @@ const createOrder = async ( | |
const currency = getCurrency(fiatCode); | |
const priceFromAPI = !amount; | |
- if (priceFromAPI && !currency.price) { | |
- await messages.notRateForCurrency(bot, user, i18n); | |
- return; | |
- } | |
+ if (priceFromAPI && !currency.price) | |
+ return await messages.notRateForCurrency(bot, user, i18n); | |
const fiatAmountData = getFiatAmountData(fiatAmount); | |
+ let sellerResponse; | |
+ let buyerResponse; | |
+ | |
+ // we calculate the response time for this user | |
+ if (type === 'sell') { | |
+ sellerResponse = minutesCeil(user.avg_funds_release_time); | |
+ } else { | |
+ buyerResponse = minutesCeil(user.avg_fiat_release_time); | |
+ } | |
const baseOrderData = { | |
...fiatAmountData, | |
@@ -76,6 +84,8 @@ const createOrder = async ( | |
priceMargin, | |
priceFromAPI, | |
currency, | |
+ buyerResponse, | |
+ sellerResponse, | |
}), | |
range_parent_id, | |
community_id, | |
@@ -127,6 +137,8 @@ const buildDescription = ( | |
priceMargin, | |
priceFromAPI, | |
currency, | |
+ sellerResponse, | |
+ buyerResponse, | |
} | |
) => { | |
try { | |
@@ -134,6 +146,18 @@ const buildDescription = ( | |
const hashtag = `#${type.toUpperCase()}${fiatCode}\n`; | |
const paymentAction = | |
type === 'sell' ? i18n.t('receive_payment') : i18n.t('pay'); | |
+ let response = ''; | |
+ if (sellerResponse !== undefined) | |
+ response = | |
+ sellerResponse === 0 | |
+ ? 'libera rapidito\n' | |
+ : i18n.t('seller_time_response', { sellerResponse }) + '\n'; | |
+ | |
+ if (buyerResponse !== undefined) | |
+ response = | |
+ buyerResponse === 0 | |
+ ? 'envia rapidito\n' | |
+ : i18n.t('buyer_time_response', { buyerResponse }) + '\n'; | |
const trades = user.trades_completed; | |
const volume = numberFormat(fiatCode, user.volume_traded); | |
const totalRating = user.total_rating; | |
@@ -185,6 +209,7 @@ const buildDescription = ( | |
description += volumeTraded; | |
description += hashtag; | |
description += tasaText; | |
+ description += response; | |
description += rateText; | |
return description; | |
diff --git a/locales/es.yaml b/locales/es.yaml | |
index 5f8937c..12722e1 100644 | |
--- a/locales/es.yaml | |
+++ b/locales/es.yaml | |
@@ -30,12 +30,13 @@ start: | | |
¡Intercambia seguro y rápido! | |
init_bot_error: Para usar este Bot primero debes inicializar el bot con el comando /start | |
non_handle_error: 👤 Para usar este bot debes activar tu username de telegram, para activarlo abre el menú de hamburguesa arriba a la izquierda, selecciona ajustes -> editar perfil -> username | |
+buyer_response: ⏱ El comprador suele enviar fiat en ${buyerResponse} minutos luego de tomada la oferta. | |
invoice_payment_request: | | |
Un usuario quiere comprarte ${order.amount} sats por ${currency} ${order.fiat_amount}. | |
Reputación del comprador: ${rate} | |
- ⏱ Comprador suele enviar fiat en ${buyerResponse} minutos luego de tomada la oferta. | |
+ ${buyerResponse} | |
Si deseas continuar por favor paga esta factura, esta factura expira en ${expirationTime} minutos | |
pending_sell: | | |
diff --git a/models/user.js b/models/user.js | |
index ddd1910..065a024 100644 | |
--- a/models/user.js | |
+++ b/models/user.js | |
@@ -23,8 +23,8 @@ const UserSchema = new mongoose.Schema({ | |
disputes: { type: Number, min: 0, default: 0 }, | |
created_at: { type: Date, default: Date.now }, | |
default_community_id: { type: String }, | |
- avg_funds_release_time: { type: Number, min: 0, default: 0 }, | |
- avg_fiat_release_time: { type: Number, min: 0, default: 0 }, | |
+ avg_funds_release_time: { type: Number, min: 0 }, // seller release time in seconds | |
+ avg_fiat_sent_time: { type: Number, min: 0 }, // buyer fiat sent time in seconds | |
}); | |
module.exports = mongoose.model('User', UserSchema); | |
diff --git a/util/index.js b/util/index.js | |
index c083a44..c02ca45 100644 | |
--- a/util/index.js | |
+++ b/util/index.js | |
@@ -402,9 +402,11 @@ const timeStringToTimestamp = timeString => { | |
return timestamp; | |
}; | |
-const minutesCeil = ( minutes) => { | |
- return Math.ceil(minutes); | |
-} | |
+const minutesCeil = seconds => { | |
+ if (isNaN(seconds)) return undefined; | |
+ | |
+ return Math.ceil(seconds / 60); | |
+}; | |
const limitOrders = 10; | |
@@ -420,8 +422,10 @@ const calculateSellerResponse = async id => { | |
for (const order of orders) { | |
const releaseFundsAt = timeStringToTimestamp(order.release_funds_at); | |
+ console.log('releaseFundsAt', releaseFundsAt); | |
const fiatSentAt = timeStringToTimestamp(order.fiat_sent_at); | |
- const timeToRelease = (releaseFundsAt - fiatSentAt) / 1000 / 60; | |
+ console.log('fiatSentAt', fiatSentAt); | |
+ const timeToRelease = (releaseFundsAt - fiatSentAt) / 1000; | |
ordersByTimeToRelease.push(timeToRelease); | |
} | |
@@ -456,7 +460,7 @@ const calculateBuyerResponse = async id => { | |
for (const order of orders) { | |
const fiatSentAt = timeStringToTimestamp(order.fiat_sent_at); | |
const takenAt = timeStringToTimestamp(order.taken_at); | |
- const timeToSent = (fiatSentAt - takenAt) / 1000 / 60; | |
+ const timeToSent = (fiatSentAt - takenAt) / 1000; | |
ordersByTimeToSent.push(timeToSent); | |
} | |
@@ -465,7 +469,7 @@ const calculateBuyerResponse = async id => { | |
ordersByTimeToSent.reduce((a, b) => a + b, 0) / orders.length; | |
const user = await User.findById({ _id: id }); | |
- user.avg_fiat_release_time = !average ? 0 : average; | |
+ user.avg_fiat_sent_time = !average ? 0 : average; | |
await user.save(); | |
return minutesCeil(average); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment