Skip to content

Commit d1556ca

Browse files
committed
fix: improve BT order watching
1 parent 0cbde4f commit d1556ca

File tree

4 files changed

+34
-35
lines changed

4 files changed

+34
-35
lines changed

src/screens/Lightning/CustomSetup.tsx

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import {
3333
} from '../../utils/conversion';
3434
import { getFiatDisplayValues } from '../../utils/displayValues';
3535
import { showToast } from '../../utils/notifications';
36+
import { estimateOrderFee } from '../../utils/blocktank';
3637
import { startChannelPurchase } from '../../store/utils/blocktank';
3738
import { EUnit } from '../../store/types/wallet';
3839
import {
@@ -272,35 +273,23 @@ const CustomSetup = ({
272273
}
273274

274275
const getChannelOpenCost = async (): Promise<void> => {
275-
// TODO: replace with estimateOrderFee
276-
// const response2 = await estimateOrderFee({
277-
// lspBalanceSat: spendingAmount,
278-
// channelExpiryWeeks: DEFAULT_CHANNEL_DURATION,
279-
// options: {
280-
// clientBalanceSat: amount,
281-
// couponCode: 'bitkit',
282-
// lspNodeId: blocktankInfo.nodes[0].pubkey,
283-
// }
284-
// // localBalance: amount,
285-
// // selectedWallet,
286-
// // selectedNetwork,
287-
// });
288-
const response = await startChannelPurchase({
289-
remoteBalance: spendingAmount,
290-
localBalance: amount,
291-
channelExpiry: DEFAULT_CHANNEL_DURATION,
292-
selectedWallet,
293-
selectedNetwork,
294-
lspNodeId: blocktankInfo.nodes[0].pubkey,
295-
turboChannel:
296-
spendingAmount <= blocktankInfo.options.max0ConfClientBalanceSat,
276+
const res = await estimateOrderFee({
277+
lspBalanceSat: amount,
278+
channelExpiryWeeks: DEFAULT_CHANNEL_DURATION,
279+
options: {
280+
clientBalanceSat: spendingAmount,
281+
couponCode: 'bitkit',
282+
lspNodeId: blocktankInfo.nodes[0].pubkey,
283+
turboChannel:
284+
spendingAmount <= blocktankInfo.options.max0ConfClientBalanceSat,
285+
},
297286
});
298-
if (response.isErr()) {
287+
if (res.isErr()) {
299288
return;
300289
}
290+
301291
const { fiatSymbol, fiatValue } = getFiatDisplayValues({
302-
satoshis:
303-
response.value.channelOpenFee + response.value.transactionFeeEstimate,
292+
satoshis: res.value,
304293
});
305294

306295
setChannelOpenFee((value) => ({

src/screens/Transfer/Setup.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ const Setup = ({ navigation }: TransferScreenProps<'Setup'>): ReactElement => {
137137
localBalance,
138138
turboChannel:
139139
remoteBalance <= blocktankInfo.options.max0ConfClientBalanceSat,
140-
channelExpiry: 12,
141140
});
142141

143142
setLoading(false);

src/store/utils/blocktank.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import {
4646
updateBlocktankOrder,
4747
updateCjitEntry,
4848
} from '../slices/blocktank';
49+
import { DEFAULT_CHANNEL_DURATION } from '../../screens/Lightning/CustomConfirm';
4950

5051
/**
5152
* Retrieves & updates the status of stored orders that may have changed.
@@ -216,7 +217,7 @@ export const refreshBlocktankInfo = async (): Promise<Result<string>> => {
216217
export const startChannelPurchase = async ({
217218
remoteBalance,
218219
localBalance,
219-
channelExpiry = 6,
220+
channelExpiry = DEFAULT_CHANNEL_DURATION,
220221
lspNodeId,
221222
couponCode,
222223
turboChannel = true,

src/utils/blocktank/index.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ export const estimateOrderFee = async (
124124
{
125125
...data.options,
126126
couponCode: data.options?.couponCode ?? 'bitkit',
127-
turboChannel: true,
128127
zeroReserve: true,
129128
},
130129
);
@@ -266,10 +265,14 @@ export const openChannel = async (
266265
* @returns {void}
267266
*/
268267
export const watchPendingOrders = (): void => {
269-
const orders = getBlocktankStore().orders.filter((order) => {
270-
return order.state === BtOrderState.CREATED;
271-
});
272-
orders.forEach((order) => watchOrder(order.id));
268+
const { orders, paidOrders } = getBlocktankStore();
269+
orders
270+
.filter((order) => {
271+
return order.state === BtOrderState.CREATED && order.id in paidOrders;
272+
})
273+
.forEach((order) => {
274+
watchOrder(order.id);
275+
});
273276
};
274277

275278
/**
@@ -298,6 +301,8 @@ export const getPendingCJitEntries = (): ICJitEntry[] => {
298301
});
299302
};
300303

304+
const watchingOrders: string[] = [];
305+
301306
/**
302307
* Continuously checks a given order until it is finalized, the response errors out or the order expires.
303308
* @param {string} orderId
@@ -307,13 +312,16 @@ export const watchOrder = async (
307312
orderId: string,
308313
frequency = 15000,
309314
): Promise<Result<string>> => {
310-
let settled = false;
311-
let error: string = '';
315+
if (watchingOrders.includes(orderId)) {
316+
return err('Already watching this order.');
317+
}
312318
const orderData = getOrderFromStorage(orderId);
313319
if (orderData.isErr()) {
314320
return err(orderData.error.message);
315321
}
316-
const expiry = orderData.value.orderExpiresAt;
322+
watchingOrders.push(orderId); // Add to watchingOrders
323+
let settled = false;
324+
let error: string = '';
317325
while (!settled && !error) {
318326
const res = await refreshOrder(orderId);
319327
if (res.isErr()) {
@@ -331,6 +339,8 @@ export const watchOrder = async (
331339
}
332340
await sleep(frequency);
333341
}
342+
watchingOrders.splice(watchingOrders.indexOf(orderId), 1); // Remove from watchingOrders
343+
const expiry = orderData.value.orderExpiresAt;
334344
return ok(`Watching order (${orderId}) until it expires at ${expiry}`);
335345
};
336346

0 commit comments

Comments
 (0)