Skip to content

Commit 9dc032a

Browse files
karooolisalvrs
andauthored
fix(entrykit): sign session wallet after top-up (#3697)
Co-authored-by: alvarius <alvarius@lattice.xyz>
1 parent daa34f0 commit 9dc032a

4 files changed

Lines changed: 34 additions & 8 deletions

File tree

.changeset/tidy-pillows-learn.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@latticexyz/entrykit": patch
3+
---
4+
5+
The login flow now only attempts to register the session account after it has been successfully funded.

packages/entrykit/src/onboarding/ConnectedSteps.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function ConnectedSteps({ userClient, initialUserAddress }: Props) {
4949
}
5050
}, [closeAccountModal, isNewConnection, prerequisites]);
5151

52-
const { sessionAddress, hasAllowance, isSpender, hasDelegation, hasGasBalance, hasQuarryBalance } =
52+
const { sessionAddress, hasAllowance, isSpender, hasDelegation, hasGasBalance, hasQuarryGasBalance } =
5353
prerequisites ?? {};
5454

5555
const steps = useMemo((): readonly Step[] => {
@@ -89,7 +89,7 @@ export function ConnectedSteps({ userClient, initialUserAddress }: Props) {
8989
} else {
9090
steps.push({
9191
id: "gasBalanceQuarry",
92-
isComplete: !!hasQuarryBalance,
92+
isComplete: !!hasQuarryGasBalance,
9393
content: (props) => <GasBalanceQuarry {...props} userAddress={userAddress} />,
9494
});
9595
}
@@ -108,7 +108,7 @@ export function ConnectedSteps({ userClient, initialUserAddress }: Props) {
108108
hasAllowance,
109109
hasDelegation,
110110
hasGasBalance,
111-
hasQuarryBalance,
111+
hasQuarryGasBalance,
112112
isSpender,
113113
paymaster,
114114
sessionAddress,

packages/entrykit/src/onboarding/Session.tsx

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,41 @@
1+
import { useEffect } from "react";
2+
import { Hex } from "viem";
13
import { Button } from "../ui/Button";
24
import { useSetupSession } from "./useSetupSession";
35
import { ConnectedClient } from "../common";
4-
import { useEffect } from "react";
56
import { useSessionClient } from "../useSessionClient";
67
import { useShowQueryError } from "../errors/useShowQueryError";
78
import { useShowMutationError } from "../errors/useShowMutationError";
89
import { StepContentProps } from "./common";
10+
import { usePrerequisites } from "./usePrerequisites";
911

1012
export type Props = StepContentProps & {
1113
userClient: ConnectedClient;
1214
registerSpender: boolean;
1315
registerDelegation: boolean;
16+
sessionAddress?: Hex;
1417
};
1518

1619
export function Session({ isActive, isExpanded, userClient, registerSpender, registerDelegation }: Props) {
1720
const sessionClient = useShowQueryError(useSessionClient(userClient.account.address));
1821
const setup = useShowMutationError(useSetupSession({ userClient }));
1922
const hasSession = !registerDelegation && !registerDelegation;
23+
const { data: prerequisites } = usePrerequisites(userClient.account.address);
24+
const { hasAllowance, hasGasBalance, hasQuarryGasBalance } = prerequisites ?? {};
2025

2126
useEffect(() => {
2227
// There seems to be a tanstack-query bug(?) where multiple simultaneous renders loses
2328
// state between the two mutations. They're not treated as shared state but rather
2429
// individual mutations, even though the keys match. And the one we want the status of
2530
// seems to stay pending. This is sorta resolved by triggering this after a timeout.
2631
const timer = setTimeout(() => {
27-
if (isActive && setup.status === "idle" && sessionClient.data && !hasSession) {
32+
if (
33+
isActive &&
34+
setup.status === "idle" &&
35+
sessionClient.data &&
36+
!hasSession &&
37+
(hasAllowance || hasGasBalance || hasQuarryGasBalance)
38+
) {
2839
setup.mutate({
2940
sessionClient: sessionClient.data,
3041
registerSpender,
@@ -33,7 +44,17 @@ export function Session({ isActive, isExpanded, userClient, registerSpender, reg
3344
}
3445
});
3546
return () => clearTimeout(timer);
36-
}, [hasSession, isActive, registerDelegation, registerSpender, sessionClient, setup]);
47+
}, [
48+
hasSession,
49+
isActive,
50+
registerDelegation,
51+
registerSpender,
52+
sessionClient,
53+
setup,
54+
hasAllowance,
55+
hasGasBalance,
56+
hasQuarryGasBalance,
57+
]);
3758

3859
return (
3960
<div className="flex flex-col gap-4">

packages/entrykit/src/onboarding/usePrerequisites.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,14 @@ export function getPrequisitesQueryOptions({
5454
const hasAllowance = allowance == null || allowance > 0n;
5555
const isSpender = spender == null ? true : spender;
5656
const hasGasBalance = sessionBalance == null || sessionBalance.value > 0n;
57-
const hasQuarryBalance = quarryBalance == null || quarryBalance > 0n;
57+
const hasQuarryGasBalance = quarryBalance == null || quarryBalance > 0n;
5858

5959
return {
6060
sessionAddress,
6161
hasAllowance,
6262
isSpender,
6363
hasGasBalance,
64-
hasQuarryBalance,
64+
hasQuarryGasBalance,
6565
hasDelegation,
6666
// we intentionally don't enforce an allowance/gas balance here
6767
complete: isSpender && hasDelegation,

0 commit comments

Comments
 (0)