-
Notifications
You must be signed in to change notification settings - Fork 34
/
NotifyOnchainFundsReceivedHandler.java
185 lines (172 loc) · 7.05 KB
/
NotifyOnchainFundsReceivedHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package org.stellar.anchor.platform.rpc;
import static org.stellar.anchor.api.platform.PlatformTransactionData.Kind.WITHDRAWAL;
import static org.stellar.anchor.api.platform.PlatformTransactionData.Kind.WITHDRAWAL_EXCHANGE;
import static org.stellar.anchor.api.rpc.method.RpcMethod.NOTIFY_ONCHAIN_FUNDS_RECEIVED;
import static org.stellar.anchor.api.sep.SepTransactionStatus.PENDING_ANCHOR;
import static org.stellar.anchor.api.sep.SepTransactionStatus.PENDING_RECEIVER;
import static org.stellar.anchor.api.sep.SepTransactionStatus.PENDING_SENDER;
import static org.stellar.anchor.api.sep.SepTransactionStatus.PENDING_USR_TRANSFER_START;
import static org.stellar.anchor.platform.utils.PaymentsUtil.addStellarTransaction;
import static org.stellar.anchor.util.Log.errorEx;
import com.google.common.collect.ImmutableSet;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
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.InternalErrorException;
import org.stellar.anchor.api.exception.rpc.InvalidParamsException;
import org.stellar.anchor.api.exception.rpc.InvalidRequestException;
import org.stellar.anchor.api.platform.PlatformTransactionData.Kind;
import org.stellar.anchor.api.platform.PlatformTransactionData.Sep;
import org.stellar.anchor.api.rpc.method.AmountAssetRequest;
import org.stellar.anchor.api.rpc.method.NotifyOnchainFundsReceivedRequest;
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.horizon.Horizon;
import org.stellar.anchor.metrics.MetricsService;
import org.stellar.anchor.platform.data.JdbcSep24Transaction;
import org.stellar.anchor.platform.data.JdbcSep6Transaction;
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;
import org.stellar.sdk.responses.operations.OperationResponse;
public class NotifyOnchainFundsReceivedHandler
extends RpcMethodHandler<NotifyOnchainFundsReceivedRequest> {
private final Horizon horizon;
public NotifyOnchainFundsReceivedHandler(
Sep6TransactionStore txn6Store,
Sep24TransactionStore txn24Store,
Sep31TransactionStore txn31Store,
RequestValidator requestValidator,
Horizon horizon,
AssetService assetService,
EventService eventService,
MetricsService metricsService) {
super(
txn6Store,
txn24Store,
txn31Store,
requestValidator,
assetService,
eventService,
metricsService,
NotifyOnchainFundsReceivedRequest.class);
this.horizon = horizon;
}
@Override
protected void validate(JdbcSepTransaction txn, NotifyOnchainFundsReceivedRequest request)
throws InvalidParamsException, InvalidRequestException, BadRequestException {
super.validate(txn, request);
if (!((request.getAmountIn() == null
&& request.getAmountOut() == null
&& request.getAmountFee() == null)
|| (request.getAmountIn() != null
&& request.getAmountOut() != null
&& request.getAmountFee() != null)
|| (request.getAmountIn() != null
&& request.getAmountOut() == null
&& request.getAmountFee() == null))) {
throw new InvalidParamsException(
"Invalid amounts combination provided: all, none or only amount_in should be set");
}
if (request.getAmountIn() != null) {
AssetValidationUtils.validateAsset(
"amount_in",
AmountAssetRequest.builder()
.amount(request.getAmountIn().getAmount())
.asset(txn.getAmountInAsset())
.build(),
assetService);
}
if (request.getAmountOut() != null) {
AssetValidationUtils.validateAsset(
"amount_out",
AmountAssetRequest.builder()
.amount(request.getAmountOut().getAmount())
.asset(txn.getAmountOutAsset())
.build(),
assetService);
}
if (request.getAmountFee() != null) {
AssetValidationUtils.validateAsset(
"amount_fee",
AmountAssetRequest.builder()
.amount(request.getAmountFee().getAmount())
.asset(txn.getAmountFeeAsset())
.build(),
true,
assetService);
}
}
@Override
public RpcMethod getRpcMethod() {
return NOTIFY_ONCHAIN_FUNDS_RECEIVED;
}
@Override
protected SepTransactionStatus getNextStatus(
JdbcSepTransaction txn, NotifyOnchainFundsReceivedRequest request)
throws InvalidRequestException {
switch (Sep.from(txn.getProtocol())) {
case SEP_6:
case SEP_24:
return PENDING_ANCHOR;
case SEP_31:
return PENDING_RECEIVER;
default:
throw new InvalidRequestException(
String.format(
"RPC method[%s] is not supported for protocol[%s]",
getRpcMethod(), txn.getProtocol()));
}
}
@Override
protected Set<SepTransactionStatus> getSupportedStatuses(JdbcSepTransaction txn) {
Set<SepTransactionStatus> supportedStatuses = new HashSet<>();
switch (Sep.from(txn.getProtocol())) {
case SEP_6:
JdbcSep6Transaction txn6 = (JdbcSep6Transaction) txn;
if (ImmutableSet.of(WITHDRAWAL, WITHDRAWAL_EXCHANGE).contains(Kind.from(txn6.getKind()))) {
supportedStatuses.add(PENDING_USR_TRANSFER_START);
}
break;
case SEP_24:
JdbcSep24Transaction txn24 = (JdbcSep24Transaction) txn;
if (WITHDRAWAL == Kind.from(txn24.getKind())) {
supportedStatuses.add(PENDING_USR_TRANSFER_START);
}
break;
case SEP_31:
supportedStatuses.add(PENDING_SENDER);
}
return supportedStatuses;
}
@Override
protected void updateTransactionWithRpcRequest(
JdbcSepTransaction txn, NotifyOnchainFundsReceivedRequest request) throws AnchorException {
String stellarTxnId = request.getStellarTransactionId();
try {
List<OperationResponse> txnOperations = horizon.getStellarTxnOperations(stellarTxnId);
addStellarTransaction(txn, stellarTxnId, txnOperations);
} catch (IOException ex) {
errorEx(String.format("Failed to retrieve stellar transaction by ID[%s]", stellarTxnId), ex);
throw new InternalErrorException(
String.format("Failed to retrieve Stellar transaction by ID[%s]", stellarTxnId), ex);
}
if (request.getAmountIn() != null) {
txn.setAmountIn(request.getAmountIn().getAmount());
}
if (request.getAmountOut() != null) {
txn.setAmountOut(request.getAmountOut().getAmount());
}
if (request.getAmountFee() != null) {
txn.setAmountFee(request.getAmountFee().getAmount());
}
}
}