This package provides a set of utilities for retrieving and processing transactions from the Solana blockchain. It supports both native SOL and token transactions (using legacy transaction processing) and handles pagination when fetching transaction signatures.
- Solana Connection: Connects to the Solana devnet.
- Pagination: Supports paginated fetching of transaction signatures.
- SOL & Token Transactions: Processes native SOL transactions and token transactions.
- Type Safety: Uses TypeScript with type guards and union types to handle legacy and versioned transactions.
Install the required dependencies via npm:
npm install solana-toolbox
Then, add this package to your project.
Import the getTransactions
function and call it with the wallet address, token type, and optional pagination options:
import { getTransactions } from "solana-toolbox";
// Example usage with custom token mapping:
const customTokenMapping = {
sol: null,
usdc_sol: "4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU",
my_custom_token: "CustomTokenMintAddress..."
};
const transactions = await getTransactions(
'WALLET_ADDRESS',
'my_custom_token',
'mainnet-beta',
{ limit: 20 },
customTokenMapping
);
interface PaginationOptions {
limit: number;
before: string | null;
}
Defines options for pagination when fetching transaction signatures.
interface TransactionPagination {
before: string | null;
hasMore: boolean;
}
Contains pagination details in the response.
interface GetTransactionsResponse {
transactions: (TransactionResponse | VersionedTransactionResponse | null)[];
pagination: TransactionPagination;
}
The response returned by getTransactions
, containing an array of transactions and pagination info.
Returns a new connection to the Solana devnet.
Fetches transaction signature info for a given public key using the specified pagination options.
-
Parameters:
-
connection
: The Solana connection instance. -
key
: The public key to fetch signatures for. -
options
: Pagination options.
-
Fetches a transaction from the blockchain using its signature and configuration.
-
Parameters:
-
connection
: The Solana connection instance. -
signature
: The transaction signature. -
transactionConfig
: Configuration options for the transaction.
-
-
Returns:
A transaction response that can be a legacy or versioned transaction, ornull
.
Type guard to check if a transaction message is legacy by verifying the presence of accountKeys
.
Processes a SOL transaction and returns a simplified object containing:
signature
-
from
andto
addresses -
amount
in SOL timestamp
If the transaction is versioned or malformed, it returns null
.
Processes a token transaction and returns a simplified object containing:
signature
-
from
andto
addresses -
amount
(with the token type) timestamp
Returns null
if the transaction is invalid or unsupported.
getSolTransactions(connection, pubKey, options, transactionConfig): Promise<GetTransactionsResponse>
Retrieves and processes SOL transactions for the specified public key.
-
Parameters:
-
connection
: The Solana connection. -
pubKey
: The public key for the wallet. -
options
: Pagination options. -
transactionConfig
: Transaction configuration.
-
-
Returns:
An object containing an array of transactions and pagination details.
getTokenTransactions(connection, pubKey, tokenMint, tokenType, options, transactionConfig): Promise<GetTransactionsResponse>
Retrieves and processes token transactions for a given wallet and token mint.
-
Parameters:
-
connection
: The Solana connection. -
pubKey
: The wallet's public key. -
tokenMint
: The mint address of the token. -
tokenType
: The token type (e.g.,"usdc_sol"
). -
options
: Pagination options. -
transactionConfig
: Transaction configuration.
-
-
Returns:
An object containing an array of token transactions and pagination details.
Main exported function that retrieves transactions for a given wallet address and token type.
-
Parameters:
-
address
: Solana wallet address -
tokenType
: Transaction type to fetch ("sol" for native SOL, "usdc_sol" for USDC, etc.) -
options
: Pagination configuration (optional) -
config
: Solana connection configuration (optional)
-
-
Returns:
Promise resolving toGetTransactionsResponse
containing processed transactions and pagination info -
Throws:
InvalidTokenTypeError
: When an unsupported token type is providedConnectionError
: When unable to establish connection to Solana networkTransactionFetchError
: When transaction retrieval fails
The package implements comprehensive error handling:
- An error is thrown if an unsupported token type is passed to
getTransactions
. - For SOL transactions, versioned transactions are currently not supported for processing. A warning is logged if encountered.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE for more information.