shardeum/shardeum · error · Error

Parameter value is not a valid bigint instance

Error message

Parameter value is not a valid bigint instance

What it means

_readableSHM formats a bigint amount of wei into a human-readable SHM string. It requires its first argument to be a native bigint; anything else (number, string, BN) fails immediately. This mirrors the parser guard one function above in the same file.

Source

Thrown at src/utils/serialization.ts:80

export const _base10BNParser = (value: bigint | DecimalString): bigint => {
  if (typeof value == 'string' && value.slice(0, 2) == '0x') {
    throw new Error('Parameter value does not seem to be a valid base 10 (decimal)')
  }
  if (typeof value === 'string' && isNaN(value as unknown as number)) {
    throw new Error('Parameter value does not seem to be a valid base 10 (decimal)')
  }
  if (typeof value === 'bigint') {
    return value
  }
  if (typeof value == 'string') {
    return BigInt(value)
  }
  throw new Error(`_base10BNParser: Unacceptable parameter value ${value}  typeof ${typeof value}`)
}

export const _readableSHM = (bnum: bigint, autoDecimal = true): string => {
  if (typeof bnum !== 'bigint') {
    throw new Error('Parameter value is not a valid bigint instance')
  }

  const unit_SHM = ' shm'
  const unit_WEI = ' wei'

  if (!autoDecimal) return bnum.toString() + unit_WEI

  const numString = bnum.toString()
  // 1 eth or 1 SHM === 10^18 wei
  // if wei value gets too big let's convert to SHM in a floating point precision.
  // 14 is where we set this threshold. hardcoded for now.
  if (numString.length > 14) {
    const floating_index = numString.length - 18

    if (floating_index <= 0) {
      const mantissa = '0'.repeat(Math.abs(floating_index)) + numString
      return '0.' + mantissa + unit_SHM
    }

View on GitHub (pinned to 0c454caf06)

Solutions

  1. Convert the value with _base10BNParser (or BigInt(...)) before calling _readableSHM
  2. Search call sites for _readableSHM and audit each argument's type at runtime
  3. Add a TS type annotation (bnum: bigint) enforcement / noImplicitAny so mismatches are caught at compile time

Example fix

// before
console.log(_readableSHM(account.balance)) // balance is a number/BN

// after
console.log(_readableSHM(BigInt(account.balance)))
Defensive patterns

Strategy: type-guard

Validate before calling

const n = typeof bnum === 'number' ? BigInt(bnum) : bnum
_readableSHM(n as bigint)

Type guard

const isBigInt = (v: unknown): v is bigint => typeof v === 'bigint'

Prevention

When it happens

Trigger: Calling _readableSHM(1000) with a plain number, passing a decimal string like '1.5', or feeding a BN.js object from older EthereumJS-style code into a logging/formatting call.

Common situations: Refactoring legacy BN-based code to bigint but forgetting log/format call sites; tests that pass numbers for convenience.

Understand the failure class

Background: Invalid argument type errors: "must be of type string", "expected X, got Y", and ERR_INVALID_ARG_TYPE explained — this error's family across 15 libraries.

Related errors


AI-assisted analysis of shardeum/shardeum@0c454caf06 (2026-08-28). Data as JSON: /api/errors/0b091f458055e689. Report an issue: GitHub.