Skip to content
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
4 changes: 4 additions & 0 deletions packages/subscription-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]

### Changed

- fix: `getTokenApproveAmount` precision by using bignumber.js ([#7070](https://github.com/MetaMask/core/pull/7070))

## [3.2.0]

### Added
Expand Down
3 changes: 2 additions & 1 deletion packages/subscription-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
"@metamask/messenger": "^0.3.0",
"@metamask/polling-controller": "^15.0.0",
"@metamask/transaction-controller": "^61.1.0",
"@metamask/utils": "^11.8.1"
"@metamask/utils": "^11.8.1",
"bignumber.js": "^9.1.2"
},
"devDependencies": {
"@metamask/auto-changelog": "^3.4.4",
Expand Down
30 changes: 11 additions & 19 deletions packages/subscription-controller/src/SubscriptionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { AuthenticationController } from '@metamask/profile-sync-controller
import type { TransactionMeta } from '@metamask/transaction-controller';
import { TransactionType } from '@metamask/transaction-controller';
import { type Hex } from '@metamask/utils';
import { BigNumber } from 'bignumber.js';

import {
ACTIVE_SUBSCRIPTION_STATUSES,
Expand Down Expand Up @@ -710,8 +711,10 @@ export class SubscriptionController extends StaticIntervalPollingController()<
*/
#getSubscriptionPriceAmount(price: ProductPrice) {
// no need to use BigInt since max unitDecimals are always 2 for price
const amount =
(price.unitAmount / 10 ** price.unitDecimals) * price.minBillingCycles;
const amount = new BigNumber(price.unitAmount)
.div(10 ** price.unitDecimals)
.multipliedBy(price.minBillingCycles)
.toString();
return amount;
}

Expand All @@ -733,25 +736,14 @@ export class SubscriptionController extends StaticIntervalPollingController()<
if (!conversionRate) {
throw new Error('Conversion rate not found');
}
// conversion rate is a float string e.g: "1.0"
// We need to handle float conversion rates with integer math for BigInt.
// We'll scale the conversion rate to an integer by multiplying by 10^4.
// conversionRate is in usd decimal. In most currencies, we only care about 2 decimals (cents)
// So, scale must be max of 10 ** 4 (most exchanges trade with max 4 decimals of usd)
// This allows us to avoid floating point math and keep precision.
const SCALE = 10n ** 4n;
const conversionRateScaled =
BigInt(Math.round(Number(conversionRate) * Number(SCALE))) / SCALE;
// price of the product
const priceAmount = this.#getSubscriptionPriceAmount(price);
const priceAmountScaled =
BigInt(Math.round(priceAmount * Number(SCALE))) / SCALE;
const priceAmount = new BigNumber(this.#getSubscriptionPriceAmount(price));

const tokenDecimal = BigInt(10) ** BigInt(tokenPaymentInfo.decimals);

const tokenAmount =
(priceAmountScaled * tokenDecimal) / conversionRateScaled;
return tokenAmount.toString();
const tokenDecimal = new BigNumber(10).pow(tokenPaymentInfo.decimals);
const tokenAmount = priceAmount
.multipliedBy(tokenDecimal)
.div(conversionRate);
return tokenAmount.toFixed(0);
}

/**
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5090,6 +5090,7 @@ __metadata:
"@metamask/utils": "npm:^11.8.1"
"@ts-bridge/cli": "npm:^0.6.4"
"@types/jest": "npm:^27.4.1"
bignumber.js: "npm:^9.1.2"
deepmerge: "npm:^4.2.2"
jest: "npm:^27.5.1"
sinon: "npm:^9.2.4"
Expand Down
Loading