balanceofsatoshis/peers/open_request_violation.js
2026-07-24 13:03:30 -07:00

98 lines
2.7 KiB
JavaScript

const {evaluateFormula} = require('@alexbosworth/formulas');
const {isArray} = Array;
const {keys} = Object;
/** Find an open request violation
{
capacities: [<Existing Public Channel Capacity Tokens Number>]
capacity: <Open Channel Capacity Tokens Number>
channel_ages: [<Blocks Since Channel Open Number>]
fee_rates: [<Outgoing Parts Per Million Fee Rate Number>]
is_clearnet: <Peer Advertises Clearnet Address Bool>
is_obsolete: <Requests Obsolete Channel Type Bool>
is_private: <Requesting Not Announced Channel Bool>
is_tor: <Peer Advertises Tor Address Bool>>
local_balance: <Open Channel Gifted Tokens Number>
public_key: <Open Channel Gifted Tokens Number>
rules: [<Open Channel Request Rule String>]
}
@throws
<Error>
@returns
{
[rule]: <Rule String>
}
*/
module.exports = args => {
if (!isArray(args.capacities)) {
throw new Error('ExpectedArrayOfCapacitiesToCheckForOpenRequestViolation');
}
if (!args.capacity) {
throw new Error('ExpectedChannelCapacityToCheckForOpenRequestViolation');
}
if (!isArray(args.channel_ages)) {
throw new Error('ExpectedChannelAgesArrayToCheckForOpenRequestViolation');
}
if (!isArray(args.fee_rates)) {
throw new Error('ExpectedArrayOfFeeRatesToCheckForOpenRequestViolation');
}
if (args.is_clearnet === undefined) {
throw new Error('ExpectedClearnetStatusToCheckOpenRequestRules');
}
if (args.is_private === undefined) {
throw new Error('ExpectedChannelPrivateStatusToCheckOpenRequestRules');
}
if (args.joint_public_capacity === undefined) {
throw new Error('ExpectedJointPublicCapacityToCheckOpenRequestRules');
}
if (args.local_balance === undefined) {
throw new Error('ExpectedLocalBalanceToCheckForOpenRequestViolation');
}
if (!args.public_key) {
throw new Error('ExpectedPeerrPublicKeyToCheckForOpenRequestViolation');
}
if (!isArray(args.rules)) {
throw new Error('ExpectedArrayOfRulesToCheckForOpenRequestViolation');
}
if (args.is_tor === undefined) {
throw new Error('ExpectedTorStatusToCheckOpenRequestRules');
}
const constants = {
btc: 1e8,
capacities: args.capacities,
capacity: args.capacity,
channel_ages: args.channel_ages,
clearnet: args.is_clearnet,
fee_rates: args.fee_rates,
joint_public_capacity: args.joint_public_capacity,
k: 1e3,
local_balance: args.local_balance,
m: 1e6,
mm: 1e6,
obsolete: !!args.is_obsolete,
private: args.is_private,
public_key: args.public_key,
tor: args.is_tor,
};
const violation = args.rules.find(rule => {
return !evaluateFormula({constants, formula: rule}).result;
});
return {rule: violation};
};