TradesService

public class TradesService : @unchecked Sendable

Service for querying trade history from the Stellar Horizon API.

Trades represent completed exchanges between two assets on the Stellar decentralized exchange. Can filter trades by asset pair, account, offer, or liquidity pool.

Example usage:

let sdk = StellarSDK()

// Get trades for a trading pair
let response = await sdk.trades.getTrades(
    forAssetPair: (selling: Asset(canonicalForm: "USD:GISSUER...")!,
                   buying: Asset(type: AssetType.ASSET_TYPE_NATIVE)!),
    limit: 50
)
switch response {
case .success(let page):
    for trade in page.records {
        print("Price: \(trade.price)")
        print("Amount: \(trade.baseAmount)")
    }
case .failure(let error):
    print("Error: \(error)")
}

See also:

  • Retrieves trades filtered by asset pair, offer ID, account, liquidity pool, or trade type with optional pagination parameters.

    See: Stellar developer docs

    Declaration

    Swift

    open func getTrades(baseAssetType: String? = nil, baseAssetCode: String? = nil, baseAssetIssuer: String? = nil, counterAssetType: String? = nil, counterAssetCode: String? = nil, counterAssetIssuer: String? = nil, offerId: String? = nil, forAccount: String? = nil, forLiquidityPool: String? = nil, tradeType: String? = nil, cursor: String? = nil, order: Order? = nil, limit: Int? = nil) async -> PageResponse<TradeResponse>.ResponseEnum

    Parameters

    baseAssetType

    Optional. Type of the base asset: “native”, “credit_alphanum4”, or “credit_alphanum12”

    baseAssetCode

    Optional. Asset code of the base asset (required if base_asset_type is not “native”)

    baseAssetIssuer

    Optional. Account ID of the base asset issuer (required if base_asset_type is not “native”)

    counterAssetType

    Optional. Type of the counter asset: “native”, “credit_alphanum4”, or “credit_alphanum12”

    counterAssetCode

    Optional. Asset code of the counter asset (required if counter_asset_type is not “native”)

    counterAssetIssuer

    Optional. Account ID of the counter asset issuer (required if counter_asset_type is not “native”)

    offerId

    Optional. Filter for trades involving a specific offer ID

    forAccount

    Optional. Filter for trades where the specified account participated

    forLiquidityPool

    Optional. Filter for trades from a specific liquidity pool (L-address or hex format)

    tradeType

    Optional. Filter by trade type: “all”, “orderbook”, or “liquidity_pool”. Default: “all”

    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 trade records or error

  • Retrieves all trades for a specific account with optional pagination parameters.

    Declaration

    Swift

    open func getTrades(forAccount accountId: String, from cursor: String? = nil, order: Order? = nil, limit: Int? = nil) async -> PageResponse<TradeResponse>.ResponseEnum

    Parameters

    accountId

    The Stellar account ID to get trades for

    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 trade records for the account or error

  • Streams real-time trade updates via Server-Sent Events from Horizon.

    Declaration

    Swift

    open func stream(for tradesType: TradesChange) -> TradesStreamItem

    Parameters

    tradesType

    The filter specifying which trades to stream (all trades with asset filters, or trades for a specific account)

    Return Value

    TradesStreamItem for receiving streaming trade updates