Skip to content

Commit

Permalink
Merge pull request #1596 from synonymdev/update-fee-estimates
Browse files Browse the repository at this point in the history
refactor(wallet): Update Fee Estimates
  • Loading branch information
coreyphillips authored Mar 7, 2024
2 parents 098420a + b5b8e30 commit b456386
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
8 changes: 3 additions & 5 deletions src/screens/Settings/Fee/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ import {
updateOnchainFees,
updateOverrideFees,
} from '../../../store/slices/fees';
import { updateOnchainFeeEstimates } from '../../../store/utils/fees';
import { selectedNetworkSelector } from '../../../store/reselect/wallet';
import { refreshOnchainFeeEstimates } from '../../../store/utils/fees';
import { EItemType, IListData } from '../../../components/List';
import Button from '../../../components/Button';
import SafeAreaInset from '../../../components/SafeAreaInset';
Expand Down Expand Up @@ -78,7 +77,6 @@ const FeeSettings = ({}: SettingsScreenProps<'FeeSettings'>): ReactElement => {
const dispatch = useAppDispatch();
const fees = useAppSelector(onChainFeesSelector);
const override = useAppSelector(overrideFeeSelector);
const selectedNetwork = useAppSelector(selectedNetworkSelector);
const [loading, setLoading] = useState(false);

const handleMinus = (title: string): void => {
Expand Down Expand Up @@ -140,7 +138,7 @@ const FeeSettings = ({}: SettingsScreenProps<'FeeSettings'>): ReactElement => {
value: updated,
type: EItemType.textButton,
onPress: (): void => {
updateOnchainFeeEstimates({ selectedNetwork, forceUpdate: true });
refreshOnchainFeeEstimates({ forceUpdate: true });
},
},
{
Expand All @@ -154,7 +152,7 @@ const FeeSettings = ({}: SettingsScreenProps<'FeeSettings'>): ReactElement => {
],
},
];
}, [override, fees, tTime, selectedNetwork, dispatch]);
}, [override, fees, tTime, dispatch]);

return (
<ThemedView style={styles.container}>
Expand Down
23 changes: 15 additions & 8 deletions src/store/utils/fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { dispatch, getFeesStore } from '../helpers';
import { updateOnchainFees } from '../slices/fees';
import { getFeeEstimates } from '../../utils/wallet/transactions';
import { EAvailableNetwork } from '../../utils/networks';
import { getSelectedNetwork } from '../../utils/wallet';
import { getOnChainWallet, getSelectedNetwork } from '../../utils/wallet';
import { IOnchainFees } from 'beignet';

export const REFRESH_INTERVAL = 60 * 30; // in seconds, 30 minutes
Expand All @@ -19,18 +19,16 @@ export const updateOnchainFeeEstimates = async ({
feeEstimates?: IOnchainFees;
}): Promise<Result<string>> => {
const feesStore = getFeesStore();
const timestamp = feesStore.onchain.timestamp;
const difference = Math.floor((Date.now() - timestamp) / 1000);

if (feesStore.override) {
return ok('On-chain fee estimates are overridden.');
}

if (!forceUpdate && difference < REFRESH_INTERVAL) {
return ok('On-chain fee estimates are up to date.');
}

if (!feeEstimates) {
const timestamp = feesStore.onchain.timestamp;
const difference = Math.floor((Date.now() - timestamp) / 1000);
if (!forceUpdate && difference < REFRESH_INTERVAL) {
return ok('On-chain fee estimates are up to date.');
}
const feeEstimatesRes = await getFeeEstimates(selectedNetwork);
if (feeEstimatesRes.isErr()) {
return err(feeEstimatesRes.error);
Expand All @@ -42,3 +40,12 @@ export const updateOnchainFeeEstimates = async ({

return ok('Successfully updated on-chain fee estimates.');
};

export const refreshOnchainFeeEstimates = async ({
forceUpdate = false,
}: {
forceUpdate?: boolean;
}): Promise<Result<IOnchainFees>> => {
const wallet = getOnChainWallet();
return await wallet.updateFeeEstimates(forceUpdate);
};
6 changes: 3 additions & 3 deletions src/utils/lightning/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ import {
TLightningNodeVersion,
} from '../../store/types/lightning';
import { getBlocktankInfo, isGeoBlocked } from '../blocktank';
import { updateOnchainFeeEstimates } from '../../store/utils/fees';
import { refreshOnchainFeeEstimates } from '../../store/utils/fees';
import { reportLdkChannelMigrations } from '../checks';
import {
__BACKUPS_SERVER_HOST__,
Expand Down Expand Up @@ -1524,8 +1524,8 @@ export const closeAllChannels = async ({
return err(refreshRes.error.message);
}

// Update fees before closing channels
await updateOnchainFeeEstimates({ selectedNetwork, forceUpdate: true });
// Force update fees before closing channels
await refreshOnchainFeeEstimates({ forceUpdate: true });

const channelsUnableToCoopClose: TChannel[] = [];
await Promise.all(
Expand Down

0 comments on commit b456386

Please sign in to comment.