Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ exports[`Bridge Selectors selectBridgeQuotes should return sorted quotes with me
"usd": "2.099927",
"valueInCurrency": "419.985546",
},
"cost": {
"usd": "8.900073",
"valueInCurrency": "1758.014454",
},
"priceImpact": {
"usd": "7.9",
"valueInCurrency": "1564.2",
Expand Down Expand Up @@ -350,6 +354,10 @@ exports[`Bridge Selectors selectBridgeQuotes should return sorted quotes with me
"usd": "2.099927",
"valueInCurrency": "419.985546",
},
"cost": {
"usd": "8.900073",
"valueInCurrency": "1758.014454",
},
"priceImpact": {
"usd": "8.9",
"valueInCurrency": "1758",
Expand Down
43 changes: 30 additions & 13 deletions packages/bridge-controller/src/selectors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2083,36 +2083,36 @@ describe('Bridge Selectors', () => {

expect(totalReceived).toMatchInlineSnapshot(`
{
"amount": "38240503",
"amount": "38423182",
"asset": {
"assetId": "eip155:137/erc20:0x3c499c542cef5e3811e1192ce70d8cc03d5c3359",
"decimals": 6,
"iconUrl": "https://media.socket.tech/tokens/all/USDC",
"name": "Native USD Coin (POS)",
"symbol": "USDC",
},
"minAmount": "37460000",
"minAmountNormalized": "37.46",
"minAmountUsd": "37.46",
"minAmountValueInCurrency": "7492",
"normalizedAmount": "38.240503",
"usd": "38.240503",
"valueInCurrency": "7648.1006",
"minAmount": "37600000",
"minAmountNormalized": "37.6",
"minAmountUsd": "37.6",
"minAmountValueInCurrency": "7520",
"normalizedAmount": "38.423182",
"usd": "38.423182",
"valueInCurrency": "7684.6364",
}
`);
expect(minimumReceived).toMatchInlineSnapshot(`
{
"amount": "37460000",
"amount": "37600000",
"asset": {
"assetId": "eip155:137/erc20:0x3c499c542cef5e3811e1192ce70d8cc03d5c3359",
"decimals": 6,
"iconUrl": "https://media.socket.tech/tokens/all/USDC",
"name": "Native USD Coin (POS)",
"symbol": "USDC",
},
"normalizedAmount": "37.46",
"usd": "37.46",
"valueInCurrency": "7492",
"normalizedAmount": "37.6",
"usd": "37.6",
"valueInCurrency": "7520",
}
`);
expect(rest).toMatchInlineSnapshot(`
Expand All @@ -2123,10 +2123,27 @@ describe('Bridge Selectors', () => {
"quotesRefreshCount": 0,
}
`);
expect(recommendedQuotes.map((quote) => quote?.cost?.valueInCurrency))
.toMatchInlineSnapshot(`
[
"-4867.87052548",
"24925.00683656",
]
`);
expect(
recommendedQuotes.map(
(quote) => quote?.quote.priceData?.cost?.valueInCurrency,
),
).toMatchInlineSnapshot(`
[
"-4867.87052548",
"24925.00683656",
]
`);
expect(recommendedQuotes.map((quote) => quote?.quote.requestId))
.toMatchInlineSnapshot(`
[
"4277a368-40d7-4e82-aa67-74f29dc5f98a",
"381c23bc-e3e4-48fe-bc53-257471e388ad",
"90ae8e69-f03a-4cf6-bab7-ed4e3431eb37",
]
`);
Expand Down
25 changes: 23 additions & 2 deletions packages/bridge-controller/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,26 @@ const selectSortedBridgeQuotes = createBridgeSelector(
'asc',
);
default:
if (
quotesWithMetadata.every(
(quote) => quote.quote.priceData?.cost?.valueInCurrency,
)
) {
return orderBy(
quotesWithMetadata,
({ quote: { priceData } }) =>
Number(priceData?.cost?.valueInCurrency),
'asc',
);
}
if (quotesWithMetadata.every((quote) => quote.cost?.valueInCurrency)) {
return orderBy(
quotesWithMetadata,
({ quote: { priceData } }) =>
Number(priceData?.cost?.valueInCurrency),
'asc',
);
}
if (
quotesWithMetadata.every(
(quote) => quote.quote.priceData?.priceImpact?.amount,
Expand All @@ -469,7 +489,8 @@ const selectSortedBridgeQuotes = createBridgeSelector(
Number(priceData?.priceImpact?.amount),
'asc',
);
} else if (
}
if (
quotesWithMetadata.every(
(quote) => quote.quote.priceData?.priceImpact?.valueInCurrency,
)
Expand Down Expand Up @@ -585,7 +606,7 @@ const selectRecommendedQuotes = createBridgeSelector(
const requestIndex = quote.quoteRequestIndex ?? 0;
acc[requestIndex] ??= quote;
return acc;
}, Array<QuoteResponse | null>(requestCount).fill(null)),
}, Array<(QuoteResponse & QuoteMetadata) | null>(requestCount).fill(null)),
);

const selectDestAmountSum = createBridgeSelector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ export const toQuoteMetadataV1 = (
}),
...includeIfTruthy(priceData?.priceImpact, {
priceImpact: toTokenAmountValues(priceData?.priceImpact),
// Use priceImpact as cost
}),
...includeIfTruthy(priceData?.cost, {
cost: toTokenAmountValues(priceData?.priceImpact),
}),
...includeIfTruthy(relayer?.[0], {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@ export const toQuoteMetadataV2 = (
: undefined;
const txFeeAsset = quoteResponseV2?.quote?.feeData?.txFee?.[0]?.asset;

const priceImpactToUse = {
usd: priceImpact?.usd ?? cost?.usd,
valueInCurrency: priceImpact?.valueInCurrency ?? cost?.valueInCurrency,
};
const networkFeeToUse = gasFee?.total ?? totalNetworkFee;

return {
Expand Down Expand Up @@ -101,12 +97,15 @@ export const toQuoteMetadataV2 = (
}),
},
priceData: {
...includeIfTruthy(priceImpactToUse, {
priceImpact: priceImpactToUse,
...includeIfTruthy(priceImpact, {
priceImpact,
}),
...includeIfTruthy(adjustedReturn, {
adjustedReturn,
}),
...includeIfTruthy(cost, {
cost,
}),
swapRate,
},
},
Expand Down
1 change: 1 addition & 0 deletions packages/bridge-controller/src/validators/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export const QuoteSchemaV2 = intersection([
'usd',
'valueInCurrency',
]),
cost: pick(AmountsAndAssetSchema, ['usd', 'valueInCurrency']),
}),
),
),
Expand Down
Loading