DEV Community

Cover image for # `@xchainjs/xchain-litecoin`
Fabricio Viskor
Fabricio Viskor

Posted on

# `@xchainjs/xchain-litecoin`

Custom Litecoin (LTC) client and utilities for XChainJS — a lightweight TypeScript SDK for building cross-chain wallets, crypto payment flows, and DeFi tooling with a common interface.

This package provides:

  • a Litecoin Client implementation (UTXO-style chain)
  • address generation & validation
  • balance, transaction history, and transaction data lookup
  • fee estimation helpers (fee rates, fees with memo, etc.)

Source package: https://github.com/xchainjs/xchainjs-lib/tree/master/packages/xchain-litecoin

XChainJS docs: https://docs.xchainjs.org/xchain-client/xchain-litecoin/


Features

  • ✅ TypeScript-first Litecoin SDK
  • ✅ BIP44/84 address derivation support (mainnet & testnet paths)
  • ✅ Create addresses from a phrase (mnemonic)
  • ✅ Validate addresses
  • ✅ Get balances, tx history, and tx data by hash
  • ✅ Transfer LTC
  • ✅ Fee estimation helpers (getFeeRates, getFeesWithMemo, etc.)

How it works (under the hood)

  • Uses bitcoinjs-lib for UTXO transaction building and signing.
  • Uses SoChain API v2 as the default public data provider endpoint.
  • Explorer URLs reference Blockstream.
  • Depends on shared XChainJS packages: @xchainjs/xchain-client, @xchainjs/xchain-crypto, @xchainjs/xchain-util.

References:


Installation

Install the package

yarn add @xchainjs/xchain-litecoin
Enter fullscreen mode Exit fullscreen mode

Peer dependencies

According to the official docs, install these as well:

yarn add @xchainjs/xchain-client @xchainjs/xchain-crypto @xchainjs/xchain-util axios bitcoinjs-lib coininfo wif
Enter fullscreen mode Exit fullscreen mode

Basic usage examples

Connect wallet to a new Litecoin Client

Create a client instance, derive an address, validate it, and fetch its balance.

import { Client } from '@xchainjs/xchain-litecoin'
import { Network } from '@xchainjs/xchain-client'
import { baseToAsset } from '@xchainjs/xchain-util'

const connectWallet = async () => {
  const phrase = 'your mnemonic phrase here'
  const ltcClient = new Client({ network: Network.Mainnet, phrase })

  const address = ltcClient.getAddress()
  console.log('Address:', address)

  const isValid = ltcClient.validateAddress(address)
  if (!isValid) throw new Error('Invalid address')

  const balances = await ltcClient.getBalance(address)
  const readable = baseToAsset(balances[0].amount).amount()
  console.log('Balance:', readable.toString())
}

connectWallet().catch(console.error)
Enter fullscreen mode Exit fullscreen mode

Transfer Litecoin (LTC)

Build and broadcast a transaction using the client.

import { Client, LTC_DECIMAL } from '@xchainjs/xchain-litecoin'
import { assetToBase, assetAmount } from '@xchainjs/xchain-util'

const transferLitecoin = async () => {
  const phrase = 'your mnemonic phrase here'
  const recipient = 'ltc recipient address here'

  const ltcClient = new Client({ phrase })

  const amount = assetToBase(assetAmount(0.01, LTC_DECIMAL))

  const txid = await ltcClient.transfer({
    amount,
    recipient,
    memo: 'memo',
  })

  console.log('TX sent:', txid)
  console.log('Explorer:', ltcClient.getExplorerTxUrl(txid))
}

transferLitecoin().catch(console.error)
Enter fullscreen mode Exit fullscreen mode

Get transfer fees and fee rate estimations

import { Client } from '@xchainjs/xchain-litecoin'
import { baseToAsset } from '@xchainjs/xchain-util'

const returnFees = async () => {
  const phrase = 'your mnemonic phrase here'
  const ltcClient = new Client({ phrase })

  const { fast, fastest, average } = await ltcClient.getFees()
  console.log('Fast:', baseToAsset(fast).amount().toString())
  console.log('Fastest:', baseToAsset(fastest).amount().toString())
  console.log('Average:', baseToAsset(average).amount().toString())

  const feeRates = await ltcClient.getFeeRates()
  console.log('FeeRates:', feeRates)
}

returnFees().catch(console.error)
Enter fullscreen mode Exit fullscreen mode

You can pass feeRate into transfer parameters:

// feeRates.fastest is a number
await ltcClient.transfer({
  amount,
  recipient,
  memo: 'memo test',
  feeRate: feeRates.fastest,
})
Enter fullscreen mode Exit fullscreen mode

Get transaction data & transaction history

import { Client } from '@xchainjs/xchain-litecoin'

const transactionData = async () => {
  const phrase = 'your mnemonic phrase here'
  const ltcClient = new Client({ phrase })

  const hash = 'your tx hash'
  const txData = await ltcClient.getTransactionData(hash)
  console.log('TxData:', txData)
}

transactionData().catch(console.error)
Enter fullscreen mode Exit fullscreen mode

Useful links

Top comments (0)