Operation

public class Operation : @unchecked Sendable

Base class for all Stellar operations.

Operations are the building blocks of Stellar transactions. Each operation represents a specific action on the Stellar network such as sending payments, creating accounts, or managing offers. Operations are grouped into transactions and submitted to the network.

You should never instantiate this class directly. Instead, use one of its subclasses that represent specific operation types. Each operation can optionally specify a source account that differs from the transaction’s source account.

Common operation types:

  • PaymentOperation: Send assets between accounts
  • CreateAccountOperation: Create and fund new accounts
  • ChangeTrustOperation: Establish trustlines for assets
  • ManageSellOfferOperation: Create or modify sell offers
  • ManageBuyOfferOperation: Create or modify buy offers
  • SetOptionsOperation: Configure account settings
  • And many more…

Operation-level source accounts: When an operation specifies a source account, that account will be used as the source for that specific operation instead of the transaction’s source account. This is useful for multi-signature transactions or channel accounts.

Example:

// Payment operation using transaction source account
let payment1 = try PaymentOperation(
    sourceAccountId: nil,
    destinationAccountId: "GDEST...",
    asset: Asset(type: AssetType.ASSET_TYPE_NATIVE),
    amount: 100.0
)

// Payment operation using different source account
let payment2 = try PaymentOperation(
    sourceAccountId: "GSOURCE...",
    destinationAccountId: "GDEST...",
    asset: Asset(type: AssetType.ASSET_TYPE_NATIVE),
    amount: 50.0
)

See also:

  • The source account for this operation. If nil, uses the transaction’s source account.

    Declaration

    Swift

    public let sourceAccountId: String?
  • The XDR representation of the source account (supports muxed accounts).

    Declaration

    Swift

    public let sourceAccountXdr: MuxedAccountXDR?
  • Creates a new operation object.

    Declaration

    Swift

    public init(sourceAccountId: String?)

    Parameters

    sourceAccountId

    Optional source account for this operation. If provided, must be a valid account ID (G-address or M-address). If nil or invalid, the transaction’s source account will be used.

  • Generates Operation XDR object.

    Declaration

    Swift

    public func toXDR() throws -> OperationXDR
  • Creates a new Operation object from the given OperationXDR object.

    Throws

    Throws StellarSDKError.invalidArgument error if the given OperationXDR object has an unknown type.

    Declaration

    Swift

    public static func fromXDR(operationXDR: OperationXDR) throws -> Operation

    Parameters

    operationXDR

    the OperationXDR object to be used for creating the new Operation object.

  • Encodes the operation to a base64-encoded XDR string for serialization or transmission.

    Declaration

    Swift

    public func toXDRBase64() throws -> String