Skip to content

feat: implement QuoteResponse V1 and V2 coercers - #9725

Merged
micaelae merged 28 commits into
mainfrom
swaps4546-v2-coercers
Aug 4, 2026
Merged

feat: implement QuoteResponse V1 and V2 coercers#9725
micaelae merged 28 commits into
mainfrom
swaps4546-v2-coercers

Conversation

@micaelae

@micaelae micaelae commented Jul 31, 2026

Copy link
Copy Markdown
Member

Explanation

Implement coercers between QuoteResponse v1 and v2

  • toQuoteResponseV2 and toQuoteResponseV1 convert quote responses to required schema when needed
  • toQuoteMetadataV2 and toQuoteMetadataV1 convert quote metadata to required schema when needed
  • toNormalizedAmountsconverts atomic amounts to display-ready values
    Export sumAmounts helper util for adding up fees or token amounts

References

Implements https://consensyssoftware.atlassian.net/browse/SWAPS-4546

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note

Medium Risk
Changes quote shape conversion and fee/amount math used in swap/bridge UI; incorrect coercion could show wrong amounts or fees, but scope is bridge-controller with heavy test coverage and no auth/data-store changes.

Overview
Adds bidirectional coercion between bridge QuoteResponse v1 and v2 so clients can migrate without breaking older consumers. toQuoteResponseV2 maps flat v1 fields (srcTokenAmount, srcAsset, bridgeId, etc.) into nested v2 src/dest, array feeData, and protocols/aggregator, including adjusting src amount when tx/metabridge fees apply (unless intent). toQuoteResponseV1 is the deprecated inverse for backwards compatibility.

Metadata is bridged across shapes: toQuoteMetadataV1 / toQuoteMetadataV2 extract or project legacy QuoteMetadata onto v2 partial quotes; toNormalizedAmounts fills normalizedAmount fields from atomic values. mergeQuoteMetadata now branches on v1 vs v2—v1 still lodash-merges legacy fields; v2 merges normalized amounts, v2-shaped metadata, and legacy fields for client testing.

Number helpers: renames calcTokenAmountcalcNormalizedTokenAmount, adds calcAtomicTokenAmount, and exports sumAmounts for aggregating fee/token amounts (same asset for atomic sums). New coercers, metadata helpers, and sumAmounts are exported from the package entrypoint; changelog documents the additions.

Reviewed by Cursor Bugbot for commit 9e07537. Bugbot is set up for automated code reviews on this repo. Configure here.

@micaelae micaelae changed the title feat: V2 coercers feat: implement QuoteResponse V1 and V2 coercers Aug 3, 2026
@micaelae
micaelae marked this pull request as ready for review August 3, 2026 22:40
@micaelae
micaelae requested review from a team as code owners August 3, 2026 22:40
Comment thread packages/bridge-controller/src/coercers/quote-response-v1-to-v2.ts
Comment thread packages/bridge-controller/src/coercers/quote-response-v2-to-v1.ts
Comment thread packages/bridge-controller/src/coercers/quote-response-v1-to-v2.ts
Base automatically changed from swaps4546-v2-schema to main August 3, 2026 22:44
@micaelae
micaelae enabled auto-merge August 3, 2026 23:53
Comment thread packages/bridge-controller/src/coercers/quote-response-v1-to-v2.ts Outdated
Comment thread packages/bridge-controller/src/coercers/quote-response-v1-to-v2.ts
Comment thread packages/bridge-controller/src/coercers/quote-response-v1-to-v2.ts Outdated
Comment thread packages/bridge-controller/src/coercers/quote-response-v1-to-v2.ts
Comment thread packages/bridge-controller/src/utils/number-formatters.ts Outdated
Comment thread packages/bridge-controller/src/utils/number-formatters.ts Outdated

@infiniteflower infiniteflower left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

https://github.com/MetaMask/core/pull/9725/changes#r3713514183, this comment applies to all toV2 functions

@cursor cursor 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.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

There are 2 total unresolved issues (including 1 from previous review).

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit c388297. Configure here.

Comment thread packages/bridge-controller/src/index.ts
@micaelae
micaelae added this pull request to the merge queue Aug 4, 2026
Merged via the queue into main with commit 8da286a Aug 4, 2026
50 checks passed
@micaelae
micaelae deleted the swaps4546-v2-coercers branch August 4, 2026 17:41
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.

2 participants