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-752] Remove amount_out requirement in RequestOnchainFunds #1451

Merged
merged 2 commits into from
Aug 12, 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 @@ -14,6 +14,7 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.stellar.anchor.api.exception.AnchorException;
import org.stellar.anchor.api.exception.BadRequestException;
import org.stellar.anchor.api.exception.SepException;
Expand Down Expand Up @@ -87,15 +88,13 @@ protected void validate(JdbcSepTransaction txn, RequestOnchainFundsRequest reque

// If none of the accepted combinations of input parameters satisfies -> throw an exception
if (!((request.getAmountIn() == null
&& request.getAmountOut() == null
&& request.getAmountFee() == null
&& request.getFeeDetails() == null
&& request.getAmountExpected() == null)
|| (request.getAmountIn() != null
&& request.getAmountOut() != null
&& (request.getAmountFee() != null || request.getFeeDetails() != null)))) {
throw new InvalidParamsException(
"All or none of the amount_in, amount_out, and (fee_details or amount_fee) should be set");
"All (amount_out is optional) or none of the amount_in, amount_out, and (fee_details or amount_fee) should be set");
}

// In case 2nd predicate in previous IF statement was TRUE
Expand Down Expand Up @@ -141,7 +140,26 @@ protected void validate(JdbcSepTransaction txn, RequestOnchainFundsRequest reque
throw new InvalidParamsException("amount_in is required");
}
if (request.getAmountOut() == null && txn.getAmountOut() == null) {
throw new InvalidParamsException("amount_out is required");
if (SEP_6 == Sep.from(txn.getProtocol())) {
JdbcSep6Transaction txn6 = (JdbcSep6Transaction) txn;
if (txn6.getQuoteId() != null) {
throw new InvalidParamsException(
"amount_out is required for transactions with firm quotes");
}
if (StringUtils.equals(txn6.getAmountInAsset(), txn6.getAmountOutAsset())) {
throw new InvalidParamsException("amount_out is required for non-exchange transactions");
}
}
if (SEP_24 == Sep.from(txn.getProtocol())) {
JdbcSep24Transaction txn24 = (JdbcSep24Transaction) txn;
if (txn24.getQuoteId() != null) {
throw new InvalidParamsException(
"amount_out is required for transactions with firm quotes");
}
if (StringUtils.equals(txn24.getAmountInAsset(), txn24.getAmountOutAsset())) {
throw new InvalidParamsException("amount_out is required for non-exchange transactions");
}
}
}
if (request.getAmountFee() == null
&& request.getFeeDetails() == null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.CsvSource
import org.junit.jupiter.params.provider.ValueSource
import org.skyscreamer.jsonassert.JSONAssert
import org.skyscreamer.jsonassert.JSONCompareMode
import org.stellar.anchor.api.event.AnchorEvent
Expand All @@ -27,10 +28,7 @@ import org.stellar.anchor.api.rpc.method.AmountAssetRequest
import org.stellar.anchor.api.rpc.method.AmountRequest
import org.stellar.anchor.api.rpc.method.RequestOnchainFundsRequest
import org.stellar.anchor.api.sep.SepTransactionStatus.*
import org.stellar.anchor.api.shared.Amount
import org.stellar.anchor.api.shared.Customers
import org.stellar.anchor.api.shared.SepDepositInfo
import org.stellar.anchor.api.shared.StellarId
import org.stellar.anchor.api.shared.*
import org.stellar.anchor.asset.AssetService
import org.stellar.anchor.asset.DefaultAssetService
import org.stellar.anchor.config.CustodyConfig
Expand Down Expand Up @@ -194,13 +192,14 @@ class RequestOnchainFundsHandlerTest {
}

@Test
fun test_handle_withoutAmounts_amount_out_absent() {
fun test_handle_withoutAmounts_fee_absent() {
val request = RequestOnchainFundsRequest.builder().transactionId(TX_ID).build()
val txn24 = JdbcSep24Transaction()
txn24.status = INCOMPLETE.toString()
txn24.kind = WITHDRAWAL.kind
txn24.amountIn = "1"
txn24.amountInAsset = STELLAR_USDC
txn24.feeDetails = FeeDetails()
val sep24TxnCapture = slot<JdbcSep24Transaction>()

every { txn6Store.findByTransactionId(any()) } returns null
Expand All @@ -209,7 +208,7 @@ class RequestOnchainFundsHandlerTest {
every { txn24Store.save(capture(sep24TxnCapture)) } returns null

val ex = assertThrows<InvalidParamsException> { handler.handle(request) }
assertEquals("amount_out is required", ex.message)
assertEquals("fee_details or amount_fee is required", ex.message)

verify(exactly = 0) { txn6Store.save(any()) }
verify(exactly = 0) { txn24Store.save(any()) }
Expand Down Expand Up @@ -263,7 +262,7 @@ class RequestOnchainFundsHandlerTest {

val ex = assertThrows<InvalidParamsException> { handler.handle(request) }
assertEquals(
"All or none of the amount_in, amount_out, and (fee_details or amount_fee) should be set",
"All (amount_out is optional) or none of the amount_in, amount_out, and (fee_details or amount_fee) should be set",
ex.message
)

Expand Down Expand Up @@ -548,6 +547,63 @@ class RequestOnchainFundsHandlerTest {
verify(exactly = 0) { sepTransactionCounter.increment() }
}

@Test
fun test_handle_sep24_with_quote_amount_out_missing() {
val request =
RequestOnchainFundsRequest.builder()
.amountIn(AmountAssetRequest("1", STELLAR_USDC))
.feeDetails(Amount("0.1", STELLAR_USDC).toRate())
.transactionId(TX_ID)
.build()
val txn24 = JdbcSep24Transaction()
txn24.status = INCOMPLETE.toString()
txn24.kind = WITHDRAWAL.kind
txn24.quoteId = "testQuoteId"
val sep24TxnCapture = slot<JdbcSep24Transaction>()

every { txn6Store.findByTransactionId(any()) } returns null
every { txn24Store.findByTransactionId(TX_ID) } returns txn24
every { txn31Store.findByTransactionId(any()) } returns null
every { txn24Store.save(capture(sep24TxnCapture)) } returns null

val ex = assertThrows<InvalidParamsException> { handler.handle(request) }
assertEquals("amount_out is required for transactions with firm quotes", ex.message)

verify(exactly = 0) { txn6Store.save(any()) }
verify(exactly = 0) { txn24Store.save(any()) }
verify(exactly = 0) { txn31Store.save(any()) }
verify(exactly = 0) { sepTransactionCounter.increment() }
}

@Test
fun test_handle_sep24_with_simple_quote() {
val request =
RequestOnchainFundsRequest.builder()
.amountIn(AmountAssetRequest("1", STELLAR_USDC))
.feeDetails(Amount("0.1", STELLAR_USDC).toRate())
.transactionId(TX_ID)
.build()
val txn24 = JdbcSep24Transaction()
txn24.status = INCOMPLETE.toString()
txn24.kind = WITHDRAWAL.kind
txn24.amountInAsset = STELLAR_USDC
txn24.amountOutAsset = STELLAR_USDC
val sep24TxnCapture = slot<JdbcSep24Transaction>()

every { txn6Store.findByTransactionId(any()) } returns null
every { txn24Store.findByTransactionId(TX_ID) } returns txn24
every { txn31Store.findByTransactionId(any()) } returns null
every { txn24Store.save(capture(sep24TxnCapture)) } returns null

val ex = assertThrows<InvalidParamsException> { handler.handle(request) }
assertEquals("amount_out is required for non-exchange transactions", ex.message)

verify(exactly = 0) { txn6Store.save(any()) }
verify(exactly = 0) { txn24Store.save(any()) }
verify(exactly = 0) { txn31Store.save(any()) }
verify(exactly = 0) { sepTransactionCounter.increment() }
}

@Test
fun test_handle_ok_sep24_withExpectedAmount() {
val request =
Expand Down Expand Up @@ -1212,6 +1268,65 @@ class RequestOnchainFundsHandlerTest {
verify(exactly = 0) { sepTransactionCounter.increment() }
}

@ParameterizedTest
@ValueSource(strings = ["withdrawal", "withdrawal-exchange"])
fun test_handle_sep6_with_quote_amount_out_missing(kind: String) {
val request =
RequestOnchainFundsRequest.builder()
.amountIn(AmountAssetRequest("1", STELLAR_USDC))
.feeDetails(Amount("0.1", STELLAR_USDC).toRate())
.transactionId(TX_ID)
.build()
val txn6 = JdbcSep6Transaction()
txn6.status = INCOMPLETE.toString()
txn6.kind = kind
txn6.quoteId = "testQuoteId"
val sep6TxnCapture = slot<JdbcSep6Transaction>()

every { txn6Store.findByTransactionId(any()) } returns txn6
every { txn24Store.findByTransactionId(TX_ID) } returns null
every { txn31Store.findByTransactionId(any()) } returns null
every { txn6Store.save(capture(sep6TxnCapture)) } returns null

val ex = assertThrows<InvalidParamsException> { handler.handle(request) }
assertEquals("amount_out is required for transactions with firm quotes", ex.message)

verify(exactly = 0) { txn6Store.save(any()) }
verify(exactly = 0) { txn24Store.save(any()) }
verify(exactly = 0) { txn31Store.save(any()) }
verify(exactly = 0) { sepTransactionCounter.increment() }
}

@ParameterizedTest
@ValueSource(strings = ["withdrawal", "withdrawal-exchange"])
fun test_handle_sep6_with_simple_quote(kind: String) {
val request =
RequestOnchainFundsRequest.builder()
.amountIn(AmountAssetRequest("1", STELLAR_USDC))
.feeDetails(Amount("0.1", STELLAR_USDC).toRate())
.transactionId(TX_ID)
.build()
val txn6 = JdbcSep6Transaction()
txn6.status = INCOMPLETE.toString()
txn6.kind = kind
txn6.amountInAsset = STELLAR_USDC
txn6.amountOutAsset = STELLAR_USDC
val sep6TxnCapture = slot<JdbcSep6Transaction>()

every { txn6Store.findByTransactionId(any()) } returns txn6
every { txn24Store.findByTransactionId(TX_ID) } returns null
every { txn31Store.findByTransactionId(any()) } returns null
every { txn6Store.save(capture(sep6TxnCapture)) } returns null

val ex = assertThrows<InvalidParamsException> { handler.handle(request) }
assertEquals("amount_out is required for non-exchange transactions", ex.message)

verify(exactly = 0) { txn6Store.save(any()) }
verify(exactly = 0) { txn24Store.save(any()) }
verify(exactly = 0) { txn31Store.save(any()) }
verify(exactly = 0) { sepTransactionCounter.increment() }
}

@CsvSource(value = ["withdrawal", "withdrawal-exchange"])
@ParameterizedTest
fun test_handle_ok_sep6_withExpectedAmount(kind: String) {
Expand Down
Loading