refactor: migrate from jsbi for currency#7391
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
✅ Files skipped from review due to trivial changes (1)
WalkthroughThis PR migrates code from the JSBI library to native JavaScript Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 0/1 reviews remaining, refill in 60 minutes.Comment |
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@libs/common-utils/src/rawToTokenAmount.ts`:
- Around line 1-2: Change rawToTokenAmount to accept value: bigint | string
instead of number and return bigint; inside the function, detect typeof value—if
bigint, use it directly; if string, validate it is an integer numeric string (no
decimal point or non-digit chars) and convert with BigInt(value), otherwise
throw a clear error; multiply the resulting BigInt by 10n **
BigInt(tokenDecimals) as before; update any callers that pass numbers to provide
bigint or numeric strings to preserve integer precision.
In `@libs/currency/src/entities/fractions/fraction.ts`:
- Around line 8-13: The toBigInt helper currently accepts numbers which allows
callers to pass unsafe IEEE-754 values; update the toBigInt function to reject
number inputs (throw a clear error advising to pass a string or bigint) instead
of converting them, so amount-backed types (Fraction, CurrencyAmount, Percent,
Price) only accept exact representations; keep handling for bigint, string, and
JSBI-like objects (use value.toString() for the latter) and ensure the thrown
message references this requirement.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 1eeeaa69-8358-4cc7-9e21-72f498d949c4
📒 Files selected for processing (15)
apps/cowswap-frontend/src/legacy/hooks/useCombinedBalance.tsapps/cowswap-frontend/src/legacy/state/orders/utils.tsapps/cowswap-frontend/src/utils/orderUtils/calculateExecutionPrice.tslibs/common-utils/src/amountFormat/index.tslibs/common-utils/src/fractionUtils.test.tslibs/common-utils/src/fractionUtils.tslibs/common-utils/src/maxAmountSpend.tslibs/common-utils/src/rawToTokenAmount.tslibs/currency/src/entities/constants.tslibs/currency/src/entities/fractions/currencyAmount.test.tslibs/currency/src/entities/fractions/currencyAmount.tslibs/currency/src/entities/fractions/fraction.test.tslibs/currency/src/entities/fractions/fraction.tslibs/currency/src/entities/fractions/percent.tslibs/currency/src/entities/fractions/price.ts
…owswap into feat/migration-from-jsbi-1
kernelwhisperer
left a comment
There was a problem hiding this comment.
I get typecheck errors:
daniel in cowswap on feat/migration-from-jsbi-1 [$] via ⬢ v22.22.1
➜ pnpm exec tsc --noEmit -p libs/common-utils/tsconfig.lib.json
libs/common-utils/src/calculateSlippageAmount.ts:9:11 - error TS2322: Type 'bigint' is not assignable to type 'JSBI'.
9 return [value.multiply(ONE_FRACTION.subtract(slippage)).quotient, value.multiply(ONE_FRACTION.add(slippage)).quotient]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libs/common-utils/src/calculateSlippageAmount.ts:9:69 - error TS2322: Type 'bigint' is not assignable to type 'JSBI'.
9 return [value.multiply(ONE_FRACTION.subtract(slippage)).quotient, value.multiply(ONE_FRACTION.add(slippage)).quotient]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
libs/common-utils/src/formatCurrencyAmount.ts:18:18 - error TS2345: Argument of type 'bigint' is not assignable to parameter of type 'JSBI'.
18 if (JSBI.equal(amount.quotient, JSBI.BigInt(0))) {
~~~~~~~~~~~~~~~
Found 3 errors in 2 files.
Errors Files
2 libs/common-utils/src/calculateSlippageAmount.ts:9
1 libs/common-utils/src/formatCurrencyAmount.ts:18
| if (typeof value === 'bigint') return value | ||
| if (typeof value === 'number') return BigInt(value) | ||
| if (typeof value === 'string') return BigInt(value) | ||
| // JSBI instance |
There was a problem hiding this comment.
Is this comment needed?
|
|
||
| // exports for external consumption | ||
| export type BigintIsh = JSBI | string | number | ||
| export type BigintIsh = JSBI | bigint | string | number |
There was a problem hiding this comment.
Will we be able to remove JSBI from here?
Summary
Migrate currency lib from jsbi. It's a first pr with replacing jsbi operations by native bigint.
To Test
Need some regress due to this refactor. Check rates and token rounding (it should works as before).
Summary by CodeRabbit
Refactor
Tests
New Features