Skip to content

Commit

Permalink
make upfront transfer check non-blocking (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
kasparkallas authored Oct 19, 2023
1 parent 2def9d6 commit 16f7c64
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-fans-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@superfluid-finance/widget": patch
---

Make the check for existing upfront transfers lighter and non-blocking
23 changes: 10 additions & 13 deletions packages/widget/src/CommandMapper.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { nanoid } from "nanoid";
import { useEffect, useMemo } from "react";
import {
Abi,
ContractFunctionConfig,
getAbiItem,
GetValue,
} from "viem";
import { Abi, ContractFunctionConfig, getAbiItem, GetValue } from "viem";
import {
useBlockNumber,
useContractRead,
Expand Down Expand Up @@ -299,6 +294,7 @@ export function SubscribeCommandMapper({
});
const {
isSuccess: isSuccessForTransferEvents,
isLoading: isLoadingForTransferEvents,
isIdle: isIdleForTransferEvents,
data: transferEvents,
} = useQuery(
Expand All @@ -316,12 +312,13 @@ export function SubscribeCommandMapper({
},
strict: true,
toBlock: blockNumber,
fromBlock: blockNumber - 10_000n, // 10k is Alchemy limit, some places have 50k. Move into environment variable?
fromBlock: blockNumber - 5_000n, // 10k is Alchemy limit, some places have 50k. Ankr has 5k. Move into environment variable?
});
return logs;
},
{
enabled: checkExistingUpfrontTransfer && isSuccessForBlockNumber,
retry: 10,
},
);

Expand All @@ -339,12 +336,12 @@ export function SubscribeCommandMapper({
}
}, [checkExistingUpfrontTransfer, isSuccessForTransferEvents]);

const didAllQueriesSucceed = useMemo(
const didAllQueriesFinish = useMemo(
() =>
!isLoadingForGetFlowRate &&
isSuccessForGetFlowRate &&
(isIdleForBlockNumber || isSuccessForBlockNumber) &&
(isIdleForTransferEvents || isSuccessForTransferEvents),
(isIdleForTransferEvents || !isLoadingForTransferEvents), // Keep the check for transfer events non-blocking.
[
isLoadingForGetFlowRate,
isSuccessForGetFlowRate,
Expand All @@ -356,7 +353,7 @@ export function SubscribeCommandMapper({
);

const contractWrites = useMemo<ContractWrite[]>(() => {
if (!didAllQueriesSucceed) {
if (!didAllQueriesFinish) {
return [];
}

Expand Down Expand Up @@ -427,14 +424,14 @@ export function SubscribeCommandMapper({
}

return contractWrites_;
}, [cmd.id, didAllQueriesSucceed]);
}, [cmd.id, didAllQueriesFinish]);

useEffect(
() =>
didAllQueriesSucceed
didAllQueriesFinish
? onMapped?.({ commandId: cmd.id, contractWrites })
: void 0,
[cmd.id, contractWrites, didAllQueriesSucceed],
[cmd.id, contractWrites, didAllQueriesFinish],
);

return null;
Expand Down

6 comments on commit 16f7c64

@vercel
Copy link

@vercel vercel bot commented on 16f7c64 Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 16f7c64 Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 16f7c64 Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 16f7c64 Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 16f7c64 Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 16f7c64 Oct 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.