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 });
const Sphinx = require("sphinx-bot");
const api_1 = require("../controllers/api");
const node_fetch_1 = require("node-fetch");
// import fetch from 'node-fetch'
const path = require("path");
const models_1 = require("../models");
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');
const msg_types = Sphinx.MSG_TYPE;
let initted = false;
const baseurl = 'https://localhost:8080';
// const baseurl = 'https://localhost:8080'
function init() {
if (initted)
return;
@ -56,12 +59,53 @@ function init() {
message.channel.send({ embed });
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 {
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;
const bot = yield getBot(message.channel.id);
if (bot && bot.meta)
@ -73,89 +117,108 @@ function init() {
message.channel.send({ embed });
return;
}
const j2 = yield 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
}),
const cmd = `loop`;
const args = [
`--tlscertpath=${config.tls_location}`,
`--macaroonpath=${config.loop_macaroon_location}`,
`--rpcserver=localhost:10009`,
'out',
`--channel=${chan}`,
`--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()
.setAuthor('LoopBot')
.setDescription('Error: ' + j2.error);
.setDescription('Channel updated to ' + chan)
.setThumbnail(botSVG);
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);
}
}
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()
.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;
}
}
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;
}
} // end else
}));
}
exports.init = init;
function getBot(tribeUUID) {
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
} });
}
});
if (!chat)
return;
const chatBot = yield models_1.models.ChatBot.findOne({ where: {
const chatBot = yield models_1.models.ChatBot.findOne({
where: {
chatId: chat.id,
botPrefix: '/loopout',
botType: constants_1.default.bot_types.builtin
} });
}
});
return chatBot;
});
}
@ -167,34 +230,33 @@ function validateAmount(amtString) {
const ok = amt > 0;
return ok;
}
const fs = require('fs');
const https = require("https");
// const fs = require('fs')
// const https = require("https");
// const homedir = require('os').homedir();
const agent = new https.Agent({
rejectUnauthorized: false
});
const env = process.env.NODE_ENV || 'development';
const config = require(path.join(__dirname, '../../config/app.json'))[env];
function doRequest(theurl, params) {
return __awaiter(this, void 0, void 0, function* () {
const ps = params || {};
try {
const macLocation = config.loop_macaroon_location;
if (!macLocation) {
throw new Error('no macaroon');
}
var macaroonString = fs.readFileSync(macLocation);
var mac = Buffer.from(macaroonString, 'utf8').toString('hex');
const theParams = Object.assign({ agent, headers: {
'Grpc-Metadata-macaroon': mac
} }, ps);
const r = yield node_fetch_1.default(theurl, theParams);
const j = yield r.json();
return j;
}
catch (e) {
throw e;
}
});
}
// const agent = new https.Agent({
// rejectUnauthorized: false
// })
// async function doRequest(theurl: string, params?: Object) {
// const ps = params || {}
// try {
// const macLocation = config.loop_macaroon_location
// if (!macLocation) {
// throw new Error('no macaroon')
// }
// var macaroonString = fs.readFileSync(macLocation);
// var mac = Buffer.from(macaroonString, 'utf8').toString('hex');
// const theParams = {
// agent,
// headers: {
// 'Grpc-Metadata-macaroon': mac
// },
// ...ps
// }
// const r = await fetch(theurl, theParams)
// const j = await r.json()
// return j
// } catch (e) {
// throw e
// }
// }
//# 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,
ts: meta.ts || 0,
sats_per_minute: amount || 0,
speed: meta.speed || '1'
};
const chat = yield models_1.models.Chat.findOne({ where: { id: chat_id } });
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,EACN,OAAO,EACP,IAAI,EACJ,WAAW,GACZ,GAMG,GAAG,CAAC,IAAI,CAAA;IAEZ,IAAI,CAAC,CAAC,YAAY,IAAI,YAAY,CAAC,MAAM,CAAC,EAAE;QAC1C,OAAO,aAAO,CAAC,GAAG,EAAE,iBAAiB,CAAC,CAAA;KACvC;IAED,IAAI,WAAW,EAAE;QACf,IAAI,IAAI,CAAC;QACT,IAAI;YACF,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA;SACxB;QAAC,OAAO,CAAC,EAAE,GAAG;QACf,IAAI,CAAC,IAAI,EAAE;YACT,OAAO,aAAO,CAAC,GAAG,EAAE,SAAS,CAAC,CAAA;SAC/B;QACD,IAAI,IAAI,IAAI,IAAI,CAAC,MAAM,EAAE;YACvB,MAAM,EAAE,GAAa;gBACnB,MAAM,EAAE,IAAI,CAAC,MAAM;gB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AM,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 { finalAction } from '../controllers/api'
import fetch from 'node-fetch'
// import fetch from 'node-fetch'
import * as path from 'path'
import { models } from '../models'
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');
const msg_types = Sphinx.MSG_TYPE
let initted = false
const baseurl = 'https://localhost:8080'
// const baseurl = 'https://localhost:8080'
export function init() {
if (initted) return
@ -42,20 +47,62 @@ export function init() {
message.channel.send({ embed })
return
}
if (messageAmount < parseInt(amt)){
if (messageAmount < parseInt(amt)) {
const embed = new Sphinx.MessageEmbed()
.setAuthor('LoopBot')
.setDescription('Incorrect amount')
message.channel.send({ embed })
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
}
// 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 {
let chan
const bot = await getBot(message.channel.id)
if (bot && bot.meta) chan = bot.meta
@ -66,90 +113,109 @@ export function init() {
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
}),
const cmd = `loop`
const args = [
`--tlscertpath=${config.tls_location}`,
`--macaroonpath=${config.loop_macaroon_location}`,
`--rpcserver=localhost:10009`,
'out',
`--channel=${chan}`,
`--amt=${amt}`,
`--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)
if (j2 && j2.error) {
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)
});
} 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()
.setAuthor('LoopBot')
.setDescription('Error: ' + j2.error)
.setDescription('Channel updated to ' + chan)
.setThumbnail(botSVG)
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)
}
}
const cmd = arr[1]
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
}
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){
const chat = await models.Chat.findOne({ where:{
uuid: tribeUUID
}})
async function getBot(tribeUUID: string) {
const chat = await models.Chat.findOne({
where: {
uuid: tribeUUID
}
})
if (!chat) return
const chatBot = await models.ChatBot.findOne({where: {
chatId: chat.id,
botPrefix: '/loopout',
botType: constants.bot_types.builtin
}})
const chatBot = await models.ChatBot.findOne({
where: {
chatId: chat.id,
botPrefix: '/loopout',
botType: constants.bot_types.builtin
}
})
return chatBot
}
@ -163,36 +229,34 @@ function validateAmount(amtString: string) {
return ok
}
const fs = require('fs')
const https = require("https");
// const fs = require('fs')
// const https = require("https");
// const homedir = require('os').homedir();
const agent = new https.Agent({
rejectUnauthorized: false
})
// const agent = new https.Agent({
// 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) {
const ps = params || {}
try {
const macLocation=config.loop_macaroon_location
if(!macLocation) {
throw new Error('no macaroon')
}
var macaroonString = fs.readFileSync(macLocation);
var mac = Buffer.from(macaroonString, 'utf8').toString('hex');
const theParams = {
agent,
headers: {
'Grpc-Metadata-macaroon': mac
},
...ps
}
const r = await fetch(theurl, theParams)
const j = await r.json()
return j
} catch (e) {
throw e
}
}
// async function doRequest(theurl: string, params?: Object) {
// const ps = params || {}
// try {
// const macLocation = config.loop_macaroon_location
// if (!macLocation) {
// throw new Error('no macaroon')
// }
// var macaroonString = fs.readFileSync(macLocation);
// var mac = Buffer.from(macaroonString, 'utf8').toString('hex');
// const theParams = {
// agent,
// headers: {
// 'Grpc-Metadata-macaroon': mac
// },
// ...ps
// }
// const r = await fetch(theurl, theParams)
// const j = await r.json()
// return j
// } catch (e) {
// throw e
// }
// }

1
src/builtin/mother.ts

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

2
src/controllers/feed.ts

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

Loading…
Cancel
Save