Commit ad2b657
authored
fix(predict): order calculations (MetaMask#22900)
<!--
Please submit this PR as a draft initially.
Do not mark it as "Ready for review" until the template has been
completely filled out, and PR status checks have passed at least once.
-->
## **Description**
<!--
Write a short description of the changes included in this pull request,
also include relevant motivation and context. Have in mind the following
questions:
1. What is the reason for the change?
2. What is the improvement/solution?
-->
We seem to be wrongly calculating the order values. The CLOB client code
seems quite different than ours, and they use `roundDown()` when
rounding the price for market orders, but that somehow works for them
because they use the `worstPrice` and their `marketPrice` and we are
using `avgPrice`.
In our case, we want the `takerAmount` to be the exact `shareAmount` we
would get with the dollar amount we are willing to spend and, since we
have that value from `matchBuyOrder`/`matchSellOrder`, we do **not**
need to recalculate it, as it will lose precision.
This not only simplifies the logic, but also removes unnecessary
calculations which were leading to incorrect approximations.
### Note
I want to continue investigating the differences between the CLOB client
logic and ours and see if we should revert to what they do.
## **Changelog**
<!--
If this PR is not End-User-Facing and should not show up in the
CHANGELOG, you can choose to either:
1. Write `CHANGELOG entry: null`
2. Label with `no-changelog`
If this PR is End-User-Facing, please write a short User-Facing
description in the past tense like:
`CHANGELOG entry: Added a new tab for users to see their NFTs`
`CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker`
(This helps the Release Engineer do their job more quickly and
accurately)
-->
CHANGELOG entry: null
## **Related issues**
Fixes:
## **Manual testing steps**
```gherkin
Feature: fix order calculations
Scenario: user wants to place a Buy order of $1 in a liquid market
Given share price is $0.11
When user tries to submit the order, we calculate that we get `9.090909090909092` shares
This leads to an `avgSharePrice = $1 / 9.090909090909092 shares = $0.109999999`
Then, if we round this number down, we get a share price of `$0.10`, which is much cheaper than it should be
This will lead to a much higher `takerAmount`, which makes the order fail
```
## **Screenshots/Recordings**
<!-- If applicable, add screenshots and/or recordings to visualize the
before and after of your change. -->
### **Before**
<!-- [screenshots/recordings] -->
### **After**
<!-- [screenshots/recordings] -->
## **Pre-merge author checklist**
- [x] I’ve followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile
Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.
## **Pre-merge reviewer checklist**
- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> Reworks market order preview to compute maker/taker amounts directly
from matched book sizes, removing avgPrice-based rounding and the unused
roundOrderAmounts helper (and its tests).
>
> - **Predict/Polymarket utils**:
> - Adjust `previewOrder` to derive amounts from order book matches:
> - BUY: `makerAmount = roundDown(size, roundConfig.size)`; `takerAmount
= roundOrderAmount(shareAmount, roundConfig.amount)`.
> - SELL: `makerAmount = roundDown(size, roundConfig.size)`;
`takerAmount = roundOrderAmount(dollarAmount, roundConfig.amount)`.
> - Remove `roundOrderAmounts` and dependency on `avgPrice`.
> - Drop `RoundConfig`-related usage for that helper.
> - **Tests**:
> - Remove `roundOrderAmounts` import and its test suite in
`utils.test.ts`.
> - Keep `previewOrder` tests aligned with the new calculation approach.
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
60bd2a3. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent 8f05e97 commit ad2b657
2 files changed
Lines changed: 8 additions & 99 deletions
Lines changed: 0 additions & 57 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
64 | 64 | | |
65 | 65 | | |
66 | 66 | | |
67 | | - | |
68 | 67 | | |
69 | 68 | | |
70 | 69 | | |
| |||
2276 | 2275 | | |
2277 | 2276 | | |
2278 | 2277 | | |
2279 | | - | |
2280 | | - | |
2281 | | - | |
2282 | | - | |
2283 | | - | |
2284 | | - | |
2285 | | - | |
2286 | | - | |
2287 | | - | |
2288 | | - | |
2289 | | - | |
2290 | | - | |
2291 | | - | |
2292 | | - | |
2293 | | - | |
2294 | | - | |
2295 | | - | |
2296 | | - | |
2297 | | - | |
2298 | | - | |
2299 | | - | |
2300 | | - | |
2301 | | - | |
2302 | | - | |
2303 | | - | |
2304 | | - | |
2305 | | - | |
2306 | | - | |
2307 | | - | |
2308 | | - | |
2309 | | - | |
2310 | | - | |
2311 | | - | |
2312 | | - | |
2313 | | - | |
2314 | | - | |
2315 | | - | |
2316 | | - | |
2317 | | - | |
2318 | | - | |
2319 | | - | |
2320 | | - | |
2321 | | - | |
2322 | | - | |
2323 | | - | |
2324 | | - | |
2325 | | - | |
2326 | | - | |
2327 | | - | |
2328 | | - | |
2329 | | - | |
2330 | | - | |
2331 | | - | |
2332 | | - | |
2333 | | - | |
2334 | | - | |
2335 | 2278 | | |
2336 | 2279 | | |
2337 | 2280 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
55 | | - | |
56 | 55 | | |
57 | 56 | | |
58 | 57 | | |
| |||
1119 | 1118 | | |
1120 | 1119 | | |
1121 | 1120 | | |
1122 | | - | |
1123 | | - | |
1124 | | - | |
1125 | | - | |
1126 | | - | |
1127 | | - | |
1128 | | - | |
1129 | | - | |
1130 | | - | |
1131 | | - | |
1132 | | - | |
1133 | | - | |
1134 | | - | |
1135 | | - | |
1136 | | - | |
1137 | | - | |
1138 | | - | |
1139 | | - | |
1140 | | - | |
1141 | | - | |
1142 | | - | |
1143 | | - | |
1144 | | - | |
1145 | | - | |
1146 | | - | |
1147 | | - | |
1148 | | - | |
1149 | | - | |
1150 | | - | |
1151 | 1121 | | |
1152 | 1122 | | |
1153 | 1123 | | |
| |||
1167 | 1137 | | |
1168 | 1138 | | |
1169 | 1139 | | |
1170 | | - | |
1171 | | - | |
1172 | | - | |
1173 | | - | |
1174 | | - | |
1175 | | - | |
| 1140 | + | |
| 1141 | + | |
| 1142 | + | |
| 1143 | + | |
1176 | 1144 | | |
1177 | 1145 | | |
1178 | 1146 | | |
| |||
1201 | 1169 | | |
1202 | 1170 | | |
1203 | 1171 | | |
1204 | | - | |
1205 | | - | |
1206 | | - | |
1207 | | - | |
1208 | | - | |
1209 | | - | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
1210 | 1176 | | |
1211 | 1177 | | |
1212 | 1178 | | |
| |||
0 commit comments