diff --git a/build.gradle.kts b/build.gradle.kts index dd127b8bb..7fec8da31 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -52,7 +52,7 @@ allprojects { } group = "exchange.dydx.abacus" -version = "1.12.14" +version = "1.12.15" repositories { google() diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/functional/vault/VaultDepositWithdrawForm.kt b/src/commonMain/kotlin/exchange.dydx.abacus/functional/vault/VaultDepositWithdrawForm.kt index 689bc8b24..cc0fc53f8 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/functional/vault/VaultDepositWithdrawForm.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/functional/vault/VaultDepositWithdrawForm.kt @@ -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 @@ -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, @@ -49,6 +53,12 @@ object VaultFormValidationErrors { textKey: String? = null, textKeyParams: List? = null ): ValidationError { + val paramsMap = mutableMapOf() + for (param in textKeyParams ?: emptyList()) { + if (param.value != null) { + paramsMap[param.key] = param.value + } + } return ValidationError( code = code, type = type, @@ -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, @@ -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() var submissionData: VaultDepositWithdrawSubmissionData? = null @@ -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()) } } } diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/protocols/LocalizerProtocol.kt b/src/commonMain/kotlin/exchange.dydx.abacus/protocols/LocalizerProtocol.kt index 9ed51abe8..d77f340bb 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/protocols/LocalizerProtocol.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/protocols/LocalizerProtocol.kt @@ -10,8 +10,8 @@ interface LocalizerProtocol { fun localize(path: String, paramsAsJson: String? = null): String } -fun LocalizerProtocol.localizeWithParams(path: String, params: Map): String? = - localize(path = path, paramsAsJson = params.toJsonPrettyPrint()) +fun LocalizerProtocol.localizeWithParams(path: String, params: Map?): String = + if (params == null) localize(path = path) else localize(path = path, paramsAsJson = params.toJsonPrettyPrint()) interface AbacusLocalizerProtocol : LocalizerProtocol { val languages: List diff --git a/src/commonTest/kotlin/exchange.dydx.abacus/functional/vault/VaultFormTests.kt b/src/commonTest/kotlin/exchange.dydx.abacus/functional/vault/VaultFormTests.kt index e9a6ac89a..5106e004c 100644 --- a/src/commonTest/kotlin/exchange.dydx.abacus/functional/vault/VaultFormTests.kt +++ b/src/commonTest/kotlin/exchange.dydx.abacus/functional/vault/VaultFormTests.kt @@ -112,7 +112,7 @@ class VaultFormTests { assertEquals( VaultFormValidationResult( errors = iListOf( - VaultFormValidationErrors.slippageTooHigh(0.02), + VaultFormValidationErrors().slippageTooHigh(0.02), ), submissionData = VaultDepositWithdrawSubmissionData( deposit = null, @@ -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( @@ -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( @@ -255,7 +255,7 @@ class VaultFormTests { assertEquals( VaultFormValidationResult( errors = iListOf( - VaultFormValidationErrors.depositTooLow(), + VaultFormValidationErrors().depositTooLow(), ), submissionData = null, summaryData = VaultFormSummaryData( diff --git a/v4_abacus.podspec b/v4_abacus.podspec index 517e07c0b..c772e1ee0 100644 --- a/v4_abacus.podspec +++ b/v4_abacus.podspec @@ -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 = ''