Skip to content

Commit

Permalink
fix: improve BT order watching
Browse files Browse the repository at this point in the history
  • Loading branch information
limpbrains committed Jan 19, 2024
1 parent 0cbde4f commit d1556ca
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 35 deletions.
39 changes: 14 additions & 25 deletions src/screens/Lightning/CustomSetup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
} from '../../utils/conversion';
import { getFiatDisplayValues } from '../../utils/displayValues';
import { showToast } from '../../utils/notifications';
import { estimateOrderFee } from '../../utils/blocktank';
import { startChannelPurchase } from '../../store/utils/blocktank';
import { EUnit } from '../../store/types/wallet';
import {
Expand Down Expand Up @@ -272,35 +273,23 @@ const CustomSetup = ({
}

const getChannelOpenCost = async (): Promise<void> => {
// TODO: replace with estimateOrderFee
// const response2 = await estimateOrderFee({
// lspBalanceSat: spendingAmount,
// channelExpiryWeeks: DEFAULT_CHANNEL_DURATION,
// options: {
// clientBalanceSat: amount,
// couponCode: 'bitkit',
// lspNodeId: blocktankInfo.nodes[0].pubkey,
// }
// // localBalance: amount,
// // selectedWallet,
// // selectedNetwork,
// });
const response = await startChannelPurchase({
remoteBalance: spendingAmount,
localBalance: amount,
channelExpiry: DEFAULT_CHANNEL_DURATION,
selectedWallet,
selectedNetwork,
lspNodeId: blocktankInfo.nodes[0].pubkey,
turboChannel:
spendingAmount <= blocktankInfo.options.max0ConfClientBalanceSat,
const res = await estimateOrderFee({
lspBalanceSat: amount,
channelExpiryWeeks: DEFAULT_CHANNEL_DURATION,
options: {
clientBalanceSat: spendingAmount,
couponCode: 'bitkit',
lspNodeId: blocktankInfo.nodes[0].pubkey,
turboChannel:
spendingAmount <= blocktankInfo.options.max0ConfClientBalanceSat,
},
});
if (response.isErr()) {
if (res.isErr()) {
return;
}

const { fiatSymbol, fiatValue } = getFiatDisplayValues({
satoshis:
response.value.channelOpenFee + response.value.transactionFeeEstimate,
satoshis: res.value,
});

setChannelOpenFee((value) => ({
Expand Down
1 change: 0 additions & 1 deletion src/screens/Transfer/Setup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ const Setup = ({ navigation }: TransferScreenProps<'Setup'>): ReactElement => {
localBalance,
turboChannel:
remoteBalance <= blocktankInfo.options.max0ConfClientBalanceSat,
channelExpiry: 12,
});

setLoading(false);
Expand Down
3 changes: 2 additions & 1 deletion src/store/utils/blocktank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import {
updateBlocktankOrder,
updateCjitEntry,
} from '../slices/blocktank';
import { DEFAULT_CHANNEL_DURATION } from '../../screens/Lightning/CustomConfirm';

/**
* Retrieves & updates the status of stored orders that may have changed.
Expand Down Expand Up @@ -216,7 +217,7 @@ export const refreshBlocktankInfo = async (): Promise<Result<string>> => {
export const startChannelPurchase = async ({
remoteBalance,
localBalance,
channelExpiry = 6,
channelExpiry = DEFAULT_CHANNEL_DURATION,
lspNodeId,
couponCode,
turboChannel = true,
Expand Down
26 changes: 18 additions & 8 deletions src/utils/blocktank/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ export const estimateOrderFee = async (
{
...data.options,
couponCode: data.options?.couponCode ?? 'bitkit',
turboChannel: true,
zeroReserve: true,
},
);
Expand Down Expand Up @@ -266,10 +265,14 @@ export const openChannel = async (
* @returns {void}
*/
export const watchPendingOrders = (): void => {
const orders = getBlocktankStore().orders.filter((order) => {
return order.state === BtOrderState.CREATED;
});
orders.forEach((order) => watchOrder(order.id));
const { orders, paidOrders } = getBlocktankStore();
orders
.filter((order) => {
return order.state === BtOrderState.CREATED && order.id in paidOrders;
})
.forEach((order) => {
watchOrder(order.id);
});
};

/**
Expand Down Expand Up @@ -298,6 +301,8 @@ export const getPendingCJitEntries = (): ICJitEntry[] => {
});
};

const watchingOrders: string[] = [];

/**
* Continuously checks a given order until it is finalized, the response errors out or the order expires.
* @param {string} orderId
Expand All @@ -307,13 +312,16 @@ export const watchOrder = async (
orderId: string,
frequency = 15000,
): Promise<Result<string>> => {
let settled = false;
let error: string = '';
if (watchingOrders.includes(orderId)) {
return err('Already watching this order.');
}
const orderData = getOrderFromStorage(orderId);
if (orderData.isErr()) {
return err(orderData.error.message);
}
const expiry = orderData.value.orderExpiresAt;
watchingOrders.push(orderId); // Add to watchingOrders
let settled = false;
let error: string = '';
while (!settled && !error) {
const res = await refreshOrder(orderId);
if (res.isErr()) {
Expand All @@ -331,6 +339,8 @@ export const watchOrder = async (
}
await sleep(frequency);
}
watchingOrders.splice(watchingOrders.indexOf(orderId), 1); // Remove from watchingOrders
const expiry = orderData.value.orderExpiresAt;
return ok(`Watching order (${orderId}) until it expires at ${expiry}`);
};

Expand Down

0 comments on commit d1556ca

Please sign in to comment.