diff --git a/docs/building-apps/wallet/sep24.mdx b/docs/building-apps/wallet/sep24.mdx index b12d7e1c8..602516e31 100644 --- a/docs/building-apps/wallet/sep24.mdx +++ b/docs/building-apps/wallet/sep24.mdx @@ -72,9 +72,7 @@ const asset = info.currencies.find(({ code }) => code === "USDC").assetId; :::info -[//]: # "TODO: link to establish trustline guide" - -Before starting with the deposit flow, make sure that the user account has established a trustline for the asset you are working with. +Before starting with the deposit flow, make sure that the user account has [established a trustline](/docs/building-apps/wallet/stellar#modify-assets-trustlines) for the asset you are working with. ::: @@ -294,7 +292,7 @@ This code example will consume all events coming from the channel until it's clo :::info -Events are stored in the channel until they are received, and calling the `receive()` method will block the channel until a message is received. You can read more about how channels work in the [channel documentation].(https://kotlinlang.org/docs/coroutines-and-channels.html#channels). +Events are stored in the channel until they are received, and calling the `receive()` method will block the channel until a message is received. You can read more about how channels work in the [channel documentation](https://kotlinlang.org/docs/coroutines-and-channels.html#channels). ::: @@ -334,6 +332,96 @@ const transactions = await anchor.getTransactionsForAsset({ +## Submitting Withdrawal Transfer + +Previously, we took a look at starting the withdrawal flow. Now, let's take a look at a full example. + +First, start the withdrawal: + + + +```kt +val withdrawal = sep24.withdraw(asset, authToken = token) +``` + + + +Next, open an interactive url : + + + +```kt +val url = withdrawal.url +// open the url +``` + + + +After that we need to wait until the anchor is ready to receive funds. To do so, we will be waiting until transaction reaches `pending_user_transfer_start` status + + + +```kt +val withdrawalWatcher = sep24.watcher().watchOneTransaction(token, withdrawal.id) +var statusChange: StatusUpdateEvent + +// Wait for user input +do { + statusChange = withdrawalWatcher.channel.receive() +} while ( + ((statusChange as? StatusChange) ?: throw Exception("Channel unexpectedly closed")) + .status != TransactionStatus.PENDING_USER_TRANSFER_START +) +``` + + + +Next, sign and submit the Stellar transfer: + + + +```kt +val anchorTransaction = (statusChange.transaction as WithdrawalTransaction) +val transfer = stellar.transaction(keypair).transferWithdrawalTransaction(anchorTransaction, asset).build() + +transfer.sign(keypair) + +stellar.submitTransaction(transfer) +``` + + + +Where `keypair` is the SEP-10 authenticated account. If you want to transfer funds from a different address, refer to [Changing Stellar Transfer Account](#changing-stellar-transfer-account) section. + +Finally, let's track transaction status updates. In this example we simply check if the transaction has been completed: + + + +```kt +var terminalStatus: TransactionStatus? = null + +do { + statusChange = withdrawalWatcher.channel.receive() + + when (statusChange) { + is StatusChange -> { + if (statusChange.status.isTerminal()) { + terminalStatus = statusChange.status + } + } + is ChannelClosed -> println("Transaction tracking finished") + is ExceptionHandlerExit -> + println("Retries exhausted trying obtain transaction data, giving up.") + } +} while (statusChange !is ChannelClosed) + +if (terminalStatus != TransactionStatus.COMPLETED) { + println("Transaction was not completed") +} +``` + + + [sep-9]: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-000p.md [sep-10]: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0010.md [sep-24]: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0024.md