Commit d126223
authored
feat: Derive fiat order sourceAmount from on-chain tx data and persist fiat order metadata (MetaMask#8694)
## Explanation
### 1. Derive fiat order `sourceAmount` from on-chain tx data
Currently, the fiat submit flow derives `sourceAmountRaw` from
`order.cryptoAmount` - a human-readable value reported by the on-ramp
provider. This value may not precisely reflect what was actually
received on-chain.
This PR reads the actual transferred amount from the completed on-chain
transaction (`order.txHash`) instead. For native tokens, the amount is
taken from `tx.value`. For ERC-20 tokens, the amount is decoded from the
`transfer(address,uint256)` call data. If the on-chain read fails or the
transaction hash is missing, the existing `order.cryptoAmount`
derivation is used as a fallback.
The implementation introduces:
- **`getTransferredAmountFromTxHash`** - a generic utility in
`utils/transaction-receipt.ts` that reads transferred amounts from any
on-chain transaction (native or ERC-20). Takes explicit `chainId` and
`tokenAddress` params for reusability.
- **`resolveSourceAmountRaw`** - a fiat-strategy-specific function in
`strategy/fiat/utils.ts` that orchestrates the on-chain read with
`order.cryptoAmount` fallback.
- **`getRawSourceAmountFromOrderCryptoAmount`** - the existing
decimal-shift conversion, moved from `fiat-submit.ts` to
`strategy/fiat/utils.ts` and renamed for clarity.
### 2. Persist fiat order metadata on `metamaskPay`
The `TransactionPayController` state is cleaned up when a transaction is
finalized (confirmed/failed/dropped). This means `fiatPayment.orderId`
and the provider info are gone by the time the user opens the
activity/transaction-details view.
To enable the mobile activity view to show a fiat order status row (and
query `RampsController:getOrder` for live status), this PR persists the
fiat order ID and provider code on `transaction.metamaskPay` **before**
polling begins in `submitFiatQuotes`.
Changes:
- **`MetamaskPayMetadata`** (`transaction-controller/types.ts`) — Added
`fiatOrderId?: string` and `fiatProvider?: string` fields.
- **`submitFiatQuotes`** (`fiat-submit.ts`) — Calls `updateTransaction`
to persist `fiatOrderId` and `fiatProvider` on `tx.metamaskPay` before
`waitForOrderCompletion` starts polling. This ensures data is available
even while the order is still in-flight.
- **Tests** — Two new tests verify metadata persistence and that
existing `metamaskPay` fields are preserved.
## References
- Related to the fiat strategy submit flow introduced in MetaMask#8347
## Checklist
- [x] I've updated the test suite for new or updated code as appropriate
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've communicated my changes to consumers by [updating changelogs
for packages I've
changed](https://github.com/MetaMask/core/tree/main/docs/processes/updating-changelogs.md)
- [ ] I've introduced [breaking
changes](https://github.com/MetaMask/core/tree/main/docs/processes/breaking-changes.md)
in this PR and have prepared draft pull requests for clients and
consumer packages to resolve them
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Medium Risk**
> Updates fiat on-ramp submission to depend on on-chain receipt/trace
parsing and persists new metadata onto `TransactionMeta`, which could
affect payment execution and activity display if RPC methods/log parsing
behave unexpectedly across networks.
>
> **Overview**
> Fiat on-ramp submit flow now **persists order identifiers** onto
`transaction.metamaskPay.fiat` (order ID + provider code) before
polling, so downstream activity views can query order status even after
controller state cleanup.
>
> The relay leg’s `sourceAmountRaw` is now **derived from the actual
on-chain transfer** referenced by `order.txHash` via new utilities that
parse ERC-20 `Transfer` logs or native transfers (using
`debug_traceTransaction` with a `tx.value` fallback), falling back to
`order.cryptoAmount` conversion when on-chain reads are unavailable.
>
> Adds `MetamaskPayMetadata.fiat` to the transaction-controller types
and updates/extends unit tests around fiat submission, amount
resolution, and receipt/trace parsing.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
46177a2. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->1 parent e81c393 commit d126223
9 files changed
Lines changed: 1120 additions & 77 deletions
File tree
- packages
- transaction-controller
- src
- transaction-pay-controller
- src
- strategy/fiat
- utils
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2170 | 2170 | | |
2171 | 2171 | | |
2172 | 2172 | | |
| 2173 | + | |
| 2174 | + | |
| 2175 | + | |
| 2176 | + | |
| 2177 | + | |
| 2178 | + | |
| 2179 | + | |
| 2180 | + | |
2173 | 2181 | | |
2174 | 2182 | | |
2175 | 2183 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
10 | 15 | | |
11 | 16 | | |
12 | 17 | | |
| |||
Lines changed: 76 additions & 25 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
24 | | - | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| 29 | + | |
28 | 30 | | |
29 | 31 | | |
30 | 32 | | |
| |||
45 | 47 | | |
46 | 48 | | |
47 | 49 | | |
| 50 | + | |
| 51 | + | |
48 | 52 | | |
49 | 53 | | |
50 | 54 | | |
| |||
230 | 234 | | |
231 | 235 | | |
232 | 236 | | |
233 | | - | |
234 | | - | |
235 | 237 | | |
236 | 238 | | |
237 | | - | |
238 | 239 | | |
239 | 240 | | |
240 | 241 | | |
| 242 | + | |
| 243 | + | |
241 | 244 | | |
242 | 245 | | |
243 | 246 | | |
| |||
246 | 249 | | |
247 | 250 | | |
248 | 251 | | |
249 | | - | |
250 | 252 | | |
| 253 | + | |
251 | 254 | | |
252 | 255 | | |
253 | 256 | | |
| |||
264 | 267 | | |
265 | 268 | | |
266 | 269 | | |
| 270 | + | |
267 | 271 | | |
268 | 272 | | |
269 | 273 | | |
| |||
274 | 278 | | |
275 | 279 | | |
276 | 280 | | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
277 | 287 | | |
278 | 288 | | |
279 | 289 | | |
| |||
297 | 307 | | |
298 | 308 | | |
299 | 309 | | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
300 | 350 | | |
301 | 351 | | |
302 | 352 | | |
| |||
511 | 561 | | |
512 | 562 | | |
513 | 563 | | |
514 | | - | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
515 | 569 | | |
516 | 570 | | |
517 | 571 | | |
| |||
549 | 603 | | |
550 | 604 | | |
551 | 605 | | |
552 | | - | |
553 | | - | |
554 | | - | |
555 | | - | |
556 | | - | |
557 | | - | |
558 | | - | |
559 | | - | |
560 | | - | |
561 | | - | |
562 | | - | |
563 | | - | |
564 | | - | |
565 | | - | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
566 | 616 | | |
567 | 617 | | |
568 | 618 | | |
| |||
582 | 632 | | |
583 | 633 | | |
584 | 634 | | |
585 | | - | |
586 | | - | |
587 | | - | |
588 | | - | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
589 | 640 | | |
590 | 641 | | |
591 | 642 | | |
| |||
Lines changed: 21 additions & 51 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
| 17 | + | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | | - | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
70 | 71 | | |
71 | 72 | | |
72 | 73 | | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
73 | 86 | | |
74 | 87 | | |
75 | 88 | | |
| |||
108 | 121 | | |
109 | 122 | | |
110 | 123 | | |
111 | | - | |
112 | | - | |
113 | | - | |
114 | | - | |
115 | | - | |
116 | | - | |
117 | | - | |
118 | | - | |
119 | | - | |
120 | | - | |
121 | | - | |
122 | | - | |
123 | | - | |
124 | | - | |
125 | | - | |
126 | | - | |
127 | | - | |
128 | | - | |
129 | | - | |
130 | | - | |
131 | | - | |
132 | | - | |
133 | | - | |
134 | | - | |
135 | | - | |
136 | | - | |
137 | | - | |
138 | | - | |
139 | | - | |
140 | | - | |
141 | | - | |
142 | | - | |
143 | | - | |
144 | | - | |
145 | | - | |
146 | 124 | | |
147 | 125 | | |
148 | 126 | | |
| |||
331 | 309 | | |
332 | 310 | | |
333 | 311 | | |
334 | | - | |
335 | | - | |
336 | | - | |
337 | | - | |
338 | | - | |
339 | | - | |
340 | | - | |
341 | | - | |
342 | | - | |
343 | | - | |
344 | | - | |
| 312 | + | |
345 | 313 | | |
346 | | - | |
347 | | - | |
348 | | - | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
349 | 319 | | |
350 | 320 | | |
351 | 321 | | |
| |||
0 commit comments