LiquidityPoolsService

public class LiquidityPoolsService : @unchecked Sendable

Service for querying liquidity pools from the Stellar Horizon API.

Liquidity pools enable automated market making (AMM) on Stellar. Each pool contains reserves of two assets and allows swapping between them at algorithmically determined prices.

Example usage:

let sdk = StellarSDK()

// Get liquidity pool details
let response = await sdk.liquidityPools.getLiquidityPoolDetails(
    liquidityPoolId: "L..."
)
switch response {
case .success(let pool):
    print("Total shares: \(pool.totalShares)")
    for reserve in pool.reserves {
        print("\(reserve.asset): \(reserve.amount)")
    }
case .failure(let error):
    print("Error: \(error)")
}

See also:

  • Retrieves details for a specific liquidity pool by its ID.

    Declaration

    Swift

    open func getLiquidityPool(poolId: String) async -> LiquidityPoolDetailsResponseEnum

    Parameters

    poolId

    The liquidity pool ID (L-address or hex format)

    Return Value

    LiquidityPoolDetailsResponseEnum with pool details or error

  • Retrieves all liquidity pools with optional filtering and pagination parameters.

    Declaration

    Swift

    open func getLiquidityPools(account: String? = nil, cursor: String? = nil, order: Order? = nil, limit: Int? = nil) async -> PageResponse<LiquidityPoolResponse>.ResponseEnum

    Parameters

    account

    Optional account ID (G… address) to filter pools by participation. When provided, returns only liquidity pools the account participates in.

    cursor

    Optional paging token, specifying where to start returning records from

    order

    Optional sort order - .ascending or .descending

    limit

    Optional maximum number of records to return. Default: 10, max: 200

    Return Value

    PageResponse containing liquidity pools or error

  • Retrieves liquidity pools filtered by reserve assets with optional pagination parameters.

    Declaration

    Swift

    open func getLiquidityPools(reserveAssetA: Asset, reserveAssetB: Asset, cursor: String? = nil, order: Order? = nil, limit: Int? = nil) async -> PageResponse<LiquidityPoolResponse>.ResponseEnum

    Parameters

    reserveAssetA

    First reserve asset to filter by

    reserveAssetB

    Second reserve asset to filter by

    cursor

    Optional paging token, specifying where to start returning records from

    order

    Optional sort order - .ascending or .descending

    limit

    Optional maximum number of records to return. Default: 10, max: 200

    Return Value

    PageResponse containing liquidity pools matching the reserve assets or error

  • Retrieves trade history for a specific liquidity pool with optional pagination parameters.

    Declaration

    Swift

    open func getLiquidityPoolTrades(poolId: String, cursor: String? = nil, order: Order? = nil, limit: Int? = nil) async -> LiquidityPoolTradesResponseEnum

    Parameters

    poolId

    The liquidity pool ID (L-address or hex format)

    cursor

    Optional paging token, specifying where to start returning records from

    order

    Optional sort order - .ascending or .descending

    limit

    Optional maximum number of records to return. Default: 10, max: 200

    Return Value

    LiquidityPoolTradesResponseEnum with trade history or error

  • Streams real-time trade updates for a specific liquidity pool.

    Creates a Server-Sent Events (SSE) stream that delivers live trade updates as they occur on the Stellar network for the specified liquidity pool. The stream provides continuous updates until explicitly closed.

    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 opened")
        case .response(id: let id, data: let trade):
            print("New trade: \(trade.baseAmount) for \(trade.counterAmount)")
        case .error(let error):
            print("Error: \(error)")
        }
    }
    
    // Close when done
    tradesStream.closeStream()
    

    Declaration

    Swift

    open func streamTrades(forPoolId poolId: String) -> LiquidityPoolTradesStreamItem

    Parameters

    poolId

    The liquidity pool ID (L-address or hex format)

    Return Value

    LiquidityPoolTradesStreamItem for receiving live trade updates

  • Retrieves liquidity pools from a specific Horizon URL.

    Used for pagination. Pass URLs from PageResponse links (e.g., next, prev).

    Declaration

    Swift

    open func getLiquidityPoolsFromUrl(url: String) async -> PageResponse<LiquidityPoolResponse>.ResponseEnum

    Parameters

    url

    The complete URL to fetch liquidity pools from

    Return Value

    PageResponse containing liquidity pools or error