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

[ANCHOR-809][SEP-6] Add funding_method to SEP-6 #1582

Merged
merged 8 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
philipliu marked this conversation as resolved.
Show resolved Hide resolved
}
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;
JiahuiWho marked this conversation as resolved.
Show resolved Hide resolved
}

/** 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;
JiahuiWho marked this conversation as resolved.
Show resolved Hide resolved
}

/** 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;
JiahuiWho marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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;
JiahuiWho marked this conversation as resolved.
Show resolved Hide resolved

/** 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;
JiahuiWho marked this conversation as resolved.
Show resolved Hide resolved

/** 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;
JiahuiWho marked this conversation as resolved.
Show resolved Hide resolved

/** 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();
philipliu marked this conversation as resolved.
Show resolved Hide resolved
requestValidator.validateTypes(
philipliu marked this conversation as resolved.
Show resolved Hide resolved
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());
philipliu marked this conversation as resolved.
Show resolved Hide resolved
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())
philipliu marked this conversation as resolved.
Show resolved Hide resolved
.type(txn.getType())
.amountExpected(
(amountExpectedAsset != null)
Expand Down
Loading
Loading