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 TransferPriceImpactValidator #762

Merged
merged 3 commits into from
Dec 3, 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.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