Skip to content

feat(perps): add copy trading entry points - #6844

Open
SeniorZhai wants to merge 16 commits into
masterfrom
feat/copy-trading-entry
Open

SeniorZhai wants to merge 16 commits into
masterfrom
feat/copy-trading-entry

Conversation

@SeniorZhai

Copy link
Copy Markdown
Member

No description provided.

@SeniorZhai
SeniorZhai force-pushed the feat/copy-trading-entry branch from 942f2b3 to fb84fe0 Compare November 11, 2026 03:09
Show publish copy trade entry on position share sheet, copy trade tag on market detail, and support maximum margin reduction limit.
@SeniorZhai
SeniorZhai force-pushed the feat/copy-trading-entry branch from fb84fe0 to 2f08904 Compare November 11, 2026 03:26
@SeniorZhai
SeniorZhai marked this pull request as ready for review November 12, 2026 01:51
@gaotic

gaotic Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

AI review done for 9da8e66 in 6m 09s · 4 comments

Quality: Needs fixes before merge. The Reduce Margin flow can show an amount that differs from Preview and leaves users without a usable retry when the server lowers the limit.

Role Model Status
Reviewer Codex codex/gpt-6.2-sol · effort high ok

Percentage and slider fills were writing eight-decimal values into the amount field, so the visible text scrolled to the trailing digits.
Switching the reduce-margin field only changes what is shown. The stored percentage stays intact, and the amount text scales down to fit.

@gaotic gaotic Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review · Grok + Codex · cf4aac6

Needs fixes before merge. I compared confirmMargin() with the merge-base submit() path and searched the repo for the removed result constants. Gradle unit tests were not compiled or run because Gradle 9.9.0 is not available offline.

AnalyticsTracker.trackPerpsReduceMarginEnd()
RxBus.publish(PerpsTradeCompletedEvent)
toast(R.string.perps_margin_submitted)
} else {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A decrease rejected with code 10653 is submitted again unchanged. This branch toasts getMixinErrorStringByCode and sets Step.Error, and Retry calls showVerifyPinThenClose(), which runs confirmMargin() with the same reduceMarginAmount. The previous submit() in PerpsMarginBottomSheetDialogFragment handled !increase && response.errorCode == 10653 by setting availableMargin = availableMarginFromError(response.error?.extra) and showing the max removable amount, so that amount could not be sent again. The liquidation estimate still stores available_margin via onMarginExceeded; this adjust response does not. On 10653, parse available_margin, show the max removable amount, and return that limit to the margin sheet instead of retrying the rejected amount.

amount = requireNotNull(reduceMarginAmount).toBigDecimal().stripTrailingZeros().toPlainString(),
),
)
if (response.isSuccess && response.data != null) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A successful decrease does not reload the position or its orders. This branch only sets Step.Done, tracks trackPerpsReduceMarginEnd, publishes PerpsTradeCompletedEvent, and toasts. The only listener for that event dismisses the market list. The previous success path called refreshSinglePosition and refreshOrders before dismiss, and closePerpsOrder still refreshes the position immediately. The open position keeps the previous margin and max_decrease_margin until the next poll (3s on the perps screens, 10s on position detail). After success, call refreshSinglePosition(positionId) and refreshOrders with the position wallet id.

const val RESULT_MARGIN_CONFIRMED = "perps_margin_confirmed"
const val RESULT_POSITION_ID = "position_id"
const val RESULT_AMOUNT = "amount"
private const val ARGS_POSITION_ID = "args_position_id"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing RESULT_MARGIN_CONFIRMED, RESULT_POSITION_ID, and RESULT_AMOUNT breaks PerpsDialogResultTest. restoredReductionReturnsTheApprovedAmountOnce still reads those constants at lines 49, 56, and 57, and nothing in this companion declares them anymore. confirmMargin() also no longer delivers that fragment result after PIN success; it calls adjustPerpsMargin directly. Update the test for the new adjust path so the unit-test source set compiles.

…ntry

# Conflicts:
#	app/src/main/java/one/mixin/android/ui/home/web3/trade/perps/PerpsMarginBottomSheetDialogFragment.kt
#	app/src/main/res/layout/fragment_perps_position_share_bottom.xml
#	app/src/main/res/values-ko/strings.xml
#	app/src/main/res/values-zh-rCN/strings.xml
#	app/src/main/res/values-zh-rTW/strings.xml
#	app/src/main/res/values/strings.xml

@gaotic gaotic Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review · GPT + Grok · e4467c3

Needs fixes before merge. git diff --check passed, and static inspection confirmed all four findings. The focused Gradle unit-test command was blocked before compilation by the environment’s inability to create the Gradle wrapper cache.


app/src/test/java/one/mixin/android/ui/home/web3/trade/perps/PerpsDialogResultTest.kt:49

The reduction result test still targets the removed fragment-result contract. RESULT_MARGIN_CONFIRMED, RESULT_POSITION_ID, and RESULT_AMOUNT are no longer declared, and PIN success now submits through adjustPerpsMargin instead of publishing that result, so the unit-test source set cannot compile. Rewrite this test around the new adjustment path or remove the obsolete result assertions.


app/src/main/java/one/mixin/android/ui/home/web3/trade/perps/PerpsCloseBottomSheetDialogFragment.kt:589

When error 10653 means the removable margin changed, this generic error branch leaves the original amount in reduceMarginAmount, so Retry calls confirmMargin() and submits the same rejected amount again. Parse response.error?.extra with availableMarginFromError, update the reduction flow with that limit, and make Retry use the resulting valid amount.


app/src/main/java/one/mixin/android/ui/home/web3/trade/perps/PerpsMarginBottomSheetDialogFragment.kt:489

The mode switch only flips reduceByPercent, while inputIsPercentage remains true until the user edits the field. For example, a 100% reduction of a $0.005 margin renders $0.00 after switching modes, but amountValue and the submission still use the underlying 0.005. Convert the input mode and value together so the displayed dollar amount matches the amount submitted, including sub-cent values.


app/src/main/java/one/mixin/android/ui/home/web3/trade/perps/PerpsCloseBottomSheetDialogFragment.kt:583

After a successful decrease, this branch only marks the sheet done and publishes the completion event. It does not refresh the position or its orders, so reopening the reduction flow can use the old margin and maxDecreaseMargin until polling runs. Refresh the position with its wallet ID and refresh its orders before dismissing or completing the flow.

@gaotic gaotic Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review · Codex · 78f646b

The follow-up removes the obsolete result test, and both diff checks passed. The Gradle wrapper could not create its cache, and the installed Gradle could not resolve the Foojay plugin offline, so the Android build and unit tests remain unverified.

RxBus.publish(PerpsTradeCompletedEvent)
toast(R.string.perps_margin_submitted)
} else {
val message = if (response.isSuccess) getString(R.string.Data_error) else

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the server returns 10653, Retry submits the same rejected amount because reduceMarginAmount is fixed in the fragment arguments. Read available_margin from the error and return the user to the editor with the updated limit so they can confirm a valid amount.

onToggleMode = {
amount = amountValue?.let { formatMarginAdjustmentInput(it, false) }.orEmpty()
inputIsPercentage = false
reduceByPercent = !reduceByPercent

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switching to dollars changes the displayed input without changing the amount sent to preview. A Kotlin check reproduced a 100% reduction of $0.005 displaying $0 while preview receives $0.005. Convert the input value and unit together so the displayed amount matches the amount sent.

amount = requireNotNull(reduceMarginAmount).toBigDecimal().stripTrailingZeros().toPlainString(),
),
)
if (response.isSuccess && response.data != null) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A successful reduction leaves the cached position and orders unchanged. The completion event only dismisses the market list, so reopening Reduce Margin before polling can show the old margin and reduction limit. Refresh the position and its orders for the position's wallet on success.

…ntry

# Conflicts:
#	app/schemas/googlePlay/one.mixin.android.db.PerpsDatabase/13.json
#	app/schemas/otherChannel/one.mixin.android.db.PerpsDatabase/13.json
#	app/src/androidTest/java/one/mixin/android/db/PerpsMigrationTest.kt
#	app/src/main/java/one/mixin/android/db/PerpsDatabase.kt
#	app/src/main/java/one/mixin/android/ui/home/web3/trade/perps/PerpsMarginBottomSheetDialogFragment.kt
#	app/src/main/java/one/mixin/android/ui/home/web3/trade/perps/PerpsPositionShareBottomFragment.kt
#	app/src/main/res/values-ko/strings.xml
#	app/src/main/res/values-zh-rCN/strings.xml
#	app/src/main/res/values-zh-rTW/strings.xml
#	app/src/main/res/values/strings.xml

@gaotic gaotic Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review · Codex · 9da8e66

Both diff checks passed. A standalone SQLite check verified that the 13-to-14 migration preserves a position and matches the exported schema metadata for both flavors, and a standalone Kotlin check reproduced the unit-toggle mismatch. The Android build and test suite remain unverified because offline Gradle could not resolve the Foojay plugin.

var loading by remember { mutableStateOf(false) }
var error by remember { mutableStateOf<String?>(null) }
var availableMargin by remember(currentPosition.margin, currentPosition.quantity) { mutableStateOf<BigDecimal?>(null) }
var availableMargin by remember(currentPosition.margin, currentPosition.quantity, currentPosition.maxDecreaseMargin) { mutableStateOf<BigDecimal?>(null) }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If polling changes only maxDecreaseMargin, this remember resets availableMargin to null, but the limit lookup does not restart because its LaunchedEffect lacks that key. The editor disables input and Preview until the user taps Retry. Keep the fetched limit when only the cap changes, or restart the lookup when the cap changes.

val marginValue = currentPosition.margin?.toBigDecimalOrNull()?.takeIf { it >= BigDecimal.ZERO }
val maximumReduction = marginValue?.let { margin -> availableMargin?.min(margin) }
val maximumReduction = availableMargin?.let { maximumMarginReduction(currentPosition.margin, currentPosition.maxDecreaseMargin, it) }
val amountValue = if (increase) marginAdjustmentAmount(amount) else reduceMarginAmount(maximumReduction?.toPlainString(), amount, inputIsPercentage)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An offline Kotlin check reproduced the reverse unit mismatch. With a $100 limit, entering $0.49 and switching to percent shows 0%, but Preview still receives $0.49. Convert the stored amount and its unit together, or show enough percentage precision to represent the amount being previewed.

RxBus.publish(PerpsTradeCompletedEvent)
toast(R.string.perps_margin_submitted)
} else {
val message = if (response.isSuccess) getString(R.string.Data_error) else

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the server rejects a reduction with 10653, this branch discards available_margin and offers Retry. Retry resubmits the same amount from the fragment arguments. Pass the updated limit back to the editor so the user can choose and confirm a valid amount.

if (response.isSuccess && response.data != null) {
step = Step.Done
AnalyticsTracker.trackPerpsReduceMarginEnd()
RxBus.publish(PerpsTradeCompletedEvent)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The success path no longer refreshes the position or its orders. The completion event only dismisses the market list, so reopening Reduce Margin before the next poll can show the old margin and limit. Restore the explicit position and order refreshes for the position’s wallet after a successful reduction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant