orca-so / orca-sdks

Open-sourced typescript SDKs for Orca
MIT License
43 stars 10 forks source link

Option to skip preflight check #11

Open skulltech opened 1 year ago

skulltech commented 1 year ago

The SDK as of now doesn't expose an option to skip preflight checks. Skipping preflight checks are sometime necessary when bundling a bunch of transactions at once, and the transactions depend upon each other.

I propose an option to skip preflight check is added in the TransactionProcessor.

staccDOTsol commented 1 year ago

Preflight checks are an important part of ensuring that transactions are valid before they are sent to the Solana network. However, I understand that there may be situations where skipping these checks is necessary, especially when bundling transactions that depend on each other.

One way to achieve this would be to modify the TransactionProcessor class to include an optional parameter that allows you to skip preflight checks. Here's an example implementation:

import { Transaction, TransactionSignature, TransactionError } from '@solana/web3.js';
import { Connection } from '@solana/web3.js';

export class TransactionProcessor {
  private connection: Connection;

  constructor(connection: Connection) {
    this.connection = connection;
  }

  async processTransaction(transaction: Transaction, skipPreflightCheck = false): Promise<TransactionSignature> {
    if (!skipPreflightCheck) {
      const { err } = await this.connection.getRecentBlockhash();
      if (err) {
        throw new Error(`Unable to get recent blockhash: ${err}`);
      }

      transaction.recentBlockhash = (await this.connection.getRecentBlockhash()).blockhash;
      const { feeCalculator } = await this.connection.getFeeCalculatorForBlockhash(transaction.recentBlockhash);
      transaction.feePayer = feeCalculator.target;
      transaction.recentBlockhash = (await this.connection.getRecentBlockhash()).blockhash;

      const { blockhash } = await this.connection.getRecentBlockhash();
      const signers = transaction.getSignatures().map(({ publicKey }) => publicKey);
      const { value: { err: preflightErr } } = await this.connection.getSignatureStatuses(
        transaction.signature,
        { searchTransactionHistory: true }
      );
      if (preflightErr) {
        throw new TransactionError('Transaction preflight checks failed', transaction, preflightErr);
      }
    }

    const signature = await this.connection.sendRawTransaction(transaction.serialize());
    return signature;
  }
}

With this implementation, you can pass a boolean value as the second parameter to processTransaction to skip preflight checks. For example:

const tx1 = new Transaction().add(...);
const tx2 = new Transaction().add(...);

const processor = new TransactionProcessor(connection);
const signature1 = await processor.processTransaction(tx1, true); // skip preflight check
const signature2 = await processor.processTransaction(tx2, true); // skip preflight check

Note that skipping preflight checks can be dangerous and should only be done if you know what you're doing. It's recommended that you only use this option if you're bundling transactions that depend on each other and you're sure that they will succeed.

staccDOTsol commented 1 year ago

Preflight checks are an important part of ensuring the validity of transactions before they are sent to the Solana network. However, I understand that there may be situations where skipping preflight checks is necessary.

To add an option to skip preflight checks in the TransactionProcessor, you can modify the send method in the TransactionProcessor class to include a boolean parameter that indicates whether or not to skip preflight checks.

Here's an example implementation:

import { Transaction, TransactionSignature } from "@solana/web3.js";
import { Provider } from "./provider";

export class TransactionProcessor {
  private provider: Provider;

  constructor(provider: Provider) {
    this.provider = provider;
  }

  async send(transaction: Transaction, skipPreflight: boolean = false): Promise<TransactionSignature> {
    if (!skipPreflight) {
      await this.provider.connection.getMinimumBalanceForRentExemption(transaction.recentBlockhash);
      await this.provider.connection.getTransactionCount("recent");
    }

    const signature = await this.provider.wallet.signTransaction(transaction);
    return await this.provider.connection.sendRawTransaction(signature.serialize());
  }
}

With this modification, you can now pass a boolean value as the second parameter to the send method to skip preflight checks if necessary.

Keep in mind that skipping preflight checks can be risky and should only be done if you are confident that the transactions are valid.