ecash-quicksend
Version:
A unified transaction manager for eCash (XEC), SLP, and ALP token transactions
107 lines • 5.19 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createRawXecTransaction = createRawXecTransaction;
const dotenv_1 = __importDefault(require("dotenv"));
dotenv_1.default.config();
const utxo_utils_1 = require("../utxo/utxo-utils");
const wallet_utils_1 = require("../wallet/wallet-utils");
const transaction_utils_1 = require("../transaction/transaction-utils");
const transaction_builder_1 = require("../transaction/transaction-builder");
async function createRawXecTransaction(recipients, utxoStrategy = 'all', addressIndex = 0, mnemonic, // 可选的助记词参数
chronikClient // 新增:可选的chronik客户端参数 (使用any类型以避免循环依赖)
) {
try {
// 验证参数
if (!Array.isArray(recipients) || recipients.length === 0) {
throw new Error('recipients must be a non-empty array');
}
// 验证每个接收方对象
for (const recipient of recipients) {
if (!recipient.address || typeof recipient.amount !== 'number') {
throw new Error('Each recipient must have address and amount fields');
}
// 如果提供了tokenId,验证其格式
if (recipient.tokenId && typeof recipient.tokenId !== 'string') {
throw new Error('tokenId must be a string if provided');
}
// 如果提供了decimals,验证其为非负整数
if (recipient.decimals !== undefined && (!Number.isInteger(recipient.decimals) || recipient.decimals < 0)) {
throw new Error('decimals must be a non-negative integer if provided');
}
}
// 初始化钱包 - 使用指定的地址索引和可选的助记词
const { walletSk, walletPk, walletP2pkh, address: utxoAddress } = (0, wallet_utils_1.initializeWallet)(addressIndex, mnemonic);
const utxos = await (0, utxo_utils_1.getUtxos)(utxoAddress, chronikClient); // 传递chronik客户端
if (utxos.length === 0) {
throw new Error(`No UTXOs found for address index ${addressIndex}`);
}
// 计算总发送金额 (只计算XEC,不包括代币)
const totalSendAmount = recipients
.filter(recipient => !recipient.tokenId) // 只计算非代币交易
.reduce((sum, recipient) => sum + recipient.amount, 0);
// 分析交易类型
const xecRecipients = recipients.filter(r => !r.tokenId);
const tokenRecipients = recipients.filter(r => r.tokenId);
// 选择UTXOs
const utxoSelection = (0, utxo_utils_1.selectUtxos)(utxos, totalSendAmount, utxoStrategy);
const { selectedUtxos } = utxoSelection;
// 记录交易摘要
(0, transaction_builder_1.logTransactionSummary)('XEC', {
地址索引: addressIndex,
策略: utxoStrategy,
接收方数量: recipients.length,
XEC接收方: xecRecipients.length,
代币接收方: tokenRecipients.length,
总发送金额: totalSendAmount,
UTXOs数量: selectedUtxos.length,
总输入: utxoSelection.totalInputValue,
预估手续费: utxoSelection.estimatedFee,
预计找零: utxoSelection.changeAmount
});
// 构建交易输入
const inputs = (0, transaction_utils_1.buildTransactionInputs)(selectedUtxos, walletP2pkh, walletSk, walletPk);
// 构建交易输出 - 为每个接收方创建输出
const outputs = [];
// 添加所有接收方输出
recipients.forEach(recipient => {
const output = {
sats: BigInt(recipient.amount),
script: (0, transaction_utils_1.createP2pkhScript)(recipient.address)
};
// 如果是代币交易,添加代币信息
if (recipient.tokenId) {
output.tokenId = recipient.tokenId;
if (recipient.decimals !== undefined) {
output.decimals = recipient.decimals;
}
}
outputs.push(output);
});
// 添加找零脚本 - 保持原始逻辑!
outputs.push(walletP2pkh);
// 构建并广播交易
const result = await (0, transaction_builder_1.buildAndBroadcastTransaction)(inputs, outputs, { chronik: chronikClient }); // 传递chronik客户端
return {
...result,
utxoSelection,
recipients: recipients.length,
xecRecipients: xecRecipients.length,
tokenRecipients: tokenRecipients.length,
totalSent: totalSendAmount,
tokenTransfers: tokenRecipients.map(r => ({
tokenId: r.tokenId,
amount: r.amount,
decimals: r.decimals,
address: r.address
}))
};
}
catch (error) {
console.error('XEC transaction creation failed:', error);
throw error;
}
}
//# sourceMappingURL=xecsend.js.map