Skip to content

Commit

Permalink
Add TransferPriceImpactValidator (#762)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruixhuang authored Dec 3, 2024
1 parent f2ed254 commit 629fc6f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
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.13.36"
version = "1.13.37"

repositories {
google()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ValidationError>? {
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<String, Any>?,
subaccount: Map<String, Any>?,
transfer: Map<String, Any>,
configs: Map<String, Any>?,
currentBlockAndHeight: BlockAndTime?,
restricted: Boolean,
environment: V4Environment?
): List<Any>? {
return null
}
}
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.13.36'
spec.version = '1.13.37'
spec.homepage = 'https://github.com/dydxprotocol/v4-abacus'
spec.source = { :http=> ''}
spec.authors = ''
Expand Down

0 comments on commit 629fc6f

Please sign in to comment.