From 44cb3d0d50e15d276da9b451214e6c59fefc3091 Mon Sep 17 00:00:00 2001 From: George Knee Date: Tue, 3 Oct 2023 11:53:31 +0100 Subject: [PATCH 1/6] try swapping out queries for event binding doesn't seem to be firing at the moment --- packages/payment-proxy-client/src/App.tsx | 32 ++++++----------------- 1 file changed, 8 insertions(+), 24 deletions(-) diff --git a/packages/payment-proxy-client/src/App.tsx b/packages/payment-proxy-client/src/App.tsx index ef7b2322b..27f04fa86 100644 --- a/packages/payment-proxy-client/src/App.tsx +++ b/packages/payment-proxy-client/src/App.tsx @@ -116,14 +116,6 @@ export default function App() { .finally(() => console.timeEnd("Connect to Nitro Node")); }, [url]); - const updateChannelInfo = async (channelId: string) => { - if (channelId == "") { - throw new Error("Empty channel id provided"); - } - const paymentChannel = await nitroClient?.GetPaymentChannel(channelId); - setPaymentChannelInfo(paymentChannel); - }; - const triggerFileDownload = (file: File) => { // This will prompt the browser to download the file const blob = new Blob([file], { type: file.type }); @@ -149,15 +141,15 @@ export default function App() { // await nitroClient.WaitForObjective(result.Id); setPaymentChannelId(result.ChannelId); - updateChannelInfo(result.ChannelId); - console.timeEnd("Create Payment Channel"); - // TODO: Slightly hacky but we wait a beat before querying so we see the updated balance - setTimeout(() => { - updateChannelInfo(result.ChannelId); - }, 1000); - }; + // nitroClient.PaymentChannelUpdated(result.ChannelId, setPaymentChannelInfo); + // const paymentChannel = await nitroClient?.GetPaymentChannel( + // result.ChannelId + // ); + // setPaymentChannelInfo(paymentChannel); + console.timeEnd("Create Payment Channel"); + }; const fetchAndDownloadFile = async () => { setErrorText(""); setFetchInProgress(true); @@ -182,7 +174,6 @@ export default function App() { nitroClient, (progress) => { setDownloadProgress(progress); - updateChannelInfo(paymentChannelInfo.ID); } ) : await fetchFile( @@ -190,17 +181,10 @@ export default function App() { skipPayment ? 0 : costPerByte * selectedFile.size, paymentChannelInfo.ID, nitroClient, - () => { - updateChannelInfo(paymentChannelInfo.ID); - } + () => {} ); setDownloadProgress(100); triggerFileDownload(file); - - // TODO: Slightly hacky but we wait a beat before querying so we see the updated balance - setTimeout(() => { - updateChannelInfo(paymentChannelInfo.ID); - }, 50); } catch (e: unknown) { console.error(e); From c67e1b751d00f0e4b245058133b716d969206595 Mon Sep 17 00:00:00 2001 From: George Knee Date: Tue, 3 Oct 2023 14:06:32 +0100 Subject: [PATCH 2/6] use a combination of queries and event listeners --- packages/payment-proxy-client/src/App.tsx | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/payment-proxy-client/src/App.tsx b/packages/payment-proxy-client/src/App.tsx index 27f04fa86..22a87a00e 100644 --- a/packages/payment-proxy-client/src/App.tsx +++ b/packages/payment-proxy-client/src/App.tsx @@ -142,11 +142,14 @@ export default function App() { setPaymentChannelId(result.ChannelId); - // nitroClient.PaymentChannelUpdated(result.ChannelId, setPaymentChannelInfo); - // const paymentChannel = await nitroClient?.GetPaymentChannel( - // result.ChannelId - // ); - // setPaymentChannelInfo(paymentChannel); + nitroClient.PaymentChannelUpdated(result.ChannelId, setPaymentChannelInfo); + + // It's possible the channel updated before we registered the handler above, so + // query the channel once now to get the latest information: + + setPaymentChannelInfo( + await nitroClient?.GetPaymentChannel(result.ChannelId) + ); console.timeEnd("Create Payment Channel"); }; From 7c6e3f5290c45d38890db4abf4b9d4eea8d2a7a5 Mon Sep 17 00:00:00 2001 From: George Knee Date: Tue, 3 Oct 2023 14:15:47 +0100 Subject: [PATCH 3/6] silence errors --- packages/payment-proxy-client/src/App.tsx | 4 ++-- packages/payment-proxy-client/src/file.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/payment-proxy-client/src/App.tsx b/packages/payment-proxy-client/src/App.tsx index 22a87a00e..0de5eb861 100644 --- a/packages/payment-proxy-client/src/App.tsx +++ b/packages/payment-proxy-client/src/App.tsx @@ -136,6 +136,7 @@ export default function App() { [hub], initialChannelBalance ); + console.timeEnd("Create Payment Channel"); // TODO: If the objective completes fast enough, we might start waiting after it's already done // await nitroClient.WaitForObjective(result.Id); @@ -183,8 +184,7 @@ export default function App() { selectedFile.url, skipPayment ? 0 : costPerByte * selectedFile.size, paymentChannelInfo.ID, - nitroClient, - () => {} + nitroClient ); setDownloadProgress(100); triggerFileDownload(file); diff --git a/packages/payment-proxy-client/src/file.ts b/packages/payment-proxy-client/src/file.ts index 5e6dd6bf8..4377a5366 100644 --- a/packages/payment-proxy-client/src/file.ts +++ b/packages/payment-proxy-client/src/file.ts @@ -6,7 +6,7 @@ export async function fetchFile( paymentAmount: number, channelId: string, nitroClient: NitroRpcClient, - updateChannelCallback: () => void + updateChannelCallback?: () => void ): Promise { console.time("Create Payment Vouncher"); const voucher = await nitroClient.CreateVoucher(channelId, paymentAmount); @@ -15,7 +15,7 @@ export async function fetchFile( console.time("Fetch file"); const req = createRequest(url, voucher); const fetchPromise = fetch(req); - updateChannelCallback(); + updateChannelCallback && updateChannelCallback(); const response = await fetchPromise; console.timeEnd("Fetch file"); if (response.status != 200) { From 5aed7bfdc04c1c7f29c98f57d71124531fc9cc8c Mon Sep 17 00:00:00 2001 From: George Knee Date: Mon, 9 Oct 2023 14:52:45 +0100 Subject: [PATCH 4/6] use onPaymentChannelUpdated --- packages/payment-proxy-client/src/App.tsx | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/payment-proxy-client/src/App.tsx b/packages/payment-proxy-client/src/App.tsx index 0de5eb861..5ee7b9162 100644 --- a/packages/payment-proxy-client/src/App.tsx +++ b/packages/payment-proxy-client/src/App.tsx @@ -143,7 +143,10 @@ export default function App() { setPaymentChannelId(result.ChannelId); - nitroClient.PaymentChannelUpdated(result.ChannelId, setPaymentChannelInfo); + nitroClient.onPaymentChannelUpdated( + result.ChannelId, + setPaymentChannelInfo + ); // It's possible the channel updated before we registered the handler above, so // query the channel once now to get the latest information: From bbf0d402194f23c96251ca9ff83c2bddbdca22e9 Mon Sep 17 00:00:00 2001 From: George Knee Date: Mon, 9 Oct 2023 14:58:50 +0100 Subject: [PATCH 5/6] fix typo --- packages/payment-proxy-client/src/App.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/payment-proxy-client/src/App.tsx b/packages/payment-proxy-client/src/App.tsx index 5ee7b9162..8c2979bed 100644 --- a/packages/payment-proxy-client/src/App.tsx +++ b/packages/payment-proxy-client/src/App.tsx @@ -136,7 +136,7 @@ export default function App() { [hub], initialChannelBalance ); - console.timeEnd("Create Payment Channel"); + console.time("Create Payment Channel"); // TODO: If the objective completes fast enough, we might start waiting after it's already done // await nitroClient.WaitForObjective(result.Id); From 09617ebdb6fcdbfcfa944264a353ddca54de1ba3 Mon Sep 17 00:00:00 2001 From: George Knee Date: Mon, 9 Oct 2023 14:59:41 +0100 Subject: [PATCH 6/6] really fix typo --- packages/payment-proxy-client/src/App.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/payment-proxy-client/src/App.tsx b/packages/payment-proxy-client/src/App.tsx index 8c2979bed..e7aa9aee4 100644 --- a/packages/payment-proxy-client/src/App.tsx +++ b/packages/payment-proxy-client/src/App.tsx @@ -136,7 +136,6 @@ export default function App() { [hub], initialChannelBalance ); - console.time("Create Payment Channel"); // TODO: If the objective completes fast enough, we might start waiting after it's already done // await nitroClient.WaitForObjective(result.Id);