Skip to content

Commit

Permalink
[ANCHOR-809][SEP-6] Add funding_method to SEP-6 (#1582)
Browse files Browse the repository at this point in the history
### Description

- In `/info` response, replace `fields`(in deposit) and `types`(in
withdraw) with `funding_methods`
- In all 4 transaction creation request, replace `type` with
`funding_method`, and this param will be mandatory

### Context

SEP-31 changes scoped in
stellar/stellar-protocol#1567

### Testing

- `./gradlew test`
  • Loading branch information
JiahuiWho authored Dec 2, 2024
1 parent 9eeedb8 commit e67e65a
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@
*/
@SuperBuilder
@NoArgsConstructor
public class GetTransactionResponse extends PlatformTransactionData {}
public class GetTransactionResponse extends PlatformTransactionData {
String fundingMethod;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.stellar.anchor.api.sep.sep6;

import com.google.gson.annotations.SerializedName;
import java.util.List;
import java.util.Map;
import lombok.Builder;
import lombok.Data;
Expand Down Expand Up @@ -77,13 +78,16 @@ public static class DepositAssetResponse {
@SerializedName("max_amount")
Long maxAmount;

@SerializedName("funding_methods")
List<String> fundingMethods;

/**
* The fields required to initiate a deposit.
*
* <p>The only field supported by the platform is <code>type</code>. Additional fields required
* for KYC are supplied asynchronously through SEP-12 requests.
*/
Map<String, AssetInfo.Field> fields;
@Deprecated Map<String, AssetInfo.Field> fields;
}

/** Withdrawal configuration. */
Expand All @@ -109,14 +113,18 @@ public static class WithdrawAssetResponse {
@SerializedName("max_amount")
Long maxAmount;

/** The maximum amount that can be withdrawn. */
@SerializedName("funding_methods")
List<String> fundingMethods;

/**
* The types of withdrawal methods supported and their fields.
*
* <p>The platform does not allow fields to be configured for withdrawal methods. Financial
* account and KYC information is supplied asynchronously through PATCH requests and SEP-12
* requests respectively.
*/
Map<String, WithdrawType> types;
@Deprecated Map<String, WithdrawType> types;
}

/** Withdrawal type configuration. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ public class StartDepositExchangeRequest {
/** The memo to use for the deposit. */
String memo;

/** Type of deposit. */
@NonNull String type;
/** Deposit method used for the transaction. */
@SerializedName("funding_method")
String fundingMethod;

/** Type of deposit. Deprecated in favor of funding_method */
@Deprecated String type;

/**
* Defaults to en if not specified or if the specified language is not supported. Currently,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ public class StartDepositRequest {
@SerializedName("email_address")
String emailAddress;

/** Type of deposit. */
String type;
/** Deposit method used for the transaction. */
@SerializedName("funding_method")
String fundingMethod;

/** Type of deposit. Deprecated in favor of funding_method */
@Deprecated String type;

/** Name of wallet to deposit to. Currently, ignored. */
@SerializedName("wallet_name")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,12 @@ public class StartWithdrawExchangeRequest {
/** The amount of the source asset the user would like to withdraw. */
@NonNull String amount;

/** The type of withdrawal to make. */
@NonNull String type;
/** Withdraw method used for the transaction. */
@SerializedName("funding_method")
String fundingMethod;

/** The type of withdrawal to make. Deprecated in favor of funding_method */
@Deprecated String type;

/** The ISO 3166-1 alpha-3 code of the user's current address. */
@SerializedName("country_code")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ public class StartWithdrawRequest {
@NonNull
String assetCode;

/** Type of withdrawal. */
String type;
/** Withdraw method used for the transaction. */
@SerializedName("funding_method")
String fundingMethod;

/** Type of withdrawal. Deprecated in favor of funding_method */
@Deprecated String type;

/** The account to withdraw from. */
String account;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.stellar.anchor.api.asset.StellarAssetInfo;
import org.stellar.anchor.api.exception.*;
import org.stellar.anchor.asset.AssetService;
import org.stellar.anchor.util.StringHelper;
import org.stellar.sdk.KeyPair;

/** SEP-6 request validations */
Expand Down Expand Up @@ -91,6 +92,12 @@ public void validateAmount(
*/
public void validateTypes(String requestType, String assetCode, List<String> validTypes)
throws SepValidationException {
if (StringHelper.isEmpty(requestType)) {
throw new SepValidationException(
String.format(
"this field cannot be null or empty for asset %s, supported types are %s",
assetCode, validTypes));
}
if (!validTypes.contains(requestType)) {
throw new SepValidationException(
String.format(
Expand Down
38 changes: 23 additions & 15 deletions core/src/main/java/org/stellar/anchor/sep6/Sep6Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ public StartDepositResponse deposit(Sep10Jwt token, StartDepositRequest request)
}

StellarAssetInfo asset = requestValidator.getDepositAsset(request.getAssetCode());
if (request.getType() != null) {
requestValidator.validateTypes(
request.getType(), asset.getCode(), asset.getSep6().getDeposit().getMethods());
}
String fundingMethod =
request.getFundingMethod() != null ? request.getFundingMethod() : request.getType();
requestValidator.validateTypes(
fundingMethod, asset.getCode(), asset.getSep6().getDeposit().getMethods());

if (request.getAmount() != null) {
requestValidator.validateAmount(
request.getAmount(),
Expand All @@ -130,7 +131,7 @@ public StartDepositResponse deposit(Sep10Jwt token, StartDepositRequest request)
.transactionId(id)
.status(SepTransactionStatus.INCOMPLETE.toString())
.kind(Sep6Transaction.Kind.DEPOSIT.toString())
.type(request.getType())
.type(fundingMethod)
.assetCode(request.getAssetCode())
.assetIssuer(asset.getIssuer())
.amountExpected(request.getAmount())
Expand Down Expand Up @@ -186,8 +187,10 @@ public StartDepositResponse depositExchange(Sep10Jwt token, StartDepositExchange
}

StellarAssetInfo buyAsset = requestValidator.getDepositAsset(request.getDestinationAsset());
String fundingMethod =
request.getFundingMethod() != null ? request.getFundingMethod() : request.getType();
requestValidator.validateTypes(
request.getType(), buyAsset.getCode(), buyAsset.getSep6().getDeposit().getMethods());
fundingMethod, buyAsset.getCode(), buyAsset.getSep6().getDeposit().getMethods());
requestValidator.validateAmount(
request.getAmount(),
buyAsset.getCode(),
Expand Down Expand Up @@ -224,7 +227,7 @@ public StartDepositResponse depositExchange(Sep10Jwt token, StartDepositExchange
.transactionId(id)
.status(SepTransactionStatus.INCOMPLETE.toString())
.kind(Sep6Transaction.Kind.DEPOSIT_EXCHANGE.toString())
.type(request.getType())
.type(fundingMethod)
.assetCode(buyAsset.getCode())
.assetIssuer(buyAsset.getIssuer())
.amountIn(amounts.getAmountIn())
Expand Down Expand Up @@ -281,10 +284,11 @@ public StartWithdrawResponse withdraw(Sep10Jwt token, StartWithdrawRequest reque
}

StellarAssetInfo asset = requestValidator.getWithdrawAsset(request.getAssetCode());
if (request.getType() != null) {
requestValidator.validateTypes(
request.getType(), asset.getCode(), asset.getSep6().getWithdraw().getMethods());
}
String fundingMethod =
request.getFundingMethod() != null ? request.getFundingMethod() : request.getType();
requestValidator.validateTypes(
fundingMethod, asset.getCode(), asset.getSep6().getWithdraw().getMethods());

if (request.getAmount() != null) {
requestValidator.validateAmount(
request.getAmount(),
Expand All @@ -304,7 +308,7 @@ public StartWithdrawResponse withdraw(Sep10Jwt token, StartWithdrawRequest reque
.transactionId(id)
.status(SepTransactionStatus.INCOMPLETE.toString())
.kind(Sep6Transaction.Kind.WITHDRAWAL.toString())
.type(request.getType())
.type(fundingMethod)
.assetCode(request.getAssetCode())
.assetIssuer(asset.getIssuer())
.amountIn(request.getAmount())
Expand Down Expand Up @@ -357,8 +361,10 @@ public StartWithdrawResponse withdrawExchange(
}

StellarAssetInfo sellAsset = requestValidator.getWithdrawAsset(request.getSourceAsset());
String fundingMethod =
request.getFundingMethod() != null ? request.getFundingMethod() : request.getType();
requestValidator.validateTypes(
request.getType(), sellAsset.getCode(), sellAsset.getSep6().getWithdraw().getMethods());
fundingMethod, sellAsset.getCode(), sellAsset.getSep6().getWithdraw().getMethods());
requestValidator.validateAmount(
request.getAmount(),
sellAsset.getCode(),
Expand Down Expand Up @@ -395,7 +401,7 @@ public StartWithdrawResponse withdrawExchange(
.transactionId(id)
.status(SepTransactionStatus.INCOMPLETE.toString())
.kind(Sep6Transaction.Kind.WITHDRAWAL_EXCHANGE.toString())
.type(request.getType())
.type(fundingMethod)
.assetCode(sellAsset.getCode())
.assetIssuer(sellAsset.getIssuer())
.amountIn(amounts.getAmountIn())
Expand Down Expand Up @@ -534,6 +540,7 @@ private InfoResponse buildInfoResponse() {
AssetInfo.Field.builder()
.description("type of deposit to make")
.choices(methods)
.optional(true)
.build();

DepositAssetResponse deposit =
Expand All @@ -542,6 +549,7 @@ private InfoResponse buildInfoResponse() {
.authenticationRequired(true)
.minAmount(asset.getSep6().getDeposit().getMinAmount())
.maxAmount(asset.getSep6().getDeposit().getMaxAmount())
.fundingMethods(methods)
.fields(ImmutableMap.of("type", type))
.build();

Expand All @@ -555,13 +563,13 @@ private InfoResponse buildInfoResponse() {
for (String method : methods) {
types.put(method, WithdrawType.builder().fields(new HashMap<>()).build());
}

WithdrawAssetResponse withdraw =
WithdrawAssetResponse.builder()
.enabled(true)
.authenticationRequired(true)
.minAmount(asset.getSep6().getWithdraw().getMinAmount())
.maxAmount(asset.getSep6().getWithdraw().getMaxAmount())
.fundingMethods(methods)
.types(types)
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public static GetTransactionResponse toGetTransactionResponse(Sep31Transaction t
.sep(PlatformTransactionData.Sep.SEP_31)
.kind(RECEIVE)
.status(SepTransactionStatus.from(txn.getStatus()))
.type(txn.getFundingMethod())
.fundingMethod(txn.getFundingMethod())
.amountExpected(new Amount(txn.getAmountExpected(), txn.getAmountInAsset()))
.amountIn(new Amount(txn.getAmountIn(), txn.getAmountInAsset()))
.amountOut(new Amount(txn.getAmountOut(), txn.getAmountOutAsset()))
Expand Down Expand Up @@ -137,6 +137,7 @@ public static GetTransactionResponse toGetTransactionResponse(
.sep(PlatformTransactionData.Sep.SEP_6)
.kind(PlatformTransactionData.Kind.from(txn.getKind()))
.status(SepTransactionStatus.from(txn.getStatus()))
.fundingMethod(txn.getType())
.type(txn.getType())
.amountExpected(
(amountExpectedAsset != null)
Expand Down
Loading

0 comments on commit e67e65a

Please sign in to comment.