From 44b24caedf9f75dc9603f0b7fd36919ae060ce99 Mon Sep 17 00:00:00 2001 From: jeremy lee Date: Wed, 5 Jun 2024 20:36:58 +0300 Subject: [PATCH 1/9] feat: top level withdrawal methods [OTE-389] --- .../v2/supervisor/OnboardingSupervisor.kt | 170 ++++++++++++++++++ 1 file changed, 170 insertions(+) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt index 3bb246f4c..b7dcc5298 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt @@ -884,6 +884,176 @@ internal class OnboardingSupervisor( } } + private fun retrieveSkipWithdrawalRouteExchange( + state: PerpetualState?, + decimals: Int, + gas: BigDecimal, + accountAddress: String, + sourceAddress: String, + subaccountNumber: Int?, + ) { + val usdcSize = helper.parser.asDecimal(state?.input?.transfer?.size?.usdcSize) + val fromAmount = if (usdcSize != null && usdcSize > gas) { + ((usdcSize - gas) * Numeric.decimal.TEN.pow(decimals)).toBigInteger() + } else { + null + } + val chainId = helper.environment.dydxChainId + val squidIntegratorId = helper.environment.squidIntegratorId + val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom + val fromAmountString = helper.parser.asString(fromAmount) + val url = helper.configs.skipV2MsgsDirect() + val fromAddress = accountAddress + if ( + fromAmount != null && + fromAmount > 0 && + fromAmountString != null && + chainId != null && + dydxTokenDemon != null && + squidIntegratorId != null + ) { + val body: Map = mapOf( + "amount_in" to fromAmountString, + "source_asset_denom" to dydxTokenDemon, + "source_asset_chain_id" to chainId, + "dest_asset_denom" to dydxTokenDemon, + "dest_asset_chain_id" to chainId, + "chain_ids_to_addresses" to mapOf( + "fromChain" to sourceAddress, + "toChain" to fromAddress, + ), + "slippage_tolerance_percent" to SLIPPAGE_PERCENT, + ) + + val oldState = stateMachine.state + val header = iMapOf( + "Content-Type" to "application/json", + ) + helper.post(url, header, body.toJsonPrettyPrint()) { _, response, code, headers -> + if (response != null) { + val requestId = helper.parser.asString(headers?.get("x-request-id")) + update(stateMachine.squidRoute(response, subaccountNumber ?: 0, requestId), oldState) + } else { + Logger.e { "retrieveSkipWithdrawalRouteExchange error, code: $code" } + } + } + } + } + + private fun retrieveSkipWithdrawalRouteNonCCTP( + state: PerpetualState?, + decimals: Int, + gas: BigDecimal, + accountAddress: String, + sourceAddress: String, + subaccountNumber: Int?, + ) { + val usdcSize = helper.parser.asDecimal(state?.input?.transfer?.size?.usdcSize) + val fromAmount = if (usdcSize != null && usdcSize > gas) { + ((usdcSize - gas) * Numeric.decimal.TEN.pow(decimals)).toBigInteger() + } else { + null + } + val chainId = helper.environment.dydxChainId + val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom + val fromAmountString = helper.parser.asString(fromAmount) + val url = helper.configs.squidRoute() + val fromAddress = accountAddress + if ( + fromAmount != null && + fromAmount > 0 && + fromAmountString != null && + chainId != null && + dydxTokenDemon != null && + url != null + ) { + val body: Map = mapOf( + "amount_in" to fromAmountString, + "source_asset_denom" to dydxTokenDemon, + "source_asset_chain_id" to chainId, + "dest_asset_denom" to dydxTokenDemon, + "dest_asset_chain_id" to chainId, + "chain_ids_to_addresses" to mapOf( + "fromChain" to sourceAddress, + "toChain" to fromAddress, + ), + "slippage_tolerance_percent" to SLIPPAGE_PERCENT, + ) + val header = iMapOf( + "Content-Type" to "application/json", + ) + val oldState = stateMachine.state + helper.post(url, header, body.toJsonPrettyPrint()) { _, response, code, headers -> + if (response != null) { + val requestId = helper.parser.asString(headers?.get("x-request-id")) + update(stateMachine.squidRoute(response, subaccountNumber ?: 0, requestId), oldState) + } else { + Logger.e { "retrieveSkipWithdrawalRouteNonCCTP error, code: $code" } + } + } + } + } + + private fun retrieveSkipWithdrawalRouteCCTP( + state: PerpetualState?, + decimals: Int, + gas: BigDecimal, + accountAddress: String, + sourceAddress: String, + subaccountNumber: Int?, + ) { + val usdcSize = helper.parser.asDecimal(state?.input?.transfer?.size?.usdcSize) + val fromAmount = if (usdcSize != null && usdcSize > gas) { + ((usdcSize - gas) * Numeric.decimal.TEN.pow(decimals)).toBigInteger() + } else { + null + } + val chainId = helper.environment.dydxChainId + val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom + val fromAmountString = helper.parser.asString(fromAmount) + val url = helper.configs.skipV2MsgsDirect() + val fromChain = helper.configs.nobleChainId() + val fromToken = helper.configs.nobleDenom() + if ( + fromAmount != null && + fromAmount > 0 && + fromAmountString != null && + chainId != null && + dydxTokenDemon != null && + fromChain != null && + fromToken != null + ) { + val body: Map = mapOf( + "amount_in" to fromAmountString, + "source_asset_denom" to fromToken, + "source_asset_chain_id" to fromChain, + "dest_asset_denom" to dydxTokenDemon, + "dest_asset_chain_id" to chainId, + "chain_ids_to_addresses" to mapOf( + "fromChain" to sourceAddress, + "toChain" to accountAddress, + ), + "slippage_tolerance_percent" to SLIPPAGE_PERCENT, + ) + val oldState = stateMachine.state + val header = iMapOf( + "Content-Type" to "application/json", + ) + helper.post(url, header, body.toJsonPrettyPrint()) { url, response, code, headers -> + if (response != null) { + val currentFromAmount = stateMachine.state?.input?.transfer?.size?.size + val oldFromAmount = oldState?.input?.transfer?.size?.size + val requestId = helper.parser.asString(headers?.get("x-request-id")) + if (currentFromAmount == oldFromAmount) { + update(stateMachine.squidRouteV2(response, subaccountNumber ?: 0, requestId), oldState) + } + } else { + Logger.e { "retrieveSkipWithdrawalRouteCCTP error, code: $code" } + } + } + } + } + private fun fetchTransferStatus( hash: String, fromChainId: String?, From 1d6495791a4834bfa784f2661d9f14439aa3bf1d Mon Sep 17 00:00:00 2001 From: jeremy lee Date: Wed, 5 Jun 2024 20:45:52 +0300 Subject: [PATCH 2/9] suppres error --- .../state/v2/supervisor/OnboardingSupervisor.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt index b7dcc5298..a01bc710c 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt @@ -884,6 +884,7 @@ internal class OnboardingSupervisor( } } + @Suppress("UnusedPrivateMember") private fun retrieveSkipWithdrawalRouteExchange( state: PerpetualState?, decimals: Int, @@ -940,6 +941,7 @@ internal class OnboardingSupervisor( } } + @Suppress("UnusedPrivateMember") private fun retrieveSkipWithdrawalRouteNonCCTP( state: PerpetualState?, decimals: Int, @@ -994,6 +996,7 @@ internal class OnboardingSupervisor( } } + @Suppress("UnusedPrivateMember") private fun retrieveSkipWithdrawalRouteCCTP( state: PerpetualState?, decimals: Int, From c72c521c7662793959bbb3460f844936f052ea14 Mon Sep 17 00:00:00 2001 From: jeremy lee Date: Thu, 6 Jun 2024 00:10:40 +0300 Subject: [PATCH 3/9] cleanup unused requestId param. add TODO for renaming squidRoute method --- .../state/model/TradingStateMachine+Squid.kt | 1 + .../state/v2/supervisor/OnboardingSupervisor.kt | 9 +++------ 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Squid.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Squid.kt index ec0831841..cee1a2089 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Squid.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Squid.kt @@ -39,6 +39,7 @@ internal fun TradingStateMachine.squidV2SdkInfo(payload: String): StateChanges? } } +// TODO: rename this to routerRoute/routerMsg/routerTxPayload internal fun TradingStateMachine.squidRoute( payload: String, subaccountNumber: Int, diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt index a01bc710c..d96713db1 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt @@ -932,8 +932,7 @@ internal class OnboardingSupervisor( ) helper.post(url, header, body.toJsonPrettyPrint()) { _, response, code, headers -> if (response != null) { - val requestId = helper.parser.asString(headers?.get("x-request-id")) - update(stateMachine.squidRoute(response, subaccountNumber ?: 0, requestId), oldState) + update(stateMachine.squidRoute(response, subaccountNumber ?: 0, null), oldState) } else { Logger.e { "retrieveSkipWithdrawalRouteExchange error, code: $code" } } @@ -987,8 +986,7 @@ internal class OnboardingSupervisor( val oldState = stateMachine.state helper.post(url, header, body.toJsonPrettyPrint()) { _, response, code, headers -> if (response != null) { - val requestId = helper.parser.asString(headers?.get("x-request-id")) - update(stateMachine.squidRoute(response, subaccountNumber ?: 0, requestId), oldState) + update(stateMachine.squidRoute(response, subaccountNumber ?: 0, null), oldState) } else { Logger.e { "retrieveSkipWithdrawalRouteNonCCTP error, code: $code" } } @@ -1046,9 +1044,8 @@ internal class OnboardingSupervisor( if (response != null) { val currentFromAmount = stateMachine.state?.input?.transfer?.size?.size val oldFromAmount = oldState?.input?.transfer?.size?.size - val requestId = helper.parser.asString(headers?.get("x-request-id")) if (currentFromAmount == oldFromAmount) { - update(stateMachine.squidRouteV2(response, subaccountNumber ?: 0, requestId), oldState) + update(stateMachine.squidRoute(response, subaccountNumber ?: 0, null), oldState) } } else { Logger.e { "retrieveSkipWithdrawalRouteCCTP error, code: $code" } From b62e37b21637c55e684ac43b78d32e890dd9e3a8 Mon Sep 17 00:00:00 2001 From: jeremy lee Date: Thu, 6 Jun 2024 00:47:04 +0300 Subject: [PATCH 4/9] suppres error --- .../state/model/TradingStateMachine+Squid.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Squid.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Squid.kt index cee1a2089..9eaa6a432 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Squid.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Squid.kt @@ -39,6 +39,7 @@ internal fun TradingStateMachine.squidV2SdkInfo(payload: String): StateChanges? } } +@Suppress("ForbiddenComment") // TODO: rename this to routerRoute/routerMsg/routerTxPayload internal fun TradingStateMachine.squidRoute( payload: String, From f71e96c449bcb8d2639b856c39bb728fa2774892 Mon Sep 17 00:00:00 2001 From: mobile-build-bot-git Date: Thu, 13 Jun 2024 23:36:44 +0000 Subject: [PATCH 5/9] Bump version --- build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index 503eea97d..d829c48d7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -51,7 +51,7 @@ allprojects { } group = "exchange.dydx.abacus" -version = "1.7.73" +version = "1.7.74" repositories { google() From e5069ed28131f39404545c753f7f11387811bcce Mon Sep 17 00:00:00 2001 From: jeremy lee Date: Fri, 14 Jun 2024 07:47:57 -0400 Subject: [PATCH 6/9] fix onboarding supervisor by pulling from next branch --- .../v2/supervisor/OnboardingSupervisor.kt | 284 +++++++++++++++--- 1 file changed, 247 insertions(+), 37 deletions(-) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt index d96713db1..2e8f048f0 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt @@ -3,6 +3,7 @@ package exchange.dydx.abacus.state.v2.supervisor import com.ionspin.kotlin.bignum.decimal.BigDecimal import exchange.dydx.abacus.output.PerpetualState import exchange.dydx.abacus.output.input.TransferType +import exchange.dydx.abacus.processor.router.skip.SkipRoutePayloadProcessor import exchange.dydx.abacus.protocols.ThreadingType import exchange.dydx.abacus.protocols.TransactionCallback import exchange.dydx.abacus.protocols.TransactionType @@ -68,11 +69,15 @@ internal class OnboardingSupervisor( } private fun retrieveSquidRoutes() { - retrieveTransferAssets() + if (stateMachine.useSkip) { + retrieveSkipTransferChains() + retrieveSkipTransferTokens() + } else { + retrieveTransferAssets() + } retrieveCctpChainIds() } - @Suppress("UnusedPrivateMember") private fun retrieveSkipTransferChains() { val oldState = stateMachine.state val chainsUrl = helper.configs.skipV1Chains() @@ -83,11 +88,10 @@ internal class OnboardingSupervisor( } } - @Suppress("UnusedPrivateMember") private fun retrieveSkipTransferTokens() { val oldState = stateMachine.state val tokensUrl = helper.configs.skipV1Assets() -// add API key injection +// add API key injection for all skip methods // val header = iMapOf("authorization" to skipAPIKey) helper.get(tokensUrl, null, null) { _, response, httpCode, _ -> if (helper.success(httpCode) && response != null) { @@ -96,7 +100,6 @@ internal class OnboardingSupervisor( } } - @Suppress("UnusedPrivateMember") private fun retrieveTransferAssets() { val oldState = stateMachine.state val url = helper.configs.squidV2Assets() @@ -162,6 +165,24 @@ internal class OnboardingSupervisor( subaccountNumber: Int?, ) { val isCctp = state?.input?.transfer?.isCctp ?: false + if (stateMachine.useSkip) { + if (isCctp) { + retrieveSkipDepositRouteCCTP( + state = state, + accountAddress = accountAddress, + sourceAddress = sourceAddress, + subaccountNumber = subaccountNumber, + ) + } else { + retrieveSkipDepositRouteNonCCTP( + state, + accountAddress, + sourceAddress, + subaccountNumber, + ) + } + return + } when (configs.squidVersion) { OnboardingConfigs.SquidVersion.V2WithdrawalOnly -> retrieveDepositRouteV1( state, @@ -213,7 +234,7 @@ internal class OnboardingSupervisor( val chainId = helper.environment.dydxChainId val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) - val url = helper.configs.squidRoute() + val url = helper.configs.skipV2MsgsDirect() if (fromChain != null && fromToken != null && fromAmount != null && fromAmount > 0 && @@ -276,7 +297,7 @@ internal class OnboardingSupervisor( val chainId = helper.environment.dydxChainId val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) - val nobleAddress = accountAddress?.toNobleAddress() + val nobleAddress = accountAddress.toNobleAddress() val url = helper.configs.skipV2MsgsDirect() val toChain = helper.configs.nobleChainId() val toToken = helper.configs.nobleDenom() @@ -301,6 +322,9 @@ internal class OnboardingSupervisor( toChain to nobleAddress, ), "slippage_tolerance_percent" to SLIPPAGE_PERCENT, + "bridges" to listOf( + "CCTP", + ), ) val oldState = stateMachine.state val header = iMapOf( @@ -647,6 +671,37 @@ internal class OnboardingSupervisor( val isCctp = CctpConfig.cctpChainIds?.any { it.isCctpEnabled(state?.input?.transfer) } ?: false val isExchange = state?.input?.transfer?.exchange != null + if (stateMachine.useSkip) { + if (isCctp) { + retrieveSkipWithdrawalRouteCCTP( + state, + decimals, + gas, + accountAddress, + sourceAddress, + subaccountNumber, + ) + } else if (isExchange) { + retrieveSkipWithdrawalRouteExchange( + state, + decimals, + gas, + accountAddress, + sourceAddress, + subaccountNumber, + ) + } else { + retrieveSkipWithdrawalRouteNonCCTP( + state, + decimals, + gas, + accountAddress, + sourceAddress, + subaccountNumber, + ) + } + return + } when (configs.squidVersion) { OnboardingConfigs.SquidVersion.V2DepositOnly -> retrieveWithdrawalRouteV1( state, @@ -884,7 +939,6 @@ internal class OnboardingSupervisor( } } - @Suppress("UnusedPrivateMember") private fun retrieveSkipWithdrawalRouteExchange( state: PerpetualState?, decimals: Int, @@ -893,14 +947,16 @@ internal class OnboardingSupervisor( sourceAddress: String, subaccountNumber: Int?, ) { + val toChain = helper.configs.nobleChainId() + val toToken = helper.configs.nobleDenom() + val toAddress = state?.input?.transfer?.address val usdcSize = helper.parser.asDecimal(state?.input?.transfer?.size?.usdcSize) val fromAmount = if (usdcSize != null && usdcSize > gas) { ((usdcSize - gas) * Numeric.decimal.TEN.pow(decimals)).toBigInteger() } else { null } - val chainId = helper.environment.dydxChainId - val squidIntegratorId = helper.environment.squidIntegratorId + val fromChain = helper.environment.dydxChainId val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val url = helper.configs.skipV2MsgsDirect() @@ -909,19 +965,20 @@ internal class OnboardingSupervisor( fromAmount != null && fromAmount > 0 && fromAmountString != null && - chainId != null && - dydxTokenDemon != null && - squidIntegratorId != null + fromChain != null && + toToken != null && + toChain != null && + dydxTokenDemon != null ) { val body: Map = mapOf( "amount_in" to fromAmountString, "source_asset_denom" to dydxTokenDemon, - "source_asset_chain_id" to chainId, - "dest_asset_denom" to dydxTokenDemon, - "dest_asset_chain_id" to chainId, + "source_asset_chain_id" to fromChain, + "dest_asset_denom" to toToken, + "dest_asset_chain_id" to toChain, "chain_ids_to_addresses" to mapOf( - "fromChain" to sourceAddress, - "toChain" to fromAddress, + fromChain to fromAddress, + toChain to toAddress, ), "slippage_tolerance_percent" to SLIPPAGE_PERCENT, ) @@ -940,7 +997,6 @@ internal class OnboardingSupervisor( } } - @Suppress("UnusedPrivateMember") private fun retrieveSkipWithdrawalRouteNonCCTP( state: PerpetualState?, decimals: Int, @@ -949,34 +1005,39 @@ internal class OnboardingSupervisor( sourceAddress: String, subaccountNumber: Int?, ) { + val toChain = state?.input?.transfer?.chain + val toToken = state?.input?.transfer?.token + val toAddress = state?.input?.transfer?.address val usdcSize = helper.parser.asDecimal(state?.input?.transfer?.size?.usdcSize) val fromAmount = if (usdcSize != null && usdcSize > gas) { ((usdcSize - gas) * Numeric.decimal.TEN.pow(decimals)).toBigInteger() } else { null } - val chainId = helper.environment.dydxChainId - val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom + val fromChain = helper.environment.dydxChainId + val fromToken = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) - val url = helper.configs.squidRoute() + val url = helper.configs.skipV2MsgsDirect() val fromAddress = accountAddress if ( + toChain != null && + toToken != null && + toAddress != null && fromAmount != null && fromAmount > 0 && fromAmountString != null && - chainId != null && - dydxTokenDemon != null && - url != null + fromChain != null && + fromToken != null ) { val body: Map = mapOf( "amount_in" to fromAmountString, - "source_asset_denom" to dydxTokenDemon, - "source_asset_chain_id" to chainId, - "dest_asset_denom" to dydxTokenDemon, - "dest_asset_chain_id" to chainId, + "source_asset_denom" to fromToken, + "source_asset_chain_id" to fromChain, + "dest_asset_denom" to toToken, + "dest_asset_chain_id" to toChain, "chain_ids_to_addresses" to mapOf( - "fromChain" to sourceAddress, - "toChain" to fromAddress, + fromChain to sourceAddress, + toChain to fromAddress, ), "slippage_tolerance_percent" to SLIPPAGE_PERCENT, ) @@ -994,7 +1055,6 @@ internal class OnboardingSupervisor( } } - @Suppress("UnusedPrivateMember") private fun retrieveSkipWithdrawalRouteCCTP( state: PerpetualState?, decimals: Int, @@ -1003,6 +1063,9 @@ internal class OnboardingSupervisor( sourceAddress: String, subaccountNumber: Int?, ) { + val toChain = state?.input?.transfer?.chain + val toToken = state?.input?.transfer?.token + val toAddress = state?.input?.transfer?.address val usdcSize = helper.parser.asDecimal(state?.input?.transfer?.size?.usdcSize) val fromAmount = if (usdcSize != null && usdcSize > gas) { ((usdcSize - gas) * Numeric.decimal.TEN.pow(decimals)).toBigInteger() @@ -1013,11 +1076,15 @@ internal class OnboardingSupervisor( val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val url = helper.configs.skipV2MsgsDirect() + val fromAddress = accountAddress.toNobleAddress() + val fromChain = helper.configs.nobleChainId() val fromToken = helper.configs.nobleDenom() - if ( + if (toChain != null && + toToken != null && fromAmount != null && fromAmount > 0 && + fromAddress != null && fromAmountString != null && chainId != null && dydxTokenDemon != null && @@ -1028,13 +1095,14 @@ internal class OnboardingSupervisor( "amount_in" to fromAmountString, "source_asset_denom" to fromToken, "source_asset_chain_id" to fromChain, - "dest_asset_denom" to dydxTokenDemon, - "dest_asset_chain_id" to chainId, + "dest_asset_denom" to toToken, + "dest_asset_chain_id" to toChain, "chain_ids_to_addresses" to mapOf( - "fromChain" to sourceAddress, - "toChain" to accountAddress, + fromChain to fromAddress, + toChain to toAddress, ), "slippage_tolerance_percent" to SLIPPAGE_PERCENT, + "smart_relay" to false, ) val oldState = stateMachine.state val header = iMapOf( @@ -1278,6 +1346,7 @@ internal class OnboardingSupervisor( if (usdcSize > Numeric.double.ZERO) { simulateWithdrawal(decimals, subaccountNumber) { gasFee -> if (gasFee != null) { +// return back and branch from top level method cctpToNoble( state, decimals, @@ -1287,6 +1356,7 @@ internal class OnboardingSupervisor( callback, ) } else { +// return back and branch from top level method cctpToNoble( state, decimals, @@ -1305,6 +1375,10 @@ internal class OnboardingSupervisor( } } + /** + * Computes whether to serve skip or squid cctpToNoble method + * The cctpToNoble methods move cctp funds from dydx subaccount wallet to the noble wallet + */ private fun cctpToNoble( state: PerpetualState?, decimals: Int, @@ -1312,6 +1386,35 @@ internal class OnboardingSupervisor( accountAddress: String, subaccountNumber: Int?, callback: TransactionCallback + ) { + if (stateMachine.useSkip) { + cctpToNobleSkip( + state, + decimals, + gas, + accountAddress, + subaccountNumber, + callback, + ) + } else { + cctpToNobleSquid( + state, + decimals, + gas, + accountAddress, + subaccountNumber, + callback, + ) + } + } + + private fun cctpToNobleSquid( + state: PerpetualState?, + decimals: Int, + gas: BigDecimal, + accountAddress: String, + subaccountNumber: Int?, + callback: TransactionCallback ) { val url = helper.configs.squidRoute() val nobleChain = helper.configs.nobleChainId() @@ -1376,6 +1479,7 @@ internal class OnboardingSupervisor( helper.send(error, callback) } else { pendingCctpWithdraw = CctpWithdrawState( +// we use skip state with squid route state?.input?.transfer?.requestPayload?.data, callback, ) @@ -1406,4 +1510,110 @@ internal class OnboardingSupervisor( helper.send(error, callback) } } + + @Suppress("ForbiddenComment") + private fun cctpToNobleSkip( + state: PerpetualState?, + decimals: Int, + gas: BigDecimal, + accountAddress: String, + subaccountNumber: Int?, + callback: TransactionCallback + ) { +// We have a lot of duplicate code for these deposit/withdrawal route calls +// It's easier to dedupe now that the url is the same and only the args differ +// TODO: Consider creating generateArgs fun to reduce code duplication + val url = helper.configs.skipV2MsgsDirect() + val nobleChain = helper.configs.nobleChainId() + val nobleToken = helper.configs.nobleDenom() + val nobleAddress = accountAddress.toNobleAddress() + val chainId = helper.environment.dydxChainId + val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom + val usdcSize = helper.parser.asDecimal(state?.input?.transfer?.size?.usdcSize) + val fromAmount = if (usdcSize != null && usdcSize > gas) { + ((usdcSize - gas) * Numeric.decimal.TEN.pow(decimals)).toBigInteger() + } else { + null + } + val fromAmountString = helper.parser.asString(fromAmount) + + if ( + nobleChain != null && + nobleToken != null && + nobleAddress != null && + chainId != null && + dydxTokenDemon != null && + fromAmountString != null && fromAmount != null && fromAmount > 0 + ) { + val body: Map = mapOf( + "amount_in" to fromAmountString, +// from dydx denom and chain + "source_asset_denom" to dydxTokenDemon, + "source_asset_chain_id" to chainId, +// to noble denom and chain + "dest_asset_denom" to nobleToken, + "dest_asset_chain_id" to nobleChain, + "chain_ids_to_addresses" to mapOf( + chainId to accountAddress, + nobleChain to nobleAddress, + ), + "slippage_tolerance_percent" to SLIPPAGE_PERCENT, + ) + + val header = iMapOf( + "Content-Type" to "application/json", + ) + helper.post(url, header, body.toJsonPrettyPrint()) { _, response, code, _ -> + val json = helper.parser.decodeJsonObject(response) + if (json != null) { + val skipRoutePayloadProcessor = SkipRoutePayloadProcessor(parser = helper.parser) + val processedPayload = skipRoutePayloadProcessor.received(existing = mapOf(), payload = json) + val ibcPayload = helper.parser.asString( + processedPayload.get("data"), + ) + if (ibcPayload != null) { + val payload = helper.jsonEncoder.encode( + mapOf( + "subaccountNumber" to (subaccountNumber ?: 0), + "amount" to state?.input?.transfer?.size?.usdcSize, + "ibcPayload" to ibcPayload.encodeBase64(), + ), + ) + helper.transaction(TransactionType.WithdrawToNobleIBC, payload) { + val error = parseTransactionResponse(it) + if (error != null) { + Logger.e { "withdrawToNobleIBC error: $error" } + helper.send(error, callback) + } else { + pendingCctpWithdraw = CctpWithdrawState( + state?.input?.transfer?.requestPayload?.data, + callback, + ) + } + } + } else { + Logger.e { "cctpToNobleSkip error, code: $code" } + val error = ParsingError( + ParsingErrorType.MissingContent, + "Missing skip response", + ) + helper.send(error, callback) + } + } else { + Logger.e { "cctpToNobleSkip error, code: $code" } + val error = ParsingError( + ParsingErrorType.MissingContent, + "Missing skip response", + ) + helper.send(error, callback) + } + } + } else { + val error = ParsingError( + ParsingErrorType.MissingRequiredData, + "Missing required data for cctp skip withdraw", + ) + helper.send(error, callback) + } + } } From 6ff73c3949f8ef844853cec5a8cc223fc4372a26 Mon Sep 17 00:00:00 2001 From: jeremy lee Date: Fri, 14 Jun 2024 07:58:29 -0400 Subject: [PATCH 7/9] address comment --- .../v2/supervisor/OnboardingSupervisor.kt | 64 +++++++++---------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt index 2e8f048f0..9231d81c0 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt @@ -232,7 +232,7 @@ internal class OnboardingSupervisor( } } val chainId = helper.environment.dydxChainId - val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom + val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val url = helper.configs.skipV2MsgsDirect() if (fromChain != null && @@ -240,14 +240,14 @@ internal class OnboardingSupervisor( fromAmount != null && fromAmount > 0 && fromAmountString != null && chainId != null && - dydxTokenDemon != null && + nativeTokenUSDCDenom != null && url != null ) { val body: Map = mapOf( "amount_in" to fromAmountString, "source_asset_denom" to fromToken, "source_asset_chain_id" to fromChain, - "dest_asset_denom" to dydxTokenDemon, + "dest_asset_denom" to nativeTokenUSDCDenom, "dest_asset_chain_id" to chainId, "chain_ids_to_addresses" to mapOf( "fromChain" to sourceAddress, @@ -295,7 +295,7 @@ internal class OnboardingSupervisor( } } val chainId = helper.environment.dydxChainId - val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom + val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val nobleAddress = accountAddress.toNobleAddress() val url = helper.configs.skipV2MsgsDirect() @@ -307,7 +307,7 @@ internal class OnboardingSupervisor( fromAmountString != null && nobleAddress != null && chainId != null && - dydxTokenDemon != null && + nativeTokenUSDCDenom != null && toChain != null && toToken != null ) { @@ -363,7 +363,7 @@ internal class OnboardingSupervisor( } val chainId = helper.environment.dydxChainId val squidIntegratorId = helper.environment.squidIntegratorId - val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom + val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val url = helper.configs.squidRoute() if (fromChain != null && @@ -371,7 +371,7 @@ internal class OnboardingSupervisor( fromAmount != null && fromAmount > 0 && fromAmountString != null && chainId != null && - dydxTokenDemon != null && + nativeTokenUSDCDenom != null && url != null && squidIntegratorId != null ) { @@ -380,7 +380,7 @@ internal class OnboardingSupervisor( "fromToken" to fromToken, "fromAmount" to fromAmountString, "toChain" to chainId, - "toToken" to dydxTokenDemon, + "toToken" to nativeTokenUSDCDenom, "toAddress" to accountAddress, "slippage" to "1", "enableForecall" to "false", @@ -425,7 +425,7 @@ internal class OnboardingSupervisor( } val chainId = helper.environment.dydxChainId val squidIntegratorId = helper.environment.squidIntegratorId - val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom + val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val nobleAddress = accountAddress.toNobleAddress() val url = helper.configs.squidV2Route() @@ -437,7 +437,7 @@ internal class OnboardingSupervisor( fromAmountString != null && nobleAddress != null && chainId != null && - dydxTokenDemon != null && + nativeTokenUSDCDenom != null && url != null && squidIntegratorId != null && toChain != null && @@ -763,7 +763,7 @@ internal class OnboardingSupervisor( } val chainId = helper.environment.dydxChainId val squidIntegratorId = helper.environment.squidIntegratorId - val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom + val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val url = helper.configs.squidRoute() val fromAddress = accountAddress @@ -774,13 +774,13 @@ internal class OnboardingSupervisor( fromAmount > 0 && fromAmountString != null && chainId != null && - dydxTokenDemon != null && + nativeTokenUSDCDenom != null && url != null && squidIntegratorId != null ) { val params: IMap = iMapOf( "fromChain" to chainId, - "fromToken" to dydxTokenDemon, + "fromToken" to nativeTokenUSDCDenom, "fromAmount" to fromAmountString, "fromAddress" to fromAddress, "toChain" to nobleChain, @@ -822,7 +822,7 @@ internal class OnboardingSupervisor( } val chainId = helper.environment.dydxChainId val squidIntegratorId = helper.environment.squidIntegratorId - val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom + val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val url = helper.configs.squidRoute() val fromAddress = accountAddress @@ -833,13 +833,13 @@ internal class OnboardingSupervisor( fromAmount > 0 && fromAmountString != null && chainId != null && - dydxTokenDemon != null && + nativeTokenUSDCDenom != null && url != null && squidIntegratorId != null ) { val params: IMap = iMapOf( "fromChain" to chainId, - "fromToken" to dydxTokenDemon, + "fromToken" to nativeTokenUSDCDenom, "fromAmount" to fromAmountString, "fromAddress" to fromAddress, "toChain" to toChain, @@ -882,7 +882,7 @@ internal class OnboardingSupervisor( } val chainId = helper.environment.dydxChainId val squidIntegratorId = helper.environment.squidIntegratorId - val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom + val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val url = helper.configs.squidV2Route() val fromAddress = accountAddress.toNobleAddress() @@ -895,7 +895,7 @@ internal class OnboardingSupervisor( fromAmount > 0 && fromAmountString != null && chainId != null && - dydxTokenDemon != null && + nativeTokenUSDCDenom != null && url != null && fromAddress != null && squidIntegratorId != null && @@ -957,7 +957,7 @@ internal class OnboardingSupervisor( null } val fromChain = helper.environment.dydxChainId - val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom + val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val url = helper.configs.skipV2MsgsDirect() val fromAddress = accountAddress @@ -968,11 +968,11 @@ internal class OnboardingSupervisor( fromChain != null && toToken != null && toChain != null && - dydxTokenDemon != null + nativeTokenUSDCDenom != null ) { val body: Map = mapOf( "amount_in" to fromAmountString, - "source_asset_denom" to dydxTokenDemon, + "source_asset_denom" to nativeTokenUSDCDenom, "source_asset_chain_id" to fromChain, "dest_asset_denom" to toToken, "dest_asset_chain_id" to toChain, @@ -1073,7 +1073,7 @@ internal class OnboardingSupervisor( null } val chainId = helper.environment.dydxChainId - val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom + val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val url = helper.configs.skipV2MsgsDirect() val fromAddress = accountAddress.toNobleAddress() @@ -1087,7 +1087,7 @@ internal class OnboardingSupervisor( fromAddress != null && fromAmountString != null && chainId != null && - dydxTokenDemon != null && + nativeTokenUSDCDenom != null && fromChain != null && fromToken != null ) { @@ -1169,13 +1169,13 @@ internal class OnboardingSupervisor( val nobleAddress = accountAddress.toNobleAddress() val chainId = helper.environment.dydxChainId val squidIntegratorId = helper.environment.squidIntegratorId - val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom + val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom if (url != null && fromChain != null && fromToken != null && nobleAddress != null && chainId != null && - dydxTokenDemon != null && + nativeTokenUSDCDenom != null && squidIntegratorId != null ) { val params: Map = mapOf( @@ -1184,7 +1184,7 @@ internal class OnboardingSupervisor( "fromAddress" to nobleAddress, "fromAmount" to amount.toPlainString(), "toChain" to chainId, - "toToken" to dydxTokenDemon, + "toToken" to nativeTokenUSDCDenom, "toAddress" to accountAddress.toString(), "slippage" to "1", "enableForecall" to "false", @@ -1422,7 +1422,7 @@ internal class OnboardingSupervisor( val nobleAddress = accountAddress.toNobleAddress() val chainId = helper.environment.dydxChainId val squidIntegratorId = helper.environment.squidIntegratorId - val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom + val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom val usdcSize = helper.parser.asDecimal(state?.input?.transfer?.size?.usdcSize) val fromAmount = if (usdcSize != null && usdcSize > gas) { ((usdcSize - gas) * Numeric.decimal.TEN.pow(decimals)).toBigInteger() @@ -1436,7 +1436,7 @@ internal class OnboardingSupervisor( nobleToken != null && nobleAddress != null && chainId != null && - dydxTokenDemon != null && + nativeTokenUSDCDenom != null && squidIntegratorId != null && fromAmountString != null && fromAmount != null && fromAmount > 0 ) { @@ -1446,7 +1446,7 @@ internal class OnboardingSupervisor( "toAddress" to nobleAddress, "fromAmount" to fromAmountString, "fromChain" to chainId, - "fromToken" to dydxTokenDemon, + "fromToken" to nativeTokenUSDCDenom, "fromAddress" to accountAddress, "slippage" to "1", "enableForecall" to "false", @@ -1528,7 +1528,7 @@ internal class OnboardingSupervisor( val nobleToken = helper.configs.nobleDenom() val nobleAddress = accountAddress.toNobleAddress() val chainId = helper.environment.dydxChainId - val dydxTokenDemon = helper.environment.tokens["usdc"]?.denom + val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom val usdcSize = helper.parser.asDecimal(state?.input?.transfer?.size?.usdcSize) val fromAmount = if (usdcSize != null && usdcSize > gas) { ((usdcSize - gas) * Numeric.decimal.TEN.pow(decimals)).toBigInteger() @@ -1542,13 +1542,13 @@ internal class OnboardingSupervisor( nobleToken != null && nobleAddress != null && chainId != null && - dydxTokenDemon != null && + nativeTokenUSDCDenom != null && fromAmountString != null && fromAmount != null && fromAmount > 0 ) { val body: Map = mapOf( "amount_in" to fromAmountString, // from dydx denom and chain - "source_asset_denom" to dydxTokenDemon, + "source_asset_denom" to nativeTokenUSDCDenom, "source_asset_chain_id" to chainId, // to noble denom and chain "dest_asset_denom" to nobleToken, From 85b889cf915644c4cdc90b66281c588477a6dd3d Mon Sep 17 00:00:00 2001 From: jeremy lee Date: Fri, 14 Jun 2024 08:52:06 -0400 Subject: [PATCH 8/9] better name. more cleanup --- .../v2/supervisor/OnboardingSupervisor.kt | 60 +++++++++---------- 1 file changed, 28 insertions(+), 32 deletions(-) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt index 9231d81c0..aa756da35 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt @@ -232,7 +232,7 @@ internal class OnboardingSupervisor( } } val chainId = helper.environment.dydxChainId - val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom + val nativeChainUSDCDenom = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val url = helper.configs.skipV2MsgsDirect() if (fromChain != null && @@ -240,14 +240,14 @@ internal class OnboardingSupervisor( fromAmount != null && fromAmount > 0 && fromAmountString != null && chainId != null && - nativeTokenUSDCDenom != null && + nativeChainUSDCDenom != null && url != null ) { val body: Map = mapOf( "amount_in" to fromAmountString, "source_asset_denom" to fromToken, "source_asset_chain_id" to fromChain, - "dest_asset_denom" to nativeTokenUSDCDenom, + "dest_asset_denom" to nativeChainUSDCDenom, "dest_asset_chain_id" to chainId, "chain_ids_to_addresses" to mapOf( "fromChain" to sourceAddress, @@ -295,7 +295,6 @@ internal class OnboardingSupervisor( } } val chainId = helper.environment.dydxChainId - val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val nobleAddress = accountAddress.toNobleAddress() val url = helper.configs.skipV2MsgsDirect() @@ -307,7 +306,6 @@ internal class OnboardingSupervisor( fromAmountString != null && nobleAddress != null && chainId != null && - nativeTokenUSDCDenom != null && toChain != null && toToken != null ) { @@ -363,7 +361,7 @@ internal class OnboardingSupervisor( } val chainId = helper.environment.dydxChainId val squidIntegratorId = helper.environment.squidIntegratorId - val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom + val nativeChainUSDCDenom = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val url = helper.configs.squidRoute() if (fromChain != null && @@ -371,7 +369,7 @@ internal class OnboardingSupervisor( fromAmount != null && fromAmount > 0 && fromAmountString != null && chainId != null && - nativeTokenUSDCDenom != null && + nativeChainUSDCDenom != null && url != null && squidIntegratorId != null ) { @@ -380,7 +378,7 @@ internal class OnboardingSupervisor( "fromToken" to fromToken, "fromAmount" to fromAmountString, "toChain" to chainId, - "toToken" to nativeTokenUSDCDenom, + "toToken" to nativeChainUSDCDenom, "toAddress" to accountAddress, "slippage" to "1", "enableForecall" to "false", @@ -425,7 +423,7 @@ internal class OnboardingSupervisor( } val chainId = helper.environment.dydxChainId val squidIntegratorId = helper.environment.squidIntegratorId - val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom + val nativeChainUSDCDenom = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val nobleAddress = accountAddress.toNobleAddress() val url = helper.configs.squidV2Route() @@ -437,7 +435,7 @@ internal class OnboardingSupervisor( fromAmountString != null && nobleAddress != null && chainId != null && - nativeTokenUSDCDenom != null && + nativeChainUSDCDenom != null && url != null && squidIntegratorId != null && toChain != null && @@ -763,7 +761,7 @@ internal class OnboardingSupervisor( } val chainId = helper.environment.dydxChainId val squidIntegratorId = helper.environment.squidIntegratorId - val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom + val nativeChainUSDCDenom = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val url = helper.configs.squidRoute() val fromAddress = accountAddress @@ -774,13 +772,13 @@ internal class OnboardingSupervisor( fromAmount > 0 && fromAmountString != null && chainId != null && - nativeTokenUSDCDenom != null && + nativeChainUSDCDenom != null && url != null && squidIntegratorId != null ) { val params: IMap = iMapOf( "fromChain" to chainId, - "fromToken" to nativeTokenUSDCDenom, + "fromToken" to nativeChainUSDCDenom, "fromAmount" to fromAmountString, "fromAddress" to fromAddress, "toChain" to nobleChain, @@ -822,7 +820,7 @@ internal class OnboardingSupervisor( } val chainId = helper.environment.dydxChainId val squidIntegratorId = helper.environment.squidIntegratorId - val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom + val nativeChainUSDCDenom = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val url = helper.configs.squidRoute() val fromAddress = accountAddress @@ -833,13 +831,13 @@ internal class OnboardingSupervisor( fromAmount > 0 && fromAmountString != null && chainId != null && - nativeTokenUSDCDenom != null && + nativeChainUSDCDenom != null && url != null && squidIntegratorId != null ) { val params: IMap = iMapOf( "fromChain" to chainId, - "fromToken" to nativeTokenUSDCDenom, + "fromToken" to nativeChainUSDCDenom, "fromAmount" to fromAmountString, "fromAddress" to fromAddress, "toChain" to toChain, @@ -882,7 +880,7 @@ internal class OnboardingSupervisor( } val chainId = helper.environment.dydxChainId val squidIntegratorId = helper.environment.squidIntegratorId - val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom + val nativeChainUSDCDenom = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val url = helper.configs.squidV2Route() val fromAddress = accountAddress.toNobleAddress() @@ -895,7 +893,7 @@ internal class OnboardingSupervisor( fromAmount > 0 && fromAmountString != null && chainId != null && - nativeTokenUSDCDenom != null && + nativeChainUSDCDenom != null && url != null && fromAddress != null && squidIntegratorId != null && @@ -957,7 +955,7 @@ internal class OnboardingSupervisor( null } val fromChain = helper.environment.dydxChainId - val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom + val nativeChainUSDCDenom = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val url = helper.configs.skipV2MsgsDirect() val fromAddress = accountAddress @@ -968,11 +966,11 @@ internal class OnboardingSupervisor( fromChain != null && toToken != null && toChain != null && - nativeTokenUSDCDenom != null + nativeChainUSDCDenom != null ) { val body: Map = mapOf( "amount_in" to fromAmountString, - "source_asset_denom" to nativeTokenUSDCDenom, + "source_asset_denom" to nativeChainUSDCDenom, "source_asset_chain_id" to fromChain, "dest_asset_denom" to toToken, "dest_asset_chain_id" to toChain, @@ -1073,7 +1071,6 @@ internal class OnboardingSupervisor( null } val chainId = helper.environment.dydxChainId - val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom val fromAmountString = helper.parser.asString(fromAmount) val url = helper.configs.skipV2MsgsDirect() val fromAddress = accountAddress.toNobleAddress() @@ -1087,7 +1084,6 @@ internal class OnboardingSupervisor( fromAddress != null && fromAmountString != null && chainId != null && - nativeTokenUSDCDenom != null && fromChain != null && fromToken != null ) { @@ -1169,13 +1165,13 @@ internal class OnboardingSupervisor( val nobleAddress = accountAddress.toNobleAddress() val chainId = helper.environment.dydxChainId val squidIntegratorId = helper.environment.squidIntegratorId - val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom + val nativeChainUSDCDenom = helper.environment.tokens["usdc"]?.denom if (url != null && fromChain != null && fromToken != null && nobleAddress != null && chainId != null && - nativeTokenUSDCDenom != null && + nativeChainUSDCDenom != null && squidIntegratorId != null ) { val params: Map = mapOf( @@ -1184,7 +1180,7 @@ internal class OnboardingSupervisor( "fromAddress" to nobleAddress, "fromAmount" to amount.toPlainString(), "toChain" to chainId, - "toToken" to nativeTokenUSDCDenom, + "toToken" to nativeChainUSDCDenom, "toAddress" to accountAddress.toString(), "slippage" to "1", "enableForecall" to "false", @@ -1422,7 +1418,7 @@ internal class OnboardingSupervisor( val nobleAddress = accountAddress.toNobleAddress() val chainId = helper.environment.dydxChainId val squidIntegratorId = helper.environment.squidIntegratorId - val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom + val nativeChainUSDCDenom = helper.environment.tokens["usdc"]?.denom val usdcSize = helper.parser.asDecimal(state?.input?.transfer?.size?.usdcSize) val fromAmount = if (usdcSize != null && usdcSize > gas) { ((usdcSize - gas) * Numeric.decimal.TEN.pow(decimals)).toBigInteger() @@ -1436,7 +1432,7 @@ internal class OnboardingSupervisor( nobleToken != null && nobleAddress != null && chainId != null && - nativeTokenUSDCDenom != null && + nativeChainUSDCDenom != null && squidIntegratorId != null && fromAmountString != null && fromAmount != null && fromAmount > 0 ) { @@ -1446,7 +1442,7 @@ internal class OnboardingSupervisor( "toAddress" to nobleAddress, "fromAmount" to fromAmountString, "fromChain" to chainId, - "fromToken" to nativeTokenUSDCDenom, + "fromToken" to nativeChainUSDCDenom, "fromAddress" to accountAddress, "slippage" to "1", "enableForecall" to "false", @@ -1528,7 +1524,7 @@ internal class OnboardingSupervisor( val nobleToken = helper.configs.nobleDenom() val nobleAddress = accountAddress.toNobleAddress() val chainId = helper.environment.dydxChainId - val nativeTokenUSDCDenom = helper.environment.tokens["usdc"]?.denom + val nativeChainUSDCDenom = helper.environment.tokens["usdc"]?.denom val usdcSize = helper.parser.asDecimal(state?.input?.transfer?.size?.usdcSize) val fromAmount = if (usdcSize != null && usdcSize > gas) { ((usdcSize - gas) * Numeric.decimal.TEN.pow(decimals)).toBigInteger() @@ -1542,13 +1538,13 @@ internal class OnboardingSupervisor( nobleToken != null && nobleAddress != null && chainId != null && - nativeTokenUSDCDenom != null && + nativeChainUSDCDenom != null && fromAmountString != null && fromAmount != null && fromAmount > 0 ) { val body: Map = mapOf( "amount_in" to fromAmountString, // from dydx denom and chain - "source_asset_denom" to nativeTokenUSDCDenom, + "source_asset_denom" to nativeChainUSDCDenom, "source_asset_chain_id" to chainId, // to noble denom and chain "dest_asset_denom" to nobleToken, From 23314256b532d270925ec674c78ff2cce6fbcf62 Mon Sep 17 00:00:00 2001 From: jeremy lee Date: Fri, 14 Jun 2024 13:30:27 -0400 Subject: [PATCH 9/9] address comments --- .../state/model/TradingStateMachine+Squid.kt | 3 +- .../v2/supervisor/OnboardingSupervisor.kt | 231 ++++++++---------- 2 files changed, 101 insertions(+), 133 deletions(-) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Squid.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Squid.kt index 9eaa6a432..2b868abf6 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Squid.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/model/TradingStateMachine+Squid.kt @@ -39,8 +39,7 @@ internal fun TradingStateMachine.squidV2SdkInfo(payload: String): StateChanges? } } -@Suppress("ForbiddenComment") -// TODO: rename this to routerRoute/routerMsg/routerTxPayload +// DO-LATER: https://linear.app/dydx/issue/OTE-350/%5Babacus%5D-cleanup internal fun TradingStateMachine.squidRoute( payload: String, subaccountNumber: Int, diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt index aa756da35..b1729d85c 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/OnboardingSupervisor.kt @@ -945,52 +945,43 @@ internal class OnboardingSupervisor( sourceAddress: String, subaccountNumber: Int?, ) { - val toChain = helper.configs.nobleChainId() - val toToken = helper.configs.nobleDenom() - val toAddress = state?.input?.transfer?.address - val usdcSize = helper.parser.asDecimal(state?.input?.transfer?.size?.usdcSize) - val fromAmount = if (usdcSize != null && usdcSize > gas) { + val toChain = helper.configs.nobleChainId() ?: return + val toToken = helper.configs.nobleDenom() ?: return + val toAddress = state?.input?.transfer?.address ?: return + val usdcSize = helper.parser.asDecimal(state?.input?.transfer?.size?.usdcSize) ?: return + val fromAmount = if (usdcSize > gas) { ((usdcSize - gas) * Numeric.decimal.TEN.pow(decimals)).toBigInteger() } else { - null + return } - val fromChain = helper.environment.dydxChainId - val nativeChainUSDCDenom = helper.environment.tokens["usdc"]?.denom - val fromAmountString = helper.parser.asString(fromAmount) + if (fromAmount <= 0) return + val fromChain = helper.environment.dydxChainId ?: return + val nativeChainUSDCDenom = helper.environment.tokens["usdc"]?.denom ?: return + val fromAmountString = helper.parser.asString(fromAmount) ?: return val url = helper.configs.skipV2MsgsDirect() val fromAddress = accountAddress - if ( - fromAmount != null && - fromAmount > 0 && - fromAmountString != null && - fromChain != null && - toToken != null && - toChain != null && - nativeChainUSDCDenom != null - ) { - val body: Map = mapOf( - "amount_in" to fromAmountString, - "source_asset_denom" to nativeChainUSDCDenom, - "source_asset_chain_id" to fromChain, - "dest_asset_denom" to toToken, - "dest_asset_chain_id" to toChain, - "chain_ids_to_addresses" to mapOf( - fromChain to fromAddress, - toChain to toAddress, - ), - "slippage_tolerance_percent" to SLIPPAGE_PERCENT, - ) + val body: Map = mapOf( + "amount_in" to fromAmountString, + "source_asset_denom" to nativeChainUSDCDenom, + "source_asset_chain_id" to fromChain, + "dest_asset_denom" to toToken, + "dest_asset_chain_id" to toChain, + "chain_ids_to_addresses" to mapOf( + fromChain to fromAddress, + toChain to toAddress, + ), + "slippage_tolerance_percent" to SLIPPAGE_PERCENT, + ) - val oldState = stateMachine.state - val header = iMapOf( - "Content-Type" to "application/json", - ) - helper.post(url, header, body.toJsonPrettyPrint()) { _, response, code, headers -> - if (response != null) { - update(stateMachine.squidRoute(response, subaccountNumber ?: 0, null), oldState) - } else { - Logger.e { "retrieveSkipWithdrawalRouteExchange error, code: $code" } - } + val oldState = stateMachine.state + val header = iMapOf( + "Content-Type" to "application/json", + ) + helper.post(url, header, body.toJsonPrettyPrint()) { _, response, code, headers -> + if (response != null) { + update(stateMachine.squidRoute(response, subaccountNumber ?: 0, null), oldState) + } else { + Logger.e { "retrieveSkipWithdrawalRouteExchange error, code: $code" } } } } @@ -1003,52 +994,40 @@ internal class OnboardingSupervisor( sourceAddress: String, subaccountNumber: Int?, ) { - val toChain = state?.input?.transfer?.chain - val toToken = state?.input?.transfer?.token - val toAddress = state?.input?.transfer?.address - val usdcSize = helper.parser.asDecimal(state?.input?.transfer?.size?.usdcSize) - val fromAmount = if (usdcSize != null && usdcSize > gas) { + val toChain = state?.input?.transfer?.chain ?: return + val toToken = state?.input?.transfer?.token ?: return + val usdcSize = helper.parser.asDecimal(state?.input?.transfer?.size?.usdcSize) ?: return + val fromAmount = if (usdcSize > gas) { ((usdcSize - gas) * Numeric.decimal.TEN.pow(decimals)).toBigInteger() } else { - null + return } - val fromChain = helper.environment.dydxChainId - val fromToken = helper.environment.tokens["usdc"]?.denom - val fromAmountString = helper.parser.asString(fromAmount) + if (fromAmount <= 0) return + val fromChain = helper.environment.dydxChainId ?: return + val fromToken = helper.environment.tokens["usdc"]?.denom ?: return + val fromAmountString = helper.parser.asString(fromAmount) ?: return val url = helper.configs.skipV2MsgsDirect() - val fromAddress = accountAddress - if ( - toChain != null && - toToken != null && - toAddress != null && - fromAmount != null && - fromAmount > 0 && - fromAmountString != null && - fromChain != null && - fromToken != null - ) { - val body: Map = mapOf( - "amount_in" to fromAmountString, - "source_asset_denom" to fromToken, - "source_asset_chain_id" to fromChain, - "dest_asset_denom" to toToken, - "dest_asset_chain_id" to toChain, - "chain_ids_to_addresses" to mapOf( - fromChain to sourceAddress, - toChain to fromAddress, - ), - "slippage_tolerance_percent" to SLIPPAGE_PERCENT, - ) - val header = iMapOf( - "Content-Type" to "application/json", - ) - val oldState = stateMachine.state - helper.post(url, header, body.toJsonPrettyPrint()) { _, response, code, headers -> - if (response != null) { - update(stateMachine.squidRoute(response, subaccountNumber ?: 0, null), oldState) - } else { - Logger.e { "retrieveSkipWithdrawalRouteNonCCTP error, code: $code" } - } + val body: Map = mapOf( + "amount_in" to fromAmountString, + "source_asset_denom" to fromToken, + "source_asset_chain_id" to fromChain, + "dest_asset_denom" to toToken, + "dest_asset_chain_id" to toChain, + "chain_ids_to_addresses" to mapOf( + fromChain to sourceAddress, + toChain to accountAddress, + ), + "slippage_tolerance_percent" to SLIPPAGE_PERCENT, + ) + val header = iMapOf( + "Content-Type" to "application/json", + ) + val oldState = stateMachine.state + helper.post(url, header, body.toJsonPrettyPrint()) { _, response, code, headers -> + if (response != null) { + update(stateMachine.squidRoute(response, subaccountNumber ?: 0, null), oldState) + } else { + Logger.e { "retrieveSkipWithdrawalRouteNonCCTP error, code: $code" } } } } @@ -1061,59 +1040,48 @@ internal class OnboardingSupervisor( sourceAddress: String, subaccountNumber: Int?, ) { - val toChain = state?.input?.transfer?.chain - val toToken = state?.input?.transfer?.token - val toAddress = state?.input?.transfer?.address - val usdcSize = helper.parser.asDecimal(state?.input?.transfer?.size?.usdcSize) - val fromAmount = if (usdcSize != null && usdcSize > gas) { + val toChain = state?.input?.transfer?.chain ?: return + val toToken = state.input.transfer.token ?: return + val toAddress = state.input.transfer.address ?: return + val usdcSize = helper.parser.asDecimal(state.input.transfer.size?.usdcSize) ?: return + val fromAmount = if (usdcSize > gas) { ((usdcSize - gas) * Numeric.decimal.TEN.pow(decimals)).toBigInteger() } else { - null + return } - val chainId = helper.environment.dydxChainId - val fromAmountString = helper.parser.asString(fromAmount) + if (fromAmount <= 0) return + val fromAmountString = helper.parser.asString(fromAmount) ?: return val url = helper.configs.skipV2MsgsDirect() - val fromAddress = accountAddress.toNobleAddress() - - val fromChain = helper.configs.nobleChainId() - val fromToken = helper.configs.nobleDenom() - if (toChain != null && - toToken != null && - fromAmount != null && - fromAmount > 0 && - fromAddress != null && - fromAmountString != null && - chainId != null && - fromChain != null && - fromToken != null - ) { - val body: Map = mapOf( - "amount_in" to fromAmountString, - "source_asset_denom" to fromToken, - "source_asset_chain_id" to fromChain, - "dest_asset_denom" to toToken, - "dest_asset_chain_id" to toChain, - "chain_ids_to_addresses" to mapOf( - fromChain to fromAddress, - toChain to toAddress, - ), - "slippage_tolerance_percent" to SLIPPAGE_PERCENT, - "smart_relay" to false, - ) - val oldState = stateMachine.state - val header = iMapOf( - "Content-Type" to "application/json", - ) - helper.post(url, header, body.toJsonPrettyPrint()) { url, response, code, headers -> - if (response != null) { - val currentFromAmount = stateMachine.state?.input?.transfer?.size?.size - val oldFromAmount = oldState?.input?.transfer?.size?.size - if (currentFromAmount == oldFromAmount) { - update(stateMachine.squidRoute(response, subaccountNumber ?: 0, null), oldState) - } - } else { - Logger.e { "retrieveSkipWithdrawalRouteCCTP error, code: $code" } + val fromAddress = accountAddress.toNobleAddress() ?: return + + val fromChain = helper.configs.nobleChainId() ?: return + val fromToken = helper.configs.nobleDenom() ?: return + val body: Map = mapOf( + "amount_in" to fromAmountString, + "source_asset_denom" to fromToken, + "source_asset_chain_id" to fromChain, + "dest_asset_denom" to toToken, + "dest_asset_chain_id" to toChain, + "chain_ids_to_addresses" to mapOf( + fromChain to fromAddress, + toChain to toAddress, + ), + "slippage_tolerance_percent" to SLIPPAGE_PERCENT, + "smart_relay" to false, + ) + val oldState = stateMachine.state + val header = iMapOf( + "Content-Type" to "application/json", + ) + helper.post(url, header, body.toJsonPrettyPrint()) { _, response, code, _ -> + if (response != null) { + val currentFromAmount = stateMachine.state?.input?.transfer?.size?.size + val oldFromAmount = oldState?.input?.transfer?.size?.size + if (currentFromAmount == oldFromAmount) { + update(stateMachine.squidRoute(response, subaccountNumber ?: 0, null), oldState) } + } else { + Logger.e { "retrieveSkipWithdrawalRouteCCTP error, code: $code" } } } } @@ -1518,7 +1486,8 @@ internal class OnboardingSupervisor( ) { // We have a lot of duplicate code for these deposit/withdrawal route calls // It's easier to dedupe now that the url is the same and only the args differ -// TODO: Consider creating generateArgs fun to reduce code duplication +// Consider creating generateArgs fun to reduce code duplication +// DO-LATER: https://linear.app/dydx/issue/OTE-350/%5Babacus%5D-cleanup val url = helper.configs.skipV2MsgsDirect() val nobleChain = helper.configs.nobleChainId() val nobleToken = helper.configs.nobleDenom()