solana-tx-utils
TypeScript icon, indicating that this package has built-in type declarations

0.1.1 • Public • Published

Solana Transaction Utilities

Simple and small library with utilities for sending, building, singing, confirming and parsing Solana transactions

Installation

npm i solana-tx-utils
  • This library depends on @solana/web3.js
npm i @solana/web3.js

Example usage

  • Build transaction from instruction, send and confirm
// Create connection, and wallet
import { Connection, SystemProgram } from '@solana/web3.js'
import { buildAndSignTxFromInstructions, sendAndConfirmTransaction } from 'solana-tx-utils'

const connection = new Connection('<RPC_URL>', options)
const wallet = new Keypair()

// Build and sign V0 transaction from instructions
const instructions = [
  SystemProgram.transfer({
    fromPubkey: ...,
    lamports: 5000,
    toPubkey: wallet.publicKey,
  }),
]
const addressLookupTables = [...]

const txData = await buildAndSignTxFromInstructions({
  signers: [wallet],
  instructions,
  addressLookupTables,
})

console.log(txData)
/*
{
  transaction: VersionedTransaction
  lastValidBlockHeight: number
}
*/

// Send and confirm signed transaction
const res = await sendAndConfirmTransaction({
  ...txData,
  connection,
})

console.log(res)
/*
If transaction was not confirmed
{
  txId: string
  status: 'TIMEOUT' | 'BLOCK_HEIGHT_EXCEEDED'
  data: null
  error: null
}
If transaction was confirmed and successfully sent
{
  txId: string
  status: 'SUCCESS'
  data: ConfirmedTransactionMeta
  error: null
}
If transaction was confirmed and resulted in error
{
  txId: string
  status: 'ERROR'
  data: null
  error: ParsedTransactionError
*/
  • Confirm transaction based on txId
import { confirmTransaction, sendTransaction } from 'solana-tx-utils'

const res = await sendAndConfirmTransaction({
	...txData,
	connection,
})

const res = confirmTransaction(
	{
		txId: '...',
		connection,
		lastValidBlockHeight,
	},
	{
		method: 'websocket', // It's possible to chose between websockets/polling for confirming transaction
	},
)

console.log(res)
/*
If transaction was not confirmed
{
  status: 'TIMEOUT' | 'BLOCK_HEIGHT_EXCEEDED'
  data: null
  error: null
}
If transaction was confirmed and successfully sent
{
  status: 'SUCCESS'
  data: ConfirmedTransactionMeta
  error: null
}
If transaction was confirmed and resulted in error
{
  status: 'ERROR'
  data: null
  error: ParsedTransactionError
*/

Local development

solana-keygen new
  • If you are running test validator on port different from 8899
  • Create .env file in /tests directory
RPC_URL_PORT=<port>
  • Run tests
pnpm validator
pnpm test
# or
pnpm test:w

Package Sidebar

Install

npm i solana-tx-utils

Weekly Downloads

0

Version

0.1.1

License

MIT

Unpacked Size

28.7 kB

Total Files

33

Last publish

Collaborators

  • dmc0125