Skip to content

Commit

Permalink
chore: constrain the connection input of sendTransaction to the m…
Browse files Browse the repository at this point in the history
…inimum interface actually required
  • Loading branch information
steveluscher committed May 10, 2024
1 parent 1e31941 commit a0ef497
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 72 deletions.
24 changes: 19 additions & 5 deletions packages/core/base/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import type { Connection, PublicKey, SendOptions, Signer, Transaction, TransactionSignature } from '@solana/web3.js';
import type { Commitment, PublicKey, SendOptions, Signer, Transaction, TransactionSignature } from '@solana/web3.js';
import EventEmitter from 'eventemitter3';
import { type WalletError, WalletNotConnectedError } from './errors.js';
import type { SupportedTransactionVersions, TransactionOrVersionedTransaction } from './transaction.js';

export { EventEmitter };

type BlockhashFetcher = (config?: {
commitment?: Commitment;
minContextSlot?: number;
}) => Promise<{ blockhash: string }>;

export interface ConnectionContext {
commitment?: Commitment;
getLatestBlockhash: BlockhashFetcher;
sendRawTransaction(
rawTransaction: Buffer | Uint8Array | Array<number>,
options?: SendOptions
): Promise<TransactionSignature>;
}

export interface WalletAdapterEvents {
connect(publicKey: PublicKey): void;
disconnect(): void;
Expand Down Expand Up @@ -35,7 +49,7 @@ export interface WalletAdapterProps<Name extends string = string> {
disconnect(): Promise<void>;
sendTransaction(
transaction: TransactionOrVersionedTransaction<this['supportedTransactionVersions']>,
connection: Connection,
connectionContext: ConnectionContext,
options?: SendTransactionOptions
): Promise<TransactionSignature>;
}
Expand Down Expand Up @@ -96,13 +110,13 @@ export abstract class BaseWalletAdapter<Name extends string = string>

abstract sendTransaction(
transaction: TransactionOrVersionedTransaction<this['supportedTransactionVersions']>,
connection: Connection,
connectionContext: ConnectionContext,
options?: SendTransactionOptions
): Promise<TransactionSignature>;

protected async prepareTransaction(
transaction: Transaction,
connection: Connection,
blockhashFetchingContext: Readonly<{ getLatestBlockhash: BlockhashFetcher }>,
options: SendOptions = {}
): Promise<Transaction> {
const publicKey = this.publicKey;
Expand All @@ -112,7 +126,7 @@ export abstract class BaseWalletAdapter<Name extends string = string>
transaction.recentBlockhash =
transaction.recentBlockhash ||
(
await connection.getLatestBlockhash({
await blockhashFetchingContext.getLatestBlockhash({
commitment: options.preflightCommitment,
minContextSlot: options.minContextSlot,
})
Expand Down
11 changes: 6 additions & 5 deletions packages/core/base/src/signer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { SolanaSignInInput, SolanaSignInOutput } from '@solana/wallet-standard-features';
import type { Connection, TransactionSignature } from '@solana/web3.js';
import type { TransactionSignature } from '@solana/web3.js';
import {
BaseWalletAdapter,
type ConnectionContext,
type SendTransactionOptions,
type WalletAdapter,
type WalletAdapterProps,
Expand All @@ -26,7 +27,7 @@ export abstract class BaseSignerWalletAdapter<Name extends string = string>
{
async sendTransaction(
transaction: TransactionOrVersionedTransaction<this['supportedTransactionVersions']>,
connection: Connection,
connectionContext: ConnectionContext,
options: SendTransactionOptions = {}
): Promise<TransactionSignature> {
let emit = true;
Expand All @@ -47,7 +48,7 @@ export abstract class BaseSignerWalletAdapter<Name extends string = string>

const rawTransaction = transaction.serialize();

return await connection.sendRawTransaction(rawTransaction, options);
return await connectionContext.sendRawTransaction(rawTransaction, options);
} catch (error: any) {
// If the error was thrown by `signTransaction`, rethrow it and don't emit a duplicate event
if (error instanceof WalletSignTransactionError) {
Expand All @@ -60,15 +61,15 @@ export abstract class BaseSignerWalletAdapter<Name extends string = string>
try {
const { signers, ...sendOptions } = options;

transaction = await this.prepareTransaction(transaction, connection, sendOptions);
transaction = await this.prepareTransaction(transaction, connectionContext, sendOptions);

signers?.length && transaction.partialSign(...signers);

transaction = await this.signTransaction(transaction);

const rawTransaction = transaction.serialize();

return await connection.sendRawTransaction(rawTransaction, sendOptions);
return await connectionContext.sendRawTransaction(rawTransaction, sendOptions);
} catch (error: any) {
// If the error was thrown by `signTransaction`, rethrow it and don't emit a duplicate event
if (error instanceof WalletSignTransactionError) {
Expand Down
10 changes: 5 additions & 5 deletions packages/wallets/alpha/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EventEmitter, SendTransactionOptions, WalletName } from '@solana/wallet-adapter-base';
import type { ConnectionContext, EventEmitter, SendTransactionOptions, WalletName } from '@solana/wallet-adapter-base';
import {
BaseMessageSignerWalletAdapter,
scopePollingDetectionStrategy,
Expand All @@ -15,7 +15,7 @@ import {
WalletSignMessageError,
WalletSignTransactionError,
} from '@solana/wallet-adapter-base';
import type { Connection, SendOptions, Transaction, TransactionSignature } from '@solana/web3.js';
import type { SendOptions, Transaction, TransactionSignature } from '@solana/web3.js';
import { PublicKey } from '@solana/web3.js';

interface AlphaWalletEvents {
Expand Down Expand Up @@ -158,7 +158,7 @@ export class AlphaWalletAdapter extends BaseMessageSignerWalletAdapter {

async sendTransaction(
transaction: Transaction,
connection: Connection,
connectionContext: ConnectionContext,
options: SendTransactionOptions = {}
): Promise<TransactionSignature> {
try {
Expand All @@ -168,11 +168,11 @@ export class AlphaWalletAdapter extends BaseMessageSignerWalletAdapter {
try {
const { signers, ...sendOptions } = options;

transaction = await this.prepareTransaction(transaction, connection, sendOptions);
transaction = await this.prepareTransaction(transaction, connectionContext, sendOptions);

signers?.length && transaction.partialSign(...signers);

sendOptions.preflightCommitment = sendOptions.preflightCommitment || connection.commitment;
sendOptions.preflightCommitment = sendOptions.preflightCommitment || connectionContext.commitment;

const { signature } = await wallet.signAndSendTransaction(transaction, sendOptions);
return signature;
Expand Down
10 changes: 5 additions & 5 deletions packages/wallets/avana/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EventEmitter, SendTransactionOptions, WalletName } from '@solana/wallet-adapter-base';
import type { ConnectionContext, EventEmitter, SendTransactionOptions, WalletName } from '@solana/wallet-adapter-base';
import {
BaseMessageSignerWalletAdapter,
scopePollingDetectionStrategy,
Expand All @@ -15,7 +15,7 @@ import {
WalletSignMessageError,
WalletSignTransactionError,
} from '@solana/wallet-adapter-base';
import type { Connection, SendOptions, Transaction, TransactionSignature } from '@solana/web3.js';
import type { SendOptions, Transaction, TransactionSignature } from '@solana/web3.js';
import { PublicKey } from '@solana/web3.js';

interface AvanaWalletEvents {
Expand Down Expand Up @@ -160,7 +160,7 @@ export class AvanaWalletAdapter extends BaseMessageSignerWalletAdapter {

async sendTransaction(
transaction: Transaction,
connection: Connection,
connectionContext: ConnectionContext,
options: SendTransactionOptions = {}
): Promise<TransactionSignature> {
try {
Expand All @@ -170,11 +170,11 @@ export class AvanaWalletAdapter extends BaseMessageSignerWalletAdapter {
try {
const { signers, ...sendOptions } = options;

transaction = await this.prepareTransaction(transaction, connection, sendOptions);
transaction = await this.prepareTransaction(transaction, connectionContext, sendOptions);

signers?.length && transaction.partialSign(...signers);

sendOptions.preflightCommitment = sendOptions.preflightCommitment || connection.commitment;
sendOptions.preflightCommitment = sendOptions.preflightCommitment || connectionContext.commitment;

const { signature } = await wallet.signAndSendTransaction(transaction, sendOptions);
return signature;
Expand Down
9 changes: 4 additions & 5 deletions packages/wallets/coinbase/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EventEmitter, SendTransactionOptions, WalletName } from '@solana/wallet-adapter-base';
import type { ConnectionContext, EventEmitter, SendTransactionOptions, WalletName } from '@solana/wallet-adapter-base';
import {
BaseMessageSignerWalletAdapter,
isVersionedTransaction,
Expand All @@ -16,7 +16,6 @@ import {
WalletSignTransactionError,
} from '@solana/wallet-adapter-base';
import type {
Connection,
SendOptions,
Transaction,
VersionedTransaction,
Expand Down Expand Up @@ -157,7 +156,7 @@ export class CoinbaseWalletAdapter extends BaseMessageSignerWalletAdapter {

async sendTransaction<T extends Transaction | VersionedTransaction>(
transaction: T,
connection: Connection,
connectionContext: ConnectionContext,
options: SendTransactionOptions = {}
): Promise<TransactionSignature> {
try {
Expand All @@ -170,11 +169,11 @@ export class CoinbaseWalletAdapter extends BaseMessageSignerWalletAdapter {
if (isVersionedTransaction(transaction)) {
signers?.length && transaction.sign(signers);
} else {
transaction = (await this.prepareTransaction(transaction, connection, sendOptions)) as T;
transaction = (await this.prepareTransaction(transaction, connectionContext, sendOptions)) as T;
signers?.length && (transaction as Transaction).partialSign(...signers);
}

sendOptions.preflightCommitment = sendOptions.preflightCommitment || connection.commitment;
sendOptions.preflightCommitment = sendOptions.preflightCommitment || connectionContext.commitment;

const { signature } = await wallet.signAndSendTransaction(transaction, sendOptions);
return signature;
Expand Down
16 changes: 5 additions & 11 deletions packages/wallets/nufi/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EventEmitter, SendTransactionOptions, WalletName } from '@solana/wallet-adapter-base';
import type { ConnectionContext, EventEmitter, SendTransactionOptions, WalletName } from '@solana/wallet-adapter-base';
import {
BaseMessageSignerWalletAdapter,
isVersionedTransaction,
Expand All @@ -16,13 +16,7 @@ import {
WalletSignMessageError,
WalletSignTransactionError,
} from '@solana/wallet-adapter-base';
import type {
Connection,
Transaction,
TransactionSignature,
TransactionVersion,
VersionedTransaction,
} from '@solana/web3.js';
import type { Transaction, TransactionSignature, TransactionVersion, VersionedTransaction } from '@solana/web3.js';
import { PublicKey } from '@solana/web3.js';

interface NufiWalletEvents {
Expand Down Expand Up @@ -161,7 +155,7 @@ export class NufiWalletAdapter extends BaseMessageSignerWalletAdapter {

async sendTransaction<T extends Transaction | VersionedTransaction>(
transaction: T,
connection: Connection,
connectionContext: ConnectionContext,
options: SendTransactionOptions = {}
): Promise<TransactionSignature> {
try {
Expand All @@ -174,11 +168,11 @@ export class NufiWalletAdapter extends BaseMessageSignerWalletAdapter {
if (isVersionedTransaction(transaction)) {
signers?.length && transaction.sign(signers);
} else {
transaction = (await this.prepareTransaction(transaction, connection, sendOptions)) as T;
transaction = (await this.prepareTransaction(transaction, connectionContext, sendOptions)) as T;
signers?.length && (transaction as Transaction).partialSign(...signers);
}

sendOptions.preflightCommitment = sendOptions.preflightCommitment || connection.commitment;
sendOptions.preflightCommitment = sendOptions.preflightCommitment || connectionContext.commitment;
const { signature } = await wallet.signAndSendTransaction(transaction);
return signature;
} catch (error: any) {
Expand Down
9 changes: 4 additions & 5 deletions packages/wallets/phantom/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EventEmitter, SendTransactionOptions, WalletName } from '@solana/wallet-adapter-base';
import type { ConnectionContext, EventEmitter, SendTransactionOptions, WalletName } from '@solana/wallet-adapter-base';
import {
BaseMessageSignerWalletAdapter,
isIosAndRedirectable,
Expand All @@ -18,7 +18,6 @@ import {
WalletSignTransactionError,
} from '@solana/wallet-adapter-base';
import type {
Connection,
SendOptions,
Transaction,
TransactionSignature,
Expand Down Expand Up @@ -193,7 +192,7 @@ export class PhantomWalletAdapter extends BaseMessageSignerWalletAdapter {

async sendTransaction<T extends Transaction | VersionedTransaction>(
transaction: T,
connection: Connection,
connectionContext: ConnectionContext,
options: SendTransactionOptions = {}
): Promise<TransactionSignature> {
try {
Expand All @@ -206,11 +205,11 @@ export class PhantomWalletAdapter extends BaseMessageSignerWalletAdapter {
if (isVersionedTransaction(transaction)) {
signers?.length && transaction.sign(signers);
} else {
transaction = (await this.prepareTransaction(transaction, connection, sendOptions)) as T;
transaction = (await this.prepareTransaction(transaction, connectionContext, sendOptions)) as T;
signers?.length && (transaction as Transaction).partialSign(...signers);
}

sendOptions.preflightCommitment = sendOptions.preflightCommitment || connection.commitment;
sendOptions.preflightCommitment = sendOptions.preflightCommitment || connectionContext.commitment;

const { signature } = await wallet.signAndSendTransaction(transaction, sendOptions);
return signature;
Expand Down
12 changes: 6 additions & 6 deletions packages/wallets/saifu/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EventEmitter, SendTransactionOptions, WalletName } from '@solana/wallet-adapter-base';
import type { ConnectionContext, EventEmitter, SendTransactionOptions, WalletName } from '@solana/wallet-adapter-base';
import {
BaseMessageSignerWalletAdapter,
scopePollingDetectionStrategy,
Expand All @@ -15,7 +15,7 @@ import {
WalletSignMessageError,
WalletSignTransactionError,
} from '@solana/wallet-adapter-base';
import type { Connection, SendOptions, Transaction, TransactionSignature } from '@solana/web3.js';
import type { SendOptions, Transaction, TransactionSignature } from '@solana/web3.js';
import { PublicKey } from '@solana/web3.js';

interface SaifuWalletEvents {
Expand Down Expand Up @@ -158,7 +158,7 @@ export class SaifuWalletAdapter extends BaseMessageSignerWalletAdapter {

async sendTransaction(
transaction: Transaction,
connection: Connection,
connectionContext: ConnectionContext,
options: SendTransactionOptions = {}
): Promise<TransactionSignature> {
try {
Expand All @@ -169,11 +169,11 @@ export class SaifuWalletAdapter extends BaseMessageSignerWalletAdapter {
try {
const { signers, ...sendOptions } = options;

transaction = await this.prepareTransaction(transaction, connection, sendOptions);
transaction = await this.prepareTransaction(transaction, connectionContext, sendOptions);

signers?.length && transaction.partialSign(...signers);

sendOptions.preflightCommitment = sendOptions.preflightCommitment || connection.commitment;
sendOptions.preflightCommitment = sendOptions.preflightCommitment || connectionContext.commitment;

const { signature } = await wallet.signAndSendTransaction(transaction, sendOptions);
return signature;
Expand All @@ -187,7 +187,7 @@ export class SaifuWalletAdapter extends BaseMessageSignerWalletAdapter {
throw error;
}

return await super.sendTransaction(transaction, connection, options);
return await super.sendTransaction(transaction, connectionContext, options);
}

async signTransaction<T extends Transaction>(transaction: T): Promise<T> {
Expand Down
10 changes: 5 additions & 5 deletions packages/wallets/sky/src/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { EventEmitter, SendTransactionOptions, WalletName } from '@solana/wallet-adapter-base';
import type { ConnectionContext, EventEmitter, SendTransactionOptions, WalletName } from '@solana/wallet-adapter-base';
import {
BaseMessageSignerWalletAdapter,
scopePollingDetectionStrategy,
Expand All @@ -15,7 +15,7 @@ import {
WalletSignMessageError,
WalletSignTransactionError,
} from '@solana/wallet-adapter-base';
import type { Connection, SendOptions, Transaction, TransactionSignature } from '@solana/web3.js';
import type { SendOptions, Transaction, TransactionSignature } from '@solana/web3.js';
import { PublicKey } from '@solana/web3.js';

interface SkyWalletEvents {
Expand Down Expand Up @@ -155,7 +155,7 @@ export class SkyWalletAdapter extends BaseMessageSignerWalletAdapter {

async sendTransaction(
transaction: Transaction,
connection: Connection,
connectionContext: ConnectionContext,
options: SendTransactionOptions = {}
): Promise<TransactionSignature> {
try {
Expand All @@ -165,11 +165,11 @@ export class SkyWalletAdapter extends BaseMessageSignerWalletAdapter {
try {
const { signers, ...sendOptions } = options;

transaction = await this.prepareTransaction(transaction, connection, sendOptions);
transaction = await this.prepareTransaction(transaction, connectionContext, sendOptions);

signers?.length && transaction.partialSign(...signers);

sendOptions.preflightCommitment = sendOptions.preflightCommitment || connection.commitment;
sendOptions.preflightCommitment = sendOptions.preflightCommitment || connectionContext.commitment;

const { signature } = await wallet.signAndSendTransaction(transaction, sendOptions);
return signature;
Expand Down
Loading

0 comments on commit a0ef497

Please sign in to comment.