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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions packages/bridge-controller/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### 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)).

### Changed

- Bump `@metamask/controller-utils` to `^11.8.0` ([#5765](https://github.com/MetaMask/core/pull/5765))
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 {
getDefaultBridgeControllerState,
} from './utils/bridge';

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

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

Expand Down
37 changes: 37 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 @@ -522,4 +523,40 @@ describe('Quote Metadata Utils', () => {
expect(result.usd).toBeNull();
});
});

describe('calcSlippagePercentage', () => {
it.each([
['100', '100', '100', '100', 0, 0],
['95', '95', '100', '100', 5, 5],
['98.3', '98.3', '100', '100', 1.7, 1.7],
[null, null, '100', '100', null, null],
['105', '105', '100', '100', 5, 5],
])(
'calcSlippagePercentage: calculate slippage absolute value for received amount %p, usd %p, sent amount %p, usd %p to expected amount %p, usd %p',
(
returnValueInCurrency: string | null,
returnUsd: string | null,
sentValueInCurrency: string | null,
sentUsd: string | null,
expectedValueInCurrency: number | null,
expectedUsd: number | null,
) => {
const result = calcSlippagePercentage(
{
valueInCurrency: returnValueInCurrency,
usd: returnUsd,
},
{
amount: '',
valueInCurrency: sentValueInCurrency,
usd: sentUsd,
},
);
expect(result.percentageInCurrency).toStrictEqual(
expectedValueInCurrency,
);
expect(result.percentageInUsd).toStrictEqual(expectedUsd);
},
);
});
});
36 changes: 36 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,42 @@ 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>,
) => {
const cost = calcCost(adjustedReturn, sentAmount);
return {
percentageInCurrency:
cost.valueInCurrency && sentAmount.valueInCurrency
? parseFloat(
new BigNumber(cost.valueInCurrency)
.div(sentAmount.valueInCurrency)
.times(100)
.abs()
.toFixed(2),
)
: null,
percentageInUsd:
Copy link
Member

Choose a reason for hiding this comment

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

Won't the percentages always be equal? We can just return 1 value in this case, and can we use a string and let the clients round/trim as needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We can 👍 . I coped the logic to always return the same as what we had, but we can change it to just one amount.

cost.usd && sentAmount.usd
? parseFloat(
new BigNumber(cost.usd)
.div(sentAmount.usd)
.times(100)
.abs()
.toFixed(2),
)
: null,
};
};

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