From 629fc6fe35b82894f6a8386ad64d4b92f2a0a05f Mon Sep 17 00:00:00 2001 From: Rui <102453770+ruixhuang@users.noreply.github.com> Date: Tue, 3 Dec 2024 06:33:20 -0800 Subject: [PATCH] Add TransferPriceImpactValidator (#762) --- build.gradle.kts | 2 +- .../validator/TransferInputValidator.kt | 2 + .../transfer/TransferPriceImpactValidator.kt | 64 +++++++++++++++++++ v4_abacus.podspec | 2 +- 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 src/commonMain/kotlin/exchange.dydx.abacus/validator/transfer/TransferPriceImpactValidator.kt diff --git a/build.gradle.kts b/build.gradle.kts index 87ce6351f..a59f18c09 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -52,7 +52,7 @@ allprojects { } group = "exchange.dydx.abacus" -version = "1.13.36" +version = "1.13.37" repositories { google() diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/validator/TransferInputValidator.kt b/src/commonMain/kotlin/exchange.dydx.abacus/validator/TransferInputValidator.kt index 965d7aea4..786f39641 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/validator/TransferInputValidator.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/validator/TransferInputValidator.kt @@ -11,6 +11,7 @@ import exchange.dydx.abacus.state.manager.V4Environment import exchange.dydx.abacus.validator.transfer.DepositValidator import exchange.dydx.abacus.validator.transfer.TransferFieldsValidator import exchange.dydx.abacus.validator.transfer.TransferOutValidator +import exchange.dydx.abacus.validator.transfer.TransferPriceImpactValidator import exchange.dydx.abacus.validator.transfer.WithdrawalCapacityValidator import exchange.dydx.abacus.validator.transfer.WithdrawalGatingValidator @@ -25,6 +26,7 @@ internal class TransferInputValidator( TransferOutValidator(localizer, formatter, parser), WithdrawalGatingValidator(localizer, formatter, parser), WithdrawalCapacityValidator(localizer, formatter, parser), + TransferPriceImpactValidator(localizer, formatter, parser), ) override fun validate( diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/validator/transfer/TransferPriceImpactValidator.kt b/src/commonMain/kotlin/exchange.dydx.abacus/validator/transfer/TransferPriceImpactValidator.kt new file mode 100644 index 000000000..70f669861 --- /dev/null +++ b/src/commonMain/kotlin/exchange.dydx.abacus/validator/transfer/TransferPriceImpactValidator.kt @@ -0,0 +1,64 @@ +package exchange.dydx.abacus.validator.transfer + +import exchange.dydx.abacus.output.input.ErrorType +import exchange.dydx.abacus.output.input.TransferType +import exchange.dydx.abacus.output.input.ValidationError +import exchange.dydx.abacus.protocols.LocalizerProtocol +import exchange.dydx.abacus.protocols.ParserProtocol +import exchange.dydx.abacus.state.app.helper.Formatter +import exchange.dydx.abacus.state.internalstate.InternalState +import exchange.dydx.abacus.state.manager.BlockAndTime +import exchange.dydx.abacus.state.manager.V4Environment +import exchange.dydx.abacus.validator.BaseInputValidator +import exchange.dydx.abacus.validator.TransferValidatorProtocol + +internal class TransferPriceImpactValidator( + localizer: LocalizerProtocol?, + formatter: Formatter?, + parser: ParserProtocol, +) : BaseInputValidator(localizer, formatter, parser), TransferValidatorProtocol { + override fun validateTransfer( + internalState: InternalState, + currentBlockAndHeight: BlockAndTime?, + restricted: Boolean, + environment: V4Environment? + ): List? { + val transfer = internalState.input.transfer + val type = transfer.type ?: return null + val aggregatePriceImpact = transfer.summary?.aggregatePriceImpact ?: return null + + val maxPriceImpact = 0.02 // 2% + + when (type) { + TransferType.deposit, TransferType.withdrawal -> { + if (aggregatePriceImpact >= maxPriceImpact) { + return listOf( + error( + type = ErrorType.error, + errorCode = "PRICE_IMPACT_TOO_HIGH", + fields = null, + actionStringKey = null, + titleStringKey = "APP.TRADE.PRICE_IMPACT", + textStringKey = "ERRORS.ONBOARDING.PRICE_IMPACT_TOO_HIGH", + ), + ) + } else { + return null + } + } + TransferType.transferOut -> return null + } + } + + override fun validateTransferDeprecated( + wallet: Map?, + subaccount: Map?, + transfer: Map, + configs: Map?, + currentBlockAndHeight: BlockAndTime?, + restricted: Boolean, + environment: V4Environment? + ): List? { + return null + } +} diff --git a/v4_abacus.podspec b/v4_abacus.podspec index 6652cdeb9..9df69ed4a 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.13.36' + spec.version = '1.13.37' spec.homepage = 'https://github.com/dydxprotocol/v4-abacus' spec.source = { :http=> ''} spec.authors = ''