diff --git a/af.py b/af.py index 84164608..0260f722 100644 --- a/af.py +++ b/af.py @@ -2,7 +2,7 @@ from django.db.models import Sum from datetime import datetime, timedelta from os import environ -from pandas import DataFrame, concat +from pandas import DataFrame, concat, merge environ['DJANGO_SETTINGS_MODULE'] = 'lndg.settings' django.setup() from gui.models import Forwards, Channels, LocalSettings, FailedHTLCs @@ -62,7 +62,9 @@ def main(channels): forwards_df_in_1d_sum = DataFrame.from_records(forwards_1d.values('chan_id_in').annotate(amt_out_msat=Sum('amt_out_msat'), fee=Sum('fee')), 'chan_id_in') if forwards.exists(): forwards_df_in_7d_sum = DataFrame.from_records(forwards.values('chan_id_in').annotate(amt_out_msat=Sum('amt_out_msat'), fee=Sum('fee')), 'chan_id_in') + forwards_df_in_7d_sum = merge(forwards_df_in_7d_sum, channels_df[['chan_id', 'remote_pubkey']], left_on='chan_id_in', right_on='chan_id', how='left') forwards_df_out_7d_sum = DataFrame.from_records(forwards.values('chan_id_out').annotate(amt_out_msat=Sum('amt_out_msat'), fee=Sum('fee')), 'chan_id_out') + forwards_df_out_7d_sum = merge(forwards_df_out_7d_sum, channels_df[['chan_id', 'remote_pubkey']], left_on='chan_id_out', right_on='chan_id', how='left') else: forwards_df_in_7d_sum = DataFrame() forwards_df_out_7d_sum = DataFrame() @@ -76,29 +78,34 @@ def main(channels): channels_df['net_routed_7day'] = channels_df.apply(lambda row: round((row['amt_routed_out_7day']-row['amt_routed_in_7day'])/row['capacity'], 1), axis=1) channels_df['local_balance'] = channels_df.apply(lambda row: row.local_balance + row.pending_outbound, axis=1) channels_df['remote_balance'] = channels_df.apply(lambda row: row.remote_balance + row.pending_inbound, axis=1) - channels_df['in_percent'] = channels_df.apply(lambda row: int(round((row['remote_balance']/row['capacity'])*100, 0)), axis=1) - channels_df['out_percent'] = channels_df.apply(lambda row: int(round((row['local_balance']/row['capacity'])*100, 0)), axis=1) channels_df['eligible'] = channels_df.apply(lambda row: (datetime.now()-row['fees_updated']).total_seconds() > (update_hours*3600), axis=1) + # Records for AutoFees to process, grouped by peer pubkey + autofees_df = channels_df[['remote_pubkey', 'capacity', 'local_balance', 'remote_balance', 'local_fee_rate', 'amt_routed_in_1day', 'amt_routed_in_7day', 'amt_routed_out_7day', 'net_routed_7day']].copy() + autofees_df = autofees_df.groupby('remote_pubkey').sum() + autofees_df['in_percent'] = autofees_df.apply(lambda row: int(round((row['remote_balance']/row['capacity'])*100, 0)), axis=1) + autofees_df['out_percent'] = autofees_df.apply(lambda row: int(round((row['local_balance']/row['capacity'])*100, 0)), axis=1) + # Low Liquidity - lowliq_df = channels_df[channels_df['out_percent'] <= lowliq_limit].copy() + lowliq_df = autofees_df[autofees_df['out_percent'] <= lowliq_limit].copy() failed_htlc_df = DataFrame.from_records(FailedHTLCs.objects.exclude(wire_failure=99).filter(timestamp__gte=filter_1day).order_by('-id').values()) if failed_htlc_df.shape[0] > 0: failed_htlc_df = failed_htlc_df[(failed_htlc_df['wire_failure']==15) & (failed_htlc_df['failure_detail']==6) & (failed_htlc_df['amount']>failed_htlc_df['chan_out_liq']+failed_htlc_df['chan_out_pending'])] - lowliq_df['failed_out_1day'] = 0 if failed_htlc_df.empty else lowliq_df.apply(lambda row: len(failed_htlc_df[failed_htlc_df['chan_id_out']==row.chan_id]), axis=1) + failed_htlc_df = merge(failed_htlc_df, channels_df[['chan_id', 'remote_pubkey']], left_on='chan_id_out', right_on='chan_id', how='left') + lowliq_df['failed_out_1day'] = 0 if failed_htlc_df.empty else merge(lowliq_df, failed_htlc_df[['remote_pubkey']], left_index=True, right_on='remote_pubkey', how='left').groupby('remote_pubkey').size().reindex(lowliq_df.index, fill_value=0) # INCREASE IF (failed htlc > threshhold) && (flow in == 0) lowliq_df['new_rate'] = lowliq_df.apply(lambda row: row['local_fee_rate']+(5*multiplier) if row['failed_out_1day']>failed_htlc_limit and row['amt_routed_in_1day'] == 0 else row['local_fee_rate'], axis=1) # Balanced Liquidity - balanced_df = channels_df[(channels_df['out_percent'] > lowliq_limit) & (channels_df['out_percent'] < excess_limit)].copy() + balanced_df = autofees_df[(autofees_df['out_percent'] > lowliq_limit) & (autofees_df['out_percent'] < excess_limit)].copy() # IF NO FLOW THEN DECREASE FEE AND IF HIGH FLOW THEN SLOWLY INCREASE FEE balanced_df['new_rate'] = balanced_df.apply(lambda row: row['local_fee_rate']+((2*multiplier)*(1+(row['net_routed_7day']/row['capacity']))) if row['net_routed_7day'] > 1 else row['local_fee_rate'], axis=1) balanced_df['new_rate'] = balanced_df.apply(lambda row: row['local_fee_rate']-(3*multiplier) if (row['amt_routed_in_7day']+row['amt_routed_out_7day']) == 0 else row['new_rate'], axis=1) # Excess Liquidity - excess_df = channels_df[channels_df['out_percent'] >= excess_limit].copy() - excess_df['revenue_7day'] = excess_df.apply(lambda row: int(forwards_df_out_7d_sum.loc[row.chan_id].fee) if forwards_df_out_7d_sum.empty == False and (forwards_df_out_7d_sum.index == row.chan_id).any() else 0, axis=1) - excess_df['revenue_assist_7day'] = excess_df.apply(lambda row: int(forwards_df_in_7d_sum.loc[row.chan_id].fee) if forwards_df_in_7d_sum.empty == False and (forwards_df_in_7d_sum.index == row.chan_id).any() else 0, axis=1) + excess_df = autofees_df[autofees_df['out_percent'] >= excess_limit].copy() + excess_df['revenue_7day'] = 0 if forwards_df_out_7d_sum.empty else merge(excess_df, forwards_df_out_7d_sum.groupby('remote_pubkey').sum('fee')[['fee']], on='remote_pubkey', how='left')['fee'].fillna(0).astype(int) + excess_df['revenue_assist_7day'] = 0 if forwards_df_in_7d_sum.empty else merge(excess_df, forwards_df_in_7d_sum.groupby('remote_pubkey').sum('fee')[['fee']], on='remote_pubkey', how='left')['fee'].fillna(0).astype(int) # DECREASE IF (assisting channel or stagnant liq) excess_df['new_rate'] = excess_df.apply(lambda row: row['local_fee_rate']-(5*multiplier) if row['net_routed_7day'] < 0 and row['revenue_assist_7day'] > (row['revenue_7day']*10) else row['local_fee_rate'], axis=1) excess_df['new_rate'] = excess_df.apply(lambda row: row['local_fee_rate']-(5*multiplier) if (row['amt_routed_in_7day']+row['amt_routed_out_7day']) == 0 else row['new_rate'], axis=1) @@ -109,7 +116,9 @@ def main(channels): result_df['new_rate'] = result_df.apply(lambda row: max_rate if max_rate < row['new_rate'] else row['new_rate'], axis=1) result_df['new_rate'] = result_df.apply(lambda row: min_rate if min_rate > row['new_rate'] else row['new_rate'], axis=1) result_df['adjustment'] = result_df.apply(lambda row: int(row['new_rate']-row['local_fee_rate']), axis=1) - return result_df + channels_df = merge(channels_df, result_df[['new_rate', 'adjustment']], on='remote_pubkey', how='left') + + return channels_df else: return DataFrame()