import type { TronProvider } from '../../tron-discovery' import type { TronNativeTransferRequest, TronTRC20TransferRequest, TronTransactionRequest } from '@meshconnect/uwc-types' import { parseError } from '../../utils/error-utils' import { TronTransactionBuilder } from './tron-transaction-builder' /** * Service for handling Tron transaction operations */ export class TronTransactionService { private builder: TronTransactionBuilder constructor() { this.builder = new TronTransactionBuilder() } /** * Send a transaction for Tron */ async sendTransaction( request: TronTransactionRequest, provider: TronProvider ): Promise { try { if ('contractAddress' in request) { return await this.sendTRC20Transaction( provider, request as TronTRC20TransferRequest ) } else if ('amount' in request) { return await this.sendNativeTransaction( provider, request as TronNativeTransferRequest ) } else { throw new Error('Invalid Tron transaction request type') } } catch (error) { parseError(error) } } /** * Send native TRX transfer */ private async sendNativeTransaction( provider: TronProvider, request: TronNativeTransferRequest ): Promise { try { const tronWeb = provider.tronWeb // Build unsigned transaction const unsignedTx = await this.builder.buildNativeTransfer( tronWeb, request ) // Sign transaction const signedTx = await tronWeb.trx.sign(unsignedTx) // Broadcast transaction // eslint-disable-next-line @typescript-eslint/no-explicit-any const result = (await tronWeb.trx.sendRawTransaction(signedTx)) as any if (!result?.result) { throw new Error(result?.message || 'Failed to broadcast transaction') } return result.txid } catch (error) { parseError(error) } } /** * Send TRC20 token transfer */ private async sendTRC20Transaction( provider: TronProvider, request: TronTRC20TransferRequest ): Promise { try { const tronWeb = provider.tronWeb // Build unsigned transaction const unsignedTx = await this.builder.buildTRC20Transfer(tronWeb, request) // Sign transaction const signedTx = await tronWeb.trx.sign(unsignedTx) // Broadcast transaction // eslint-disable-next-line @typescript-eslint/no-explicit-any const result = (await tronWeb.trx.sendRawTransaction(signedTx)) as any if (!result?.result) { throw new Error(result?.message || 'Failed to broadcast transaction') } return result.txid } catch (error) { parseError(error) } } }