Browse Source

Merge pull request #116 from stakwork/looper

Looper
dependabot/npm_and_yarn/ini-1.3.7
Evan Feenstra 4 years ago
committed by GitHub
parent
commit
1d99da895d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 262
      dist/src/builtin/loop.js
  2. 2
      dist/src/builtin/loop.js.map
  3. 2
      dist/src/builtin/mother.js.map
  4. 1
      dist/src/controllers/feed.js
  5. 2
      dist/src/controllers/feed.js.map
  6. 284
      src/builtin/loop.ts
  7. 1
      src/builtin/mother.ts
  8. 2
      src/controllers/feed.ts

262
dist/src/builtin/loop.js

@ -11,14 +11,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const Sphinx = require("sphinx-bot"); const Sphinx = require("sphinx-bot");
const api_1 = require("../controllers/api"); const api_1 = require("../controllers/api");
const node_fetch_1 = require("node-fetch"); // import fetch from 'node-fetch'
const path = require("path"); const path = require("path");
const models_1 = require("../models"); const models_1 = require("../models");
const constants_1 = require("../constants"); const constants_1 = require("../constants");
const child_process_1 = require("child_process");
const env = process.env.NODE_ENV || 'development';
const config = require(path.join(__dirname, '../../config/app.json'))[env];
var validate = require('bitcoin-address-validation'); var validate = require('bitcoin-address-validation');
const msg_types = Sphinx.MSG_TYPE; const msg_types = Sphinx.MSG_TYPE;
let initted = false; let initted = false;
const baseurl = 'https://localhost:8080'; // const baseurl = 'https://localhost:8080'
function init() { function init() {
if (initted) if (initted)
return; return;
@ -56,12 +59,53 @@ function init() {
message.channel.send({ embed }); message.channel.send({ embed });
return; return;
} }
// try {
// const j = await doRequest(baseurl + '/v1/loop/out/quote/' + amt)
// console.log("=> LOOP QUOTE RES", j)
// if (!(j && j.swap_fee_sat && j.prepay_amt_sat)) {
// return
// }
// let chan
// const bot = await getBot(message.channel.id)
// if (bot && bot.meta) chan = bot.meta
// if (!chan) {
// const embed = new Sphinx.MessageEmbed()
// .setAuthor('LoopBot')
// .setDescription('No channel set')
// message.channel.send({ embed })
// return
// }
// const j2 = await doRequest(baseurl + '/v1/loop/out', {
// method: 'POST',
// body: JSON.stringify({
// amt: amt,
// dest: addy,
// outgoing_chan_set: [chan],
// max_swap_fee: j.swap_fee_sat,
// max_prepay_amt: j.prepay_amt_sat
// }),
// })
// console.log("=> LOOP RESPONSE", j2)
// if (j2 && j2.error) {
// const embed = new Sphinx.MessageEmbed()
// .setAuthor('LoopBot')
// .setDescription('Error: ' + j2.error)
// message.channel.send({ embed })
// return
// }
// // if (!(j2 && j2.server_message)) {
// // return
// // }
// const embed = new Sphinx.MessageEmbed()
// .setAuthor('LoopBot')
// .setTitle('Payment was sent!')
// // .setDescription('Success!')
// message.channel.send({ embed })
// return
// } catch (e) {
// console.log('LoopBot error', e)
// }
try { try {
const j = yield doRequest(baseurl + '/v1/loop/out/quote/' + amt);
console.log("=> LOOP QUOTE RES", j);
if (!(j && j.swap_fee_sat && j.prepay_amt_sat)) {
return;
}
let chan; let chan;
const bot = yield getBot(message.channel.id); const bot = yield getBot(message.channel.id);
if (bot && bot.meta) if (bot && bot.meta)
@ -73,89 +117,108 @@ function init() {
message.channel.send({ embed }); message.channel.send({ embed });
return; return;
} }
const j2 = yield doRequest(baseurl + '/v1/loop/out', { const cmd = `loop`;
method: 'POST', const args = [
body: JSON.stringify({ `--tlscertpath=${config.tls_location}`,
amt: amt, `--macaroonpath=${config.loop_macaroon_location}`,
dest: addy, `--rpcserver=localhost:10009`,
outgoing_chan_set: [chan], 'out',
max_swap_fee: j.swap_fee_sat, `--channel=${chan}`,
max_prepay_amt: j.prepay_amt_sat `--amt=${amt}`,
}), `--fast`,
`--addr=${addy}`
];
console.log("=> SPAWN", cmd, args);
let childProcess = child_process_1.spawn(cmd, args);
childProcess.stdout.on('data', function (data) {
const stdout = data.toString();
console.log("LOOPBOT stdout:", stdout);
if (stdout) {
console.log('=> LOOPBOT stdout', stdout);
if (stdout.includes('CONTINUE SWAP?')) {
childProcess.stdin.write('y\n');
}
if (stdout.startsWith('Swap initiated')) {
const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setTitle('Payment was sent!');
// .setDescription('Success!')
message.channel.send({ embed });
return;
}
}
});
childProcess.stderr.on('data', function (data) {
console.log("STDERR:", data.toString());
});
childProcess.on('error', (error) => {
console.log("error", error.toString());
});
childProcess.on('close', (code) => {
console.log("CHILD PROCESS closed", code);
}); });
console.log("=> LOOP RESPONSE", j2); }
if (j2 && j2.error) { catch (e) {
console.log('LoopBot error', e);
}
}
else {
const cmd = arr[1];
const isAdmin = message.member.roles.find(role => role.name === 'Admin');
if (isAdmin && cmd.startsWith('setchan=')) {
const bot = yield getBot(message.channel.id);
const arr = cmd.split('=');
if (bot && arr.length > 1) {
const chan = arr[1];
yield bot.update({ meta: chan });
const embed = new Sphinx.MessageEmbed() const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot') .setAuthor('LoopBot')
.setDescription('Error: ' + j2.error); .setDescription('Channel updated to ' + chan)
.setThumbnail(botSVG);
message.channel.send({ embed }); message.channel.send({ embed });
return; return;
} }
// if (!(j2 && j2.server_message)) {
// return
// }
const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setTitle('Payment was sent!');
// .setDescription('Success!')
message.channel.send({ embed });
return;
}
catch (e) {
console.log('LoopBot error', e);
} }
} switch (cmd) {
const cmd = arr[1]; case 'help':
const isAdmin = message.member.roles.find(role => role.name === 'Admin'); const embed = new Sphinx.MessageEmbed()
if (isAdmin && cmd.startsWith('setchan=')) { .setAuthor('LoopBot')
const bot = yield getBot(message.channel.id); .setTitle('LoopBot Commands:')
const arr = cmd.split('='); .addFields([
if (bot && arr.length > 1) { { name: 'Send to your on-chain address', value: '/loopout {ADDRESS} {AMOUNT}' },
const chan = arr[1]; { name: 'Set Channel', value: '/loopout setchan=***' },
yield bot.update({ meta: chan }); { name: 'Help', value: '/loopout help' },
const embed = new Sphinx.MessageEmbed() ])
.setAuthor('LoopBot') .setThumbnail(botSVG);
.setDescription('Channel updated to ' + chan) message.channel.send({ embed });
.setThumbnail(botSVG); return;
message.channel.send({ embed }); default:
return; const embed2 = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setDescription('Command not recognized');
message.channel.send({ embed: embed2 });
return;
} }
} } // end else
switch (cmd) {
case 'help':
const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setTitle('LoopBot Commands:')
.addFields([
{ name: 'Send to your on-chain address', value: '/loopout {ADDRESS} {AMOUNT}' },
{ name: 'Set Channel', value: '/loopout setchan=***' },
{ name: 'Help', value: '/loopout help' },
])
.setThumbnail(botSVG);
message.channel.send({ embed });
return;
default:
const embed2 = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setDescription('Command not recognized');
message.channel.send({ embed: embed2 });
return;
}
})); }));
} }
exports.init = init; exports.init = init;
function getBot(tribeUUID) { function getBot(tribeUUID) {
return __awaiter(this, void 0, void 0, function* () { return __awaiter(this, void 0, void 0, function* () {
const chat = yield models_1.models.Chat.findOne({ where: { const chat = yield models_1.models.Chat.findOne({
where: {
uuid: tribeUUID uuid: tribeUUID
} }); }
});
if (!chat) if (!chat)
return; return;
const chatBot = yield models_1.models.ChatBot.findOne({ where: { const chatBot = yield models_1.models.ChatBot.findOne({
where: {
chatId: chat.id, chatId: chat.id,
botPrefix: '/loopout', botPrefix: '/loopout',
botType: constants_1.default.bot_types.builtin botType: constants_1.default.bot_types.builtin
} }); }
});
return chatBot; return chatBot;
}); });
} }
@ -167,34 +230,33 @@ function validateAmount(amtString) {
const ok = amt > 0; const ok = amt > 0;
return ok; return ok;
} }
const fs = require('fs'); // const fs = require('fs')
const https = require("https"); // const https = require("https");
// const homedir = require('os').homedir(); // const homedir = require('os').homedir();
const agent = new https.Agent({ // const agent = new https.Agent({
rejectUnauthorized: false // rejectUnauthorized: false
}); // })
const env = process.env.NODE_ENV || 'development'; // async function doRequest(theurl: string, params?: Object) {
const config = require(path.join(__dirname, '../../config/app.json'))[env]; // const ps = params || {}
function doRequest(theurl, params) { // try {
return __awaiter(this, void 0, void 0, function* () { // const macLocation = config.loop_macaroon_location
const ps = params || {}; // if (!macLocation) {
try { // throw new Error('no macaroon')
const macLocation = config.loop_macaroon_location; // }
if (!macLocation) { // var macaroonString = fs.readFileSync(macLocation);
throw new Error('no macaroon'); // var mac = Buffer.from(macaroonString, 'utf8').toString('hex');
} // const theParams = {
var macaroonString = fs.readFileSync(macLocation); // agent,
var mac = Buffer.from(macaroonString, 'utf8').toString('hex'); // headers: {
const theParams = Object.assign({ agent, headers: { // 'Grpc-Metadata-macaroon': mac
'Grpc-Metadata-macaroon': mac // },
} }, ps); // ...ps
const r = yield node_fetch_1.default(theurl, theParams); // }
const j = yield r.json(); // const r = await fetch(theurl, theParams)
return j; // const j = await r.json()
} // return j
catch (e) { // } catch (e) {
throw e; // throw e
} // }
}); // }
}
//# sourceMappingURL=loop.js.map //# sourceMappingURL=loop.js.map

2
dist/src/builtin/loop.js.map

File diff suppressed because one or more lines are too long

2
dist/src/builtin/mother.js.map

File diff suppressed because one or more lines are too long

1
dist/src/controllers/feed.js

@ -32,6 +32,7 @@ exports.streamFeed = (req, res) => __awaiter(void 0, void 0, void 0, function* (
itemID: meta.itemID, itemID: meta.itemID,
ts: meta.ts || 0, ts: meta.ts || 0,
sats_per_minute: amount || 0, sats_per_minute: amount || 0,
speed: meta.speed || '1'
}; };
const chat = yield models_1.models.Chat.findOne({ where: { id: chat_id } }); const chat = yield models_1.models.Chat.findOne({ where: { id: chat_id } });
if (!chat) { if (!chat) {

2
dist/src/controllers/feed.js.map

@ -1 +1 @@
{"version":3,"file":"feed.js","sourceRoot":"","sources":["../../../src/controllers/feed.ts"],"names":[],"mappings":";;;;;;;;;;;AACA,sCAAkC;AAClC,sCAAqC;AACrC,sCAA+C;AAC/C,4CAAoC;AAevB,QAAA,UAAU,GAAG,CAAO,GAAG,EAAE,GAAG,EAAE,EAAE;IAC3C,MAAM,EACJ,YAAY,EACZ,MAAM,EAC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AU,EAAE,MAAM,GAAG,IAAI;oBACzB,WAAW,EAAE,EAAE;oBACf,IAAI;oBACJ,cAAc,EAAE,IAAI,IAAI,EAAE;oBAC1B,MAAM,EAAE,mBAAS,CAAC,QAAQ,CAAC,SAAS;oBACpC,SAAS,EAAE,IAAI;oBACf,SAAS,EAAE,IAAI;iBAChB,CAAC,CAAA;gBACF,SAAS,CAAC,EAAE,eAAe,EAAE,MAAM,EAAE,CAAC,CAAA;YACxC,CAAC;YACD,OAAO,EAAE,CAAC,KAAK,EAAE,EAAE;gBACjB,SAAS,CAAC,KAAK,CAAC,CAAA;YAClB,CAAC;SACF,CAAC,CAAA;IACJ,CAAC;CAAA;AAlCD,4CAkCC;AAED,SAAe,YAAY,CAAC,KAAK,EAAE,QAAQ;;QACzC,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE;YACjD,MAAM,QAAQ,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,KAAK,EAAE,KAAK,CAAC,CAAC;SAC5C;IACH,CAAC;CAAA"}

284
src/builtin/loop.ts

@ -1,15 +1,20 @@
import * as Sphinx from 'sphinx-bot' import * as Sphinx from 'sphinx-bot'
import { finalAction } from '../controllers/api' import { finalAction } from '../controllers/api'
import fetch from 'node-fetch' // import fetch from 'node-fetch'
import * as path from 'path' import * as path from 'path'
import { models } from '../models' import { models } from '../models'
import constants from '../constants' import constants from '../constants'
import { spawn } from 'child_process'
const env = process.env.NODE_ENV || 'development';
const config = require(path.join(__dirname, '../../config/app.json'))[env]
var validate = require('bitcoin-address-validation'); var validate = require('bitcoin-address-validation');
const msg_types = Sphinx.MSG_TYPE const msg_types = Sphinx.MSG_TYPE
let initted = false let initted = false
const baseurl = 'https://localhost:8080' // const baseurl = 'https://localhost:8080'
export function init() { export function init() {
if (initted) return if (initted) return
@ -42,20 +47,62 @@ export function init() {
message.channel.send({ embed }) message.channel.send({ embed })
return return
} }
if (messageAmount < parseInt(amt)){ if (messageAmount < parseInt(amt)) {
const embed = new Sphinx.MessageEmbed() const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot') .setAuthor('LoopBot')
.setDescription('Incorrect amount') .setDescription('Incorrect amount')
message.channel.send({ embed }) message.channel.send({ embed })
return return
} }
try { // try {
const j = await doRequest(baseurl + '/v1/loop/out/quote/' + amt) // const j = await doRequest(baseurl + '/v1/loop/out/quote/' + amt)
console.log("=> LOOP QUOTE RES", j) // console.log("=> LOOP QUOTE RES", j)
if (!(j && j.swap_fee_sat && j.prepay_amt_sat)) { // if (!(j && j.swap_fee_sat && j.prepay_amt_sat)) {
return // return
} // }
// let chan
// const bot = await getBot(message.channel.id)
// if (bot && bot.meta) chan = bot.meta
// if (!chan) {
// const embed = new Sphinx.MessageEmbed()
// .setAuthor('LoopBot')
// .setDescription('No channel set')
// message.channel.send({ embed })
// return
// }
// const j2 = await doRequest(baseurl + '/v1/loop/out', {
// method: 'POST',
// body: JSON.stringify({
// amt: amt,
// dest: addy,
// outgoing_chan_set: [chan],
// max_swap_fee: j.swap_fee_sat,
// max_prepay_amt: j.prepay_amt_sat
// }),
// })
// console.log("=> LOOP RESPONSE", j2)
// if (j2 && j2.error) {
// const embed = new Sphinx.MessageEmbed()
// .setAuthor('LoopBot')
// .setDescription('Error: ' + j2.error)
// message.channel.send({ embed })
// return
// }
// // if (!(j2 && j2.server_message)) {
// // return
// // }
// const embed = new Sphinx.MessageEmbed()
// .setAuthor('LoopBot')
// .setTitle('Payment was sent!')
// // .setDescription('Success!')
// message.channel.send({ embed })
// return
// } catch (e) {
// console.log('LoopBot error', e)
// }
try {
let chan let chan
const bot = await getBot(message.channel.id) const bot = await getBot(message.channel.id)
if (bot && bot.meta) chan = bot.meta if (bot && bot.meta) chan = bot.meta
@ -66,90 +113,109 @@ export function init() {
message.channel.send({ embed }) message.channel.send({ embed })
return return
} }
const cmd = `loop`
const j2 = await doRequest(baseurl + '/v1/loop/out', { const args = [
method: 'POST', `--tlscertpath=${config.tls_location}`,
body: JSON.stringify({ `--macaroonpath=${config.loop_macaroon_location}`,
amt: amt, `--rpcserver=localhost:10009`,
dest: addy, 'out',
outgoing_chan_set: [chan], `--channel=${chan}`,
max_swap_fee: j.swap_fee_sat, `--amt=${amt}`,
max_prepay_amt: j.prepay_amt_sat `--fast`,
}), `--addr=${addy}`
]
console.log("=> SPAWN",cmd,args)
let childProcess = spawn(cmd, args)
childProcess.stdout.on('data', function (data) {
const stdout = data.toString()
console.log("LOOPBOT stdout:",stdout)
if(stdout){
console.log('=> LOOPBOT stdout',stdout)
if(stdout.includes('CONTINUE SWAP?')) {
childProcess.stdin.write('y\n')
}
if(stdout.startsWith('Swap initiated')) {
const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setTitle('Payment was sent!')
// .setDescription('Success!')
message.channel.send({ embed })
return
}
}
}) })
console.log("=> LOOP RESPONSE", j2) childProcess.stderr.on('data', function (data) {
if (j2 && j2.error) { console.log("STDERR:",data.toString())
});
childProcess.on('error', (error) => {
console.log("error",error.toString());
});
childProcess.on('close', (code) => {
console.log("CHILD PROCESS closed",code)
});
} catch (e) {
console.log('LoopBot error', e)
}
} else {
const cmd = arr[1]
const isAdmin = message.member.roles.find(role => role.name === 'Admin')
if (isAdmin && cmd.startsWith('setchan=')) {
const bot = await getBot(message.channel.id)
const arr = cmd.split('=')
if (bot && arr.length > 1) {
const chan = arr[1]
await bot.update({ meta: chan })
const embed = new Sphinx.MessageEmbed() const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot') .setAuthor('LoopBot')
.setDescription('Error: ' + j2.error) .setDescription('Channel updated to ' + chan)
.setThumbnail(botSVG)
message.channel.send({ embed }) message.channel.send({ embed })
return return
} }
// if (!(j2 && j2.server_message)) {
// return
// }
const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setTitle('Payment was sent!')
// .setDescription('Success!')
message.channel.send({ embed })
return
} catch (e) {
console.log('LoopBot error', e)
} }
} switch (cmd) {
case 'help':
const cmd = arr[1] const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setTitle('LoopBot Commands:')
.addFields([
{ name: 'Send to your on-chain address', value: '/loopout {ADDRESS} {AMOUNT}' },
{ name: 'Set Channel', value: '/loopout setchan=***' },
{ name: 'Help', value: '/loopout help' },
])
.setThumbnail(botSVG)
message.channel.send({ embed })
return
default:
const embed2 = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setDescription('Command not recognized')
message.channel.send({ embed: embed2 })
return
}
const isAdmin = message.member.roles.find(role => role.name === 'Admin') } // end else
if(isAdmin && cmd.startsWith('setchan=')) {
const bot = await getBot(message.channel.id)
const arr = cmd.split('=')
if(bot && arr.length>1) {
const chan = arr[1]
await bot.update({meta: chan})
const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setDescription('Channel updated to '+chan)
.setThumbnail(botSVG)
message.channel.send({ embed })
return
}
}
switch (cmd) {
case 'help':
const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setTitle('LoopBot Commands:')
.addFields([
{ name: 'Send to your on-chain address', value: '/loopout {ADDRESS} {AMOUNT}' },
{ name: 'Set Channel', value: '/loopout setchan=***' },
{ name: 'Help', value: '/loopout help' },
])
.setThumbnail(botSVG)
message.channel.send({ embed })
return
default:
const embed2 = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setDescription('Command not recognized')
message.channel.send({ embed: embed2 })
return
}
}) })
} }
async function getBot(tribeUUID:string){ async function getBot(tribeUUID: string) {
const chat = await models.Chat.findOne({ where:{ const chat = await models.Chat.findOne({
uuid: tribeUUID where: {
}}) uuid: tribeUUID
}
})
if (!chat) return if (!chat) return
const chatBot = await models.ChatBot.findOne({where: { const chatBot = await models.ChatBot.findOne({
chatId: chat.id, where: {
botPrefix: '/loopout', chatId: chat.id,
botType: constants.bot_types.builtin botPrefix: '/loopout',
}}) botType: constants.bot_types.builtin
}
})
return chatBot return chatBot
} }
@ -163,36 +229,34 @@ function validateAmount(amtString: string) {
return ok return ok
} }
const fs = require('fs') // const fs = require('fs')
const https = require("https"); // const https = require("https");
// const homedir = require('os').homedir(); // const homedir = require('os').homedir();
const agent = new https.Agent({ // const agent = new https.Agent({
rejectUnauthorized: false // rejectUnauthorized: false
}) // })
const env = process.env.NODE_ENV || 'development';
const config = require(path.join(__dirname, '../../config/app.json'))[env]
async function doRequest(theurl: string, params?: Object) { // async function doRequest(theurl: string, params?: Object) {
const ps = params || {} // const ps = params || {}
try { // try {
const macLocation=config.loop_macaroon_location // const macLocation = config.loop_macaroon_location
if(!macLocation) { // if (!macLocation) {
throw new Error('no macaroon') // throw new Error('no macaroon')
} // }
var macaroonString = fs.readFileSync(macLocation); // var macaroonString = fs.readFileSync(macLocation);
var mac = Buffer.from(macaroonString, 'utf8').toString('hex'); // var mac = Buffer.from(macaroonString, 'utf8').toString('hex');
const theParams = { // const theParams = {
agent, // agent,
headers: { // headers: {
'Grpc-Metadata-macaroon': mac // 'Grpc-Metadata-macaroon': mac
}, // },
...ps // ...ps
} // }
const r = await fetch(theurl, theParams) // const r = await fetch(theurl, theParams)
const j = await r.json() // const j = await r.json()
return j // return j
} catch (e) { // } catch (e) {
throw e // throw e
} // }
} // }

1
src/builtin/mother.ts

@ -36,6 +36,7 @@ export function init() {
client.login('_', finalAction) client.login('_', finalAction)
client.on(msg_types.MESSAGE, async (message: Sphinx.Message) => { client.on(msg_types.MESSAGE, async (message: Sphinx.Message) => {
const arr = (message.content && message.content.split(' ')) || [] const arr = (message.content && message.content.split(' ')) || []
if (arr.length < 2) return if (arr.length < 2) return
if (arr[0] !== '/bot') return if (arr[0] !== '/bot') return

2
src/controllers/feed.ts

@ -8,6 +8,7 @@ export interface ChatMeta {
itemID: number, itemID: number,
ts: number, ts: number,
sats_per_minute: number, sats_per_minute: number,
speed?: string,
} }
type DestinationType = 'wallet' | 'node' type DestinationType = 'wallet' | 'node'
@ -49,6 +50,7 @@ export const streamFeed = async (req, res) => {
itemID: meta.itemID, itemID: meta.itemID,
ts: meta.ts || 0, ts: meta.ts || 0,
sats_per_minute: amount || 0, sats_per_minute: amount || 0,
speed: meta.speed || '1'
} }
const chat = await models.Chat.findOne({ where: { id: chat_id } }) const chat = await models.Chat.findOne({ where: { id: chat_id } })
if (!chat) { if (!chat) {

Loading…
Cancel
Save