Skip to content

Commit

Permalink
Add SEP-24 withdrawal flow docs (#178)
Browse files Browse the repository at this point in the history
* Add SEP-24 docs

* Fix review comments
  • Loading branch information
Ifropc authored Jul 12, 2023
1 parent 26992e9 commit d13b429
Showing 1 changed file with 92 additions and 4 deletions.
96 changes: 92 additions & 4 deletions docs/building-apps/wallet/sep24.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

:::

Expand Down Expand Up @@ -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).

:::

Expand Down Expand Up @@ -334,6 +332,96 @@ const transactions = await anchor.getTransactionsForAsset({

</CodeExample>

## 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:

<CodeExample>

```kt
val withdrawal = sep24.withdraw(asset, authToken = token)
```

</CodeExample>

Next, open an interactive url :

<CodeExample>

```kt
val url = withdrawal.url
// open the url
```

</CodeExample>

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

<CodeExample>

```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
)
```

</CodeExample>

Next, sign and submit the Stellar transfer:

<CodeExample>

```kt
val anchorTransaction = (statusChange.transaction as WithdrawalTransaction)
val transfer = stellar.transaction(keypair).transferWithdrawalTransaction(anchorTransaction, asset).build()

transfer.sign(keypair)

stellar.submitTransaction(transfer)
```

</CodeExample>

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:

<CodeExample>

```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")
}
```

</CodeExample>

[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
Expand Down

0 comments on commit d13b429

Please sign in to comment.