|
|
@ -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,34 +113,27 @@ 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 |
|
|
|
}), |
|
|
|
}) |
|
|
|
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 |
|
|
|
const cmd = `loop --tlscertpath=${config.tls_location} --rpcserver=localhost:10009 out --channel=${chan} --amt=${amt} --fast --addr=${addy}` |
|
|
|
console.log("=> LOOPBOT cmd:",cmd) |
|
|
|
let childProcess = spawn(cmd) |
|
|
|
childProcess.stdout.on('data', function (data) { |
|
|
|
const stdout = data.toString() |
|
|
|
console.log("LOOPBOT stdout:",stdout) |
|
|
|
if(stdout){ |
|
|
|
console.log('=> LOOPBOT stdout',stdout) |
|
|
|
if(stdout.startsWith('CONTINUE SWAP?')) { |
|
|
|
childProcess.stdin.write('y\n') |
|
|
|
} |
|
|
|
// if (!(j2 && j2.server_message)) {
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
if(stdout.startsWith('Swap initiated')) { |
|
|
|
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) |
|
|
|
} |
|
|
@ -103,15 +143,15 @@ export function init() { |
|
|
|
|
|
|
|
const isAdmin = message.member.roles.find(role => role.name === 'Admin') |
|
|
|
|
|
|
|
if(isAdmin && cmd.startsWith('setchan=')) { |
|
|
|
if (isAdmin && cmd.startsWith('setchan=')) { |
|
|
|
const bot = await getBot(message.channel.id) |
|
|
|
const arr = cmd.split('=') |
|
|
|
if(bot && arr.length>1) { |
|
|
|
if (bot && arr.length > 1) { |
|
|
|
const chan = arr[1] |
|
|
|
await bot.update({meta: chan}) |
|
|
|
await bot.update({ meta: chan }) |
|
|
|
const embed = new Sphinx.MessageEmbed() |
|
|
|
.setAuthor('LoopBot') |
|
|
|
.setDescription('Channel updated to '+chan) |
|
|
|
.setDescription('Channel updated to ' + chan) |
|
|
|
.setThumbnail(botSVG) |
|
|
|
message.channel.send({ embed }) |
|
|
|
return |
|
|
@ -140,16 +180,20 @@ export function init() { |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
async function getBot(tribeUUID:string){ |
|
|
|
const chat = await models.Chat.findOne({ where:{ |
|
|
|
async function getBot(tribeUUID: string) { |
|
|
|
const chat = await models.Chat.findOne({ |
|
|
|
where: { |
|
|
|
uuid: tribeUUID |
|
|
|
}}) |
|
|
|
} |
|
|
|
}) |
|
|
|
if (!chat) return |
|
|
|
const chatBot = await models.ChatBot.findOne({where: { |
|
|
|
const chatBot = await models.ChatBot.findOne({ |
|
|
|
where: { |
|
|
|
chatId: chat.id, |
|
|
|
botPrefix: '/loopout', |
|
|
|
botType: constants.bot_types.builtin |
|
|
|
}}) |
|
|
|
} |
|
|
|
}) |
|
|
|
return chatBot |
|
|
|
} |
|
|
|
|
|
|
@ -163,36 +207,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 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 |
|
|
|
} |
|
|
|
} |
|
|
|
// 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
|
|
|
|
// }
|
|
|
|
// }
|