|
| 1 | + |
| 2 | +# SEP-0024 - TransferServerSEP24Service |
| 3 | + |
| 4 | +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). |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +## Create a TransferServerSEP24Service instance |
| 9 | + |
| 10 | +**By providing the domain hosting the stellar.toml file** |
| 11 | + |
| 12 | +```dart |
| 13 | +final transferService = await TransferServerSEP24Service.fromDomain("place.domain.com"); |
| 14 | +``` |
| 15 | + |
| 16 | +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. |
| 17 | + |
| 18 | +**Or by providing the service url** |
| 19 | + |
| 20 | +Alternatively one can create a TransferServerSEP24Service instance by providing the transfer server url directly via the constructor: |
| 21 | + |
| 22 | +```dart |
| 23 | +final transferService = TransferServerSEP24Service("http://api.stellar-anchor.org/transfer"); |
| 24 | +``` |
| 25 | + |
| 26 | +## Get Anchor Information |
| 27 | + |
| 28 | +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). |
| 29 | + |
| 30 | +```dart |
| 31 | +SEP24InfoResponse infoResponse = await transferService.info(); |
| 32 | +``` |
| 33 | + |
| 34 | +## Fee |
| 35 | + |
| 36 | +If there is a fee and the fee schedule is not complex, the info response already contains the fee data for a given asset. |
| 37 | + |
| 38 | +```dart |
| 39 | +double? feeFixed = infoResponse.depositAssets?["USDC"]?.feeFixed; |
| 40 | +double? feePercent = infoResponse.depositAssets?["USDC"]?.feePercent; |
| 41 | +double? feeMinimum = infoResponse.depositAssets?["USDC"]?.feeMinimum; |
| 42 | +print("USDC fixed fee for deposit: $feeFixed"); |
| 43 | +print("USDC percentage fee for deposit: $feePercent"); |
| 44 | +print("USDC minimum fee for deposit: $feeMinimum"); |
| 45 | +``` |
| 46 | + |
| 47 | +Otherwise, one can check if the fee endpoint of the anchor is enabled and if so, request the fee from there. |
| 48 | + |
| 49 | +```dart |
| 50 | +bool feeEndpointEnabled = infoResponse.feeEndpointInfo?.enabled == true; |
| 51 | +if (feeEndpointEnabled) { |
| 52 | + SEP24FeeRequest feeRequest = SEP24FeeRequest(); |
| 53 | + feeRequest.operation = "deposit"; |
| 54 | + feeRequest.type = "SEPA"; |
| 55 | + feeRequest.assetCode = "USD"; |
| 56 | + feeRequest.amount = 2034.09; |
| 57 | + feeRequest.jwt = jwtToken; |
| 58 | + |
| 59 | + SEP24FeeResponse feeResponse = await transferService.fee(feeRequest); |
| 60 | + double? fee = feeResponse.fee; |
| 61 | + print("fee : $fee"); |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +## Interactive Flows |
| 66 | + |
| 67 | +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). |
| 68 | +We will use the jwt token in the examples below as the SEP-10 authentication token, obtained earlier. |
| 69 | + |
| 70 | +### Deposit |
| 71 | +To initiate an operation, we need to know the asset code. |
| 72 | + |
| 73 | +```dart |
| 74 | +SEP24DepositRequest request = new SEP24DepositRequest(); |
| 75 | +request.assetCode = "USDC"; |
| 76 | +request.jwt = jwtToken; |
| 77 | +
|
| 78 | +SEP24InteractiveResponse response = await transferService.deposit(request); |
| 79 | +``` |
| 80 | + |
| 81 | +As a result, you will get an interactive response from the anchor. |
| 82 | +Open the received URL in an iframe and deposit the transaction ID for future reference: |
| 83 | + |
| 84 | +```dart |
| 85 | +String url = response.url; |
| 86 | +String id = response.id; |
| 87 | +``` |
| 88 | + |
| 89 | +### Withdraw |
| 90 | + |
| 91 | +Similarly to the deposit flow, a basic withdrawal flow has the same method signature and response type: |
| 92 | + |
| 93 | +```dart |
| 94 | +SEP24WithdrawRequest request = new SEP24WithdrawRequest(); |
| 95 | +request.assetCode = "USDC"; |
| 96 | +request.type = "bank_account"; |
| 97 | +request.jwt = jwtToken; |
| 98 | +
|
| 99 | +SEP24InteractiveResponse response = await transferService.withdraw(request); |
| 100 | +``` |
| 101 | + |
| 102 | +As a result, you will get an interactive response from the anchor. |
| 103 | +Open the received URL in an iframe and deposit the transaction ID for future reference: |
| 104 | + |
| 105 | +```dart |
| 106 | +String url = response.url; |
| 107 | +String id = response.id; |
| 108 | +``` |
| 109 | + |
| 110 | +### Providing KYC Info |
| 111 | +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). |
| 112 | +In turn, the anchor will pre-fill this information in the interactive popup. |
| 113 | + |
| 114 | +```dart |
| 115 | +SEP24DepositRequest request = new SEP24DepositRequest(); |
| 116 | +request.assetCode = "USDC"; |
| 117 | +request.jwt = jwtToken; |
| 118 | +
|
| 119 | +StandardKYCFields kycFields = StandardKYCFields(); |
| 120 | +kycFields.naturalPersonKYCFields = NaturalPersonKYCFields(); |
| 121 | +kycFields.naturalPersonKYCFields!.emailAddress = "[email protected]"; |
| 122 | +kycFields.naturalPersonKYCFields!.photoIdFront = await Util.readFile(path); |
| 123 | +
|
| 124 | +request.kycFields = kycFields; |
| 125 | +
|
| 126 | +SEP24InteractiveResponse response = await transferService.deposit(request); |
| 127 | +``` |
| 128 | + |
| 129 | +### Changing Stellar Transfer Account |
| 130 | + |
| 131 | +By default, the Stellar transfer will be sent to the authenticated account (with a memo) that initiated the deposit. |
| 132 | + |
| 133 | +While in most cases it's acceptable, some wallets may split their accounts. To do so, pass additional account (and optionally a memo): |
| 134 | + |
| 135 | +```dart |
| 136 | +SEP24DepositRequest request = new SEP24DepositRequest(); |
| 137 | +request.assetCode = "USDC"; |
| 138 | +request.account = "G..."; |
| 139 | +request.memo = "my memo"; |
| 140 | +request.memoType = "text"; |
| 141 | +request.jwt = jwtToken; |
| 142 | +
|
| 143 | +
|
| 144 | +SEP24InteractiveResponse response = await transferService.deposit(request); |
| 145 | +``` |
| 146 | +Similarly, for a withdrawal, the origin account of the Stellar transaction could be changed. |
| 147 | + |
| 148 | + |
| 149 | +## Getting Transaction Info |
| 150 | + |
| 151 | +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. |
| 152 | + |
| 153 | +```dart |
| 154 | +SEP24TransactionsRequest request = SEP24TransactionsRequest(); |
| 155 | +request.assetCode = "ETH"; |
| 156 | +request.jwt = jwtToken; |
| 157 | +
|
| 158 | +SEP24TransactionsResponse response = await transferService.transactions(request); |
| 159 | +List<SEP24Transaction> transactions = response.transactions; |
| 160 | +``` |
| 161 | + |
| 162 | +Single Transaction: |
| 163 | + |
| 164 | +```dart |
| 165 | +SEP24TransactionRequest request = SEP24TransactionRequest(); |
| 166 | +request.stellarTransactionId = "17a670bc424ff5ce3b386dbfaae9990b66a2a37b4fbe51547e8794962a3f9e6a"; |
| 167 | +request.jwt = jwtToken; |
| 168 | +
|
| 169 | +SEP24TransactionResponse response = await transferService.transaction(request); |
| 170 | +SEP24Transaction transaction = response.transaction; |
| 171 | +``` |
| 172 | + |
| 173 | +### Further readings |
| 174 | + |
| 175 | +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). |
| 176 | + |
0 commit comments