Skip to content

Commit

Permalink
sdk: use tx rpc for transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
yurushao committed Jan 22, 2025
1 parent 5fb68c0 commit f1ca4ed
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
13 changes: 11 additions & 2 deletions anchor/src/client/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,19 @@ export class BaseClient {
tx: VersionedTransaction | Transaction,
signerOverride?: Keypair,
): Promise<TransactionSignature> {
// 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,
]);
}
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion anchor/src/utils/blockhash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit f1ca4ed

Please sign in to comment.