Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add signAndSendTransaction to Solflare, Backpack and Trust Wallet Ada… #2190

Merged
merged 8 commits into from
May 8, 2024
11 changes: 6 additions & 5 deletions packages/solana/src/connectors/walletAdapters.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { BackpackWalletAdapter as SolanaBackpackWalletAdapter } from '@solana/wallet-adapter-backpack'
import { Transaction, VersionedTransaction } from '@solana/web3.js'

import { SolStoreUtil } from '../../utils/scaffold/index.js'

import type { ConfirmOptions, Signer } from '@solana/web3.js'

export class BackpackWalletAdapter extends SolanaBackpackWalletAdapter {
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')
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { SolflareWalletAdapter as SolanaSolflareWalletAdapter } from '@solana/wallet-adapter-solflare'
import { Transaction, VersionedTransaction } from '@solana/web3.js'

import { SolStoreUtil } from '../../utils/scaffold/index.js'

import type { ConfirmOptions, Signer } from '@solana/web3.js'

export class SolflareWalletAdapter extends SolanaSolflareWalletAdapter {
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')
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { TrustWalletAdapter as SolanaTrustWalletAdapter } from '@solana/wallet-adapter-trust'
import { Transaction, VersionedTransaction } from '@solana/web3.js'

import { SolStoreUtil } from '../../utils/scaffold/index.js'

import type { ConfirmOptions, Signer } from '@solana/web3.js'

export class TrustWalletAdapter extends SolanaTrustWalletAdapter {
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')
}
}
3 changes: 3 additions & 0 deletions packages/solana/src/connectors/walletAdapters/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
export { PhantomWalletAdapter } from './PhantomAdapter.js'
export { BackpackWalletAdapter } from './BackpackWalletAdapter.js'
export { SolflareWalletAdapter } from './SolflareWalletAdapter.js'
export { TrustWalletAdapter } from './TrustWalletAdapter.js'
Loading