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:
- Stellar developer docs
- TradeAggregationsService for aggregated trade statistics
-
getTrades(baseAssetType:AsynchronousbaseAssetCode: baseAssetIssuer: counterAssetType: counterAssetCode: counterAssetIssuer: offerId: forAccount: forLiquidityPool: tradeType: cursor: order: limit: ) Retrieves trades filtered by asset pair, offer ID, account, liquidity pool, or trade type with optional pagination parameters.
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>.ResponseEnumParameters
baseAssetTypeOptional. Type of the base asset: “native”, “credit_alphanum4”, or “credit_alphanum12”
baseAssetCodeOptional. Asset code of the base asset (required if base_asset_type is not “native”)
baseAssetIssuerOptional. Account ID of the base asset issuer (required if base_asset_type is not “native”)
counterAssetTypeOptional. Type of the counter asset: “native”, “credit_alphanum4”, or “credit_alphanum12”
counterAssetCodeOptional. Asset code of the counter asset (required if counter_asset_type is not “native”)
counterAssetIssuerOptional. Account ID of the counter asset issuer (required if counter_asset_type is not “native”)
offerIdOptional. Filter for trades involving a specific offer ID
forAccountOptional. Filter for trades where the specified account participated
forLiquidityPoolOptional. Filter for trades from a specific liquidity pool (L-address or hex format)
tradeTypeOptional. Filter by trade type: “all”, “orderbook”, or “liquidity_pool”. Default: “all”
cursorOptional paging token, specifying where to start returning records from
orderOptional sort order - .ascending or .descending
limitOptional maximum number of records to return. Default: 10, max: 200
Return Value
PageResponse containing trade records or error
-
getTrades(forAccount:Asynchronousfrom: order: limit: ) 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>.ResponseEnumParameters
accountIdThe Stellar account ID to get trades for
cursorOptional paging token, specifying where to start returning records from
orderOptional sort order - .ascending or .descending
limitOptional 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) -> TradesStreamItemParameters
tradesTypeThe 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
View on GitHub
Install in Dash