thunderhub/server/schema/bos/resolvers.ts

146 lines
3.5 KiB
TypeScript
Raw Normal View History

2021-01-19 23:23:26 +01:00
import fs from 'fs';
import { ContextType } from 'server/types/apiTypes';
2020-08-19 08:35:17 +02:00
import { to, toWithError } from 'server/helpers/async';
import { logger } from 'server/helpers/logger';
import { rebalance } from 'balanceofsatoshis/swaps';
2020-08-19 08:35:17 +02:00
import { pay } from 'balanceofsatoshis/network';
import { getAccountingReport } from 'balanceofsatoshis/balances';
2021-08-27 11:41:14 +02:00
import { fetchRequest } from 'balanceofsatoshis/commands';
import { RebalanceResponseType } from 'server/types/balanceofsatoshis.types';
2020-08-19 08:35:17 +02:00
import { getErrorMsg } from 'server/helpers/helpers';
type PayType = {
max_fee: Number;
max_paths: Number;
request: String;
message?: String;
out?: String[];
};
type RebalanceType = {
avoid?: String[];
in_through?: String;
max_fee?: Number;
max_fee_rate?: Number;
max_rebalance?: Number;
timeout_minutes?: Number;
node?: String;
out_channels?: String[];
out_through?: String;
2021-01-24 22:58:22 +01:00
out_inbound?: Number;
};
type AccountingType = {
category?: String;
currency?: String;
fiat?: String;
month?: String;
year?: String;
};
export const bosResolvers = {
Query: {
getAccountingReport: async (
_: undefined,
params: AccountingType,
context: ContextType
) => {
const { lnd } = context;
const response = await to(
getAccountingReport({
lnd,
logger,
2021-08-27 11:41:14 +02:00
request: fetchRequest,
is_csv: true,
...params,
})
);
return response;
},
},
Mutation: {
2020-08-19 08:35:17 +02:00
bosPay: async (_: undefined, params: PayType, context: ContextType) => {
const { lnd } = context;
const { max_fee, max_paths, message, out, request } = params;
const props = {
max_fee,
max_paths,
...(message && { message }),
out: out || [],
2021-07-24 12:03:30 +02:00
avoid: [],
2020-08-19 08:35:17 +02:00
};
logger.debug('Paying invoice with params: %o', props);
const [response, error] = await toWithError(
pay({
lnd,
logger,
...props,
request,
})
);
if (error) {
logger.error('Error paying invoice: %o', error);
throw new Error(getErrorMsg(error));
}
logger.debug('Paid invoice: %o', response);
return true;
},
bosRebalance: async (
_: undefined,
2021-01-24 22:58:22 +01:00
{
avoid,
in_through,
max_fee,
max_fee_rate,
max_rebalance,
timeout_minutes,
node,
out_through,
2021-01-24 22:58:22 +01:00
out_inbound,
}: RebalanceType,
{ lnd }: ContextType
) => {
const filteredParams = {
2020-08-27 18:46:44 +02:00
out_channels: [],
2020-06-27 19:15:49 +02:00
avoid,
...(in_through && { in_through }),
...(max_fee && max_fee > 0 && { max_fee }),
...(max_fee_rate && max_fee_rate > 0 && { max_fee_rate }),
...(timeout_minutes && timeout_minutes > 0 && { timeout_minutes }),
2021-01-24 22:58:22 +01:00
...(max_rebalance &&
max_rebalance > 0 && { max_rebalance: `${max_rebalance}` }),
...(node && { node }),
...(out_through && { out_through }),
2021-01-24 22:58:22 +01:00
...(out_inbound &&
out_inbound > 0 && { out_inbound: `${out_inbound}` }),
};
logger.info('Rebalance Params: %o', filteredParams);
const response = await to<RebalanceResponseType>(
rebalance({
lnd,
logger,
2021-01-19 23:23:26 +01:00
fs: { getFile: fs.readFile },
...filteredParams,
})
);
const result = {
increase: response.rebalance[0],
decrease: response.rebalance[1],
result: response.rebalance[2],
};
return result;
},
},
};