Skip to content

chore: calc slippage percentage #5723

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
merged 7 commits into from
May 16, 2025
Merged
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
1 change: 1 addition & 0 deletions packages/bridge-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add and export `calcSlippagePercentage`, a utility that calculates the absolute slippage percentage based on the adjusted return and the sent amount ([#5723](https://github.com/MetaMask/core/pull/5723)).
- Error logs for invalid getQuote responses ([#5816](https://github.com/MetaMask/core/pull/5816))

### Changed
Expand Down
6 changes: 5 additions & 1 deletion packages/bridge-controller/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ export {
isCrossChain,
} from './utils/bridge';

export { isValidQuoteRequest, formatEtaInMinutes } from './utils/quote';
export {
isValidQuoteRequest,
formatEtaInMinutes,
calcSlippagePercentage,
} from './utils/quote';

export { calcLatestSrcBalance } from './utils/balance';

Expand Down
34 changes: 34 additions & 0 deletions packages/bridge-controller/src/utils/quote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
calcSwapRate,
calcCost,
formatEtaInMinutes,
calcSlippagePercentage,
} from './quote';
import type {
GenericQuoteRequest,
Expand Down Expand Up @@ -534,4 +535,37 @@ describe('Quote Metadata Utils', () => {
expect(result.usd).toBeNull();
});
});

describe('calcSlippagePercentage', () => {
it.each([
['100', null, '100', null, '0'],
['95', '95', '100', '100', '5'],
['98.3', '98.3', '100', '100', '1.7'],
[null, '100', null, '100', '0'],
[null, null, null, '100', null],
['105', '105', '100', '100', '5'],
])(
'calcSlippagePercentage: calculate slippage absolute value for received amount %p, usd %p, sent amount %p, usd %p to expected slippage %p',
(
returnValueInCurrency: string | null,
returnUsd: string | null,
sentValueInCurrency: string | null,
sentUsd: string | null,
expectedSlippage: string | null,
) => {
const result = calcSlippagePercentage(
{
valueInCurrency: returnValueInCurrency,
usd: returnUsd,
},
{
amount: '1000',
valueInCurrency: sentValueInCurrency,
usd: sentUsd,
},
);
expect(result).toBe(expectedSlippage);
},
);
});
});
32 changes: 32 additions & 0 deletions packages/bridge-controller/src/utils/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,38 @@ export const calcCost = (
: null,
});

/**
* Calculates the slippage absolute value percentage based on the adjusted return and sent amount.
*
* @param adjustedReturn - Adjusted return value
* @param sentAmount - Sent amount value
* @returns the slippage in percentage
*/
export const calcSlippagePercentage = (
adjustedReturn: ReturnType<typeof calcAdjustedReturn>,
sentAmount: ReturnType<typeof calcSentAmount>,
): string | null => {
const cost = calcCost(adjustedReturn, sentAmount);

if (cost.valueInCurrency && sentAmount.valueInCurrency) {
return new BigNumber(cost.valueInCurrency)
.div(sentAmount.valueInCurrency)
.times(100)
.abs()
.toString();
}

if (cost.usd && sentAmount.usd) {
return new BigNumber(cost.usd)
.div(sentAmount.usd)
.times(100)
.abs()
.toString();
}

return null;
};

export const formatEtaInMinutes = (
estimatedProcessingTimeInSeconds: number,
) => {
Expand Down
Loading