Pārlūkot izejas kodu

Refactor other example components

Jordan Sexton 3 gadi atpakaļ
vecāks
revīzija
08c8b04981

+ 4 - 7
packages/starter/example/src/components/RequestAirdrop.tsx

@@ -12,13 +12,10 @@ export const RequestAirdrop: FC = () => {
     const notify = useNotify();
 
     const onClick = useCallback(async () => {
-        if (!publicKey) {
-            notify('error', 'Wallet not connected!');
-            return;
-        }
-
-        let signature: TransactionSignature = '';
+        let signature: TransactionSignature | undefined = undefined;
         try {
+            if (!publicKey) throw new Error('Wallet not connected!');
+
             signature = await connection.requestAirdrop(publicKey, LAMPORTS_PER_SOL);
             notify('info', 'Airdrop requested:', signature);
 
@@ -27,7 +24,7 @@ export const RequestAirdrop: FC = () => {
         } catch (error: any) {
             notify('error', `Airdrop failed! ${error?.message}`, signature);
         }
-    }, [publicKey, notify, connection]);
+    }, [publicKey, connection, notify]);
 
     return (
         <Button variant="contained" color="secondary" onClick={onClick} disabled={!publicKey}>

+ 8 - 18
packages/starter/example/src/components/SendLegacyTransaction.tsx

@@ -13,21 +13,13 @@ export const SendLegacyTransaction: FC = () => {
     const supportedTransactionVersions = wallet?.adapter.supportedTransactionVersions;
 
     const onClick = useCallback(async () => {
-        if (!publicKey) {
-            notify('error', 'Wallet not connected!');
-            return;
-        }
-
-        if (!supportedTransactionVersions) {
-            notify('error', "Wallet doesn't support versioned transactions!");
-            return;
-        } else if (!supportedTransactionVersions.has('legacy')) {
-            notify('error', "Wallet doesn't support legacy transactions!");
-            return;
-        }
-
-        let signature: TransactionSignature = '';
+        let signature: TransactionSignature | undefined = undefined;
         try {
+            if (!publicKey) throw new Error('Wallet not connected!');
+            if (!supportedTransactionVersions) throw new Error("Wallet doesn't support versioned transactions!");
+            if (!supportedTransactionVersions.has('legacy'))
+                throw new Error("Wallet doesn't support legacy transactions!");
+
             const {
                 context: { slot: minContextSlot },
                 value: { blockhash, lastValidBlockHeight },
@@ -35,6 +27,7 @@ export const SendLegacyTransaction: FC = () => {
 
             const message = new TransactionMessage({
                 payerKey: publicKey,
+                recentBlockhash: blockhash,
                 instructions: [
                     {
                         data: Buffer.from('Hello, from the Solana Wallet Adapter example app!'),
@@ -42,9 +35,7 @@ export const SendLegacyTransaction: FC = () => {
                         programId: new PublicKey('MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr'),
                     },
                 ],
-                recentBlockhash: blockhash,
             });
-
             const transaction = new VersionedTransaction(message.compileToLegacyMessage());
 
             signature = await sendTransaction(transaction, connection, { minContextSlot });
@@ -54,9 +45,8 @@ export const SendLegacyTransaction: FC = () => {
             notify('success', 'Transaction successful!', signature);
         } catch (error: any) {
             notify('error', `Transaction failed! ${error?.message}`, signature);
-            return;
         }
-    }, [publicKey, notify, connection, sendTransaction, supportedTransactionVersions]);
+    }, [publicKey, supportedTransactionVersions, connection, sendTransaction, notify]);
 
     return (
         <Button

+ 13 - 14
packages/starter/example/src/components/SendTransaction.tsx

@@ -12,14 +12,19 @@ export const SendTransaction: FC = () => {
     const notify = useNotify();
 
     const onClick = useCallback(async () => {
-        if (!publicKey) {
-            notify('error', 'Wallet not connected!');
-            return;
-        }
-
-        let signature: TransactionSignature = '';
+        let signature: TransactionSignature | undefined = undefined;
         try {
-            const transaction = new Transaction().add(
+            if (!publicKey) throw new Error('Wallet not connected!');
+
+            const {
+                context: { slot: minContextSlot },
+                value: { blockhash, lastValidBlockHeight },
+            } = await connection.getLatestBlockhashAndContext();
+
+            const transaction = new Transaction({
+                feePayer: publicKey,
+                recentBlockhash: blockhash,
+            }).add(
                 new TransactionInstruction({
                     data: Buffer.from('Hello, from the Solana Wallet Adapter example app!'),
                     keys: [],
@@ -27,11 +32,6 @@ export const SendTransaction: FC = () => {
                 })
             );
 
-            const {
-                context: { slot: minContextSlot },
-                value: { blockhash, lastValidBlockHeight },
-            } = await connection.getLatestBlockhashAndContext();
-
             signature = await sendTransaction(transaction, connection, { minContextSlot });
             notify('info', 'Transaction sent:', signature);
 
@@ -39,9 +39,8 @@ export const SendTransaction: FC = () => {
             notify('success', 'Transaction successful!', signature);
         } catch (error: any) {
             notify('error', `Transaction failed! ${error?.message}`, signature);
-            return;
         }
-    }, [publicKey, notify, connection, sendTransaction]);
+    }, [publicKey, connection, sendTransaction, notify]);
 
     return (
         <Button variant="contained" color="secondary" onClick={onClick} disabled={!publicKey}>

+ 9 - 24
packages/starter/example/src/components/SendV0Transaction.tsx

@@ -13,21 +13,12 @@ export const SendV0Transaction: FC = () => {
     const supportedTransactionVersions = wallet?.adapter.supportedTransactionVersions;
 
     const onClick = useCallback(async () => {
-        if (!publicKey) {
-            notify('error', 'Wallet not connected!');
-            return;
-        }
-
-        if (!supportedTransactionVersions) {
-            notify('error', "Wallet doesn't support versioned transactions!");
-            return;
-        } else if (!supportedTransactionVersions.has(0)) {
-            notify('error', "Wallet doesn't support v0 transactions!");
-            return;
-        }
-
-        let signature: TransactionSignature = '';
+        let signature: TransactionSignature | undefined = undefined;
         try {
+            if (!publicKey) throw new Error('Wallet not connected!');
+            if (!supportedTransactionVersions) throw new Error("Wallet doesn't support versioned transactions!");
+            if (!supportedTransactionVersions.has(0)) throw new Error("Wallet doesn't support v0 transactions!");
+
             /**
              * This lookup table only exists on devnet and can be replaced as
              * needed.  To create and manage a lookup table, use the `solana
@@ -36,10 +27,7 @@ export const SendV0Transaction: FC = () => {
             const { value: lookupTable } = await connection.getAddressLookupTable(
                 new PublicKey('F3MfgEJe1TApJiA14nN2m4uAH4EBVrqdBnHeGeSXvQ7B')
             );
-            if (!lookupTable) {
-                notify('error', "Address lookup table wasn't found!");
-                return;
-            }
+            if (!lookupTable) throw new Error("Address lookup table wasn't found!");
 
             const {
                 context: { slot: minContextSlot },
@@ -48,6 +36,7 @@ export const SendV0Transaction: FC = () => {
 
             const message = new TransactionMessage({
                 payerKey: publicKey,
+                recentBlockhash: blockhash,
                 instructions: [
                     {
                         data: Buffer.from('Hello, from the Solana Wallet Adapter example app!'),
@@ -59,11 +48,8 @@ export const SendV0Transaction: FC = () => {
                         programId: new PublicKey('Memo1UhkJRfHyvLMcVucJwxXeuD728EqVDDwQDxFMNo'),
                     },
                 ],
-                recentBlockhash: blockhash,
             });
-
-            const lookupTables = [lookupTable];
-            const transaction = new VersionedTransaction(message.compileToV0Message(lookupTables));
+            const transaction = new VersionedTransaction(message.compileToV0Message([lookupTable]));
 
             signature = await sendTransaction(transaction, connection, { minContextSlot });
             notify('info', 'Transaction sent:', signature);
@@ -72,9 +58,8 @@ export const SendV0Transaction: FC = () => {
             notify('success', 'Transaction successful!', signature);
         } catch (error: any) {
             notify('error', `Transaction failed! ${error?.message}`, signature);
-            return;
         }
-    }, [publicKey, notify, connection, sendTransaction, supportedTransactionVersions]);
+    }, [publicKey, supportedTransactionVersions, connection, sendTransaction, notify]);
 
     return (
         <Button

+ 5 - 9
packages/starter/example/src/components/SignMessage.tsx

@@ -12,26 +12,22 @@ export const SignMessage: FC = () => {
 
     const onClick = useCallback(async () => {
         try {
-            // `publicKey` will be null if the wallet isn't connected
             if (!publicKey) throw new Error('Wallet not connected!');
-            // `signMessage` will be undefined if the wallet doesn't support it
             if (!signMessage) throw new Error('Wallet does not support message signing!');
 
-            // Encode anything as bytes
             const message = new TextEncoder().encode('Hello, world!');
-            // Sign the bytes using the wallet
             const signature = await signMessage(message);
-            // Verify that the bytes were signed using the private key that matches the known public key
-            if (!sign.detached.verify(message, signature, publicKey.toBytes())) throw new Error('Invalid signature!');
+            if (!sign.detached.verify(message, signature, publicKey.toBytes()))
+                throw new Error('Message signature invalid!');
 
             notify('success', `Message signature: ${bs58.encode(signature)}`);
         } catch (error: any) {
-            notify('error', `Signing failed: ${error?.message}`);
+            notify('error', `Message signing failing: ${error?.message}`);
         }
-    }, [publicKey, notify, signMessage]);
+    }, [publicKey, signMessage, notify]);
 
     return signMessage ? (
-        <Button variant="contained" color="secondary" onClick={onClick} disabled={!publicKey}>
+        <Button variant="contained" color="secondary" onClick={onClick} disabled={!publicKey || !signMessage}>
             Sign Message
         </Button>
     ) : null;