Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ function LoadPlugin({
});

if (xnftAddress === DEFAULT_PUBKEY_STR) {
console.log("LoadPlugin: Loading Simulator plugin", { xnftAddress, plugin: plugin.xnftAddress.toString() });
return <Simulator plugin={plugin} deepXnftPath={deepXnftPath} />;
}
return <PluginDisplay plugin={plugin} deepXnftPath={deepXnftPath} />;
Expand Down
19 changes: 13 additions & 6 deletions packages/app-extension/src/components/Unlocked/Apps/Simulator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function Simulator({
deepXnftPath: string;
}) {
const refresh = useJavaScriptRefresh(SIMULATOR_URL);
console.log("Simulator: refresh triggered", { refresh, SIMULATOR_URL });
return (
<PluginDisplay key={refresh} plugin={plugin} deepXnftPath={deepXnftPath} />
);
Expand All @@ -27,16 +28,22 @@ function useJavaScriptRefresh(url: string): number {
let previous: any = null;
const i = setInterval(() => {
(async () => {
const js = await (await fetch(url)).text();
const noTSjs = js?.replaceAll(removeTimestamps, ""); // remove cachebusting timestamps next.js
if (previous !== null && previous !== noTSjs) {
setRefresh((r) => r + 1);
try {
const js = await (await fetch(url)).text();
const noTSjs = js?.replaceAll(removeTimestamps, ""); // remove cachebusting timestamps next.js
console.log("Simulator: checking for refresh", { url, hasPrevious: previous !== null, jsLength: js?.length });
if (previous !== null && previous !== noTSjs) {
console.log("Simulator: refresh triggered due to JS change");
setRefresh((r) => r + 1);
}
previous = noTSjs;
} catch (error) {
console.error("Simulator: error fetching JS", error);
}
previous = noTSjs;
})();
}, 1000);
return () => clearInterval(i);
}, []);
}, [url]);

return refresh;
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,13 +252,21 @@ const UninstallConfirmationCard = ({ xnft }: { xnft: any }) => {
//
// Confirm tx.
//
logger.debug("starting transaction confirmation", { txSig });
try {
await confirmTransaction(
// Add a UI-level timeout to prevent indefinite hanging
const confirmationPromise = confirmTransaction(
ctx.connection,
txSig,
ctx.commitment === "finalized" ? "finalized" : "confirmed"
);

const timeoutPromise = new Promise((_, reject) =>
setTimeout(() => reject(new Error("UI timeout: confirmation took too long")), 45000)
);

await Promise.race([confirmationPromise, timeoutPromise]);
logger.debug("transaction confirmed successfully", { txSig });
setCardType("complete");
} catch (err: any) {
logger.error("unable to confirm", err);
Expand Down
26 changes: 23 additions & 3 deletions packages/recoil/src/hooks/solana/usePlugins.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export function usePluginUrl(address?: string) {
useEffect(() => {
(async () => {
if (address?.toString() === "11111111111111111111111111111111") {
console.log("usePluginUrl: Setting Simulator URL", { address });
setUrl("Simulator");
} else if (cached) {
setUrl(cached.iframeRootUrl);
Expand All @@ -83,7 +84,7 @@ export function usePluginUrl(address?: string) {
}
}
})();
}, [cached]);
}, [cached, address]);

return url;
}
Expand All @@ -96,7 +97,8 @@ export function useFreshPlugin(address?: string): {
const connectionUrls = useConnectionUrls();
const activePublicKeys = useActivePublicKeys();
const [result, setResult] = useState<Plugin | undefined>(
PLUGIN_CACHE.get(address ?? "")
// For Simulator, don't use cache to ensure fresh instance
address === "11111111111111111111111111111111" ? undefined : PLUGIN_CACHE.get(address ?? "")
);
const [state, setState] = useState<"loading" | "done" | "error">("loading");

Expand Down Expand Up @@ -127,7 +129,10 @@ export function useFreshPlugin(address?: string): {
request: setTransactionRequest,
openPlugin,
});
PLUGIN_CACHE.set(address, plugin);
// For Simulator, don't cache to prevent stale connections
if (address !== "11111111111111111111111111111111") {
PLUGIN_CACHE.set(address, plugin);
}
setResult(plugin);
setState("done");
} catch (err) {
Expand All @@ -144,6 +149,21 @@ export function useFreshPlugin(address?: string): {
}

export function getPlugin(p: any): Plugin {
// For Simulator, don't cache to prevent stale connections
if (p.install.account.xnft.toString() === "11111111111111111111111111111111") {
console.log("getPlugin: Creating fresh Simulator plugin instance", { xnftAddress: p.install.account.xnft.toString() });
return new Plugin(
p.install.account.xnft,
p.install.publicKey,
p.url,
p.iconUrl,
p.splashUrls ?? {},
p.title,
p.activeWallets,
p.connectionUrls
);
}

let plug = PLUGIN_CACHE.get(p.install.account.xnft.toString());
if (!plug) {
plug = new Plugin(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ import type {
GetVersionedTransactionConfig,
} from "@solana/web3.js";

import { getLogger } from "@coral-xyz/common";

const logger = getLogger("confirm-transaction");

export async function confirmTransaction(
c: Connection,
txSig: string,
commitmentOrConfig?: GetVersionedTransactionConfig | Finality
): Promise<ReturnType<(typeof c)["getParsedTransaction"]>> {
return new Promise(async (resolve, reject) => {
logger.debug("starting confirmation with timeout", { txSig });
setTimeout(
() =>
reject(new Error(`30 second timeout: unable to confirm transaction`)),
30000
);
await new Promise((resolve) => setTimeout(resolve, 5000));
logger.debug("initial 5s delay complete, starting polling", { txSig });

const config = {
// Support confirming Versioned Transactions
Expand All @@ -27,11 +33,20 @@ export async function confirmTransaction(
: commitmentOrConfig),
};

let attempts = 0;
let tx = await c.getParsedTransaction(txSig, config);
logger.debug("first getParsedTransaction attempt", { txSig, found: tx !== null });
while (tx === null) {
attempts++;
logger.debug(`polling attempt ${attempts}`, { txSig });
tx = await c.getParsedTransaction(txSig, config);
await new Promise((resolve) => setTimeout(resolve, 1000));
if (attempts > 25) { // Prevent infinite loop, though timeout should catch it
logger.warn("exceeded max polling attempts", { txSig, attempts });
break;
}
}
logger.debug("confirmation complete", { txSig, found: tx !== null, attempts });
resolve(tx);
});
}