This guide is available on four different programming languages: Typescript, Kotlin, Flutter (Dart) and Swift. You can change the shown version on each page via the buttons above.
Stellar object that provides a connection to Horizon. In this section, we will look at the usages of this class.
Accounts
The most basic entity on the Stellar network is an account. Let’s look into AccountService that provides the capability to work with accounts:- Kotlin
- TypeScript
- Flutter
- Swift
- Kotlin
- TypeScript
- Flutter
- Swift
If using react-native,
createKeypair won’t work. Instead use the helper method createKeypairFromRandom like this:Build Transaction
The transaction builder allows you to create various transactions that can be signed and submitted to the Stellar network. Some transactions can be sponsored.Building Basic Transactions
First, let’s look into building basic transactions.Create Account
The create account transaction activates/creates an account with a starting balance of XLM (1 XLM by default).- Kotlin
- TypeScript
- Flutter
- Swift
Modify Account
You can lock the master key of the account by setting its weight to 0. Use caution when locking the account’s master key. Make sure you have set the correct signers and weights. Otherwise, you will lock the account irreversibly.- Kotlin
- TypeScript
- Flutter
- Swift
- Kotlin
- TypeScript
- Flutter
- Swift
- Kotlin
- TypeScript
- Flutter
- Swift
- Kotlin
- TypeScript
- Flutter
- Swift
Modify Assets (Trustlines)
Add an asset (trustline) to the account. This allows the account to receive transfers of the asset.- Kotlin
- TypeScript
- Flutter
- Swift
- Kotlin
- TypeScript
- Flutter
- Swift
Swap
Exchange an account’s asset for a different asset. The account must have a trustline for the destination asset. A swap is filled at whatever rate the network can find when the transaction is applied, which may differ from the rate you quoted to the user. The minimum destination amount (destMin) is the slippage limit that protects against that: it is the least amount of the destination asset the account is willing to receive for the amount it sends. If the swap cannot deliver at least that much, the transaction fails instead of executing at a worse rate. Derive it from a current quote minus the slippage tolerance you are willing to accept.
- TypeScript
- Flutter
- Swift
Path Pay
Send one asset from the source account and receive a different asset in the destination account. Like a swap, a path payment is filled at the rate available when the transaction is applied, so it takes a slippage limit. Which limit depends on which side of the payment is fixed:- When you fix the amount to send (
sendAmount), passdestMin: the least amount of the destination asset the recipient must receive. - When you fix the amount to deliver (
destAmount), passsendMax: the maximum amount of the send asset the source account is willing to spend.
- TypeScript
- Flutter
- Swift
destAmount together with sendMax:
- TypeScript
- Flutter
- Swift
Set Memo
Set a memo on the transaction. The memo object can be imported from “@stellar/stellar-sdk”.- TypeScript
- Flutter
- Swift
Account Merge
Merges account into a destination account.- TypeScript
- Flutter
- Swift
Fund Testnet Account
Fund an account on the Stellar test network- TypeScript
- Flutter
- Swift
Building Advanced Transactions
In some cases a private key may not be known prior to forming a transaction. For example, a new account must be funded to exist and the wallet may not have the key for the account so may request the create account transaction to be sponsored by a third party.- Kotlin
- TypeScript
- Flutter
- Swift
- Kotlin
- TypeScript
- Flutter
- Swift
externalKeyPair) to be signed.
- Kotlin
- TypeScript
- Flutter
- Swift
You can read more about passing XDR transaction to the server in the chapter below.
- Kotlin
- TypeScript
- Flutter
- Swift
- Kotlin
- TypeScript
- Flutter
- Swift
Adding an Operation
Add a custom Operation to a transaction. This can be any Operation supported by the Stellar network. The Operation object can be imported from “@stellar/stellar-sdk”.- TypeScript
- Flutter
- Swift
Sponsoring Transactions
Sponsor Operations
Some operations, that modify account reserves can be sponsored. For sponsored operations, the sponsoring account will be paying for the reserves instead of the account that being sponsored. This allows you to do some operations, even if account doesn’t have enough funds to perform such operations. To sponsor a transaction, simply start asponsoring block (Kotlin), or simply create a building function (describing which operations are to be sponsored) and pass it to the sponsoring method (TypeScript):
- Kotlin
- TypeScript
- Flutter
- Swift
Only some operations can be sponsored, and a sponsoring block (Kotlin) / builder (TypeScript) has a slightly different set of functions available compared to the regular
TransactionBuilder. Note, that a transaction must be signed by both the sponsor account (sponsoringKeyPair) and the account being sponsored (sponsoredKeyPair).Sponsoring Account Creation
One of the things that can be done via sponsoring is to create an account with a 0 starting balance. This account creation can be created by simply writing:- Kotlin
- TypeScript
- Flutter
- Swift
sponsoredKeyPair. Due to this, we did not need to pass a sponsored account value to the sponsoring block (Kotlin) / method (TypeScript). Since when ommitted, the sponsored account defaults to the transaction source account (sponsoredKeyPair).
However, this time, the sponsored account (freshly created newKeyPair) is different from the transaction source account. Therefore, it’s necessary to specify it. Otherwise, the transaction will contain a malformed operation. As before, the transaction must be signed by both keys.
Sponsoring Account Creation and Modification
If you want to create an account and modify it in one transaction, it’s possible to do so with passing asponsoredAccount optional argument to the sponsored block (Kotlin) / sponsoring method (TypeScript) (newKeyPair below). If this argument is present, all operations inside the sponsored block will be sourced by this sponsoredAccount. (Except account creation, which is always sourced by the sponsor).
- Kotlin
- TypeScript
- Flutter
- Swift
Fee-Bump Transaction
If you wish to modify a newly created account with a 0 balance, it’s also possible to do so viaFeeBump. It can be combined with a sponsoring block (Kotlin) / method (TypeScript) to achieve the same result as in the example above. However, with FeeBump it’s also possible to add more operations (that don’t require sponsoring), such as a transfer.
First, let’s create a transaction that will replace the master key of an account with a new keypair.
- Kotlin
- TypeScript
- Flutter
- Swift
- Kotlin
- TypeScript
- Flutter
- Swift
- Kotlin
- TypeScript
- Flutter
- Swift
sponsorKeyPair and sponsoredKeyPair and may not even have any XLM funds on its account.
- Kotlin
- TypeScript
- Flutter
- Swift
Using XDR to Send Transaction Data
Note, that a wallet may not have a signing key forsponsorKeyPair. In that case, it’s necessary to convert the transaction to XDR, send it to the server, containing sponsorKey and return the signed transaction back to the wallet. Let’s use the previous example of sponsoring account creation, but this time with the sponsor key being unknown to the wallet. The first step is to define the public key of the sponsor keypair:
- Kotlin
- TypeScript
- Flutter
- Swift
newKeyPair. This time, convert the transaction to XDR:
- Kotlin
- TypeScript
- Flutter
- Swift
- Kotlin
- TypeScript
- Flutter
- Swift
- Kotlin
- TypeScript
- Flutter
- Swift
Submit Transaction
It’s strongly recommended to use the wallet SDK transaction submission functions instead of Horizon alternatives. The wallet SDK gracefully handles timeout and out-of-fee exceptions.
- Kotlin
- TypeScript
- Flutter
- Swift
- Kotlin
- TypeScript
- Flutter
- Swift
sourceAccountKeyPair. Every 30 seconds this function will re-construct this transaction with a new fee (increased by 100 stroops), repeating signing and submitting. Once the transaction is successful, the function will return the transaction body. Note, that any other error will terminate the retry cycle and an exception will be thrown.
Accessing Horizon SDK
It’s very simple to use the Horizon SDK connecting to the same Horizon instance as aWallet class. To do so, simply call:
- Kotlin
- TypeScript
- Flutter
- Swift
- Kotlin
- TypeScript
- Flutter
- Swift