Skip to content

Commit

Permalink
feat: router vendor env flag [OTE-351]
Browse files Browse the repository at this point in the history
  • Loading branch information
yogurtandjam committed Jun 5, 2024
1 parent ef33a43 commit 00ae245
Show file tree
Hide file tree
Showing 18 changed files with 141 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package exchange.dydx.abacus.processor.router

import exchange.dydx.abacus.output.input.SelectionOption
import exchange.dydx.abacus.output.input.TransferInputChainResource
import exchange.dydx.abacus.output.input.TransferInputTokenResource

interface IRouterProcessor {
var tokens: List<Any>?
var chains: List<Any>?
Expand Down Expand Up @@ -44,8 +48,8 @@ interface IRouterProcessor {
fun selectedTokenDecimals(tokenAddress: String?, selectedChainId: String?): String?
fun filteredTokens(chainId: String?): List<Any>?
fun defaultTokenAddress(chainId: String?): String?
fun chainResources(chainId: String?): Map<String, Any>?
fun tokenResources(chainId: String?): Map<String, Any>?
fun chainResources(chainId: String?): Map<String, TransferInputChainResource>?
fun tokenResources(chainId: String?): Map<String, TransferInputTokenResource>?
fun chainOptions(): List<Any>
fun tokenOptions(chainId: String?): List<Any>
fun tokenOptions(chainId: String?): List<SelectionOption>
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ open class StateManagerAdaptor(
Formatter(uiImplementations.formatter),
127,
false,
useSkip = appConfigs.routerVendor == AppConfigs.RouterVendor.Skip,
)

internal var indexerConfig: IndexerURIs?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ internal fun V4StateManagerAdaptor.retrieveDepositExchanges() {
}
}
exchangeList = exchanges
stateMachine.squidProcessor.exchangeDestinationChainId = configs.nobleChainId()
stateMachine.routerProcessor.exchangeDestinationChainId = configs.nobleChainId()
}
}
}
Expand All @@ -86,7 +86,7 @@ private fun V4StateManagerAdaptor.retrieveDepositRouteV1(state: PerpetualState?)
val fromChain = state?.input?.transfer?.chain
val fromToken = state?.input?.transfer?.token
val fromAmount = parser.asDecimal(state?.input?.transfer?.size?.size)?.let {
val decimals = parser.asInt(stateMachine.squidProcessor.selectedTokenDecimals(tokenAddress = fromToken, selectedChainId = fromChain))
val decimals = parser.asInt(stateMachine.routerProcessor.selectedTokenDecimals(tokenAddress = fromToken, selectedChainId = fromChain))
if (decimals != null) {
(it * Numeric.decimal.TEN.pow(decimals)).toBigInteger()
} else {
Expand Down Expand Up @@ -144,7 +144,7 @@ private fun V4StateManagerAdaptor.retrieveDepositRouteV2(state: PerpetualState?)
val fromChain = state?.input?.transfer?.chain
val fromToken = state?.input?.transfer?.token
val fromAmount = parser.asDecimal(state?.input?.transfer?.size?.size)?.let {
val decimals = parser.asInt(stateMachine.squidProcessor.selectedTokenDecimals(tokenAddress = fromToken, selectedChainId = fromChain))
val decimals = parser.asInt(stateMachine.routerProcessor.selectedTokenDecimals(tokenAddress = fromToken, selectedChainId = fromChain))
if (decimals != null) {
(it * Numeric.decimal.TEN.pow(decimals)).toBigInteger()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import exchange.dydx.abacus.state.model.onChainStakingRewards
import exchange.dydx.abacus.state.model.onChainUnbonding
import exchange.dydx.abacus.state.model.onChainUserFeeTier
import exchange.dydx.abacus.state.model.onChainUserStats
import exchange.dydx.abacus.state.model.routerChains
import exchange.dydx.abacus.state.model.routerTokens
import exchange.dydx.abacus.state.model.squidV2SdkInfo
import exchange.dydx.abacus.state.model.updateHeight
import exchange.dydx.abacus.utils.CoroutineTimer
Expand Down Expand Up @@ -305,6 +307,30 @@ class V4StateManagerAdaptor(
}
}

@Suppress("UnusedPrivateMember")
private fun retrieveSkipTransferChains() {
val oldState = stateMachine.state
val chainsUrl = configs.skipV1Chains()
get(chainsUrl, null, null) { _, response, httpCode, _ ->
if (success(httpCode) && response != null) {
update(stateMachine.routerChains(response), oldState)
}
}
}

@Suppress("UnusedPrivateMember")
private fun retrieveSkipTransferTokens() {
val oldState = stateMachine.state
val tokensUrl = configs.skipV1Assets()
// add API key injection
// val header = iMapOf("authorization" to skipAPIKey)
get(tokensUrl, null, null) { _, response, httpCode, _ ->
if (success(httpCode) && response != null) {
update(stateMachine.routerTokens(response), oldState)
}
}
}

private fun bestEffortConnectChain() {
findOptimalNode { url ->
this.validatorUrl = url
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ class AppConfigs(
}
var squidVersion: SquidVersion = SquidVersion.V2

enum class RouterVendor {
Skip,
Squid
}
var routerVendor: RouterVendor = RouterVendor.Squid

companion object {
val forApp = AppConfigs(subscribeToCandles = true, loadRemote = true)
val forAppDebug = AppConfigs(subscribeToCandles = true, loadRemote = false, enableLogger = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ class PerpTradingStateMachine(
formatter: Formatter?,
maxSubaccountNumber: Int,
useParentSubaccount: Boolean,
useSkip: Boolean
) :
TradingStateMachine(environment, localizer, formatter, maxSubaccountNumber, useParentSubaccount) {
TradingStateMachine(environment, localizer, formatter, maxSubaccountNumber, useParentSubaccount, useSkip = useSkip) {
/*
Placeholder for now. Eventually, the code specifically for Perpetual will be in this class
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import kotlinx.serialization.json.jsonObject
internal fun TradingStateMachine.routerChains(payload: String): StateChanges? {
val json = parser.decodeJsonObject(payload)
return if (json != null) {
input = squidProcessor.receivedChains(input, json)
input = routerProcessor.receivedChains(input, json)
StateChanges(iListOf(Changes.input))
} else {
StateChanges.noChange
Expand All @@ -19,7 +19,7 @@ internal fun TradingStateMachine.routerChains(payload: String): StateChanges? {
internal fun TradingStateMachine.routerTokens(payload: String): StateChanges? {
val json = parser.decodeJsonObject(payload)
return if (json != null) {
input = squidProcessor.receivedTokens(input, json)
input = routerProcessor.receivedTokens(input, json)
StateChanges(iListOf(Changes.input))
} else {
StateChanges.noChange
Expand All @@ -29,7 +29,7 @@ internal fun TradingStateMachine.routerTokens(payload: String): StateChanges? {
internal fun TradingStateMachine.squidV2SdkInfo(payload: String): StateChanges? {
val json = parser.decodeJsonObject(payload)
return if (json != null) {
input = squidProcessor.receivedV2SdkInfo(input, json)
input = routerProcessor.receivedV2SdkInfo(input, json)
StateChanges(iListOf(Changes.input))
} else {
StateChanges.noChange
Expand All @@ -43,7 +43,7 @@ internal fun TradingStateMachine.squidRoute(
): StateChanges? {
val json = parser.decodeJsonObject(payload)
return if (json != null) {
input = squidProcessor.receivedRoute(input, json, requestId)
input = routerProcessor.receivedRoute(input, json, requestId)
StateChanges(
iListOf(Changes.input, Changes.subaccount),
subaccountNumbers = iListOf(subaccountNumber),
Expand All @@ -60,7 +60,7 @@ internal fun TradingStateMachine.squidRouteV2(
): StateChanges? {
val json = parser.decodeJsonObject(payload)
return if (json != null) {
input = squidProcessor.receivedRouteV2(input, json, requestId)
input = routerProcessor.receivedRouteV2(input, json, requestId)
StateChanges(
iListOf(Changes.input, Changes.subaccount),
subaccountNumbers = iListOf(subaccountNumber),
Expand All @@ -75,6 +75,6 @@ internal fun TradingStateMachine.squidStatus(
transactionId: String?
): StateChanges? {
val json = Json.parseToJsonElement(payload).jsonObject.toMap()
transferStatuses = squidProcessor.receivedStatus(transferStatuses, json, transactionId)
transferStatuses = routerProcessor.receivedStatus(transferStatuses, json, transactionId)
return StateChanges(iListOf(Changes.transferStatuses))
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ fun TradingStateMachine.transfer(
transfer.safeSet("chain", "chain")
transfer.safeSet("token", "usdc")
} else {
val chainType = squidProcessor.defaultChainId()
val chainType = routerProcessor.defaultChainId()
if (chainType != null) {
updateTransferToChainType(transfer, chainType)
}
transfer.safeSet("token", squidProcessor.defaultTokenAddress(chainType))
transfer.safeSet("token", routerProcessor.defaultTokenAddress(chainType))
}
}
changes = StateChanges(
Expand Down Expand Up @@ -200,25 +200,25 @@ private fun TradingStateMachine.updateTransferToTokenType(transfer: MutableMap<S
} else {
transfer.safeSet(
"resources.tokenSymbol",
squidProcessor.selectedTokenSymbol(tokenAddress = tokenAddress, selectedChainId = selectedChainId),
routerProcessor.selectedTokenSymbol(tokenAddress = tokenAddress, selectedChainId = selectedChainId),
)
transfer.safeSet(
"resources.tokenDecimals",
squidProcessor.selectedTokenDecimals(tokenAddress = tokenAddress, selectedChainId = selectedChainId),
routerProcessor.selectedTokenDecimals(tokenAddress = tokenAddress, selectedChainId = selectedChainId),
)
}
transfer.safeSet("route", null)
transfer.safeSet("requestPayload", null)
}

private fun TradingStateMachine.updateTransferToChainType(transfer: MutableMap<String, Any>, chainType: String) {
val tokenOptions = squidProcessor.tokenOptions(chainType)
val tokenOptions = routerProcessor.tokenOptions(chainType)
if (transfer["type"] != "TRANSFER_OUT") {
internalState.transfer.tokens = tokenOptions
transfer.safeSet("chain", chainType)
transfer.safeSet("token", squidProcessor.defaultTokenAddress(chainType))
internalState.transfer.chainResources = squidProcessor.chainResources(chainType)
internalState.transfer.tokenResources = squidProcessor.tokenResources(chainType)
transfer.safeSet("token", routerProcessor.defaultTokenAddress(chainType))
internalState.transfer.chainResources = routerProcessor.chainResources(chainType)
internalState.transfer.tokenResources = routerProcessor.tokenResources(chainType)
}
transfer.safeSet("exchange", null)
transfer.safeSet("size.size", null)
Expand All @@ -235,21 +235,21 @@ private fun TradingStateMachine.updateTransferToChainType(transfer: MutableMap<S
)
transfer.safeSet(
"resources.chainResources",
squidProcessor.chainResources(chainType),
routerProcessor.chainResources(chainType),
)
transfer.safeSet(
"resources.tokenResources",
squidProcessor.tokenResources(chainType),
routerProcessor.tokenResources(chainType),
)
}

private fun TradingStateMachine.updateTransferExchangeType(transfer: MutableMap<String, Any>, exchange: String) {
val exchangeDestinationChainId = squidProcessor.exchangeDestinationChainId
val tokenOptions = squidProcessor.tokenOptions(exchangeDestinationChainId)
val exchangeDestinationChainId = routerProcessor.exchangeDestinationChainId
val tokenOptions = routerProcessor.tokenOptions(exchangeDestinationChainId)
if (transfer["type"] != "TRANSFER_OUT") {
internalState.transfer.tokens = tokenOptions
transfer.safeSet("token", squidProcessor.defaultTokenAddress(exchangeDestinationChainId))
internalState.transfer.tokenResources = squidProcessor.tokenResources(exchangeDestinationChainId)
transfer.safeSet("token", routerProcessor.defaultTokenAddress(exchangeDestinationChainId))
internalState.transfer.tokenResources = routerProcessor.tokenResources(exchangeDestinationChainId)

// needed to pass tests, remove later
transfer.safeSet(
Expand All @@ -262,7 +262,7 @@ private fun TradingStateMachine.updateTransferExchangeType(transfer: MutableMap<
)
transfer.safeSet(
"resources.tokenResources",
squidProcessor.tokenResources(exchangeDestinationChainId),
routerProcessor.tokenResources(exchangeDestinationChainId),
)
}
transfer.safeSet("exchange", exchange)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import exchange.dydx.abacus.processor.assets.AssetsProcessor
import exchange.dydx.abacus.processor.configs.ConfigsProcessor
import exchange.dydx.abacus.processor.launchIncentive.LaunchIncentiveProcessor
import exchange.dydx.abacus.processor.markets.MarketsSummaryProcessor
import exchange.dydx.abacus.processor.router.IRouterProcessor
import exchange.dydx.abacus.processor.router.skip.SkipProcessor
import exchange.dydx.abacus.processor.router.squid.SquidProcessor
import exchange.dydx.abacus.processor.wallet.WalletProcessor
import exchange.dydx.abacus.protocols.LocalizerProtocol
Expand Down Expand Up @@ -81,6 +83,7 @@ open class TradingStateMachine(
private val formatter: Formatter?,
private val maxSubaccountNumber: Int,
private val useParentSubaccount: Boolean,
val useSkip: Boolean,
) {
internal val internalState: InternalState = InternalState()

Expand All @@ -93,7 +96,7 @@ open class TradingStateMachine(
}
internal val walletProcessor = WalletProcessor(parser)
internal val configsProcessor = ConfigsProcessor(parser)
internal val squidProcessor = SquidProcessor(parser, internalState.transfer)
internal val routerProcessor = constructRouterProcessor()
internal val rewardsProcessor = RewardsProcessor(parser)
internal val launchIncentiveProcessor = LaunchIncentiveProcessor(parser)

Expand Down Expand Up @@ -258,6 +261,11 @@ open class TradingStateMachine(
return socket(url, json, subaccountNumber, height)
}

private fun constructRouterProcessor(): IRouterProcessor {
if (useSkip) return SkipProcessor(parser = parser, internalState = internalState.transfer)
return SquidProcessor(parser = parser, internalState = internalState.transfer)
}

@Throws(Exception::class)
private fun socket(
url: AbUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import exchange.dydx.abacus.state.v2.supervisor.ConnectionDelegate
import exchange.dydx.abacus.state.v2.supervisor.ConnectionsSupervisor
import exchange.dydx.abacus.state.v2.supervisor.MarketsSupervisor
import exchange.dydx.abacus.state.v2.supervisor.NetworkHelper
import exchange.dydx.abacus.state.v2.supervisor.OnboardingConfigs
import exchange.dydx.abacus.state.v2.supervisor.OnboardingSupervisor
import exchange.dydx.abacus.state.v2.supervisor.SystemSupervisor
import exchange.dydx.abacus.state.v2.supervisor.accountAddress
Expand Down Expand Up @@ -110,6 +111,7 @@ internal class StateManagerAdaptorV2(
Formatter(uiImplementations.formatter),
127,
appConfigs.accountConfigs.subaccountConfigs.useParentSubaccount,
useSkip = appConfigs.onboardingConfigs.routerVendor == OnboardingConfigs.RouterVendor.Skip,
)

internal val jsonEncoder = JsonEncoder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,13 @@ data class OnboardingConfigs(

var squidVersion: SquidVersion = SquidVersion.V2

enum class RouterVendor {
Squid,
Skip
}

var routerVendor: RouterVendor = RouterVendor.Squid

companion object {
val forApp = OnboardingConfigs(
retrieveSquidRoutes = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ internal class OnboardingSupervisor(
}
}

@Suppress("UnusedPrivateMember")
private fun retrieveTransferAssets() {
val oldState = stateMachine.state
val url = helper.configs.squidV2Assets()
Expand Down Expand Up @@ -147,7 +148,7 @@ internal class OnboardingSupervisor(
}
}
ExchangeConfig.exchangeList = exchanges
stateMachine.squidProcessor.exchangeDestinationChainId =
stateMachine.routerProcessor.exchangeDestinationChainId =
helper.configs.nobleChainId()
}
}
Expand Down Expand Up @@ -197,7 +198,7 @@ internal class OnboardingSupervisor(
val fromToken = state?.input?.transfer?.token
val fromAmount = helper.parser.asDecimal(state?.input?.transfer?.size?.size)?.let {
val decimals =
helper.parser.asInt(stateMachine.squidProcessor.selectedTokenDecimals(tokenAddress = fromToken, selectedChainId = fromChain))
helper.parser.asInt(stateMachine.routerProcessor.selectedTokenDecimals(tokenAddress = fromToken, selectedChainId = fromChain))
if (decimals != null) {
(it * Numeric.decimal.TEN.pow(decimals)).toBigInteger()
} else {
Expand Down Expand Up @@ -259,7 +260,7 @@ internal class OnboardingSupervisor(
val fromToken = state?.input?.transfer?.token
val fromAmount = helper.parser.asDecimal(state?.input?.transfer?.size?.size)?.let {
val decimals =
helper.parser.asInt(stateMachine.squidProcessor.selectedTokenDecimals(tokenAddress = fromToken, selectedChainId = fromChain))
helper.parser.asInt(stateMachine.routerProcessor.selectedTokenDecimals(tokenAddress = fromToken, selectedChainId = fromChain))
if (decimals != null) {
(it * Numeric.decimal.TEN.pow(decimals)).toBigInteger()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ open class BaseTests(private val maxSubaccountNumber: Int, internal val useParen
formatter = null,
maxSubaccountNumber = maxSubaccountNumber,
useParentSubaccount = useParentSubaccount,
useSkip = false,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ open class V4BaseTests(useParentSubaccount: Boolean = false) : BaseTests(127, us
internal val testRestUrl =
"https://indexer.v4staging.dydx.exchange"
override fun createState(useParentSubaccount: Boolean): PerpTradingStateMachine {
return PerpTradingStateMachine(mock.v4Environment, null, null, 127, useParentSubaccount)
return PerpTradingStateMachine(mock.v4Environment, null, null, 127, useParentSubaccount, useSkip = false)
}

internal open fun loadMarkets(): StateResponse {
Expand Down
Loading

0 comments on commit 00ae245

Please sign in to comment.