balance command

This commit is contained in:
Alex Bosworth 2019-02-10 16:15:06 -08:00
commit 2d58ece5c7
No known key found for this signature in database
GPG key ID: E80D2F3F311FD87E
15 changed files with 4388 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
.env
*~
node_modules

1
.tm_properties Normal file
View file

@ -0,0 +1 @@
exclude = '{$exclude,node_modules}'

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2019 Alex Bosworth
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

9
README.md Normal file
View file

@ -0,0 +1,9 @@
# Balance of Satoshis
Commands for working with Lightning balances.
```
bos balance
// Outputs the full satoshis balance of the node
```

3
async/index.js Normal file
View file

@ -0,0 +1,3 @@
const returnResult = require('./return_result');
module.exports = {returnResult};

22
async/return_result.js Normal file
View file

@ -0,0 +1,22 @@
/** Return result of async auto flow
{
[of]: <Attribute String>
}
@returns
<(err, res) Function>
*/
module.exports = (args, cbk) => {
return (err, res) => {
if (!!err) {
return cbk(err);
}
if (!!args.of) {
return cbk(null, res[args.of]);
}
return cbk();
};
};

26
bos Executable file
View file

@ -0,0 +1,26 @@
#!/usr/bin/env node
const prog = require('caporal');
const {getBalance} = require('./funds');
const {version} = require('./package');
prog
.version(version)
.command('balance', 'Get total tokens')
.action((args, options, logger) => {
return new Promise((resolve, reject) => {
return getBalance({}, (err, res) => {
if (!!err) {
return reject(err);
}
const {balance} = res;
logger.info(`${balance}`);
return resolve();
});
});
});
prog.parse(process.argv);

59
funds/get_balance.js Normal file
View file

@ -0,0 +1,59 @@
const asyncAuto = require('async/auto');
const {getChainBalance} = require('ln-service');
const {getChannelBalance} = require('ln-service');
const {getPendingChainBalance} = require('ln-service');
const {lightningDaemon} = require('ln-service');
const {lndCredentials} = require('./../lnd');
const {returnResult} = require('./../async');
const credentials = lndCredentials({});
/** Get the existing balance
{}
@returns via cbk
{
balance: <Tokens Number>
}
*/
module.exports = ({}, cbk) => {
return asyncAuto({
// Lnd
lnd: cbk => {
return cbk(null, lightningDaemon({
cert: credentials.cert,
macaroon: credentials.macaroon,
socket: credentials.socket,
}));
},
// Get the chain balance
getChainBalance: ['lnd', ({lnd}, cbk) => getChainBalance({lnd}, cbk)],
// Get the channel balance
getChannelBalance: ['lnd', ({lnd}, cbk) => getChannelBalance({lnd}, cbk)],
// Get the pending balance
getPending: ['lnd', ({lnd}, cbk) => getPendingChainBalance({lnd}, cbk)],
// Total balances
balance: [
'getChainBalance',
'getChannelBalance',
'getPending',
({getChainBalance, getChannelBalance, getPending}, cbk) =>
{
const balances = [
getChainBalance.chain_balance,
getChannelBalance.channel_balance,
getChannelBalance.pending_balance,
getPending.pending_chain_balance,
];
return cbk(null, {balance: balances.reduce((sum, n) => n + sum, 0)});
}],
},
returnResult({of: 'balance'}, cbk));
};

3
funds/index.js Normal file
View file

@ -0,0 +1,3 @@
const getBalance = require('./get_balance');
module.exports = {getBalance};

3
lnd/index.js Normal file
View file

@ -0,0 +1,3 @@
const lndCredentials = require('./lnd_credentials');
module.exports = {lndCredentials};

29
lnd/lnd_credentials.js Normal file
View file

@ -0,0 +1,29 @@
const {join} = require('path');
const {readFileSync} = require('fs');
const lndDirectory = require('./lnd_directory');
const b64 = 'base64';
const certPath = ['tls.cert'];
const macaroonPath = ['data', 'chain', 'bitcoin', 'mainnet', 'admin.macaroon'];
const {path} = lndDirectory({});
const socket = 'localhost:10009';
/** Lnd credentials
{}
@returns
{
cert: <Cert String>
macaroon: <Macaroon String>
socket: <Socket String>
}
*/
module.exports = ({}) => {
return {
socket,
cert: readFileSync(join(...[path].concat(certPath))).toString(b64),
macaroon: readFileSync(join(...[path].concat(macaroonPath))).toString(b64),
};
};

24
lnd/lnd_directory.js Normal file
View file

@ -0,0 +1,24 @@
const {homedir} = require('os');
const {join} = require('path');
const {platform} = require('os');
const platforms = require('./platforms');
/** LND directory path
{}
@returns
{
path: <LND Directory Path String>
}
*/
module.exports = ({}) => {
switch (platform()) {
case platforms.macOS:
return {path: join(homedir(), 'Library', 'Application Support', 'Lnd')};
default:
return {path: join(homedir(), 'lnd')};
}
};

4
lnd/platforms.json Normal file
View file

@ -0,0 +1,4 @@
{
"macOS": "darwin",
"windows": "win32"
}

4161
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

20
package.json Normal file
View file

@ -0,0 +1,20 @@
{
"author": "Alex Bosworth",
"bugs": {
"url": "https://github.com/alexbosworth/balanceofsatoshis/issues"
},
"dependencies": {
"async": "2.6.1",
"caporal": "1.1.0",
"ln-service": "file:/Users/alex/repos/ln-service"
},
"description": "Lightning balance CLI",
"license": "MIT",
"main": "index.js",
"name": "balanceofsatoshis",
"repository": {
"type": "git",
"url": "https://github.com/alexbosworth/balanceofsatoshis.git"
},
"version": "1.0.0"
}