From bb03617734f15fd80d401e5803b39fec3538c2de Mon Sep 17 00:00:00 2001 From: jaredvu Date: Tue, 19 Nov 2024 16:06:55 -0800 Subject: [PATCH 1/6] add 250ms delay to order placement --- .../SubaccountTransactionSupervisor.kt | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt index 642f60f23..23b16bb28 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt @@ -6,6 +6,7 @@ import exchange.dydx.abacus.output.account.TransferRecordType import exchange.dydx.abacus.output.input.OrderSide import exchange.dydx.abacus.output.input.OrderStatus import exchange.dydx.abacus.protocols.AnalyticsEvent +import exchange.dydx.abacus.protocols.LocalTimerProtocol import exchange.dydx.abacus.protocols.ThreadingType import exchange.dydx.abacus.protocols.TransactionCallback import exchange.dydx.abacus.protocols.TransactionType @@ -29,12 +30,14 @@ import exchange.dydx.abacus.state.model.findOrder import exchange.dydx.abacus.state.model.orderCanceled import exchange.dydx.abacus.utils.AnalyticsUtils import exchange.dydx.abacus.utils.CONDITIONAL_ORDER_FLAGS +import exchange.dydx.abacus.utils.CoroutineTimer import exchange.dydx.abacus.utils.IMap import exchange.dydx.abacus.utils.IMutableList import exchange.dydx.abacus.utils.Logger import exchange.dydx.abacus.utils.NUM_PARENT_SUBACCOUNTS import exchange.dydx.abacus.utils.ParsingHelper import exchange.dydx.abacus.utils.SHORT_TERM_ORDER_FLAGS +import exchange.dydx.abacus.utils.Timer import exchange.dydx.abacus.utils.iMapOf import kollections.iListOf import kollections.iMutableListOf @@ -652,13 +655,24 @@ internal class SubaccountTransactionSupervisor( val isolatedMarginTransactionCallback = { response: String? -> val error = parseTransactionResponse(response) if (error == null) { - submitTransaction( - transactionType = TransactionType.PlaceOrder, - transactionPayloadString = string, - onSubmitTransaction = onSubmitOrderTransaction, - transactionCallback = orderTransactionCallback, - useTransactionQueue = useTransactionQueue, - ) + // Return submitTransaction after a delay to ensure the transfer is confirmed + val timer = helper.ioImplementations.timer ?: CoroutineTimer.instance + + val delayedPlaceOrder = timer.schedule( + delay = 0.25, + repeat = null + ) { + submitTransaction( + transactionType = TransactionType.PlaceOrder, + transactionPayloadString = string, + onSubmitTransaction = onSubmitOrderTransaction, + transactionCallback = orderTransactionCallback, + useTransactionQueue = useTransactionQueue, + ) + true + } + + delayedPlaceOrder.cancel() } else { // remove pending isolated order since it will not be placed val isolatedOrderRecord = this.pendingIsolatedOrderRecords.firstOrNull { From f131eedc9dcfee56781bcc43cb4ce481b49f16a2 Mon Sep 17 00:00:00 2001 From: jaredvu Date: Tue, 19 Nov 2024 16:07:54 -0800 Subject: [PATCH 2/6] remove unused import --- .../state/v2/supervisor/SubaccountTransactionSupervisor.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt index 23b16bb28..55284bb07 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt @@ -6,7 +6,6 @@ import exchange.dydx.abacus.output.account.TransferRecordType import exchange.dydx.abacus.output.input.OrderSide import exchange.dydx.abacus.output.input.OrderStatus import exchange.dydx.abacus.protocols.AnalyticsEvent -import exchange.dydx.abacus.protocols.LocalTimerProtocol import exchange.dydx.abacus.protocols.ThreadingType import exchange.dydx.abacus.protocols.TransactionCallback import exchange.dydx.abacus.protocols.TransactionType From b7d3b4af02ab68dfacffae09fd5e567bbac76c4d Mon Sep 17 00:00:00 2001 From: jaredvu Date: Wed, 20 Nov 2024 11:26:20 -0800 Subject: [PATCH 3/6] update timer syntax --- .../SubaccountTransactionSupervisor.kt | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt index 55284bb07..496d39819 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt @@ -6,12 +6,14 @@ import exchange.dydx.abacus.output.account.TransferRecordType import exchange.dydx.abacus.output.input.OrderSide import exchange.dydx.abacus.output.input.OrderStatus import exchange.dydx.abacus.protocols.AnalyticsEvent +import exchange.dydx.abacus.protocols.LocalTimerProtocol import exchange.dydx.abacus.protocols.ThreadingType import exchange.dydx.abacus.protocols.TransactionCallback import exchange.dydx.abacus.protocols.TransactionType import exchange.dydx.abacus.responses.ParsingError import exchange.dydx.abacus.responses.ParsingErrorType import exchange.dydx.abacus.responses.ParsingException +import exchange.dydx.abacus.protocols.run import exchange.dydx.abacus.state.manager.CancelOrderRecord import exchange.dydx.abacus.state.manager.FaucetRecord import exchange.dydx.abacus.state.manager.HumanReadableCancelAllOrdersPayload @@ -36,7 +38,6 @@ import exchange.dydx.abacus.utils.Logger import exchange.dydx.abacus.utils.NUM_PARENT_SUBACCOUNTS import exchange.dydx.abacus.utils.ParsingHelper import exchange.dydx.abacus.utils.SHORT_TERM_ORDER_FLAGS -import exchange.dydx.abacus.utils.Timer import exchange.dydx.abacus.utils.iMapOf import kollections.iListOf import kollections.iMutableListOf @@ -99,6 +100,14 @@ internal class SubaccountTransactionSupervisor( private val cancelingOrphanedTriggerOrders = mutableSetOf() private val reclaimingChildSubaccountNumbers = mutableSetOf() + private var isolatedMarginOrderTimer: LocalTimerProtocol? = null + set(value) { + if (field !== value) { + field?.cancel() + field = value + } + } + private fun fromSlTpDialogParams(fromSlTpDialog: Boolean): IMap { return iMapOf( "fromSlTpDialog" to fromSlTpDialog, @@ -657,10 +666,7 @@ internal class SubaccountTransactionSupervisor( // Return submitTransaction after a delay to ensure the transfer is confirmed val timer = helper.ioImplementations.timer ?: CoroutineTimer.instance - val delayedPlaceOrder = timer.schedule( - delay = 0.25, - repeat = null - ) { + isolatedMarginOrderTimer = timer.run(0.25) { submitTransaction( transactionType = TransactionType.PlaceOrder, transactionPayloadString = string, @@ -668,10 +674,7 @@ internal class SubaccountTransactionSupervisor( transactionCallback = orderTransactionCallback, useTransactionQueue = useTransactionQueue, ) - true } - - delayedPlaceOrder.cancel() } else { // remove pending isolated order since it will not be placed val isolatedOrderRecord = this.pendingIsolatedOrderRecords.firstOrNull { From c6df5cbf73b3801cdb6ce06ce9274bfbaa6f7d2b Mon Sep 17 00:00:00 2001 From: jaredvu Date: Wed, 20 Nov 2024 11:30:18 -0800 Subject: [PATCH 4/6] bump version --- build.gradle.kts | 2 +- v4_abacus.podspec | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index b29aac99d..0dce37b2f 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -52,7 +52,7 @@ allprojects { } group = "exchange.dydx.abacus" -version = "1.13.33" +version = "1.13.34" repositories { google() diff --git a/v4_abacus.podspec b/v4_abacus.podspec index 62bc0e5c0..991936106 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.33' + spec.version = '1.13.34' spec.homepage = 'https://github.com/dydxprotocol/v4-abacus' spec.source = { :http=> ''} spec.authors = '' From a16eedeb0109cabc7bd8d8d1aa4fd9dd42d5466d Mon Sep 17 00:00:00 2001 From: jaredvu Date: Wed, 20 Nov 2024 11:33:34 -0800 Subject: [PATCH 5/6] spotlessApply --- .../state/v2/supervisor/SubaccountTransactionSupervisor.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt index 496d39819..5af3355f3 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt @@ -10,10 +10,10 @@ import exchange.dydx.abacus.protocols.LocalTimerProtocol import exchange.dydx.abacus.protocols.ThreadingType import exchange.dydx.abacus.protocols.TransactionCallback import exchange.dydx.abacus.protocols.TransactionType +import exchange.dydx.abacus.protocols.run import exchange.dydx.abacus.responses.ParsingError import exchange.dydx.abacus.responses.ParsingErrorType import exchange.dydx.abacus.responses.ParsingException -import exchange.dydx.abacus.protocols.run import exchange.dydx.abacus.state.manager.CancelOrderRecord import exchange.dydx.abacus.state.manager.FaucetRecord import exchange.dydx.abacus.state.manager.HumanReadableCancelAllOrdersPayload From b5403a86a2e432b13d6813b01b00c35409cbbde5 Mon Sep 17 00:00:00 2001 From: jaredvu Date: Wed, 20 Nov 2024 11:34:48 -0800 Subject: [PATCH 6/6] pull into constants --- .../state/v2/supervisor/SubaccountTransactionSupervisor.kt | 3 ++- src/commonMain/kotlin/exchange.dydx.abacus/utils/Constants.kt | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt index 5af3355f3..63b702ae7 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/state/v2/supervisor/SubaccountTransactionSupervisor.kt @@ -36,6 +36,7 @@ import exchange.dydx.abacus.utils.IMap import exchange.dydx.abacus.utils.IMutableList import exchange.dydx.abacus.utils.Logger import exchange.dydx.abacus.utils.NUM_PARENT_SUBACCOUNTS +import exchange.dydx.abacus.utils.POST_TRANSFER_PLACE_ORDER_DELAY import exchange.dydx.abacus.utils.ParsingHelper import exchange.dydx.abacus.utils.SHORT_TERM_ORDER_FLAGS import exchange.dydx.abacus.utils.iMapOf @@ -666,7 +667,7 @@ internal class SubaccountTransactionSupervisor( // Return submitTransaction after a delay to ensure the transfer is confirmed val timer = helper.ioImplementations.timer ?: CoroutineTimer.instance - isolatedMarginOrderTimer = timer.run(0.25) { + isolatedMarginOrderTimer = timer.run(POST_TRANSFER_PLACE_ORDER_DELAY) { submitTransaction( transactionType = TransactionType.PlaceOrder, transactionPayloadString = string, diff --git a/src/commonMain/kotlin/exchange.dydx.abacus/utils/Constants.kt b/src/commonMain/kotlin/exchange.dydx.abacus/utils/Constants.kt index 04cbcdb78..1af2fb9e6 100644 --- a/src/commonMain/kotlin/exchange.dydx.abacus/utils/Constants.kt +++ b/src/commonMain/kotlin/exchange.dydx.abacus/utils/Constants.kt @@ -18,6 +18,7 @@ internal const val MAX_FREE_COLLATERAL_BUFFER_PERCENT = 0.95 internal const val MAX_LEVERAGE_BUFFER_PERCENT = 0.98 internal const val MARGIN_COLLATERALIZATION_CHECK_BUFFER = 0.01; internal const val DEFAULT_TARGET_LEVERAGE = 2.0; +internal const val POST_TRANSFER_PLACE_ORDER_DELAY = 0.25; // Order flags internal const val SHORT_TERM_ORDER_FLAGS = 0