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

Add optional localization to vault validation errors #698

Merged
merged 2 commits into from
Oct 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
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ allprojects {
}

group = "exchange.dydx.abacus"
version = "1.12.14"
version = "1.12.15"

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import exchange.dydx.abacus.output.input.ErrorResources
import exchange.dydx.abacus.output.input.ErrorString
import exchange.dydx.abacus.output.input.ErrorType
import exchange.dydx.abacus.output.input.ValidationError
import exchange.dydx.abacus.protocols.LocalizerProtocol
import exchange.dydx.abacus.protocols.asTypedObject
import exchange.dydx.abacus.protocols.localizeWithParams
import exchange.dydx.abacus.utils.IList
import exchange.dydx.abacus.utils.Parser
import exchange.dydx.abacus.utils.format
Expand Down Expand Up @@ -40,7 +42,9 @@ data class VaultFormAccountData(
val canViewAccount: Boolean?,
)

object VaultFormValidationErrors {
internal class VaultFormValidationErrors(
private val localizer: LocalizerProtocol? = null,
) {
private fun createError(
code: String,
type: ErrorType,
Expand All @@ -49,6 +53,12 @@ object VaultFormValidationErrors {
textKey: String? = null,
textKeyParams: List<ErrorParam>? = null
): ValidationError {
val paramsMap = mutableMapOf<String, String>()
for (param in textKeyParams ?: emptyList()) {
if (param.value != null) {
paramsMap[param.key] = param.value
}
}
return ValidationError(
code = code,
type = type,
Expand All @@ -57,12 +67,18 @@ object VaultFormValidationErrors {
link = null,
linkText = null,
resources = ErrorResources(
title = titleKey?.let { ErrorString(stringKey = it, params = null, localized = null) },
title = titleKey?.let {
ErrorString(
stringKey = it,
params = null,
localized = localizer?.localize(it),
)
},
text = textKey?.let {
ErrorString(
stringKey = it,
params = textKeyParams?.toIList(),
localized = null,
localized = localizer?.localizeWithParams(it, paramsMap),
)
},
action = null,
Expand Down Expand Up @@ -227,8 +243,10 @@ object VaultDepositWithdrawFormValidator {
formData: VaultFormData,
accountData: VaultFormAccountData?,
vaultAccount: VaultAccount?,
slippageResponse: OnChainVaultDepositWithdrawSlippageResponse?
slippageResponse: OnChainVaultDepositWithdrawSlippageResponse?,
localizer: LocalizerProtocol? = null,
): VaultFormValidationResult {
val vaultFormValidationErrors = VaultFormValidationErrors(localizer)
val errors = mutableListOf<ValidationError>()
var submissionData: VaultDepositWithdrawSubmissionData? = null

Expand Down Expand Up @@ -287,55 +305,55 @@ object VaultDepositWithdrawFormValidator {

// Perform validation checks and populate errors list
if (accountData == null) {
errors.add(VaultFormValidationErrors.accountDataMissing(accountData?.canViewAccount))
errors.add(vaultFormValidationErrors.accountDataMissing(accountData?.canViewAccount))
}

if (amount == 0.0) {
errors.add(VaultFormValidationErrors.amountEmpty(formData.action))
errors.add(vaultFormValidationErrors.amountEmpty(formData.action))
}

// can't actually submit if we are missing key validation information
if (formData.inConfirmationStep && formData.action === VaultFormAction.WITHDRAW) {
if (vaultAccount == null) {
errors.add(VaultFormValidationErrors.vaultAccountMissing())
errors.add(vaultFormValidationErrors.vaultAccountMissing())
}
if (slippageResponse == null || sharesToAttemptWithdraw == null) {
errors.add(VaultFormValidationErrors.slippageResponseMissing())
errors.add(vaultFormValidationErrors.slippageResponseMissing())
}
}

if (formData.inConfirmationStep && formData.action === VaultFormAction.DEPOSIT) {
if (accountData?.marginUsage == null || accountData.freeCollateral == null) {
errors.add(VaultFormValidationErrors.accountDataMissing(accountData?.canViewAccount))
errors.add(vaultFormValidationErrors.accountDataMissing(accountData?.canViewAccount))
}
}

when (formData.action) {
VaultFormAction.DEPOSIT -> {
if (postOpFreeCollateral != null && postOpFreeCollateral < 0) {
errors.add(VaultFormValidationErrors.depositTooHigh())
errors.add(vaultFormValidationErrors.depositTooHigh())
}
if (amount > 0 && amount < MIN_DEPOSIT_FE_THRESHOLD) {
errors.add(VaultFormValidationErrors.depositTooLow())
errors.add(vaultFormValidationErrors.depositTooLow())
}
}
VaultFormAction.WITHDRAW -> {
if (postOpVaultBalance != null && postOpVaultBalance < 0) {
errors.add(VaultFormValidationErrors.withdrawTooHigh())
errors.add(vaultFormValidationErrors.withdrawTooHigh())
}
if (postOpVaultBalance != null && postOpVaultBalance >= 0 && amount > 0 && vaultAccount?.withdrawableUsdc != null && amount > vaultAccount.withdrawableUsdc) {
errors.add(VaultFormValidationErrors.withdrawingLockedBalance())
errors.add(vaultFormValidationErrors.withdrawingLockedBalance())
}
if (sharesToAttemptWithdraw != null && slippageResponse != null && sharesToAttemptWithdraw != slippageResponse.sharesToWithdraw.numShares) {
errors.add(
VaultFormValidationErrors.slippageResponseWrongShares(),
vaultFormValidationErrors.slippageResponseWrongShares(),
)
}
if (slippagePercent >= SLIPPAGE_PERCENT_WARN) {
errors.add(VaultFormValidationErrors.slippageTooHigh(slippagePercent))
errors.add(vaultFormValidationErrors.slippageTooHigh(slippagePercent))
}
if (needSlippageAck && !formData.acknowledgedSlippage && formData.inConfirmationStep) {
errors.add(VaultFormValidationErrors.mustAckSlippage())
errors.add(vaultFormValidationErrors.mustAckSlippage())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ interface LocalizerProtocol {
fun localize(path: String, paramsAsJson: String? = null): String
}

fun LocalizerProtocol.localizeWithParams(path: String, params: Map<String, String>): String? =
localize(path = path, paramsAsJson = params.toJsonPrettyPrint())
fun LocalizerProtocol.localizeWithParams(path: String, params: Map<String, String>?): String =
if (params == null) localize(path = path) else localize(path = path, paramsAsJson = params.toJsonPrettyPrint())

interface AbacusLocalizerProtocol : LocalizerProtocol {
val languages: List<SelectionOption>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class VaultFormTests {
assertEquals(
VaultFormValidationResult(
errors = iListOf(
VaultFormValidationErrors.slippageTooHigh(0.02),
VaultFormValidationErrors().slippageTooHigh(0.02),
),
submissionData = VaultDepositWithdrawSubmissionData(
deposit = null,
Expand Down Expand Up @@ -165,8 +165,8 @@ class VaultFormTests {
assertEquals(
VaultFormValidationResult(
errors = iListOf(
VaultFormValidationErrors.slippageResponseWrongShares(),
VaultFormValidationErrors.slippageTooHigh(0.02),
VaultFormValidationErrors().slippageResponseWrongShares(),
VaultFormValidationErrors().slippageTooHigh(0.02),
),
submissionData = null,
summaryData = VaultFormSummaryData(
Expand Down Expand Up @@ -211,9 +211,9 @@ class VaultFormTests {
assertEquals(
VaultFormValidationResult(
errors = iListOf(
VaultFormValidationErrors.withdrawTooHigh(),
VaultFormValidationErrors.slippageTooHigh(0.166666),
VaultFormValidationErrors.mustAckSlippage(),
VaultFormValidationErrors().withdrawTooHigh(),
VaultFormValidationErrors().slippageTooHigh(0.166666),
VaultFormValidationErrors().mustAckSlippage(),
),
submissionData = null,
summaryData = VaultFormSummaryData(
Expand Down Expand Up @@ -255,7 +255,7 @@ class VaultFormTests {
assertEquals(
VaultFormValidationResult(
errors = iListOf(
VaultFormValidationErrors.depositTooLow(),
VaultFormValidationErrors().depositTooLow(),
),
submissionData = null,
summaryData = VaultFormSummaryData(
Expand Down
2 changes: 1 addition & 1 deletion v4_abacus.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = 'v4_abacus'
spec.version = '1.12.14'
spec.version = '1.12.15'
spec.homepage = 'https://github.com/dydxprotocol/v4-abacus'
spec.source = { :http=> ''}
spec.authors = ''
Expand Down