Skip to main content

Function Signature

sponsorTransaction(params: {
  transaction: VersionedTransaction | Transaction;
  traceContext?: TraceContext;
}): Promise<VersionedTransaction | Transaction>

Description

Sends an unsigned Solana transaction to Dynamic’s gas sponsorship endpoint (/solana/sponsorTransaction), which replaces the fee payer with the sponsor’s address. The returned transaction has the sponsor as fee payer and should be passed to signTransaction() instead of the original. Gas sponsorship must be enabled for your environment in the Dynamic dashboard before calling this method.
For most use cases, pass sponsor: true to signTransaction() instead of calling sponsorTransaction() directly. Use this method when you need to inspect or modify the sponsored transaction before signing.

Parameters

Required Parameters

  • transaction (VersionedTransaction | Transaction) - The unsigned Solana transaction. Supports both legacy Transaction and VersionedTransaction.

Optional Parameters

  • traceContext (TraceContext) - Distributed tracing context for observability.

Returns

Promise<VersionedTransaction | Transaction> - The sponsored transaction with the gas sponsor set as fee payer. Returns the same transaction type that was passed in.

Example

import { DynamicSvmWalletClient } from '@dynamic-labs-wallet/node-svm';
import { Connection, Transaction, SystemProgram, PublicKey } from '@solana/web3.js';

const svmClient = await authenticatedSvmClient();
const connection = new Connection(process.env.SOLANA_RPC_URL ?? 'https://api.mainnet-beta.solana.com');

const { blockhash } = await connection.getLatestBlockhash();

const transaction = new Transaction().add(
  SystemProgram.transfer({
    fromPubkey: new PublicKey(walletMetadata.accountAddress),
    toPubkey: new PublicKey(recipientAddress),
    lamports: 1000000,
  })
);
transaction.recentBlockhash = blockhash;
transaction.feePayer = new PublicKey(walletMetadata.accountAddress);

// Sponsor replaces the fee payer
const sponsoredTx = await svmClient.sponsorTransaction({ transaction });

// Inspect or modify the sponsored transaction, then sign
const signatureBase58 = await svmClient.signTransaction({
  walletMetadata,
  externalServerKeyShares,
  transaction: sponsoredTx,
});

Error Handling

The method throws when sponsorship is unavailable or not enabled:
try {
  const sponsoredTx = await svmClient.sponsorTransaction({ transaction });
} catch (error) {
  if (error.message.includes('400')) {
    console.error('Gas sponsorship is not enabled for this environment');
  } else {
    console.error('Sponsorship failed:', error.message);
  }
}