Sources

Many of the of the APIs made available via Queries are also available in a streaming mode. Access to the streaming endpoints is made available on the interface by way of *Source methods. Whereas queries provide access to historical data, sources can be used to subscribe to updates from the network in near real-time.

Sources are a concept native to Akka Streams. They provide resilient access to upstream data and support backpressure should the data be arriving faster than the application is capable of processing.

Before sources can be used, an actor system and materializer need to be brought into implicit scope.

source//  implicit val system = ActorSystem("stellar-sources")
//  implicit val materializer = ActorMaterializer()
  import scala.concurrent.ExecutionContext.Implicits.global

Once done, sources may be used as a method for subscribing to updates. For example:

source      // print each new transaction's hash
//      TestNetwork.transactionSource().runForeach(txn => println(txn.hash))

      // a source of transactions for a given account
//      val accnTxnSource: Source[TransactionHistory, NotUsed] =
//        TestNetwork.transactionsByAccountSource(publicKey)

      // a source of transactions for ledger #3,
      // started from the beginning of time to ensure we get everything
//      val ledgerTxnSource = TestNetwork.transactionsByLedgerSource(1, Record(0))

Like queries, sources fall into several categories.

Effects

s are the changes that have been effected on the network as a result of operations successfully processed.

source      // a source of all new effects
//      val effectsSource: Source[EffectResponse, NotUsed] = TestNetwork.effectsSource()

      // a source of all new effects for a given account
//      val effectsForAccountSource = TestNetwork.effectsByAccountSource(publicKey)

Ledgers

s represent the state of the network at any time. They are created sequentially as the state of the network changes.

source      // a source of all new ledgers
//      val ledgersSource: Source[LedgerResponse, NotUsed] = TestNetwork.ledgersSource()

Offers

s can be issued by accounts to buy or sell assets. Sources for offers is available only by account.

source//      val offersByAccountSource: Source[OfferResponse, NotUsed] =
//        TestNetwork.offersByAccountSource(publicKey)

Operations

s are changes to the ledger. They represent the action, as opposed to the effects resulting from the action.

Operations returned by these queries are wrapped in the type. This indicates that the operation has been part of a successful transaction, and provides details about that transaction.

source      // a source of all new operations
//      val operationsSource: Source[Transacted[Operation], NotUsed] = TestNetwork.operationsSource()

      // a source of all new operations involving a specified account
//      val operationsByAccountSource = TestNetwork.operationsByAccountSource(publicKey)

OrderBooks

s include all the offers to buy or sell a specific asset. The source for an orderbook will present offers for that pair.

source//      val beerForHugsBigOrderBookSource: Source[OrderBook, NotUsed] =
//        TestNetwork.orderBookSource(
//          selling = Asset("FabulousBeer", publicKey),
//          buying = Asset("HUG", publicKey),
//        )

Payments

s are the subset of Operations that cause payments to be made to an account. This is similar to the Operations query methods, but will only return CreateAccount and Payment operations.

source      // a source of all new payment operations
//      val paymentsSource: Source[Transacted[PayOperation], NotUsed] = TestNetwork.paymentsSource()

      // a source of all new payment operations involving a specified account
//      val paymentsByAccountSource = TestNetwork.paymentsByAccountSource(publicKey)

Transactions

Transactions are the fundamental unit of change in the network and are composed of at least one and at most 100 operations. These sources stream validated transactions, in the form of responses (as opposed to transactions that are composed and submitted to the network).

source      // print each new transaction's hash
//      TestNetwork.transactionSource().runForeach(txn => println(txn.hash))

      // a source of transactions for a given account
//      val accnTxnSource: Source[TransactionHistory, NotUsed] =
//        TestNetwork.transactionsByAccountSource(publicKey)

      // a source of transactions for ledger #3,
      // started from the beginning of time to ensure we get everything
//      val ledgerTxnSource = TestNetwork.transactionsByLedgerSource(1, Record(0))

Continue reading to learn how to query for information on organizations that use the Stellar network via Domains.