diff --git a/packages/solana/src/connectors/walletAdapters.ts b/packages/solana/src/connectors/walletAdapters.ts index aa88bd22dc..8890a9050b 100644 --- a/packages/solana/src/connectors/walletAdapters.ts +++ b/packages/solana/src/connectors/walletAdapters.ts @@ -1,8 +1,9 @@ -import { SolflareWalletAdapter } from '@solana/wallet-adapter-solflare' -import { TrustWalletAdapter } from '@solana/wallet-adapter-trust' -import { BackpackWalletAdapter } from '@solana/wallet-adapter-backpack' - -import { PhantomWalletAdapter } from './walletAdapters/index.js' +import { + PhantomWalletAdapter, + BackpackWalletAdapter, + SolflareWalletAdapter, + TrustWalletAdapter +} from './walletAdapters/index.js' import type { BaseWalletAdapter } from '@solana/wallet-adapter-base' import type { Connector } from '@web3modal/scaffold' diff --git a/packages/solana/src/connectors/walletAdapters/BackpackWalletAdapter.ts b/packages/solana/src/connectors/walletAdapters/BackpackWalletAdapter.ts new file mode 100644 index 0000000000..b933deacbb --- /dev/null +++ b/packages/solana/src/connectors/walletAdapters/BackpackWalletAdapter.ts @@ -0,0 +1,6 @@ +import { BackpackWalletAdapter as SolanaBackpackWalletAdapter } from '@solana/wallet-adapter-backpack' +import { signAndSendTransaction } from './utils.js' + +export class BackpackWalletAdapter extends SolanaBackpackWalletAdapter { + signAndSendTransaction = signAndSendTransaction(this) +} diff --git a/packages/solana/src/connectors/walletAdapters/PhantomAdapter.ts b/packages/solana/src/connectors/walletAdapters/PhantomAdapter.ts index 8037c03088..e2e5649521 100644 --- a/packages/solana/src/connectors/walletAdapters/PhantomAdapter.ts +++ b/packages/solana/src/connectors/walletAdapters/PhantomAdapter.ts @@ -1,47 +1,6 @@ import { PhantomWalletAdapter as SolanaPhantomWalletAdapter } from '@solana/wallet-adapter-phantom' -import { Transaction, VersionedTransaction } from '@solana/web3.js' - -import { SolStoreUtil } from '../../utils/scaffold/index.js' - -import type { ConfirmOptions, Signer } from '@solana/web3.js' +import { signAndSendTransaction } from './utils.js' export class PhantomWalletAdapter extends SolanaPhantomWalletAdapter { - async signAndSendTransaction( - transactionParam: Transaction | VersionedTransaction, - signers: Signer[], - confirmOptions?: ConfirmOptions - ) { - if (!SolStoreUtil.state.connection) { - throw Error('Not Connected') - } - - if (transactionParam instanceof VersionedTransaction) { - throw Error('Versioned transactions are not supported') - } - - if (signers.length) { - transactionParam.partialSign(...signers) - } - - const signature = await this.sendTransaction( - transactionParam, - SolStoreUtil.state.connection, - confirmOptions - ) - - if (signature) { - const latestBlockHash = await SolStoreUtil.state.connection?.getLatestBlockhash() - if (latestBlockHash?.blockhash) { - await SolStoreUtil.state.connection?.confirmTransaction({ - blockhash: latestBlockHash.blockhash, - lastValidBlockHeight: latestBlockHash.lastValidBlockHeight, - signature - }) - - return signature - } - } - - throw Error('Transaction Failed') - } + signAndSendTransaction = signAndSendTransaction(this) } diff --git a/packages/solana/src/connectors/walletAdapters/SolflareWalletAdapter.ts b/packages/solana/src/connectors/walletAdapters/SolflareWalletAdapter.ts new file mode 100644 index 0000000000..55ad8bf591 --- /dev/null +++ b/packages/solana/src/connectors/walletAdapters/SolflareWalletAdapter.ts @@ -0,0 +1,6 @@ +import { SolflareWalletAdapter as SolanaSolflareWalletAdapter } from '@solana/wallet-adapter-solflare' +import { signAndSendTransaction } from './utils.js' + +export class SolflareWalletAdapter extends SolanaSolflareWalletAdapter { + signAndSendTransaction = signAndSendTransaction(this) +} diff --git a/packages/solana/src/connectors/walletAdapters/TrustWalletAdapter.ts b/packages/solana/src/connectors/walletAdapters/TrustWalletAdapter.ts new file mode 100644 index 0000000000..270e24fc2e --- /dev/null +++ b/packages/solana/src/connectors/walletAdapters/TrustWalletAdapter.ts @@ -0,0 +1,6 @@ +import { TrustWalletAdapter as SolanaTrustWalletAdapter } from '@solana/wallet-adapter-trust' +import { signAndSendTransaction } from './utils.js' + +export class TrustWalletAdapter extends SolanaTrustWalletAdapter { + signAndSendTransaction = signAndSendTransaction(this) +} diff --git a/packages/solana/src/connectors/walletAdapters/index.ts b/packages/solana/src/connectors/walletAdapters/index.ts index ddc50560e8..f2ea248856 100644 --- a/packages/solana/src/connectors/walletAdapters/index.ts +++ b/packages/solana/src/connectors/walletAdapters/index.ts @@ -1 +1,4 @@ export { PhantomWalletAdapter } from './PhantomAdapter.js' +export { BackpackWalletAdapter } from './BackpackWalletAdapter.js' +export { SolflareWalletAdapter } from './SolflareWalletAdapter.js' +export { TrustWalletAdapter } from './TrustWalletAdapter.js' diff --git a/packages/solana/src/connectors/walletAdapters/utils.ts b/packages/solana/src/connectors/walletAdapters/utils.ts new file mode 100644 index 0000000000..6be7f5b437 --- /dev/null +++ b/packages/solana/src/connectors/walletAdapters/utils.ts @@ -0,0 +1,47 @@ +import { Transaction, VersionedTransaction } from '@solana/web3.js' + +import { SolStoreUtil } from '../../utils/scaffold/index.js' + +import type { ConfirmOptions, Signer } from '@solana/web3.js' +import type { Adapter } from '@solana/wallet-adapter-base' + +export function signAndSendTransaction(adapter: Adapter) { + return async ( + transactionParam: Transaction | VersionedTransaction, + signers: Signer[], + confirmOptions?: ConfirmOptions + ) => { + if (!SolStoreUtil.state.connection) { + throw Error('Not Connected') + } + + if (transactionParam instanceof VersionedTransaction) { + throw Error('Versioned transactions are not supported') + } + + if (signers.length) { + transactionParam.partialSign(...signers) + } + + const signature = await adapter.sendTransaction( + transactionParam, + SolStoreUtil.state.connection, + confirmOptions + ) + + if (signature) { + const latestBlockHash = await SolStoreUtil.state.connection?.getLatestBlockhash() + if (latestBlockHash?.blockhash) { + await SolStoreUtil.state.connection?.confirmTransaction({ + blockhash: latestBlockHash.blockhash, + lastValidBlockHeight: latestBlockHash.lastValidBlockHeight, + signature + }) + + return signature + } + } + + throw Error('Transaction Failed') + } +}