Browse Source

Disable buttons if adapter doesn't support versioned tx

Justin Starry 3 years ago
parent
commit
21728d2b9c

+ 11 - 5
packages/starter/example/src/components/SendLegacyTransaction.tsx

@@ -1,15 +1,16 @@
 import { Button } from '@mui/material';
 import { useConnection, useWallet } from '@solana/wallet-adapter-react';
-import { AddressLookupTableAccount, MessageV0, TransactionMessage, TransactionSignature, VersionedTransaction } from '@solana/web3.js';
-import { PublicKey, Transaction, TransactionInstruction } from '@solana/web3.js';
+import { TransactionMessage, TransactionSignature, VersionedTransaction } from '@solana/web3.js';
+import { PublicKey } from '@solana/web3.js';
 import type { FC } from 'react';
 import React, { useCallback } from 'react';
 import { useNotify } from './notify';
 
 export const SendLegacyTransaction: FC = () => {
     const { connection } = useConnection();
-    const { publicKey, sendTransaction } = useWallet();
+    const { publicKey, sendTransaction, wallet } = useWallet();
     const notify = useNotify();
+    const supportedTransactionVersions = wallet?.adapter.supportedTransactionVersions;
 
     const onClick = useCallback(async () => {
         if (!publicKey) {
@@ -17,6 +18,11 @@ export const SendLegacyTransaction: FC = () => {
             return;
         }
 
+        if (!supportedTransactionVersions) {
+            notify('error', 'Wallet doesn\'t support versioned transactions!');
+            return;
+        }
+
         let signature: TransactionSignature = '';
         try {
             const {
@@ -44,10 +50,10 @@ export const SendLegacyTransaction: FC = () => {
             notify('error', `Transaction failed! ${error?.message}`, signature);
             return;
         }
-    }, [publicKey, notify, connection, sendTransaction]);
+    }, [publicKey, notify, connection, sendTransaction, supportedTransactionVersions]);
 
     return (
-        <Button variant="contained" color="secondary" onClick={onClick} disabled={!publicKey}>
+        <Button variant="contained" color="secondary" onClick={onClick} disabled={!publicKey || !supportedTransactionVersions}>
             Send Legacy Transaction (devnet)
         </Button>
     );

+ 15 - 5
packages/starter/example/src/components/SendV0Transaction.tsx

@@ -1,15 +1,16 @@
 import { Button } from '@mui/material';
 import { useConnection, useWallet } from '@solana/wallet-adapter-react';
-import { AddressLookupTableAccount, AddressLookupTableInstruction, AddressLookupTableProgram, MessageV0, TransactionMessage, TransactionSignature, VersionedTransaction } from '@solana/web3.js';
-import { PublicKey, Transaction, TransactionInstruction } from '@solana/web3.js';
+import { AddressLookupTableAccount, TransactionMessage, TransactionSignature, VersionedTransaction } from '@solana/web3.js';
+import { PublicKey } from '@solana/web3.js';
 import type { FC } from 'react';
 import React, { useCallback } from 'react';
 import { useNotify } from './notify';
 
 export const SendV0Transaction: FC = () => {
     const { connection } = useConnection();
-    const { publicKey, sendTransaction } = useWallet();
+    const { publicKey, sendTransaction, wallet } = useWallet();
     const notify = useNotify();
+    const supportedTransactionVersions = wallet?.adapter.supportedTransactionVersions;
 
     const onClick = useCallback(async () => {
         if (!publicKey) {
@@ -17,6 +18,14 @@ export const SendV0Transaction: FC = () => {
             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 = '';
         try {
             const lookupTable = (await connection.getAddressLookupTable(new PublicKey("F3MfgEJe1TApJiA14nN2m4uAH4EBVrqdBnHeGeSXvQ7B"))).value;
@@ -56,10 +65,11 @@ export const SendV0Transaction: FC = () => {
             notify('error', `Transaction failed! ${error?.message}`, signature);
             return;
         }
-    }, [publicKey, notify, connection, sendTransaction]);
+    }, [publicKey, notify, connection, sendTransaction, supportedTransactionVersions]);
 
+    const disabled = !publicKey || !(supportedTransactionVersions && supportedTransactionVersions.has(0));
     return (
-        <Button variant="contained" color="secondary" onClick={onClick} disabled={!publicKey}>
+        <Button variant="contained" color="secondary" onClick={onClick} disabled={disabled}>
             Send V0 Transaction using Address Lookup Table (devnet)
         </Button>
     );