Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PARTNER-63] Add documentation for integrating with the Anchor with code examples #164

Merged
merged 18 commits into from
Jul 7, 2023
2 changes: 1 addition & 1 deletion docs/anchoring-assets/overview.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Stellar has anchor services operating worldwide. View the [Anchor Directory](htt

Anchors can issue their own assets on the Stellar network, or they can honor assets that already exist. To learn about issuing assets, check out the [Issue Assets section](https://developers.stellar.org/docs/category/issue-assets).

This documentation will instruct you on how to become an anchor. To understand how to integrate anchor services into your blockchain-based application, check out the [Build Apps section](https://developers.stellar.org/docs/category/deposit-and-withdraw-from-anchors). If you’re looking specifically for MoneyGram Access, see the [Integrate with MoneyGram Access tutorial](https://developers.stellar.org/docs/tutorials/moneygram-access-integration-guide).
This documentation will instruct you on how to become an anchor. To understand how to integrate anchor services into your blockchain-based application, check out the [Build Apps section](/docs/category/deposit-and-withdraw-from-anchors) and [Connect to Anchors section][/docs/category]. If you’re looking specifically for MoneyGram Access, see the [Integrate with MoneyGram Access tutorial](https://developers.stellar.org/docs/tutorials/moneygram-access-integration-guide).

## Stellar Ecosystem Proposals (SEPs) and interoperability

Expand Down
7 changes: 7 additions & 0 deletions docs/connect-to-anchors/_category_.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"position": 45,
"label": "Become a Wallet",
"link": {
"type": "generated-index"
}
}
167 changes: 167 additions & 0 deletions docs/connect-to-anchors/intro.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
---
title: Getting Started
sidebar_position: 20
---

import { CodeExample } from "@site/src/components/CodeExample";

In this guide we will use wallet SDK to integrate with the Stellar blockchain and connect to anchors.

## Installation

First, you need to add the SDK dependency to your project.

<CodeExample>

```kotlin
// gradle.kts
implementation("org.stellar:wallet-sdk:[version]")
```

</CodeExample>

You can get the latest available version on the [project GitHub page](https://github.com/stellar/kotlin-wallet-sdk)

## Working With The SDK

Let's start with the main class that provides all SDK functionality. It's advised to have a singleton wallet object shared across the app. Creating wallet with default configuration connected to testnet is simple:

<CodeExample>

```kotlin
val wallet = Wallet(StellarConfiguration.Testnet)
```

</CodeExample>

The wallet instance can be further configured. For example, to connect to public main network:

<CodeExample>

```kotlin
val walletMainnet = Wallet(StellarConfiguration(Network.PUBLIC, "https://horizon.stellar.org"))
```

</CodeExample>

There is one more available configuration for wallet that allows to configure internal logic of the SDK. For example, to test with local servers on http protocol, http can be manually enabled.

<CodeExample>

```kotlin
val walletCustom = Wallet(
StellarConfiguration.Testnet,
ApplicationConfiguration { defaultRequest { url { protocol = URLProtocol.HTTP } } }
)
```

</CodeExample>

### Configuring client

The Wallet SDK uses the [ktor client](https://ktor.io/docs/getting-started-ktor-client.html) for all network requests (excluding Horizon, where the Stellar SDK's http client is used). Currently, the okhttp engine is configured to be used with the client. You can read more about how to configure the ktor client [here](https://ktor.io/docs/create-client.html#configure-client). For example, the client can be globally configured:

<CodeExample>

```kotlin
val walletCustomClient =
Wallet(
StellarConfiguration.Testnet,
ApplicationConfiguration(
defaultClientConfig = {
engine { this.config { this.connectTimeout(Duration.ofSeconds(10)) } }
install(HttpRequestRetry) {
retryOnServerErrors(maxRetries = 5)
exponentialDelay()
}
}
)
)
```

</CodeExample>

This code will set the connect timeout to 10 seconds via the [okhttp configuration](https://ktor.io/docs/http-client-engines.html#okhttp) and also installs the [retry plugin](https://ktor.io/docs/client-retry.html). You can also specify client configuration for specific Wallet SDK classes. For example, to change connect timeout when connecting to some anchor server:

<CodeExample>

```kotlin
val anchorCustomClient =
walletCustomClient.anchor("example.com") {
engine { this.config { this.connectTimeout(Duration.ofSeconds(30)) } }
}
```

</CodeExample>

### Closing resources

After the wallet class is no longer used, it's necessary to close all clients used by it. While in some applications it may not be required (e.g. the wallet lives for the whole lifetime of the app), in other cases it can be required. If your wallet class is short-lived, it's recommended to close client resources using close function:

<CodeExample>

```kotlin
fun closeWallet() {
wallet.close()
}
```

</CodeExample>

## Stellar Basics

Wallet SDK provides some extra functionality on top of existing [Horizon SDK]. For interaction with the Stellar network, SDK covers only basics, used in a typical wallet flow. For more advanced use cases underlying [Horizon SDK] should be used instead.

To interact with Horizon instance configured in previous steps, simply do:

<CodeExample>

```kotlin
val stellar = wallet.stellar()
```

</CodeExample>

This example will create a Stellar class, that manages connection to Horizon service.

:::note

Default configuration connects to public Stellar Horizon instance. You can change this behavior as described [above](#working-with-the-sdk)

:::

You can read more about working with Stellar network in the [respective section](./stellar.mdx).

## Anchor Basics

Primarily use of the SDK is to provide an easy way to connect to Anchors via sets of protocols known as SEPs. Let's look into connecting to the Stellar test anchor:

<CodeExample>

```kotlin
val anchor = wallet.anchor("https://testanchor.stellar.org")
```

</CodeExample>

And the most basic interaction of fetching [SEP-1] info file:

<CodeExample>

```kotlin
suspend fun anchorToml(): TomlInfo {
return anchor.getInfo()
}
```

</CodeExample>

Anchor class also supports [stellar authentication] ([SEP-10]) and [hosted deposit and withdrawal] ([SEP-24]) features.

[sep-1]: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0001.md
[sep-10]: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0010.md
[sep-12]: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0012.md
[sep-24]: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0024.md
[stellar authentication]: ./sep10.mdx
[hosted deposit and withdrawal]: ./sep24.mdx
[horizon sdk]: /docs/tools-and-sdks#sdk-library
42 changes: 42 additions & 0 deletions docs/connect-to-anchors/overview.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: Overview
sidebar_position: 10
---

import { CodeExample } from "@site/src/components/CodeExample";

## What is a wallet?

Wallet in application that allows its user to hold assets, such as native Stellar native asset (XLM), stablecoins (USDC) or any other asset issued on chain. Wallets can also help users exchange their assets, sometimes using entities called anchors.

## Custodial vs. Non-Custodial Applications

Some applications, such as centralized exchanges, are custodial, meaning the application has access to the private keys of the acccounts holding its users’ funds on Stellar. Typically, custodial applications pool user funds into a smaller set of managed Stellar accounts, called pooled, shared, or omnibus accounts.

Other applications are non-custodial, meaning the application does not have access to the private keys of the accounts holding its users' funds on Stellar. Typically, non-custodial applications create or import a pre-existing Stellar account for each user.

These two approaches require minor but concrete differences in how applications integrate with MoneyGram Access. The sub-sections below will describe these differences, but the rest of this tutorial will assume the application is custodial.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets remove the reference to MoneyGram and replace it with a generic reference to anchors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yeah a lot of it is copy-paste...
I want to re-word and refactor it later when I have a chance to do so


## What is an anchor?

An anchor is a Stellar-specific term for the on and off-ramps that connect the Stellar network to traditional financial rails, such as financial institutions or fintech companies. Anchors accept deposits of fiat currencies (such as the US dollar, Argentine peso, or Nigerian naira) via existing rails (such as bank deposits or cash-in points), then sends the user the equivalent digital tokens on the Stellar network. The equivalent digital tokens can either represent that same fiat currency or another digital token altogether. Alternatively, anchors allow token holders to redeem their tokens for the real-world assets they represent.

Stellar has anchor services operating worldwide. View the [Anchor Directory](https://resources.stellar.org/anchors?) for more information on existing Stellar anchors.

This documentation will instruct you on how to connect to the anchor as the wallet.

## Stellar Ecosystem Proposals (SEPs) and interoperability

Stellar is an open-source network that is designed to interoperate with traditional financial institutions, various types of assets, and other networks. Network participants implement Stellar Ecosystem Proposals (SEPs) to ensure they can interoperate with other products and services on the network. SEPs are publicly created, open-source documents that live in a [GitHub repository](https://github.com/stellar/stellar-protocol/tree/master/ecosystem#stellar-ecosystem-proposals-seps) and they define how asset issuers, applications, exchanges, and other service providers should interact and interoperate.

Read more about SEPs in the [SEPs section](https://developers.stellar.org/docs/fundamentals-and-concepts/stellar-ecosystem-proposals).

As a wallet, the most important SEPs are [SEP-24]: Hosted Deposit and Withdrawal, and [SEP-31]: Cross Border Payments API. You’ll also work with [SEP-10]: Stellar Authentication, [SEP-12]: KYC API, and [SEP-38]: Anchor RFQ API.

[sep-1]: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0001.md
[sep-9]: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0009.md
[sep-10]: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0010.md
[sep-12]: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0012.md
[sep-24]: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0024.md
[sep-31]: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0031.md
[sep-38]: https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0038.md
Loading