Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@fontsource-variable/sora": "^5.2.8",
"@noble/hashes": "^2.4.0",
"@qrlwallet/connect": "4.0.0",
"@qrlwallet/connect-ui": "0.3.0",
"@qrlwallet/connect-ui": "0.6.0",
"@radix-ui/react-slot": "^1.1.2",
"@radix-ui/react-tabs": "^1.1.1",
"@theqrl/web3": "^2.0.0",
Expand Down
22 changes: 21 additions & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from "react";
import { useCallback, useEffect, useSyncExternalStore } from "react";
import { observer } from "mobx-react-lite";
import { BrowserRouter, Route, Routes } from "react-router";
import { AlertTriangle, X } from "lucide-react";
Expand All @@ -8,6 +8,7 @@ import { TxBanner } from "@/components/TxBanner";
import { WalletPickerModal } from "@/components/WalletPickerModal";
import { QrPairModal } from "@/components/QrPairModal";
import { RouteSeo } from "@/components/RouteSeo";
import { ApprovalHint } from "@/components/ApprovalHint";
import { StakePage } from "@/pages/StakePage";
import { WithdrawalsPage } from "@/pages/WithdrawalsPage";
import { StatsPage } from "@/pages/StatsPage";
Expand All @@ -22,11 +23,30 @@ const App = observer(() => {
void poolStore.init();
}, [poolStore]);

const subscribeApprovals = useCallback(
(listener: () => void) => poolStore.approvals.subscribe(listener),
[poolStore],
);
const approvalPending = useSyncExternalStore(subscribeApprovals, () =>
poolStore.approvals.pending(),
);

return (
<BrowserRouter>
<RouteSeo />
<Header />

{/* The wallet a request is waiting for, named where the user is
looking: a prompt can sit unseen behind the wallet's toolbar icon
or on a paired device. It only informs, so clicks pass through. */}
<div className="pointer-events-none fixed right-4 top-20 z-50 w-[calc(100%-2rem)] max-w-sm">
<ApprovalHint
wallet={poolStore.approvalWallet}
pending={approvalPending}
className="rounded-xl bg-background [&:has([shown])]:shadow-xl"
/>
</div>

{poolStore.rpcError && (
<div className="border-b border-destructive/40 bg-destructive/10">
<div className="mx-auto flex max-w-5xl items-center gap-2 px-4 py-2 text-sm text-destructive">
Expand Down
59 changes: 59 additions & 0 deletions frontend/src/components/ApprovalHint.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { useEffect, useLayoutEffect, useRef } from "react";
import {
defineQrlApprovalHint,
QrlApprovalHint,
QRL_APPROVAL_HINT_TAG,
} from "@qrlwallet/connect-ui/approval";
import type { ApprovalWallet } from "@qrlwallet/connect-ui/wallets";

interface Props {
/** The wallet the open request went to, or null when it is unknown. */
wallet: ApprovalWallet | null;
/** A wallet request is open and has not been answered. */
pending: boolean;
className?: string;
}

const UNKNOWN_WALLET: ApprovalWallet = {
name: "",
icon: null,
kind: "extension",
};

/**
* Thin React wrapper around <qrl-approval-hint> from @qrlwallet/connect-ui.
* The element shows itself once `pending` has lasted its delay, names the
* wallet to open and never opens or raises anything.
*/
export function ApprovalHint({ wallet, pending, className }: Props) {
const hostRef = useRef<HTMLDivElement | null>(null);
const elRef = useRef<QrlApprovalHint | null>(null);

useLayoutEffect(() => {
defineQrlApprovalHint();
// createElement works with whichever copy of the element class holds the
// tag; a constructor call only with this one.
const el = document.createElement(QRL_APPROVAL_HINT_TAG);
if (!(el instanceof QrlApprovalHint)) return undefined;
hostRef.current?.append(el);
elRef.current = el;
return () => {
el.remove();
elRef.current = null;
};
}, []);

useEffect(() => {
if (elRef.current) elRef.current.wallet = wallet ?? UNKNOWN_WALLET;
}, [wallet]);

useEffect(() => {
if (elRef.current) elRef.current.pending = pending;
}, [pending]);

// The host takes its spacing only while the hint shows, so a hidden hint
// leaves no gap.
return (
<div ref={hostRef} className={className} data-testid="approval-hint" />
);
}
73 changes: 65 additions & 8 deletions frontend/src/stores/poolStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import {
} from "@qrlwallet/connect";
// The dependency-free subpath: this module is loaded in plain Node by the
// store tests, and the package index pulls in the DOM pairing modal.
import { groupMyQrlWallet } from "@qrlwallet/connect-ui/wallets";
import {
approvalWalletFor,
groupMyQrlWallet,
type ApprovalWallet,
} from "@qrlwallet/connect-ui/wallets";
// Also free of DOM code: counts the wallet requests that wait for the user.
import { createApprovalRequests } from "@qrlwallet/connect-ui/flow";
import { NativeQrlPoolABI } from "@/abi/NativeQrlPool";
import { ACTIVE_NETWORK, type NetworkConfig } from "@/config/networks";
import { getQrlWeb3, type Web3Instance } from "@/utils/web3/web3Lazy";
Expand Down Expand Up @@ -94,6 +100,15 @@ export interface OtherWalletRow {
/** A row shown in the picker. */
export type DiscoveredWallet = MyQrlWalletRow | OtherWalletRow;

/**
* The relay always is MyQRLWallet on a paired device. One object, so the
* hint is not handed a new wallet on every relay event.
*/
const RELAY_APPROVAL_WALLET: ApprovalWallet = approvalWalletFor({
...QRL_CONNECT_PROVIDER_INFO,
uuid: "",
});

/** Which transport the active provider uses (drives the tx param shape). */
type ProviderKind = "extension" | "relay";

Expand Down Expand Up @@ -223,6 +238,17 @@ export class PoolStore {
pairingStatus = "";
/** Display name of the connected wallet (e.g. "MyQRLWallet"). */
activeWalletName: string | null = null;
/**
* The wallet a request is waiting for, for the hint that names it: the
* connected wallet, or the one a connection request just went to.
*/
approvalWallet: ApprovalWallet | null = null;
/**
* The wallet requests that wait for the user. Open from the moment a
* request goes to the wallet until the wallet answers; a prompt can sit
* unseen behind the wallet's toolbar icon or on a paired device.
*/
readonly approvals = createApprovalRequests();
account: AccountState | null = null;
withdrawals: WithdrawalRequestView[] = [];
pendingDeposits: PendingDepositView[] = [];
Expand Down Expand Up @@ -276,6 +302,7 @@ export class PoolStore {
wiredExtensionProviders: false,
transactionGeneration: false,
onEip6963Announce: false,
approvals: false,
} as const;
makeAutoObservable<this, keyof typeof overrides>(this, overrides);
}
Expand Down Expand Up @@ -390,6 +417,7 @@ export class PoolStore {
this.provider = qrl;
runInAction(() => {
this.activeWalletName = QRL_CONNECT_PROVIDER_INFO.name;
this.approvalWallet = RELAY_APPROVAL_WALLET;
this.pairingStatus = qrl.getStatus();
});
}
Expand Down Expand Up @@ -474,8 +502,12 @@ export class PoolStore {
this.provider = qrl;
this.providerKind = "relay";
this.relayEstablished = false;
// A new relay connection: a request still open in the wallet before
// belongs to that wallet, and this page no longer waits for it.
this.approvals.reset();
runInAction(() => {
this.activeWalletName = QRL_CONNECT_PROVIDER_INFO.name;
this.approvalWallet = RELAY_APPROVAL_WALLET;
this.account = null;
this.withdrawals = [];
this.activity = [];
Expand Down Expand Up @@ -556,9 +588,16 @@ export class PoolStore {
if (!this.connectionAttemptGuard.isCurrent(attemptGeneration)) {
throw new Error("Wallet connection attempt changed");
}
return detail.provider.request({
method: "qrl_requestAccounts",
// A new extension connection starts its own count.
this.approvals.reset();
runInAction(() => {
this.approvalWallet = approvalWalletFor(detail.info);
});
return this.approvals.track(() =>
detail.provider.request({
method: "qrl_requestAccounts",
}),
);
},
(accounts) => {
if (!this.connectionAttemptGuard.isCurrent(attemptGeneration)) {
Expand Down Expand Up @@ -616,8 +655,12 @@ export class PoolStore {
this.provider = qrl;
this.providerKind = "relay";
this.relayEstablished = false;
// A new relay connection: a request still open in the wallet before
// belongs to that wallet, and this page no longer waits for it.
this.approvals.reset();
runInAction(() => {
this.activeWalletName = QRL_CONNECT_PROVIDER_INFO.name;
this.approvalWallet = RELAY_APPROVAL_WALLET;
this.account = null;
this.withdrawals = [];
this.activity = [];
Expand Down Expand Up @@ -715,6 +758,13 @@ export class PoolStore {
const isNewAddress = !this.account || this.account.address !== address;
runInAction(() => {
this.activeWalletName = name;
// An extension keeps the announcement it was named from when its
// connection began.
if (kind === "relay") {
this.approvalWallet = RELAY_APPROVAL_WALLET;
} else if (this.approvalWallet?.name !== name) {
this.approvalWallet = { name, icon: null, kind: "extension" };
}
this.walletPickerOpen = false;
this.pairingUri = null;
this.connectError = null;
Expand Down Expand Up @@ -785,7 +835,10 @@ export class PoolStore {
this.providerKind = null;
this.relayUserDisconnected = false;
this.relayEstablished = false;
// A request lost with the connection never settles: forget it.
this.approvals.reset();
runInAction(() => {
this.approvalWallet = null;
this.account = null;
this.withdrawals = [];
this.activity = [];
Expand Down Expand Up @@ -982,7 +1035,9 @@ export class PoolStore {
throw new Error("Wallet returned an invalid QRL account cache");
const accounts = cached.length
? cached
: await qrl.request({ method: "qrl_requestAccounts" });
: await this.approvals.track(() =>
qrl.request({ method: "qrl_requestAccounts" }),
);
const address = requireQrlAccount(accounts);
if (
this.providerKind !== "relay" ||
Expand Down Expand Up @@ -1497,10 +1552,12 @@ export class PoolStore {
"Switch your wallet to the chain configured for this native pool.",
);

const hash = await provider.request({
method: "qrl_sendTransaction",
params: [txParams],
});
const hash = await this.approvals.track(() =>
provider.request({
method: "qrl_sendTransaction",
params: [txParams],
}),
);
if (!isCurrent()) return false;
txHash = readTransactionHash(hash);
runInAction(() => {
Expand Down
Loading