diff --git a/packages/use-solana/src/adapters/solana.ts b/packages/use-solana/src/adapters/solana.ts index 7d227493..2c28c698 100644 --- a/packages/use-solana/src/adapters/solana.ts +++ b/packages/use-solana/src/adapters/solana.ts @@ -4,11 +4,13 @@ import type { } from "@saberhq/solana-contrib"; import { doSignAndBroadcastTransaction, + isVersionedTransaction, PendingTransaction, } from "@saberhq/solana-contrib"; import type { EventEmitter, SignerWalletAdapter, + SupportedTransactionVersions, WalletAdapterEvents, } from "@solana/wallet-adapter-base"; import { BaseSignerWalletAdapter } from "@solana/wallet-adapter-base"; @@ -23,18 +25,39 @@ import type { import type { ConnectedWallet, WalletAdapter } from "./types"; +type SolanaWalletAdapterInterface = Omit< + SignerWalletAdapter, + | "sendTransaction" + | "signTransaction" + | "signAllTransactions" + | keyof EventEmitter +> & + EventEmitter & { + supportedTransactionVersions: Set<"legacy"> | null; + signTransaction: (transaction: T) => Promise; + signAllTransactions: ( + transactions: T[], + ) => Promise; + }; + +type SolanaWalletAdapterSupportingVersioned = Omit< + SolanaWalletAdapterInterface, + "sendTransaction" | "signTransaction" | "signAllTransactions" +> & { + supportedTransactionVersions: Set; + signTransaction: ( + transaction: T, + ) => Promise; + signAllTransactions: ( + transactions: T[], + ) => Promise; +}; + export class SolanaWalletAdapter implements WalletAdapter { constructor( - readonly adapter: Omit< - SignerWalletAdapter, - "sendTransaction" | keyof EventEmitter - > & - EventEmitter & { - signTransaction: (transaction: T) => Promise; - signAllTransactions: ( - transactions: T[], - ) => Promise; - }, + readonly adapter: + | SolanaWalletAdapterInterface + | SolanaWalletAdapterSupportingVersioned, ) {} async signAndBroadcastTransaction( @@ -121,7 +144,17 @@ export class SolanaWalletAdapter implements WalletAdapter { async signAllTransactions( transactions: T[], ): Promise { - return this.adapter.signAllTransactions(transactions); + transactions.forEach((tx) => { + if ( + isVersionedTransaction(tx) && + !this.adapter.supportedTransactionVersions?.has(0) + ) { + throw new Error("Adapter does not support versioned transactions"); + } + }); + // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/ban-ts-comment + // @ts-ignore + return (await this.adapter.signAllTransactions(transactions)) as T[]; } get publicKey(): PublicKey | null { @@ -134,8 +167,15 @@ export class SolanaWalletAdapter implements WalletAdapter { if (!this.adapter) { return transaction; } - - return this.adapter.signTransaction(transaction); + if ( + isVersionedTransaction(transaction) && + !this.adapter.supportedTransactionVersions?.has(0) + ) { + throw new Error("Adapter does not support versioned transactions"); + } + // eslint-disable-next-line @typescript-eslint/no-unsafe-return, @typescript-eslint/ban-ts-comment + // @ts-ignore + return (await this.adapter.signTransaction(transaction)) as T; } connect = async (): Promise => {