-
-
Notifications
You must be signed in to change notification settings - Fork 293
feat: implement QuoteResponse V1 and V2 coercers #9725
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
Merged
+1,395
−76
Merged
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
158b79f
wip
micaelae 06bb224
Revert "wip"
micaelae 3397c50
feat: v2 schema
micaelae 267d7f3
feat: V2 coercers
micaelae e9fc1d7
chore: changelog
micaelae d178568
Merge branch 'swaps4546-v2-schema' into swaps4546-v2-coercers
micaelae 66750d1
v2
micaelae c0166df
coercers
micaelae 370fb4d
Merge branch 'main' into swaps4546-v2-schema
micaelae 65b56a2
Merge branch 'swaps4546-v2-schema' into swaps4546-v2-coercers
micaelae f229640
fix: lint
micaelae f4fb464
fix: test coverage
micaelae eeb0a73
chore: changelog
micaelae 7321ae0
chore: undo Stellar testnet removal
micaelae 8bd9439
chore: update comment
micaelae e8b7fa7
chore: rename NumberStringSchema -> PositiveNumberStringSchema
micaelae 80cd0e7
Merge branch 'swaps4546-v2-schema' into swaps4546-v2-coercers
micaelae c421f64
Merge branch 'main' into swaps4546-v2-coercers
micaelae 81307ec
fix: intent
micaelae 3d460fc
fix: coverage
micaelae c388297
Merge branch 'main' into swaps4546-v2-coercers
micaelae 03be8a6
refactor: rename token amount formatters
micaelae bfede7a
fix: export sumAmounts
micaelae 157df42
fix: changelog
micaelae cda6f82
chore: remove unused vars
micaelae 466cc59
chore: rm unknown types
micaelae f61417e
Merge branch 'main' into swaps4546-v2-coercers
micaelae 9e07537
chore: restore unknown
micaelae 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
172 changes: 172 additions & 0 deletions
172
packages/bridge-controller/src/coercers/quote-response-v1-to-v2.test.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| import { KnownCaipNamespace } from '@metamask/utils'; | ||
| import { QuoteMetadata } from 'src/utils/quote-metadata/types'; | ||
|
|
||
| import { mockBridgeQuotesErc20Erc20V2Migration } from '../../tests/mock-quotes-erc20-erc20-migration-v2.js'; | ||
| import { mockBridgeQuotesErc20Erc20V1 } from '../../tests/mock-quotes-erc20-erc20.js'; | ||
| import { mergeQuoteMetadata } from '../utils/quote-metadata/merge.js'; | ||
| import { toQuoteMetadataV1 } from '../utils/quote-metadata/to-quote-metadata-v1.js'; | ||
| import { toQuoteResponseV2 } from './quote-response-v1-to-v2.js'; | ||
|
|
||
| const TEST_METADATA: QuoteMetadata = { | ||
| sentAmount: { | ||
| amount: '14', | ||
| usd: undefined, | ||
| valueInCurrency: undefined, | ||
| }, | ||
| toTokenAmount: { | ||
| amount: '13.98428', | ||
| usd: undefined, | ||
| valueInCurrency: undefined, | ||
| }, | ||
| minToTokenAmount: { | ||
| amount: '13.7', | ||
| usd: undefined, | ||
| valueInCurrency: undefined, | ||
| }, | ||
| relayerFee: { | ||
| amount: '0.00001', | ||
| usd: undefined, | ||
| valueInCurrency: undefined, | ||
| }, | ||
| totalNetworkFee: { | ||
| amount: '0.001', | ||
| usd: undefined, | ||
| valueInCurrency: undefined, | ||
| }, | ||
| gasFee: { | ||
| total: { amount: '0.00099', usd: undefined, valueInCurrency: undefined }, | ||
| }, | ||
| swapRate: '0.99887714285714285714', | ||
| priceImpact: { | ||
| usd: '1.5', | ||
| valueInCurrency: '1.5', | ||
| }, | ||
| }; | ||
|
|
||
| const quoteResponseV1WithMetadata = { | ||
| ...mockBridgeQuotesErc20Erc20V1[0], | ||
| ...TEST_METADATA, | ||
| }; | ||
|
|
||
| describe('quote-response-v2 migration', () => { | ||
| describe('toQuoteResponseV2', () => { | ||
| it('should return a validation error for an invalid quote response', () => { | ||
| const quoteResponse = { | ||
| quote: { | ||
| requestId: '123', | ||
| }, | ||
| }; | ||
|
|
||
| expect(() => | ||
| toQuoteResponseV2(quoteResponse), | ||
| ).toThrowErrorMatchingInlineSnapshot( | ||
| `"At path: quote.src -- Expected an object, but received: undefined"`, | ||
| ); | ||
| }); | ||
|
|
||
| it('should return QuoteResponse with no normalized amounts and no metadata (V1 input)', () => { | ||
| const quoteResponseV2 = toQuoteResponseV2(quoteResponseV1WithMetadata); | ||
|
|
||
| const expectedQuoteResponseV2 = mockBridgeQuotesErc20Erc20V2Migration[0]; | ||
| delete expectedQuoteResponseV2.quote.feeData.network; | ||
| expect( | ||
| quoteResponseV2.quote.feeData?.network?.[0]?.amount, | ||
| ).toBeUndefined(); | ||
|
|
||
| expect(quoteResponseV2).toStrictEqual({ | ||
| ...expectedQuoteResponseV2, | ||
| ...TEST_METADATA, | ||
| namespace: KnownCaipNamespace.Eip155, | ||
| chainId: 'eip155:10', | ||
| }); | ||
|
|
||
| const extractedMetadata = toQuoteMetadataV1(quoteResponseV2); | ||
| expect(extractedMetadata).toStrictEqual(TEST_METADATA); | ||
| }); | ||
|
|
||
| it('should return QuoteResponse with no normalized amounts and preserve metadata (V1 input)', () => { | ||
| const quoteResponseV2 = mergeQuoteMetadata( | ||
| toQuoteResponseV2(quoteResponseV1WithMetadata), | ||
| TEST_METADATA, | ||
| ); | ||
| const expectedQuoteResponseV2 = mergeQuoteMetadata( | ||
| mockBridgeQuotesErc20Erc20V2Migration[0], | ||
| TEST_METADATA, | ||
| ); | ||
|
|
||
| expect(expectedQuoteResponseV2.quote.feeData).toMatchInlineSnapshot(` | ||
| { | ||
| "metabridge": [ | ||
| { | ||
| "amount": "0", | ||
| "asset": { | ||
| "assetId": "eip155:10/erc20:0x0b2c639c533813f4aa9d7837caf62653d097ff85", | ||
| "decimals": 6, | ||
| "name": "USD Coin", | ||
| "symbol": "USDC", | ||
| }, | ||
| }, | ||
| ], | ||
| "network": [ | ||
| { | ||
| "amount": "990000000000000", | ||
| "asset": { | ||
| "assetId": "eip155:10/slip44:60", | ||
| "decimals": 18, | ||
| "name": "Ether", | ||
| "symbol": "ETH", | ||
| }, | ||
| "normalizedAmount": "0.00099", | ||
| "usd": undefined, | ||
| "valueInCurrency": undefined, | ||
| }, | ||
| ], | ||
| "relayer": [ | ||
| { | ||
| "amount": "10000000000000", | ||
| "asset": { | ||
| "assetId": "eip155:10/slip44:60", | ||
| "decimals": 18, | ||
| "name": "Ether", | ||
| "symbol": "ETH", | ||
| }, | ||
| "normalizedAmount": "0.00001", | ||
| "usd": undefined, | ||
| "valueInCurrency": undefined, | ||
| }, | ||
| ], | ||
| "txFee": undefined, | ||
| } | ||
| `); | ||
|
|
||
| const extractedMetadata = toQuoteMetadataV1(quoteResponseV2); | ||
| expect(quoteResponseV2).toStrictEqual({ | ||
| ...expectedQuoteResponseV2, | ||
| ...TEST_METADATA, | ||
| namespace: KnownCaipNamespace.Eip155, | ||
| chainId: 'eip155:10', | ||
| }); | ||
| expect(extractedMetadata).toStrictEqual(TEST_METADATA); | ||
| }); | ||
|
|
||
| it('should return QuoteResponse and preserve metadata (V2 input)', () => { | ||
| const quoteResponse = { | ||
| ...mockBridgeQuotesErc20Erc20V2Migration[0], | ||
| ...TEST_METADATA, | ||
| }; | ||
| const quoteResponseV2 = toQuoteResponseV2(quoteResponse); | ||
| expect(quoteResponseV2).toStrictEqual({ | ||
| ...quoteResponse, | ||
| namespace: KnownCaipNamespace.Eip155, | ||
| chainId: 'eip155:10', | ||
| }); | ||
| expect(toQuoteMetadataV1(quoteResponseV2)).toStrictEqual(TEST_METADATA); | ||
| }); | ||
|
|
||
| it('should throw an error for a null input', () => { | ||
| expect(() => toQuoteResponseV2(null)).toThrow( | ||
| 'Expected an object, but received: null', | ||
| ); | ||
| }); | ||
| }); | ||
| }); |
199 changes: 199 additions & 0 deletions
199
packages/bridge-controller/src/coercers/quote-response-v1-to-v2.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| import { create, coerce, Infer, is, intersection } from '@metamask/superstruct'; | ||
| import { parseCaipAssetType } from '@metamask/utils'; | ||
| import { BigNumber } from 'bignumber.js'; | ||
|
|
||
| import { formatAddressToAssetId } from '../utils/caip-formatters.js'; | ||
| import { sumAmounts } from '../utils/number-formatters.js'; | ||
| import { | ||
| BridgeAssetSchema, | ||
| BridgeAssetV2Schema, | ||
| MinimalAssetSchema, | ||
| } from '../validators/bridge-asset.js'; | ||
| import { QuoteResponseSchemaV1 } from '../validators/quote-response-v1.js'; | ||
| import type { QuoteResponseV1 } from '../validators/quote-response-v1.js'; | ||
| import { | ||
| QuoteResponseSchemaV2, | ||
| validateQuoteResponse, | ||
| } from '../validators/quote-response.js'; | ||
| import type { QuoteResponse } from '../validators/quote-response.js'; | ||
| import { QuoteSchemaV2, FeeType, QuoteSchema } from '../validators/quote.js'; | ||
| import { StepSchemaV2, StepSchema } from '../validators/step.js'; | ||
|
|
||
| const BridgeAssetV2FromV1 = coerce( | ||
| BridgeAssetV2Schema, | ||
| intersection([BridgeAssetSchema, MinimalAssetSchema]), | ||
| (value) => { | ||
| const { chainId, address, iconUrl, icon, assetId, ...rest } = value; | ||
|
|
||
| const resolvedIconUrl = iconUrl ?? icon; | ||
|
|
||
| return { | ||
| assetId: | ||
| assetId ?? | ||
| /* istanbul ignore next */ formatAddressToAssetId(address, chainId), | ||
| ...(resolvedIconUrl && { iconUrl: resolvedIconUrl }), | ||
| ...rest, | ||
| }; | ||
| }, | ||
| ); | ||
|
|
||
| export const toBridgeAssetV2 = ( | ||
| data: unknown, | ||
| ): Infer<typeof BridgeAssetV2Schema> => { | ||
| return create(data, BridgeAssetV2FromV1); | ||
| }; | ||
|
|
||
| const StepSchemaV2FromV1 = coerce(StepSchemaV2, StepSchema, (value) => { | ||
| const { srcAsset, destAsset, action } = value; | ||
| return { | ||
| action, | ||
| src: { | ||
| asset: toBridgeAssetV2(srcAsset), | ||
| }, | ||
| dest: { | ||
| asset: toBridgeAssetV2(destAsset), | ||
| }, | ||
| }; | ||
| }); | ||
| const toStepV2 = (step: Infer<typeof StepSchema>): Infer<typeof StepSchemaV2> => | ||
| create(step, StepSchemaV2FromV1); | ||
|
|
||
| const QuoteV2FromV1 = coerce(QuoteSchemaV2, QuoteSchema, (value) => { | ||
| const { | ||
| srcTokenAmount, | ||
| destTokenAmount, | ||
| minDestTokenAmount, | ||
| srcAsset, | ||
| destAsset, | ||
| srcChainId, | ||
| destChainId, | ||
| walletAddress, | ||
| destWalletAddress, | ||
| priceData, | ||
| feeData, | ||
| bridgeId, | ||
| bridges, | ||
| steps, | ||
| intent, | ||
| ...restQuote | ||
| } = value; | ||
|
|
||
| return { | ||
| src: { | ||
| amount: new BigNumber(srcTokenAmount) | ||
| .plus( | ||
| intent | ||
| ? 0 | ||
| : (sumAmounts([ | ||
| feeData[FeeType.TX_FEE], | ||
| feeData[FeeType.METABRIDGE], | ||
| ])?.amount ?? 0), | ||
| ) | ||
| .toFixed(), | ||
| asset: toBridgeAssetV2(srcAsset), | ||
| ...(walletAddress && { walletAddress }), | ||
| }, | ||
| dest: { | ||
| amount: destTokenAmount, | ||
| asset: toBridgeAssetV2(destAsset), | ||
| ...(destWalletAddress && { walletAddress: destWalletAddress }), | ||
| minAmount: minDestTokenAmount, | ||
| }, | ||
| priceData: { | ||
| ...(priceData?.priceImpact && { | ||
| priceImpact: { | ||
| amount: priceData.priceImpact, | ||
| }, | ||
| }), | ||
| }, | ||
| feeData: { | ||
| [FeeType.METABRIDGE]: [ | ||
| { | ||
| ...feeData[FeeType.METABRIDGE], | ||
| asset: toBridgeAssetV2(feeData[FeeType.METABRIDGE].asset), | ||
| ...(priceData?.totalFeeAmountUsd && /* istanbul ignore next */ { | ||
| usd: priceData?.totalFeeAmountUsd, | ||
| }), | ||
| }, | ||
| ], | ||
| ...(feeData[FeeType.TX_FEE] && /* istanbul ignore next */ { | ||
| [FeeType.TX_FEE]: [ | ||
| { | ||
| ...feeData[FeeType.TX_FEE], | ||
| asset: toBridgeAssetV2(feeData[FeeType.TX_FEE].asset), | ||
| }, | ||
| ], | ||
| }), | ||
| }, | ||
| steps: steps?.map(toStepV2), | ||
| ...restQuote, | ||
| ...(intent && /* istanbul ignore next */ { intent }), | ||
| protocols: bridges, | ||
| aggregator: bridgeId, | ||
| }; | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| const toQuoteV2 = ( | ||
| quote: Infer<typeof QuoteSchema>, | ||
| ): Infer<typeof QuoteSchemaV2> => { | ||
| const quoteV2 = create(quote, QuoteV2FromV1); | ||
| return quoteV2; | ||
| }; | ||
|
|
||
| const QuoteResponseV2FromV1 = coerce( | ||
| QuoteResponseSchemaV2, | ||
| QuoteResponseSchemaV1, | ||
| (value: QuoteResponseV1) => { | ||
| const { quote, l1GasFeesInHexWei, nonEvmFeesInNative, ...rest } = value; | ||
| const { srcAsset } = quote; | ||
|
|
||
| const { | ||
| chain: { namespace }, | ||
| chainId, | ||
| } = parseCaipAssetType(srcAsset.assetId); | ||
|
|
||
| return { | ||
| ...rest, | ||
| ...(nonEvmFeesInNative && { nonEvmFeesInNative }), | ||
| ...(l1GasFeesInHexWei && { l1GasFeesInHexWei }), | ||
| namespace, | ||
| chainId, | ||
| quote: toQuoteV2(quote), | ||
| }; | ||
| }, | ||
| ); | ||
|
|
||
| /** | ||
| * Converts a partial quote response to a {@link QuoteResponse}. | ||
| * This does not preserve any post-fetch metadata. | ||
| * | ||
| * @param quoteResponse - The {@link QuoteResponseV1} to convert | ||
| * @returns The {@link QuoteResponse} | ||
| */ | ||
| export function toQuoteResponseV2(quoteResponse: unknown): QuoteResponse { | ||
| let quoteResponseV2: QuoteResponse | null = null; | ||
|
|
||
| // V1 quote | ||
| /* istanbul ignore else */ | ||
| if (is(quoteResponse, QuoteResponseSchemaV1)) { | ||
| quoteResponseV2 = create(quoteResponse, QuoteResponseV2FromV1); | ||
| } | ||
| // V2 quote | ||
| else if (validateQuoteResponse(quoteResponse)) { | ||
| quoteResponseV2 = quoteResponse; | ||
| } | ||
|
|
||
| /* istanbul ignore else */ | ||
| if (quoteResponseV2) { | ||
| const { | ||
| chain: { namespace }, | ||
| chainId, | ||
| } = parseCaipAssetType(quoteResponseV2.quote.src.asset.assetId); | ||
|
|
||
| // Add namespace, chainId | ||
| return { ...quoteResponseV2, namespace: namespace as never, chainId }; | ||
| } | ||
|
|
||
| /* istanbul ignore next */ | ||
| throw new Error('QuoteResponseV1 to V2 conversion failed'); | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.