Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tyleroooo committed Aug 22, 2024
1 parent c46d6d6 commit 25d09db
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ enum class ErrorType {
AMOUNT_EMPTY,
DEPOSIT_TOO_HIGH,
WITHDRAW_TOO_HIGH,
WITHDRAWING_LOCKED_BALANCE,
SLIPPAGE_TOO_HIGH,
MUST_ACK_SLIPPAGE,
VAULT_ACCOUNT_MISSING,
Expand Down Expand Up @@ -209,6 +210,9 @@ object VaultDepositWithdrawFormValidator {
if (postOpVaultBalance != null && postOpVaultBalance < 0) {
errors.add(ValidationError(ErrorSeverity.ERROR, ErrorType.WITHDRAW_TOO_HIGH))
}
if (postOpVaultBalance != null && postOpVaultBalance >= 0 && amount > 0 && vaultAccount?.withdrawableUsdc != null && amount > vaultAccount.withdrawableUsdc) {
errors.add(ValidationError(ErrorSeverity.ERROR, ErrorType.WITHDRAWING_LOCKED_BALANCE))
}
if (sharesToAttemptWithdraw != null && slippageResponse != null && sharesToAttemptWithdraw != slippageResponse.shares) {
errors.add(
ValidationError(
Expand Down Expand Up @@ -258,7 +262,7 @@ object VaultDepositWithdrawFormValidator {
freeCollateral = postOpFreeCollateral,
vaultBalance = postOpVaultBalance,
estimatedSlippage = slippagePercent,
estimatedAmountReceived = slippageResponse?.expectedAmount,
estimatedAmountReceived = if (formData.action === TransactionAction.WITHDRAW) slippageResponse?.expectedAmount else null,
)

return ValidationResult(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class VaultAccountTests {
withdrawableUsdc = 5000.0,
allTimeReturnUsdc = 6000.0,
totalVaultTransfersCount = 2,
balanceShares = 100.0,
lockedShares = 50.0,
vaultTransfers = listOf(
VaultTransfer(
timestampMs = 1659465600000.0,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
package exchange.dydx.abacus.functional.vault

import exchange.dydx.abacus.functional.vault.VaultAccountCalculator.calculateUserVaultInfo
import indexer.codegen.IndexerTransferBetweenResponse
import indexer.codegen.IndexerTransferResponseObject
import indexer.codegen.IndexerTransferType
import kotlin.test.Test
import kotlin.test.assertEquals

class VaultFormTests {

private fun makeVaultAccount(balanceShares: Double, balanceUsdc: Double, withdrawableUsdc: Double): VaultAccount {
return VaultAccount(
balanceShares = balanceShares,
lockedShares = null, // currently ignored but we should probably populate this at some point
vaultTransfers = null,
allTimeReturnUsdc = null,
totalVaultTransfersCount = null,
withdrawableUsdc = withdrawableUsdc,
balanceUsdc = balanceUsdc,
)
}

@Test
fun testDepositValidation() {
val result = VaultDepositWithdrawFormValidator.validateVaultForm(
formData = FormData(
action = TransactionAction.DEPOSIT,
amount = 100.0,
acknowledgedSlippage = true,
inConfirmationStep = true
),
accountData = AccountData(
marginUsage = 0.5,
freeCollateral = 1000.0
),
vaultAccount = makeVaultAccount(
balanceUsdc = 500.0,
balanceShares = 250.0,
withdrawableUsdc = 500.0,
),
slippageResponse = null
)

assertEquals(
ValidationResult(
errors = emptyList(),
submissionData = SubmissionData(
deposit = DepositData(
subaccountFrom = "0",
amount = 100.0
),
withdraw = null
),
summaryData = SummaryData(
needSlippageAck = false,
marginUsage = 0.5263157894736843,
freeCollateral = 900.0,
vaultBalance = 600.0,
estimatedSlippage = 0.0,
estimatedAmountReceived = null
)
),
result
)
}

@Test
fun testWithdrawValidation() {
val result = VaultDepositWithdrawFormValidator.validateVaultForm(
formData = FormData(
action = TransactionAction.WITHDRAW,
amount = 100.0,
acknowledgedSlippage = true,
inConfirmationStep = true
),
accountData = AccountData(
marginUsage = 0.5,
freeCollateral = 1000.0
),
vaultAccount = makeVaultAccount(
balanceUsdc = 500.0,
withdrawableUsdc = 500.0,
balanceShares = 500.0
),
slippageResponse = SlippageResponse(
shares = 100.0,
expectedAmount = 98.0
)
)

assertEquals(
ValidationResult(
errors = listOf(
ValidationError(ErrorSeverity.WARNING, ErrorType.SLIPPAGE_TOO_HIGH)
),
submissionData = SubmissionData(
deposit = null,
withdraw = WithdrawData(
subaccountTo = "0",
shares = 100.0,
minAmount = 98 * .99
)
),
summaryData = SummaryData(
needSlippageAck = true,
marginUsage = 0.4766444232602478,
freeCollateral = 1098.0,
vaultBalance = 400.0,
estimatedSlippage = 0.02,
estimatedAmountReceived = 98.0
)
),
result
)
}

@Test
fun testWithdrawBadSlippageResponse() {
// same as previous but your slippage response is out of date
val result = VaultDepositWithdrawFormValidator.validateVaultForm(
formData = FormData(
action = TransactionAction.WITHDRAW,
amount = 100.0,
acknowledgedSlippage = true,
inConfirmationStep = true
),
accountData = AccountData(
marginUsage = 0.5,
freeCollateral = 1000.0
),
vaultAccount = makeVaultAccount(
balanceUsdc = 500.0,
withdrawableUsdc = 500.0,
balanceShares = 500.0
),
slippageResponse = SlippageResponse(
shares = 120.0,
expectedAmount = 98.0
)
)

assertEquals(
ValidationResult(
errors = listOf(
ValidationError(ErrorSeverity.ERROR, ErrorType.SLIPPAGE_RESPONSE_WRONG_SHARES),
ValidationError(ErrorSeverity.WARNING, ErrorType.SLIPPAGE_TOO_HIGH),
),
submissionData = null,
summaryData = SummaryData(
needSlippageAck = true,
marginUsage = 0.4766444232602478,
freeCollateral = 1098.0,
vaultBalance = 400.0,
estimatedSlippage = 0.020000000000000018, // unfortunate precision issues with direct equality checks
estimatedAmountReceived = 98.0
)
),
result
)
}

@Test
fun testInvalidWithdraw() {
val result = VaultDepositWithdrawFormValidator.validateVaultForm(
formData = FormData(
action = TransactionAction.WITHDRAW,
amount = 600.0,
acknowledgedSlippage = false,
inConfirmationStep = true
),
accountData = AccountData(
marginUsage = 0.5,
freeCollateral = 1000.0
),
vaultAccount = makeVaultAccount(
balanceUsdc = 500.0,
withdrawableUsdc = 500.0,
balanceShares = 500.0,
),
slippageResponse = SlippageResponse(
shares = 600.0,
expectedAmount = 500.0
)
)

assertEquals(
ValidationResult(
errors = listOf(
ValidationError(ErrorSeverity.ERROR, ErrorType.WITHDRAW_TOO_HIGH),
ValidationError(ErrorSeverity.WARNING, ErrorType.SLIPPAGE_TOO_HIGH),
ValidationError(ErrorSeverity.ERROR, ErrorType.MUST_ACK_SLIPPAGE)
),
submissionData = null,
summaryData = SummaryData(
needSlippageAck = true,
marginUsage = 0.4,
freeCollateral = 1500.0,
vaultBalance = -100.0,
estimatedSlippage = 0.16666666666666663,
estimatedAmountReceived = 500.0
)
),
result
)
}
}

0 comments on commit 25d09db

Please sign in to comment.