Repository navigation
fix collectFees native - #1642
Conversation
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
/run-security-scan |
alexcos20
left a comment
There was a problem hiding this comment.
AI automated code review (Gemini 3).
Overall risk: low
Summary:
The PR successfully resolves crashes related to BigInt math on non-EIP-1559 chains by properly handling null values for baseFeePerGas and maxFeePerGas. The implementation correctly defaults missing base fees to 0n and falls back to legacy gas price calculation if EIP-1559 fee data is incomplete. Excellent test coverage has been added to ensure these edge cases are locked down. LGTM!
Comments:
• [INFO][bug] Excellent fix for the BigInt type collision. Using the nullish coalescing operator ?? 0n safely bridges the gap between EIP-1559 and legacy chains, ensuring type safety in all subsequent math operations.
• [INFO][style] Good edge-case handling here. Using != null correctly checks for both null and undefined. Safely falling back to the legacy branch prevents exception-throwing operations and ensures transaction compatibility on chains with incomplete EIP-1559 support.
• [INFO][style] Comprehensive coverage for the different EIP-1559 and legacy combinations. Minor architectural note: While using any for originalGetProvider and temporarily overwriting methods works fine here, consider using a mocking library like sinon.stub() or jest.spyOn() for cleaner and type-safe mock management and restoration in future iterations.
Fix
collectFeesnative-token crash on non-EIP-1559 chains ("Cannot mix BigInt and other types")Branch:
bug/fix_collectfess_native_token(base:main)Type: bug fix + hardening + regression tests
Summary
The
collectFeesadmin command crashed withTypeError: Cannot mix BigInt and other types, use explicit conversionswhenever it swept the native token on a legacy / non-EIP-1559 chain (Ganache /
barge
chainId 8996). The node returned HTTP 500 (and the equivalent P2P error),which also blocked ocean.js CI.
Root cause:
Blockchain.calculateGasCostreadblock.baseFeePerGas, which isnullon non-EIP-1559 chains, then did
baseFee + priorityFee. Addingnullto abigintthrows the
Cannot mix BigInt and other typesTypeError.This PR defaults a missing base fee to
0n, hardens the same bug class ingetGasOptions, and adds unit tests that reproduce the crash conditions.Root cause detail
src/utils/blockchain.ts→calculateGasCost:calculateGasCostis only reached from the native-token branch of the collectFeeshandler (
src/components/core/admin/collectFeesHandler.ts,tokenAddress === ZeroAddress),which is why only the native-token path failed and the ERC20 path did not.
Call path:
CollectFeesHandler.handle→ native branch (tokenAddress === ZeroAddress)→
blockchain.calculateGasCost(...)→baseFee + priorityFeewithbaseFee === null.Changes
1.
src/utils/blockchain.ts—calculateGasCost(the fix)baseFeePerGasto0nso the gas-cost math stays entirely inBigInt on legacy chains.
block?.optional chain also guards the (rarer) case wheregetBlock('latest')returns
null.bigintbase fee passesthrough
?? 0nuntouched.Why
0nis safe here:calculateGasCostis used only as an advisory balancepre-check in
collectFeesHandler.ts(if (balance < calculateGasCost(...)) reject).The value actually transferred on-chain is always exactly the requested amount —
never "balance minus gas" — so a looser estimate can only cause an attempt that the
RPC then rejects for insufficient funds (surfaced as an error), never an over-transfer.
The change strictly improves behavior on non-EIP-1559 chains and loosens no real
protection. (On legacy chains the estimate uses the hardcoded 2 gwei priority fee
rather than the chain's
gasPrice, so it is approximate but conservative for apre-check.)
2.
src/utils/blockchain.ts—getGasOptions(same bug class, hardened)getGasOptionshad the same latent assumption: on a chain reporting a priority fee buta
nullmaxFeePerGas,feeData.maxFeePerGas * multiplierwould throw the sameTypeError. Instead of defaulting the missing field to0n(which would floormaxFeePerGasto1 wei— below the priority fee, producing an invalid,unsubmittable fee pair), the EIP-1559 branch now requires both fields to be present
and otherwise falls through to the existing
gasPricebranch — the correct legacybehavior. Normal EIP-1559 chains are unaffected. (
getGasOptionsis used by the escrowflow, not by collectFees, so this is defensive hardening of the same bug class.)
3.
src/test/unit/blockchain.test.ts— regression testsTwo new
describeblocks, provider/signer stubbed (no live chain needed), eachrestoring the stub in
afterEach:calculateGasCost gas-fee mathbaseFeePerGasisnull(the reproduced crash)getBlock('latest')returnsnullgetGasOptions fee-field handlinggasPricewhenmaxFeePerGasisnull(partial data)gasPricewhenmaxPriorityFeePerGasisnull(legacy chain)All 6 pass:
Reproduce (before the fix)
chainId 8996+ ocean-node):./start_ocean.sh.collectFeesfor the native token:{ "command": "collectFees", "chainId": 8996, "tokenAddress": "0x0000000000000000000000000000000000000000", "tokenAmount": "0.0001", "destinationAddress": "0x..." }TypeError: Cannot mix BigInt and other typesbefore the fix; returns atx hash after it.
The added unit tests reproduce the same condition without barge.
Review summary
Reviewed by three focused agents (code quality, QA/test coverage, security):
requested value, so a looser gas pre-check can only cause a self-reverting tx reported
as an error (fail-safe). Admin-only command, auth/validation path unchanged, no key
handling touched.
getGasOptionshardening (defaulting fields to
0n) produced an invalidmaxFee < prioritypair andcontained dead code; the fix was revised to fall through to the
gasPricebranch(addressed in this PR). Test stubs are now restored in
afterEach.Out of scope (pre-existing, not addressed here)
tokenAmountgiven): the guard comparesbalanceagainst
balance + gasCost, which is always true → always rejects with "Amount toohigh". Pre-dates this change and fails safe. If native full-sweep is intended to work,
the guard should compare against
balance - gasCostand sendbalance - gasCost.parseUnitsis not wrapped in try/catch (unlike the ERC20 branch), soa malformed
tokenAmountsurfaces as a generic 500 rather than a clean 400. Cosmetic,admin-only input.
Scope / files
src/utils/blockchain.ts—calculateGasCostnull-guard (the fix) +getGasOptionshardening.
src/test/unit/blockchain.test.ts— 6 regression tests.EIP-1559 chains (mainnet / L2s) is unchanged.
Summary by CodeRabbit
gasPricewhen EIP-1559 fee values are incomplete.