Skip to content

Commit

Permalink
add example for sep24
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-rogobete committed Jul 11, 2023
1 parent eb245b6 commit 017499a
Show file tree
Hide file tree
Showing 6 changed files with 211 additions and 30 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [1.5.8] - 11.Jul.2023.
- add SEP-24 support
-
## [1.5.7] - 10.Jul.2023.
- make streaming indefinite
- fix names in XdrSCSpec classes
Expand Down
58 changes: 30 additions & 28 deletions README.md

Large diffs are not rendered by default.

Binary file modified documentation/sdk_api_doc.zip
Binary file not shown.
176 changes: 176 additions & 0 deletions documentation/sdk_examples/sep-0024.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@

# SEP-0024 - TransferServerSEP24Service

Helps clients to interact with anchors in a standard way defined by [SEP-0024: Hosted Deposit and Withdrawal](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0024.md).



## Create a TransferServerSEP24Service instance

**By providing the domain hosting the stellar.toml file**

```dart
final transferService = await TransferServerSEP24Service.fromDomain("place.domain.com");
```

This will automatically load and parse the stellar.toml file. It will then create the TransferServerSEP24Service instance by using the transfer server url provided in the stellar.toml file.

**Or by providing the service url**

Alternatively one can create a TransferServerSEP24Service instance by providing the transfer server url directly via the constructor:

```dart
final transferService = TransferServerSEP24Service("http://api.stellar-anchor.org/transfer");
```

## Get Anchor Information

First, let's get the information about the anchor's support for [SEP-24](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0024.md). This request doesn't require authentication, and will return generic info, such as supported currencies, and features supported by the anchor. You can get a full list of returned fields in the [SEP-24 specification](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0024.md#info).

```dart
SEP24InfoResponse infoResponse = await transferService.info();
```

## Fee

If there is a fee and the fee schedule is not complex, the info response already contains the fee data for a given asset.

```dart
double? feeFixed = infoResponse.depositAssets?["USDC"]?.feeFixed;
double? feePercent = infoResponse.depositAssets?["USDC"]?.feePercent;
double? feeMinimum = infoResponse.depositAssets?["USDC"]?.feeMinimum;
print("USDC fixed fee for deposit: $feeFixed");
print("USDC percentage fee for deposit: $feePercent");
print("USDC minimum fee for deposit: $feeMinimum");
```

Otherwise, one can check if the fee endpoint of the anchor is enabled and if so, request the fee from there.

```dart
bool feeEndpointEnabled = infoResponse.feeEndpointInfo?.enabled == true;
if (feeEndpointEnabled) {
SEP24FeeRequest feeRequest = SEP24FeeRequest();
feeRequest.operation = "deposit";
feeRequest.type = "SEPA";
feeRequest.assetCode = "USD";
feeRequest.amount = 2034.09;
feeRequest.jwt = jwtToken;
SEP24FeeResponse feeResponse = await transferService.fee(feeRequest);
double? fee = feeResponse.fee;
print("fee : $fee");
}
```

## Interactive Flows

Before getting started, make sure you have connected to the anchor and received an authentication token, by using the SDKs [WebAuthService](https://github.com/Soneso/stellar_flutter_sdk/blob/master/documentation/sdk_examples/sep-0010-webauth.md).
We will use the jwt token in the examples below as the SEP-10 authentication token, obtained earlier.

### Deposit
To initiate an operation, we need to know the asset code.

```dart
SEP24DepositRequest request = new SEP24DepositRequest();
request.assetCode = "USDC";
request.jwt = jwtToken;
SEP24InteractiveResponse response = await transferService.deposit(request);
```

As a result, you will get an interactive response from the anchor.
Open the received URL in an iframe and deposit the transaction ID for future reference:

```dart
String url = response.url;
String id = response.id;
```

### Withdraw

Similarly to the deposit flow, a basic withdrawal flow has the same method signature and response type:

```dart
SEP24WithdrawRequest request = new SEP24WithdrawRequest();
request.assetCode = "USDC";
request.type = "bank_account";
request.jwt = jwtToken;
SEP24InteractiveResponse response = await transferService.withdraw(request);
```

As a result, you will get an interactive response from the anchor.
Open the received URL in an iframe and deposit the transaction ID for future reference:

```dart
String url = response.url;
String id = response.id;
```

### Providing KYC Info
To improve the user experience, the SEP-24 standard supports passing user KYC to the anchor via [SEP-9](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0009.md).
In turn, the anchor will pre-fill this information in the interactive popup.

```dart
SEP24DepositRequest request = new SEP24DepositRequest();
request.assetCode = "USDC";
request.jwt = jwtToken;
StandardKYCFields kycFields = StandardKYCFields();
kycFields.naturalPersonKYCFields = NaturalPersonKYCFields();
kycFields.naturalPersonKYCFields!.emailAddress = "[email protected]";
kycFields.naturalPersonKYCFields!.photoIdFront = await Util.readFile(path);
request.kycFields = kycFields;
SEP24InteractiveResponse response = await transferService.deposit(request);
```

### Changing Stellar Transfer Account

By default, the Stellar transfer will be sent to the authenticated account (with a memo) that initiated the deposit.

While in most cases it's acceptable, some wallets may split their accounts. To do so, pass additional account (and optionally a memo):

```dart
SEP24DepositRequest request = new SEP24DepositRequest();
request.assetCode = "USDC";
request.account = "G...";
request.memo = "my memo";
request.memoType = "text";
request.jwt = jwtToken;
SEP24InteractiveResponse response = await transferService.deposit(request);
```
Similarly, for a withdrawal, the origin account of the Stellar transaction could be changed.


## Getting Transaction Info

On the typical flow, the wallet would get transaction data to notify users about status updates. This is done via the SEP-24 GET /transaction and GET /transactions endpoint.

```dart
SEP24TransactionsRequest request = SEP24TransactionsRequest();
request.assetCode = "ETH";
request.jwt = jwtToken;
SEP24TransactionsResponse response = await transferService.transactions(request);
List<SEP24Transaction> transactions = response.transactions;
```

Single Transaction:

```dart
SEP24TransactionRequest request = SEP24TransactionRequest();
request.stellarTransactionId = "17a670bc424ff5ce3b386dbfaae9990b66a2a37b4fbe51547e8794962a3f9e6a";
request.jwt = jwtToken;
SEP24TransactionResponse response = await transferService.transaction(request);
SEP24Transaction transaction = response.transaction;
```

### Further readings

For more info, see also the class documentation of [TransferServerSEP24Service](https://github.com/Soneso/stellar_flutter_sdk/blob/master/lib/src/sep/0024/sep24_service.dart) and the SDK's [SEP-24 test cases](https://github.com/Soneso/stellar_flutter_sdk/blob/master/test/sep0024_test.dart).

2 changes: 1 addition & 1 deletion lib/src/stellar_sdk.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import 'requests/liquidity_pools_request_builder.dart';

/// Main class of the flutter stellar sdk.
class StellarSDK {
static const versionNumber = "1.5.7";
static const versionNumber = "1.5.8";

static final StellarSDK PUBLIC = StellarSDK("https://horizon.stellar.org");
static final StellarSDK TESTNET = StellarSDK("https://horizon-testnet.stellar.org");
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: stellar_flutter_sdk
description: A stellar blockchain sdk that query's horizon, build, signs and submits transactions to the stellar network.
version: 1.5.7
version: 1.5.8
homepage: https://github.com/Soneso/stellar_flutter_sdk

environment:
Expand Down

0 comments on commit 017499a

Please sign in to comment.