LiquidityPoolTradesStreamItem

public final class LiquidityPoolTradesStreamItem : Sendable

Streams liquidity pool trade data from the Horizon API using Server-Sent Events (SSE) for real-time updates.

This stream provides live updates for trades involving a specific liquidity pool, delivering new trade events as they occur on the Stellar network. Each update contains complete trade details including traded assets, amounts, prices, and participating accounts or liquidity pools.

Example usage:

let sdk = StellarSDK()
let poolId = "L..." // Liquidity pool ID (L-address or hex format)
let tradesStream = sdk.liquidityPools.streamTrades(forPoolId: poolId)

tradesStream.onReceive { response in
    switch response {
    case .open:
        print("Stream connection established")
    case .response(id: let id, data: let trade):
        print("Trade received - Type: \(trade.tradeType)")
        print("Base: \(trade.baseAmount) \(trade.baseAssetCode ?? "XLM")")
        print("Counter: \(trade.counterAmount) \(trade.counterAssetCode ?? "XLM")")
        print("Price: \(trade.price.n)/\(trade.price.d)")
    case .error(let error):
        print("Stream error: \(error)")
    }
}

// Close stream when done
tradesStream.closeStream()

See also:

  • Stellar developer docs
  • TradeResponse for the trade data structure
  • LiquidityPoolsService for related liquidity pool operations
  • Creates a new liquidity pool trades stream for the specified Horizon API endpoint.

    Declaration

    Swift

    public init(requestUrl: String)

    Parameters

    requestUrl

    The complete Horizon API URL for streaming liquidity pool trades

  • Establishes the SSE connection and delivers trade responses as they arrive from Horizon.

    The response closure is called multiple times:

    • Once with .open when the connection is established
    • Each time with .response when a new trade is received
    • With .error if any error occurs during streaming

    Declaration

    Swift

    public func onReceive(response: @escaping StreamResponseEnum<TradeResponse>.ResponseClosure)

    Parameters

    response

    Closure called with stream events. Called on a background thread.

  • Closes the event stream and releases resources.

    Call this method when you no longer need to receive trade updates for the liquidity pool. After closing, the stream cannot be reopened - create a new LiquidityPoolTradesStreamItem instead.

    Declaration

    Swift

    public func closeStream()