Browse Source

Merge pull request #95 from stakwork/chan-reserve

Chan reserve
dependabot/npm_and_yarn/ini-1.3.7
Evan Feenstra 4 years ago
committed by GitHub
parent
commit
bb7959a356
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 30
      dist/src/controllers/details.js
  2. 2
      dist/src/controllers/details.js.map
  3. 32
      dist/src/utils/lightning.js
  4. 2
      dist/src/utils/lightning.js.map
  5. 35
      src/controllers/details.ts
  6. 28
      src/utils/lightning.ts

30
dist/src/controllers/details.js

@ -95,18 +95,24 @@ exports.getBalance = (req, res) => __awaiter(void 0, void 0, void 0, function* (
date.setMilliseconds(0);
const owner = yield models_1.models.Contact.findOne({ where: { isOwner: true } });
owner.update({ lastActive: date });
const lightning = lightning_1.loadLightning();
var request = {};
lightning.channelBalance(request, function (err, response) {
res.status(200);
if (err == null) {
res.json({ success: true, response });
}
else {
res.json({ success: false });
}
res.end();
});
res.status(200);
try {
const response = yield lightning_1.channelBalance();
const channelList = yield lightning_1.listChannels();
const { channels } = channelList;
const reserve = channels.reduce((a, chan) => a + parseInt(chan.local_chan_reserve_sat), 0);
res.json({ success: true, response: {
reserve,
full_balance: parseInt(response.balance),
balance: parseInt(response.balance) - reserve,
pending_open_balance: parseInt(response.pending_open_balance),
} });
}
catch (e) {
console.log("ERROR getBalance", e);
res.json({ success: false });
}
res.end();
});
exports.getLocalRemoteBalance = (req, res) => __awaiter(void 0, void 0, void 0, function* () {
const lightning = lightning_1.loadLightning();

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

File diff suppressed because one or more lines are too long

32
dist/src/utils/lightning.js

@ -443,6 +443,38 @@ function getInfo() {
});
}
exports.getInfo = getInfo;
function listChannels() {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
const lightning = loadLightning();
lightning.listChannels({}, function (err, response) {
if (err == null) {
resolve(response);
}
else {
reject(err);
}
});
});
});
}
exports.listChannels = listChannels;
function channelBalance() {
return __awaiter(this, void 0, void 0, function* () {
return new Promise((resolve, reject) => {
const lightning = loadLightning();
lightning.channelBalance({}, function (err, response) {
if (err == null) {
resolve(response);
}
else {
reject(err);
}
});
});
});
}
exports.channelBalance = channelBalance;
function ascii_to_hexa(str) {
var arr1 = [];
for (var n = 0, l = str.length; n < l; n++) {

2
dist/src/utils/lightning.js.map

File diff suppressed because one or more lines are too long

35
src/controllers/details.ts

@ -1,4 +1,4 @@
import { loadLightning, queryRoute } from '../utils/lightning'
import { loadLightning, queryRoute, channelBalance, listChannels } from '../utils/lightning'
import { success, failure } from '../utils/res'
import * as readLastLines from 'read-last-lines'
import { nodeinfo } from '../utils/nodeinfo';
@ -76,6 +76,11 @@ export const getChannels = async (req, res) => {
});
};
interface BalanceRes {
pending_open_balance: number
balance: number
reserve: number
}
export const getBalance = async (req, res) => {
var date = new Date()
@ -83,17 +88,23 @@ export const getBalance = async (req, res) => {
const owner = await models.Contact.findOne({ where: { isOwner: true } })
owner.update({ lastActive: date })
const lightning = loadLightning()
var request = {}
lightning.channelBalance(request, function (err, response) {
res.status(200);
if (err == null) {
res.json({ success: true, response });
} else {
res.json({ success: false });
}
res.end();
});
res.status(200);
try {
const response = await channelBalance()
const channelList = await listChannels()
const { channels } = channelList
const reserve = channels.reduce((a, chan) => a + parseInt(chan.local_chan_reserve_sat), 0)
res.json({ success: true, response:<BalanceRes>{
reserve,
full_balance: parseInt(response.balance),
balance: parseInt(response.balance) - reserve,
pending_open_balance: parseInt(response.pending_open_balance),
} });
} catch(e) {
console.log("ERROR getBalance",e)
res.json({ success: false });
}
res.end();
};
export const getLocalRemoteBalance = async (req, res) => {

28
src/utils/lightning.ts

@ -390,6 +390,32 @@ async function getInfo(): Promise<{[k:string]:any}>{
})
}
async function listChannels(): Promise<{[k:string]:any}>{
return new Promise((resolve,reject)=>{
const lightning = loadLightning()
lightning.listChannels({}, function(err, response) {
if (err == null) {
resolve(response)
} else {
reject(err)
}
});
})
}
async function channelBalance(): Promise<{[k:string]:any}>{
return new Promise((resolve,reject)=>{
const lightning = loadLightning()
lightning.channelBalance({}, function(err, response) {
if (err == null) {
resolve(response)
} else {
reject(err)
}
});
})
}
function ascii_to_hexa(str){
var arr1 = <string[]> [];
for (var n = 0, l = str.length; n < l; n ++) {
@ -423,4 +449,6 @@ export {
listAllInvoices,
listAllPaymentsFull,
queryRoute,
listChannels,
channelBalance,
}

Loading…
Cancel
Save