# `@xchainjs/xchain-litecoin`

发布: (2025年12月20日 GMT+8 00:39)
3 min read
原文: Dev.to

请提供您想要翻译的完整文本(除源链接外的内容),我将把它翻译成简体中文并保留原始的 Markdown 格式、代码块和 URL。谢谢!

此软件包提供的功能

  • Litecoin Client 实现(UTXO‑style 链)
  • 地址生成与验证
  • 余额、交易历史以及交易‑data 查询
  • 手续费估算助手(getFeeRatesgetFeesWithMemo 等)

Source package:

XChainJS docs:

功能

  • ✅ TypeScript优先的Litecoin SDK
  • ✅ 支持 BIP44/84 地址派生(主网 & 测试网)
  • ✅ 从助记词短语创建地址
  • ✅ 验证地址
  • ✅ 通过哈希获取余额、交易历史和交易数据
  • ✅ 转账 LTC
  • ✅ 费用估算助手(getFeeRatesgetFeesWithMemo,…)

工作原理(内部实现)

  • 使用 bitcoinjs-lib 构建和签署 UTXO 交易。
  • 使用 SoChain API v2 作为默认的公共数据提供者端点。
  • 区块浏览器 URL 引用 Blockstream
  • 依赖共享的 XChainJS 包:@xchainjs/xchain-client@xchainjs/xchain-crypto@xchainjs/xchain-util

参考

  • 文档 “How it works”:
  • 文档 “How to use”:

安装

安装包

yarn add @xchainjs/xchain-litecoin

同伴依赖

根据官方文档,也请安装以下依赖:

yarn add @xchainjs/xchain-client @xchainjs/xchain-crypto @xchainjs/xchain-util \
         axios bitcoinjs-lib coininfo wif

基本使用示例

将钱包连接到新的 Litecoin 客户端

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)

转账 Litecoin (LTC)

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)

获取转账费用和费用率估算

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)

您可以在 transfer 参数中传入 feeRate

// feeRates.fastest 是一个数字
await ltcClient.transfer({
  amount,
  recipient,
  memo: 'memo test',
  feeRate: feeRates.fastest,
})

获取交易数据和交易历史

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)

有用链接

  • Package source (GitHub):
  • Docs (overview):
  • Docs (how it works):
  • Docs (how to use):
in-litecoin/how-to-use.html
Back to Blog

相关文章

阅读更多 »

# `@xchainjs/xchain-ethereum`

@xchainjs/xchain-ethereum 是 XChainJS 生态系统的官方 Ethereum 客户端——一个模块化、TypeScript‑first SDK,用于构建跨链钱包、crypto…

Vue + XChainJS 示例

Vue + XChainJS 示例的封面图片 https://media2.dev.to/dynamic/image/width=1000,height=420,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads...

# XChainJS 检查交易示例

此示例演示如何使用 XChainJS 检查和跟踪区块链交易。实时演示与源代码 - 实时演示 CodeSandbox: - 源代码 GitHub:...