-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMintQR.tsx
88 lines (75 loc) · 2.98 KB
/
MintQR.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { useEffect, useMemo, useRef } from "react";
import { Flex } from "@chakra-ui/react";
import {
createQR,
encodeURL,
TransactionRequestURLFields,
findReference,
FindReferenceError,
} from "@solana/pay";
import { Keypair } from "@solana/web3.js";
import { useConnection } from "@solana/wallet-adapter-react";
import useToastHook from "@/hooks/useToastHook";
export default function MintQR() {
const { connection } = useConnection();
// Initialize a ref used for the QR code
const qrRef = useRef<HTMLDivElement>(null);
// Generate a random reference address that is added to the Solana Pay transaction
// This allows us to find the transaction on the network once it's been sent by the mobile wallet
const reference = useMemo(() => Keypair.generate().publicKey, []);
// Keep track of the most recent transaction that was notified, so we can reuse the reference address
// Alternatively, you can generate a new reference address for each transaction
const mostRecentNotifiedTransaction = useRef<string | undefined>(undefined);
// Toast notification hook
const displayToast = useToastHook();
useEffect(() => {
// The API URL, which will be used to create the Solana Pay URL
// Append the reference address to the URL as a query parameter
const { location } = window;
const apiUrl = `${location.protocol}//${
location.host
}/api/mintNft?reference=${reference.toBase58()}`;
// Create Solana Pay URL
const urlParams: TransactionRequestURLFields = {
link: new URL(apiUrl),
};
const solanaUrl = encodeURL(urlParams);
// Create QR code encoded with Solana Pay URL
const qr = createQR(
solanaUrl, // The Solana Pay URL
512, // The size of the QR code
"transparent" // The background color of the QR code
);
// Update the ref with the QR code
if (qrRef.current) {
qrRef.current.innerHTML = "";
qr.append(qrRef.current);
}
}, [reference]);
useEffect(() => {
// Poll the network for transactions that include the reference address
const interval = setInterval(async () => {
try {
// Find transactions that include the reference address
const signatureInfo = await findReference(connection, reference, {
until: mostRecentNotifiedTransaction.current, // Only look for transactions after the most recent one we've found
finality: "confirmed",
});
// Update the most recent transaction with the transaction we just found
mostRecentNotifiedTransaction.current = signatureInfo.signature;
// Toast notification
displayToast(signatureInfo.signature);
} catch (e) {
if (e instanceof FindReferenceError) {
// No transaction found yet, ignore this error
return;
}
console.error("Unknown error", e);
}
}, 1000); // Check for new transactions every second
return () => {
clearInterval(interval);
};
}, [reference]);
return <Flex ref={qrRef} />;
}