-
Notifications
You must be signed in to change notification settings - Fork 15
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
Fix exception when closing a position of > 100% margin usage #761
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ import exchange.dydx.abacus.protocols.ThreadingType | |
import exchange.dydx.abacus.protocols.TransactionCallback | ||
import exchange.dydx.abacus.protocols.asTypedObject | ||
import exchange.dydx.abacus.protocols.readCachedTextFile | ||
import exchange.dydx.abacus.responses.ParsingError | ||
import exchange.dydx.abacus.state.app.adaptors.V4TransactionErrors | ||
import exchange.dydx.abacus.state.app.helper.DynamicLocalizer | ||
import exchange.dydx.abacus.state.manager.ApiData | ||
|
@@ -49,6 +50,7 @@ import exchange.dydx.abacus.utils.DummyFormatter | |
import exchange.dydx.abacus.utils.DummyLocalizer | ||
import exchange.dydx.abacus.utils.IList | ||
import exchange.dydx.abacus.utils.IOImplementations | ||
import exchange.dydx.abacus.utils.JsonEncoder | ||
import exchange.dydx.abacus.utils.Logger | ||
import exchange.dydx.abacus.utils.Parser | ||
import exchange.dydx.abacus.utils.ProtocolNativeImpFactory | ||
|
@@ -506,50 +508,111 @@ class AsyncAbacusStateManagerV2( | |
} | ||
|
||
override fun placeOrderPayload(): HumanReadablePlaceOrderPayload? { | ||
return adaptor?.placeOrderPayload() | ||
try { | ||
return adaptor?.placeOrderPayload() | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("placeOrderPayload", error) | ||
throw e | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All those "..Payload" functions are called by the web FE.. I think web code catches the exception to display the error. Here we just catch the exception for logging and re-throw it. |
||
} | ||
} | ||
|
||
override fun closePositionPayload(): HumanReadablePlaceOrderPayload? { | ||
return adaptor?.closePositionPayload() | ||
try { | ||
return adaptor?.closePositionPayload() | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("closePositionPayload", error) | ||
throw e | ||
} | ||
} | ||
|
||
override fun cancelOrderPayload(orderId: String): HumanReadableCancelOrderPayload? { | ||
return adaptor?.cancelOrderPayload(orderId) | ||
try { | ||
return adaptor?.cancelOrderPayload(orderId) | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("cancelOrderPayload", error) | ||
throw e | ||
} | ||
} | ||
|
||
override fun cancelAllOrdersPayload(marketId: String?): HumanReadableCancelAllOrdersPayload? { | ||
return adaptor?.cancelAllOrdersPayload(marketId) | ||
try { | ||
return adaptor?.cancelAllOrdersPayload(marketId) | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("cancelAllOrdersPayload", error) | ||
throw e | ||
} | ||
} | ||
|
||
override fun closeAllPositionsPayload(): HumanReadableCloseAllPositionsPayload? { | ||
return adaptor?.closeAllPositionsPayload() | ||
try { | ||
return adaptor?.closeAllPositionsPayload() | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("closeAllPositionsPayload", error) | ||
throw e | ||
} | ||
} | ||
|
||
override fun triggerOrdersPayload(): HumanReadableTriggerOrdersPayload? { | ||
return adaptor?.triggerOrdersPayload() | ||
try { | ||
return adaptor?.triggerOrdersPayload() | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("triggerOrdersPayload", error) | ||
throw e | ||
} | ||
} | ||
|
||
override fun adjustIsolatedMarginPayload(): HumanReadableSubaccountTransferPayload? { | ||
return adaptor?.adjustIsolatedMarginPayload() | ||
try { | ||
return adaptor?.adjustIsolatedMarginPayload() | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("adjustIsolatedMarginPayload", error) | ||
throw e | ||
} | ||
} | ||
|
||
override fun depositPayload(): HumanReadableDepositPayload? { | ||
return adaptor?.depositPayload() | ||
try { | ||
return adaptor?.depositPayload() | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("depositPayload", error) | ||
throw e | ||
} | ||
} | ||
|
||
override fun withdrawPayload(): HumanReadableWithdrawPayload? { | ||
return adaptor?.withdrawPayload() | ||
try { | ||
return adaptor?.withdrawPayload() | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("withdrawPayload", error) | ||
throw e | ||
} | ||
} | ||
|
||
override fun subaccountTransferPayload(): HumanReadableSubaccountTransferPayload? { | ||
return adaptor?.subaccountTransferPayload() | ||
try { | ||
return adaptor?.subaccountTransferPayload() | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("subaccountTransferPayload", error) | ||
throw e | ||
} | ||
} | ||
|
||
override fun commitPlaceOrder(callback: TransactionCallback): HumanReadablePlaceOrderPayload? { | ||
return try { | ||
adaptor?.commitPlaceOrder(callback) | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("commitPlaceOrder", error) | ||
callback(false, error, null) | ||
null | ||
} | ||
|
@@ -560,6 +623,7 @@ class AsyncAbacusStateManagerV2( | |
adaptor?.commitTriggerOrders(callback) | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("commitTriggerOrders", error) | ||
callback(false, error, null) | ||
null | ||
} | ||
|
@@ -570,6 +634,7 @@ class AsyncAbacusStateManagerV2( | |
adaptor?.commitAdjustIsolatedMargin(callback) | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("commitAdjustIsolatedMargin", error) | ||
callback(false, error, null) | ||
null | ||
} | ||
|
@@ -580,6 +645,7 @@ class AsyncAbacusStateManagerV2( | |
adaptor?.commitClosePosition(callback) | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("commitClosePosition", error) | ||
callback(false, error, null) | ||
null | ||
} | ||
|
@@ -594,6 +660,7 @@ class AsyncAbacusStateManagerV2( | |
adaptor?.commitTransfer(callback) | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("commitTransfer", error) | ||
callback(false, error, null) | ||
} | ||
} | ||
|
@@ -603,6 +670,7 @@ class AsyncAbacusStateManagerV2( | |
adaptor?.commitCCTPWithdraw(callback) | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("commitCCTPWithdraw", error) | ||
callback(false, error, null) | ||
} | ||
} | ||
|
@@ -612,6 +680,7 @@ class AsyncAbacusStateManagerV2( | |
adaptor?.faucet(amount, callback) | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("faucet", error) | ||
callback(false, error, null) | ||
} | ||
} | ||
|
@@ -621,6 +690,7 @@ class AsyncAbacusStateManagerV2( | |
adaptor?.cancelOrder(orderId, callback) | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("cancelOrder", error) | ||
callback(false, error, null) | ||
} | ||
} | ||
|
@@ -630,6 +700,7 @@ class AsyncAbacusStateManagerV2( | |
adaptor?.cancelAllOrders(marketId, callback) | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("cancelAllOrders", error) | ||
callback(false, error, null) | ||
} | ||
} | ||
|
@@ -639,6 +710,7 @@ class AsyncAbacusStateManagerV2( | |
adaptor?.closeAllPositions(callback) | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("closeAllPositions", error) | ||
callback(false, error, null) | ||
null | ||
} | ||
|
@@ -649,10 +721,30 @@ class AsyncAbacusStateManagerV2( | |
adaptor?.triggerCompliance(action, callback) | ||
} catch (e: Exception) { | ||
val error = V4TransactionErrors.error(null, e.toString()) | ||
trackTransactionError("triggerCompliance", error) | ||
callback(false, error, null) | ||
} | ||
} | ||
|
||
private fun trackTransactionError(functionName: String, error: ParsingError?) { | ||
if (error == null) { | ||
return | ||
} | ||
|
||
val params = mapOf( | ||
"functionName" to functionName, | ||
"errorType" to error.type.rawValue, | ||
"errorMessage" to error.message, | ||
"stackTrace" to error.stackTrace, | ||
) | ||
ioImplementations.threading?.async(ThreadingType.main) { | ||
ioImplementations.tracking?.log( | ||
event = "ClientTransactionError", | ||
data = JsonEncoder().encode(params), | ||
) | ||
} | ||
} | ||
|
||
// Bridge functions. | ||
// If client is not using cancelOrder function, it should call orderCanceled function with | ||
// payload from v4-client to process state | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't need to check freeCollateral > 0 if the trade is reduceOnly.