From f1ca4ed6b86bda84a5eb981b5ad6e7c1c35b8c0c Mon Sep 17 00:00:00 2001 From: Yuru Shao Date: Wed, 22 Jan 2025 00:48:21 -0800 Subject: [PATCH] sdk: use tx rpc for transactions --- anchor/src/client/base.ts | 13 +++++++++++-- anchor/src/utils/blockhash.ts | 2 +- cli/README.md | 2 ++ cli/src/utils.ts | 5 +++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/anchor/src/client/base.ts b/anchor/src/client/base.ts index 7d4ade2d..e5c6062f 100644 --- a/anchor/src/client/base.ts +++ b/anchor/src/client/base.ts @@ -263,10 +263,19 @@ export class BaseClient { tx: VersionedTransaction | Transaction, signerOverride?: Keypair, ): Promise { + // Use dedicated connection for sending transactions if available + const { NEXT_PUBLIC_TX_RPC, TX_RPC } = process.env; + const txConnection = new Connection( + NEXT_PUBLIC_TX_RPC || TX_RPC || this.provider.connection.rpcEndpoint, + { + commitment: "confirmed", + }, + ); + // This is just a convenient method so that in tests we can send legacy // txs, for example transfer SOL, create ATA, etc. if (tx instanceof Transaction) { - return await sendAndConfirmTransaction(this.provider.connection, tx, [ + return await sendAndConfirmTransaction(txConnection, tx, [ signerOverride || this.getWallet().payer, ]); } @@ -285,7 +294,7 @@ export class BaseClient { serializedTx = signedTx.serialize(); } - const txSig = await connection.sendRawTransaction(serializedTx, { + const txSig = await txConnection.sendRawTransaction(serializedTx, { // skip simulation since we just did it to compute CUs // however this means that we need to reconstruct the error, if // the tx fails on chain execution. diff --git a/anchor/src/utils/blockhash.ts b/anchor/src/utils/blockhash.ts index a8358f3b..574d4b3e 100644 --- a/anchor/src/utils/blockhash.ts +++ b/anchor/src/utils/blockhash.ts @@ -18,7 +18,7 @@ export class BlockhashWithCache { constructor( provider: anchor.Provider, isBrowser: boolean, - ttl: number = 15_000, + ttl: number = 5_000, ) { this.provider = provider; this.isBrowser = isBrowser; diff --git a/cli/README.md b/cli/README.md index 02f65228..7ccc6b18 100644 --- a/cli/README.md +++ b/cli/README.md @@ -30,6 +30,7 @@ The CLI expects a configuration file at `~/.config/glam/config.json`. The file s { "cluster": "", "json_rpc_url": "", + "tx_rpc_url": "", "keypair_path": "", "priority_fee": { "micro_lamports": 0, @@ -44,6 +45,7 @@ Here's a quick explanation of each field: - `cluster`: Value must be one of `mainnet-beta`, `devnet`, or `localnet`. - `json_rpc_url`: The URL of your preferred Solana JSON RPC endpoint. +- `tx_rpc_url`: Optional. If not set it will default to `json_rpc_url`. Use this to specify a separate RPC endpoint you want to use for sending transactions. - `keypair_path`: Path to your keypair JSON file. - `priority_fee`: - `micro_lamports`: Optional (defaults to 0). If provided, `level` and `helius_api_key` will be ignored. diff --git a/cli/src/utils.ts b/cli/src/utils.ts index 8d0ea44f..4e073e21 100644 --- a/cli/src/utils.ts +++ b/cli/src/utils.ts @@ -11,6 +11,7 @@ import path from "path"; export type CliConfig = { cluster: string; json_rpc_url: string; + tx_rpc_url: string; keypair_path: string; priority_fee?: { micro_lamports?: number; @@ -59,6 +60,10 @@ export const loadingConfig = () => { ); } + if (cliConfig.tx_rpc_url) { + process.env.TX_RPC = cliConfig.tx_rpc_url; + } + process.env.ANCHOR_PROVIDER_URL = cliConfig.json_rpc_url; process.env.ANCHOR_WALLET = cliConfig.keypair_path;