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-525] Introduce NotifyAmountsAssetsUpdated RPC #1188

Closed
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
@@ -0,0 +1,27 @@
package org.stellar.anchor.api.rpc.method;

import com.google.gson.annotations.SerializedName;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.SuperBuilder;

@Data
@SuperBuilder
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class NotifyAmountsAssetsUpdatedRequest extends RpcMethodParamsRequest {

@SerializedName("amount_in")
@NotNull
private AmountAssetRequest amountIn;

@SerializedName("amount_out")
@NotNull
private AmountAssetRequest amountOut;

@SerializedName("amount_fee")
@NotNull
private AmountAssetRequest amountFee;
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public enum RpcMethod {
@SerializedName("request_onchain_funds")
REQUEST_ONCHAIN_FUNDS("request_onchain_funds"),

@SerializedName("notify_amounts_assets_updated")
NOTIFY_AMOUNTS_ASSETS_UPDATED("notify_amounts_assets_updated"),

@SerializedName("notify_amounts_updated")
NOTIFY_AMOUNTS_UPDATED("notify_amounts_updated"),

Expand Down
18 changes: 0 additions & 18 deletions core/src/main/java/org/stellar/anchor/sep6/Sep6Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,6 @@ public StartDepositResponse deposit(Sep10Jwt token, StartDepositRequest request)
.type(request.getType())
.assetCode(request.getAssetCode())
.assetIssuer(asset.getIssuer())
// NB: these are purposely set to incorrect values.
// amount_out and amount_fee assets cannot be determined when the
// platform creates the transaction, but the RPC API requires
// these to be set during a notify_amounts_updated call.
.amountOut(request.getAmount())
.amountOutAsset(asset.getSep38AssetName())
.amountFee("0")
.amountFeeAsset(asset.getSep38AssetName())
.amountExpected(request.getAmount())
.startedAt(Instant.now())
.sep10Account(token.getAccount())
Expand Down Expand Up @@ -254,16 +246,6 @@ public StartWithdrawResponse withdraw(Sep10Jwt token, StartWithdrawRequest reque
.type(request.getType())
.assetCode(request.getAssetCode())
.assetIssuer(asset.getIssuer())
.amountIn(request.getAmount())
.amountInAsset(asset.getSep38AssetName())
// NB: these are purposely set to incorrect values.
// amount_out and amount_fee assets cannot be determined when the
// platform creates the transaction, but the RPC API requires
// these to be set during a notify_amounts_updated call.
.amountOut(request.getAmount())
.amountOutAsset(asset.getSep38AssetName())
.amountFee("0")
.amountFeeAsset(asset.getSep38AssetName())
.amountExpected(request.getAmount())
.startedAt(Instant.now())
.sep10Account(token.getAccount())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ data class RequestOffchainFundsRequest(
data class RequestOnchainFundsRequest(
@SerialName("transaction_id") override val transactionId: String,
override val message: String,
@SerialName("amount_in") val amountIn: AmountAssetRequest,
@SerialName("amount_out") val amountOut: AmountAssetRequest,
@SerialName("amount_fee") val amountFee: AmountAssetRequest,
@SerialName("amount_in") val amountIn: AmountAssetRequest? = null,
@SerialName("amount_out") val amountOut: AmountAssetRequest? = null,
@SerialName("amount_fee") val amountFee: AmountAssetRequest? = null,
@SerialName("amount_expected") val amountExpected: AmountRequest? = null
) : RpcActionParamsRequest()

Expand Down Expand Up @@ -123,13 +123,22 @@ data class NotifyOffchainFundsPendingRequest(
) : RpcActionParamsRequest()

@Serializable
data class NotifyAmountsUpdatedRequest(
data class NotifyAmountsAssetsUpdatedRequest(
@SerialName("transaction_id") override val transactionId: String,
override val message: String? = null,
@SerialName("amount_in") val amountIn: AmountAssetRequest,
@SerialName("amount_out") val amountOut: AmountAssetRequest,
@SerialName("amount_fee") val amountFee: AmountAssetRequest
) : RpcActionParamsRequest()

@Serializable
data class NotifyAmountsUpdatedRequest(
@SerialName("transaction_id") override val transactionId: String,
override val message: String? = null,
@SerialName("amount_out") val amountOut: AmountRequest,
@SerialName("amount_fee") val amountFee: AmountRequest
) : RpcActionParamsRequest()

@Serializable
data class NotifyOffchainFundsSentRequest(
@SerialName("transaction_id") override val transactionId: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,6 @@ class Sep6EventProcessor(
RequestOffchainFundsRequest(
transactionId = transaction.id,
message = "Please deposit the amount to the following bank account",
amountIn =
AmountAssetRequest(
asset = "iso4217:USD",
amount = transaction.amountExpected.amount
),
amountOut =
AmountAssetRequest(
asset = transaction.amountOut.asset,
amount = transaction.amountOut.amount
),
amountFee = AmountAssetRequest(asset = "iso4217:USD", amount = "0"),
instructions =
mapOf(
"organization.bank_number" to
Expand All @@ -237,19 +226,7 @@ class Sep6EventProcessor(
"request_onchain_funds",
RequestOnchainFundsRequest(
transactionId = transaction.id,
message = "Please deposit the amount to the following address",
amountIn =
AmountAssetRequest(
asset = transaction.amountExpected.asset,
amount = transaction.amountExpected.amount
),
amountOut =
AmountAssetRequest(
asset = "iso4217:USD",
amount = transaction.amountExpected.amount
),
amountFee =
AmountAssetRequest(asset = transaction.amountExpected.asset, amount = "0")
message = "Please deposit the amount to the following address"
)
)
}
Expand All @@ -275,21 +252,57 @@ class Sep6EventProcessor(
}

private fun updateAmounts(event: AnchorEvent) {
val asset =
runBlocking {
when (event.transaction.kind) {
Kind.DEPOSIT -> event.transaction.amountExpected.asset
Kind.WITHDRAWAL -> "iso4217:USD"
else -> throw RuntimeException("Unsupported kind: ${event.transaction.kind}")
Kind.DEPOSIT ->
runBlocking {
sepHelper.rpcAction(
"notify_amounts_assets_updated",
NotifyAmountsAssetsUpdatedRequest(
transactionId = event.transaction.id,
message = "Amounts updated",
amountIn =
AmountAssetRequest(
asset = "iso4217:USD",
amount = event.transaction.amountExpected.amount
),
amountOut =
AmountAssetRequest(
asset = event.transaction.amountExpected.asset,
amount = event.transaction.amountExpected.amount
),
amountFee = AmountAssetRequest(asset = "iso4217:USD", amount = "0")
)
)
}
Kind.WITHDRAWAL ->
runBlocking {
sepHelper.rpcAction(
"notify_amounts_assets_updated",
NotifyAmountsAssetsUpdatedRequest(
transactionId = event.transaction.id,
message = "Amounts updated",
amountIn =
AmountAssetRequest(
asset = event.transaction.amountExpected.asset,
amount = event.transaction.amountExpected.amount
),
amountOut =
AmountAssetRequest(
asset = "iso4217:USD",
amount = event.transaction.amountExpected.amount
),
amountFee =
AmountAssetRequest(asset = event.transaction.amountExpected.asset, amount = "0")
)
)
}
else -> {
log.warn(
"Received transaction created event with unsupported kind: ${event.transaction.kind}"
)
}
}
runBlocking {
sepHelper.rpcAction(
"notify_amounts_updated",
NotifyAmountsUpdatedRequest(
transactionId = event.transaction.id,
amountOut = AmountAssetRequest(asset, amount = event.transaction.amountExpected.amount),
amountFee = AmountAssetRequest(asset, amount = "0")
)
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,7 @@
import org.stellar.anchor.platform.config.PropertyCustodyConfig;
import org.stellar.anchor.platform.config.RpcConfig;
import org.stellar.anchor.platform.data.JdbcTransactionPendingTrustRepo;
import org.stellar.anchor.platform.rpc.DoStellarPaymentHandler;
import org.stellar.anchor.platform.rpc.DoStellarRefundHandler;
import org.stellar.anchor.platform.rpc.NotifyAmountsUpdatedHandler;
import org.stellar.anchor.platform.rpc.NotifyCustomerInfoUpdatedHandler;
import org.stellar.anchor.platform.rpc.NotifyInteractiveFlowCompletedHandler;
import org.stellar.anchor.platform.rpc.NotifyOffchainFundsAvailableHandler;
import org.stellar.anchor.platform.rpc.NotifyOffchainFundsPendingHandler;
import org.stellar.anchor.platform.rpc.NotifyOffchainFundsReceivedHandler;
import org.stellar.anchor.platform.rpc.NotifyOffchainFundsSentHandler;
import org.stellar.anchor.platform.rpc.NotifyOnchainFundsReceivedHandler;
import org.stellar.anchor.platform.rpc.NotifyOnchainFundsSentHandler;
import org.stellar.anchor.platform.rpc.NotifyRefundPendingHandler;
import org.stellar.anchor.platform.rpc.NotifyRefundSentHandler;
import org.stellar.anchor.platform.rpc.NotifyTransactionErrorHandler;
import org.stellar.anchor.platform.rpc.NotifyTransactionExpiredHandler;
import org.stellar.anchor.platform.rpc.NotifyTransactionRecoveryHandler;
import org.stellar.anchor.platform.rpc.NotifyTrustSetHandler;
import org.stellar.anchor.platform.rpc.RequestCustomerInfoUpdateHandler;
import org.stellar.anchor.platform.rpc.RequestOffchainFundsHandler;
import org.stellar.anchor.platform.rpc.RequestOnchainFundsHandler;
import org.stellar.anchor.platform.rpc.RequestTrustlineHandler;
import org.stellar.anchor.platform.rpc.RpcMethodHandler;
import org.stellar.anchor.platform.rpc.*;
import org.stellar.anchor.platform.service.RpcService;
import org.stellar.anchor.platform.validator.RequestValidator;
import org.stellar.anchor.sep24.Sep24DepositInfoGenerator;
Expand Down Expand Up @@ -100,6 +79,25 @@ DoStellarRefundHandler doStellarRefundHandler(
metricsService);
}

@Bean
NotifyAmountsAssetsUpdatedHandler notifyAmountsAssetsUpdatedHandler(
Sep6TransactionStore txn6Store,
Sep24TransactionStore txn24Store,
Sep31TransactionStore txn31Store,
RequestValidator requestValidator,
AssetService assetService,
EventService eventService,
MetricsService metricsService) {
return new NotifyAmountsAssetsUpdatedHandler(
txn6Store,
txn24Store,
txn31Store,
requestValidator,
assetService,
eventService,
metricsService);
}

@Bean
NotifyAmountsUpdatedHandler notifyAmountsUpdatedHandler(
Sep6TransactionStore txn6Store,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package org.stellar.anchor.platform.rpc;

import static java.util.Collections.emptySet;
import static org.stellar.anchor.api.platform.PlatformTransactionData.Sep.SEP_6;
import static org.stellar.anchor.api.rpc.method.RpcMethod.NOTIFY_AMOUNTS_ASSETS_UPDATED;
import static org.stellar.anchor.api.sep.SepTransactionStatus.*;

import java.util.Set;
import org.stellar.anchor.api.exception.AnchorException;
import org.stellar.anchor.api.exception.BadRequestException;
import org.stellar.anchor.api.exception.rpc.InvalidParamsException;
import org.stellar.anchor.api.exception.rpc.InvalidRequestException;
import org.stellar.anchor.api.platform.PlatformTransactionData.Sep;
import org.stellar.anchor.api.rpc.method.AmountAssetRequest;
import org.stellar.anchor.api.rpc.method.NotifyAmountsAssetsUpdatedRequest;
import org.stellar.anchor.api.rpc.method.RpcMethod;
import org.stellar.anchor.api.sep.SepTransactionStatus;
import org.stellar.anchor.asset.AssetService;
import org.stellar.anchor.event.EventService;
import org.stellar.anchor.metrics.MetricsService;
import org.stellar.anchor.platform.data.JdbcSepTransaction;
import org.stellar.anchor.platform.utils.AssetValidationUtils;
import org.stellar.anchor.platform.validator.RequestValidator;
import org.stellar.anchor.sep24.Sep24TransactionStore;
import org.stellar.anchor.sep31.Sep31TransactionStore;
import org.stellar.anchor.sep6.Sep6TransactionStore;

public class NotifyAmountsAssetsUpdatedHandler
extends RpcMethodHandler<NotifyAmountsAssetsUpdatedRequest> {

public NotifyAmountsAssetsUpdatedHandler(
Sep6TransactionStore txn6Store,
Sep24TransactionStore txn24Store,
Sep31TransactionStore txn31Store,
RequestValidator requestValidator,
AssetService assetService,
EventService eventService,
MetricsService metricsService) {
super(
txn6Store,
txn24Store,
txn31Store,
requestValidator,
assetService,
eventService,
metricsService,
NotifyAmountsAssetsUpdatedRequest.class);
}

@Override
protected void validate(JdbcSepTransaction txn, NotifyAmountsAssetsUpdatedRequest request)
throws BadRequestException, InvalidParamsException, InvalidRequestException {
super.validate(txn, request);

AssetValidationUtils.validateAsset(
"amount_in",
AmountAssetRequest.builder()
.amount(request.getAmountIn().getAmount())
.asset(request.getAmountIn().getAsset())
.build(),
assetService);

AssetValidationUtils.validateAsset(
"amount_out",
AmountAssetRequest.builder()
.amount(request.getAmountOut().getAmount())
.asset(request.getAmountOut().getAsset())
.build(),
assetService);

AssetValidationUtils.validateAsset(
"amount_fee",
AmountAssetRequest.builder()
.amount(request.getAmountFee().getAmount())
.asset(request.getAmountFee().getAsset())
.build(),
true,
assetService);
}

@Override
public RpcMethod getRpcMethod() {
return NOTIFY_AMOUNTS_ASSETS_UPDATED;
}

@Override
protected SepTransactionStatus getNextStatus(
JdbcSepTransaction txn, NotifyAmountsAssetsUpdatedRequest request)
throws InvalidRequestException, InvalidParamsException {
return PENDING_ANCHOR;
}

@Override
protected Set<SepTransactionStatus> getSupportedStatuses(JdbcSepTransaction txn) {
if (Sep.from(txn.getProtocol()) == SEP_6) {
return Set.of(INCOMPLETE, PENDING_ANCHOR, PENDING_CUSTOMER_INFO_UPDATE);
}
return emptySet();
}

@Override
protected void updateTransactionWithRpcRequest(
JdbcSepTransaction txn, NotifyAmountsAssetsUpdatedRequest request) throws AnchorException {
txn.setAmountIn(request.getAmountIn().getAmount());
txn.setAmountInAsset(request.getAmountIn().getAsset());

txn.setAmountOut(request.getAmountOut().getAmount());
txn.setAmountOutAsset(request.getAmountOut().getAsset());

txn.setAmountFee(request.getAmountFee().getAmount());
txn.setAmountFeeAsset(request.getAmountFee().getAsset());
}
}
Loading