Skip to content

Commit

Permalink
WIP3
Browse files Browse the repository at this point in the history
Dotenv is not needed & maybe should
create some model for currencies on line 1147
  • Loading branch information
Mersho committed Aug 29, 2023
1 parent 0da0893 commit e043913
Show file tree
Hide file tree
Showing 8 changed files with 430 additions and 292 deletions.
351 changes: 179 additions & 172 deletions bot/messages.js → bot/messages.ts

Large diffs are not rendered by default.

65 changes: 65 additions & 0 deletions environment.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
declare global {
namespace NodeJS {
interface ProcessEnv {

LND_CERT_BASE64: string;

LND_MACAROON_BASE64: string;
LND_GRPC_HOST: string;
BOT_TOKEN: string;

MAX_FEE: number;

FEE_PERCENT: number;

DB_USER: string;
DB_PASS: string;
DB_HOST: string;
DB_PORT: string;
DB_NAME: string;

MONGO_URI: string;

INVOICE_EXPIRATION_WINDOW: number;

HOLD_INVOICE_EXPIRATION_WINDOW: number;

CHANNEL: string;
ADMIN_CHANNEL: string;
HELP_GROUP: string;

MAX_DISPUTES: number;

HOLD_INVOICE_CLTV_DELTA: number;
HOLD_INVOICE_CLTV_DELTA_SAFETY_WINDOW: number;

PENDING_PAYMENT_WINDOW: number;

FIAT_RATE_NAME: string;
FIAT_RATE_EP: string;
NODE_ENV: string;

ORDER_PUBLISHED_EXPIRATION_WINDOW: number;

MIN_PAYMENT_AMT: number;

MAX_PENDING_ORDERS: number;

LOG_LEVEL: string;

MAX_ROUTING_FEE: number;

PAYMENT_ATTEMPTS: number;

DISPUTE_CHANNEL: string;

COMMUNITY_TTL: number;

NOSTR_SK: string
}
}
}

// If this file has no import/export statements (i.e. is a script)
// convert it into a module by adding an empty export statement.
export { }
9 changes: 0 additions & 9 deletions models/config.js

This file was deleted.

17 changes: 17 additions & 0 deletions models/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import mongoose, { Document, Schema } from 'mongoose';

export interface IConfig extends Document {
maintenance: boolean;
node_status: string;
node_uri: string;
}


const configSchema = new Schema<IConfig>({
maintenance: { type: Boolean, default: false },
node_status: { type: String, default: 'down' },
node_uri: { type: String },
});


module.exports = mongoose.model<IConfig>('Config', configSchema);
85 changes: 0 additions & 85 deletions models/order.js

This file was deleted.

126 changes: 126 additions & 0 deletions models/order.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import mongoose, { Document, Schema } from 'mongoose';

export interface IOrder extends Document {
description?: string;
amount: number;
max_amount: number;
min_amount: number;
fee: number;
bot_fee: number;
community_fee:number;
routing_fee: number;
hash: string;
secret: string;
creator_id: string;
seller_id: string;
buyer_id: string;
buyer_invoice: string;
buyer_dispute: boolean
seller_dispute: boolean
buyer_cooperativecancel: boolean;
seller_cooperativecancel: boolean;
canceled_by: string
status: string;
type: string;
fiat_amount: number;
fiat_code: string;
payment_method: string;
created_at: Date;
invoice_held_at: Date;
taken_at: Date;
tg_chat_id: string;
tg_order_message: string;
tg_channel_message1: string;
range_parent_id: string;
price_from_api: boolean;
price_margin: number;
calculated: boolean;
admin_warned: boolean;
paid_hold_buyer_invoice_updated: boolean;
community_id: string;
is_public: boolean;
}

const orderSchema = new Schema<IOrder>({
description: { type: String, required: true },
amount: {
// amount in satoshis
type: Number,
min: 0,
},
max_amount: {
// max amount in fiat
type: Number,
min: 0,
},
min_amount: {
// min amount in fiat
type: Number,
min: 0,
},
fee: { type: Number, min: 0 },
bot_fee: { type: Number, min: 0 }, // bot MAX_FEE at the moment of order creation
community_fee: { type: Number, min: 0 }, // community FEE_PERCENT at the moment of order creation
routing_fee: { type: Number, min: 0, default: 0 },
hash: {
type: String,
index: {
unique: true,
partialFilterExpression: { hash: { $type: 'string' } },
},
}, // hold invoice hash
secret: {
type: String,
index: {
unique: true,
partialFilterExpression: { secret: { $type: 'string' } },
},
}, // hold invoice secret
creator_id: { type: String },
seller_id: { type: String },
buyer_id: { type: String },
buyer_invoice: { type: String },
buyer_dispute: { type: Boolean, default: false },
seller_dispute: { type: Boolean, default: false },
buyer_cooperativecancel: { type: Boolean, default: false },
seller_cooperativecancel: { type: Boolean, default: false },
canceled_by: { type: String },
status: {
type: String,
enum: [
'WAITING_PAYMENT', // buyer waiting for seller pay hold invoice
'WAITING_BUYER_INVOICE', // seller waiting for buyer add invoice where will receive sats
'PENDING', // order published on CHANNEL but not taken yet
'ACTIVE', // order taken
'FIAT_SENT', // buyer indicates the fiat payment is already done
'CLOSED', // order closed
'DISPUTE', // one of the parties started a dispute
'CANCELED',
'SUCCESS',
'PAID_HOLD_INVOICE', // seller released funds
'CANCELED_BY_ADMIN',
'EXPIRED', // Expired orders, stated changed by a job
'COMPLETED_BY_ADMIN',
],
},
type: { type: String },
fiat_amount: { type: Number, min: 1 }, // amount in fiat
fiat_code: { type: String },
payment_method: { type: String, required: true },
created_at: { type: Date, default: Date.now },
invoice_held_at: { type: Date },
taken_at: { type: Date },
tg_chat_id: { type: String },
tg_order_message: { type: String },
tg_channel_message1: { type: String },
range_parent_id: { type: String }, // If the order have a parent we save the Id
price_from_api: { type: Boolean },
price_margin: { type: Number, default: 0 },
calculated: { type: Boolean, default: false },
admin_warned: { type: Boolean, default: false }, // We set this to true when the bot warns admins the order is about to expire
paid_hold_buyer_invoice_updated: { type: Boolean, default: false }, // We set this to true when buyer executes /setinvoice on a order PAID_HOLD_INVOICE
community_id: { type: String },
is_public: { type: Boolean, default: true }
});

module.exports = mongoose.model<IOrder>('Order', orderSchema);
26 changes: 0 additions & 26 deletions models/pending_payment.js

This file was deleted.

43 changes: 43 additions & 0 deletions models/pending_payment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import mongoose, { Document, Schema } from 'mongoose';

export interface IPendingPayment extends Document {
description: string;
amount: number;
attempts: number;
paid: boolean;
is_invoice_expired: boolean;
payment_request: string;
hash: string;
created_at: Date;
paid_at: Date;
user_id: string;
order_id: string;
community_id: string;
}


const PendingPaymentSchema = new Schema<IPendingPayment>({
description: { type: String },
amount: {
// amount in satoshis
type: Number,
min: [1, 'Minimum amount is 1 sat'],
validate: {
validator: Number.isInteger,
message: '{VALUE} is not an integer value',
},
},
attempts: { type: Number, min: 0, default: 0 },
paid: { type: Boolean, default: false },
is_invoice_expired: { type: Boolean, default: false },
payment_request: { type: String },
hash: { type: String },
created_at: { type: Date, default: Date.now },
paid_at: { type: Date },
user_id: { type: String },
order_id: { type: String },
community_id: { type: String },
});


module.exports = mongoose.model<IPendingPayment>('PendingPayment', PendingPaymentSchema);

0 comments on commit e043913

Please sign in to comment.