QuoteService

public final class QuoteService : @unchecked Sendable

Implements SEP-0038 - Anchor RFQ (Request for Quote) API.

This class provides price discovery and firm quotes for asset exchanges. Anchors use this to offer indicative and firm exchange rates for converting between on-chain and off-chain assets. Essential for cross-asset deposit/withdrawal operations.

Typical Usage

let service = QuoteService(serviceAddress: "https://anchor.example.com")

// Get indicative price
let priceResult = await service.price(
    context: "sep6",
    sellAsset: "iso4217:USD",
    buyAsset: "stellar:USDC:G...",
    sellAmount: "100",
    jwt: jwtToken
)

// Request firm quote
let quoteRequest = Sep38PostQuoteRequest(
    context: "sep6",
    sellAsset: "iso4217:USD",
    buyAsset: "stellar:USDC:G...",
    sellAmount: "100"
)
let quoteResult = await service.postQuote(request: quoteRequest, jwt: jwtToken)

// Use quote in SEP-6 deposit-exchange
if case .success(let quote) = quoteResult {
    let depositRequest = DepositExchangeRequest(
        destinationAsset: quote.buyAsset,
        sourceAsset: quote.sellAsset,
        amount: quote.sellAmount,
        quoteId: quote.id,
        account: accountId,
        jwt: jwtToken
    )
    // Submit to TransferServerService
}

See also:

  • SEP-0038 Specification
  • [TransferServerService] for SEP-6 integration
  • [InteractiveService] for SEP-24 integration
  • The base URL of the SEP-38 quote service endpoint for price discovery and firm quotes.

    Declaration

    Swift

    public let serviceAddress: String
  • Creates a QuoteService instance with a direct service endpoint URL.

    Declaration

    Swift

    public init(serviceAddress: String)

    Parameters

    serviceAddress

    The URL of the SEP-38 quote server (e.g., “https://anchor.example.com/sep38”)

  • info(jwt:) Asynchronous

    Returns the supported Stellar assets and off-chain assets available for trading.

    See: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0038.md#get-info

    Declaration

    Swift

    public func info(jwt: String? = nil) async -> Sep38InfoResponseEnum

    Parameters

    jwt

    Optional JWT token obtained from SEP-10 authentication

    Return Value

    Sep38InfoResponseEnum with supported assets and delivery methods, or an error

  • Fetches indicative prices of available off-chain assets in exchange for a Stellar asset and vice versa.

    See: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0038.md#get-prices

    Declaration

    Swift

    public func prices(sellAsset:String,
                       sellAmount:String,
                       sellDeliveryMethod:String? = nil,
                       buyDeliveryMethod:String? = nil,
                       countryCode:String? = nil,
                       jwt:String? = nil) async -> Sep38PricesResponseEnum

    Parameters

    sellAsset

    The asset you want to sell, using the Asset Identification Format (e.g., “stellar:USDC:G…”, “iso4217:USD”)

    sellAmount

    The amount of sell_asset the client would exchange for each of the buy_assets

    sellDeliveryMethod

    Optional, one of the name values specified by the sell_delivery_methods array for the associated asset returned from GET /info. Can be provided if the user is delivering an off-chain asset to the anchor but is not strictly required.

    buyDeliveryMethod

    Optional, one of the name values specified by the buy_delivery_methods array for the associated asset returned from GET /info. Can be provided if the user intends to receive an off-chain asset from the anchor but is not strictly required.

    countryCode

    Optional, the ISO 3166-2 or ISO-3166-1 alpha-2 code of the user’s current address. Should be provided if there are two or more country codes available for the desired asset in GET /info.

    jwt

    Optional JWT token obtained from SEP-10 authentication

    Return Value

    Sep38PricesResponseEnum with indicative prices for available assets, or an error

  • Fetches the indicative price for a given asset pair.

    The caller must provide either sellAmount or buyAmount, but not both.

    See: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0038.md#get-price

    Declaration

    Swift

    public func price(context:String,
                      sellAsset:String,
                      buyAsset:String,
                      sellAmount:String? = nil,
                      buyAmount:String? = nil, 
                      sellDeliveryMethod:String? = nil,
                      buyDeliveryMethod:String? = nil,
                      countryCode:String? = nil,
                      jwt:String? = nil) async -> Sep38PriceResponseEnum

    Parameters

    context

    The context for what this quote will be used for. Must be one of “sep6” or “sep31”.

    sellAsset

    The asset the client would like to sell (e.g., “stellar:USDC:G…”, “iso4217:ARS”)

    buyAsset

    The asset the client would like to exchange for sellAsset

    sellAmount

    Optional, the amount of sellAsset the client would like to exchange for buyAsset

    buyAmount

    Optional, the amount of buyAsset the client would like to exchange for sellAsset

    sellDeliveryMethod

    Optional, one of the name values specified by the sell_delivery_methods array for the associated asset returned from GET /info. Can be provided if the user is delivering an off-chain asset to the anchor but is not strictly required.

    buyDeliveryMethod

    Optional, one of the name values specified by the buy_delivery_methods array for the associated asset returned from GET /info. Can be provided if the user intends to receive an off-chain asset from the anchor but is not strictly required.

    countryCode

    Optional, the ISO 3166-2 or ISO-3166-1 alpha-2 code of the user’s current address. Should be provided if there are two or more country codes available for the desired asset in GET /info.

    jwt

    Optional JWT token obtained from SEP-10 authentication

    Return Value

    Sep38PriceResponseEnum with indicative exchange rate, or an error

  • Requests a firm quote for a Stellar asset and off-chain asset pair.

    Unlike indicative prices, firm quotes are guaranteed by the anchor for a limited time. The returned quote ID can be used in SEP-6 or SEP-31 transactions.

    See: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0038.md#post-quote

    Declaration

    Swift

    public func postQuote(request: Sep38PostQuoteRequest, jwt: String) async -> Sep38QuoteResponseEnum

    Parameters

    request

    Sep38PostQuoteRequest containing asset pair and amount details

    jwt

    JWT token obtained from SEP-10 authentication (required)

    Return Value

    Sep38QuoteResponseEnum with firm quote details including expiration, or an error

  • getQuote(id:jwt:) Asynchronous

    Fetches a previously-provided firm quote by ID.

    Use this to retrieve quote details or check if a quote is still valid before using it.

    See: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0038.md#get-quote

    Declaration

    Swift

    public func getQuote(id: String, jwt: String? = nil) async -> Sep38QuoteResponseEnum

    Parameters

    id

    The quote ID returned from postQuote

    jwt

    Optional JWT token obtained from SEP-10 authentication

    Return Value

    Sep38QuoteResponseEnum with quote details, or an error