Skip to content

Commit ce2b22e

Browse files
chore: change log
1 parent 8dd27f0 commit ce2b22e

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

packages/bridge-controller/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- 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)).
13+
1014
### Changed
1115

1216
- Bump `@metamask/controller-utils` to `^11.8.0` ([#5765](https://github.com/MetaMask/core/pull/5765))

packages/bridge-controller/src/index.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,11 @@ export {
103103
getDefaultBridgeControllerState,
104104
} from './utils/bridge';
105105

106-
export { isValidQuoteRequest, formatEtaInMinutes } from './utils/quote';
106+
export {
107+
isValidQuoteRequest,
108+
formatEtaInMinutes,
109+
calcSlippagePercentage,
110+
} from './utils/quote';
107111

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

packages/bridge-controller/src/utils/quote.test.ts

+37
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
calcSwapRate,
1515
calcCost,
1616
formatEtaInMinutes,
17+
calcSlippagePercentage,
1718
} from './quote';
1819
import type {
1920
GenericQuoteRequest,
@@ -522,4 +523,40 @@ describe('Quote Metadata Utils', () => {
522523
expect(result.usd).toBeNull();
523524
});
524525
});
526+
527+
describe('calcSlippagePercentage', () => {
528+
it.each([
529+
['100', '100', '100', '100', 0, 0],
530+
['95', '95', '100', '100', 5, 5],
531+
['98.3', '98.3', '100', '100', 1.7, 1.7],
532+
[null, null, '100', '100', null, null],
533+
['105', '105', '100', '100', 5, 5],
534+
])(
535+
'calcSlippagePercentage: calculate slippage absolute value for received amount %p, usd %p, sent amount %p, usd %p to expected amount %p, usd %p',
536+
(
537+
returnValueInCurrency: string | null,
538+
returnUsd: string | null,
539+
sentValueInCurrency: string | null,
540+
sentUsd: string | null,
541+
expectedValueInCurrency: number | null,
542+
expectedUsd: number | null,
543+
) => {
544+
const result = calcSlippagePercentage(
545+
{
546+
valueInCurrency: returnValueInCurrency,
547+
usd: returnUsd,
548+
},
549+
{
550+
amount: '',
551+
valueInCurrency: sentValueInCurrency,
552+
usd: sentUsd,
553+
},
554+
);
555+
expect(result.percentageInCurrency).toStrictEqual(
556+
expectedValueInCurrency,
557+
);
558+
expect(result.percentageInUsd).toStrictEqual(expectedUsd);
559+
},
560+
);
561+
});
525562
});

packages/bridge-controller/src/utils/quote.ts

+36
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,42 @@ export const calcCost = (
304304
: null,
305305
});
306306

307+
/**
308+
* Calculates the slippage absolute value percentage based on the adjusted return and sent amount.
309+
*
310+
* @param adjustedReturn - Adjusted return value
311+
* @param sentAmount - Sent amount value
312+
* @returns the slippage in percentage
313+
*/
314+
export const calcSlippagePercentage = (
315+
adjustedReturn: ReturnType<typeof calcAdjustedReturn>,
316+
sentAmount: ReturnType<typeof calcSentAmount>,
317+
) => {
318+
const cost = calcCost(adjustedReturn, sentAmount);
319+
return {
320+
percentageInCurrency:
321+
cost.valueInCurrency && sentAmount.valueInCurrency
322+
? parseFloat(
323+
new BigNumber(cost.valueInCurrency)
324+
.div(sentAmount.valueInCurrency)
325+
.times(100)
326+
.abs()
327+
.toFixed(2),
328+
)
329+
: null,
330+
percentageInUsd:
331+
cost.usd && sentAmount.usd
332+
? parseFloat(
333+
new BigNumber(cost.usd)
334+
.div(sentAmount.usd)
335+
.times(100)
336+
.abs()
337+
.toFixed(2),
338+
)
339+
: null,
340+
};
341+
};
342+
307343
export const formatEtaInMinutes = (
308344
estimatedProcessingTimeInSeconds: number,
309345
) => {

0 commit comments

Comments
 (0)