unionlabs/union · error · Error
Invalid gas price string
Error message
Invalid gas price string
What it means
Thrown by the patched GasPrice.fromString in @cosmjs/stargate when the input does not match ^([0-9.]+)([a-zA-Z][a-zA-Z0-9/:._-]*)$ — i.e. it is not <decimal amount immediately followed by denom starting with a letter>. The regex rejects separators, signs, and scientific notation before checkDenom ever runs.
Source
Thrown at patches/@cosmjs__stargate.patch:81
- toString() {
- return this.amount.toString() + this.denom;
+ constructor(amount, denom) {
+ this.amount = amount
+ this.denom = denom
+ }
+ /**
+ * Parses a gas price formatted as `<amount><denom>`, e.g. `GasPrice.fromString("0.012utoken")`.
+ *
+ * The denom must match the Cosmos SDK 0.42 pattern (https://github.com/cosmos/cosmos-sdk/blob/v0.42.4/types/coin.go#L599-L601).
+ * See `GasPrice` in @cosmjs/stargate for a more generic matcher.
+ *
+ * Separators are not yet supported.
+ */
+ static fromString(gasPrice) {
+ // Use Decimal.fromUserInput and checkDenom for detailed checks and helpful error messages
+ const matchResult = gasPrice.match(/^([0-9.]+)([a-zA-Z][a-zA-Z0-9/:._-]*)$/)
+ if (!matchResult) {
+ throw new Error("Invalid gas price string")
}
+ const [_, amount, denom] = matchResult
+ checkDenom(denom)
+ const fractionalDigits = 18
+ const decimalAmount = math_1.Decimal.fromUserInput(amount, fractionalDigits)
+ return new GasPrice(decimalAmount, denom)
+ }
+ /**
+ * Returns a string representation of this gas price, e.g. "0.025uatom".
+ * This can be used as an input to `GasPrice.fromString`.
+ */
+ toString() {
+ return this.amount.toString() + this.denom
+ }
}
-exports.GasPrice = GasPrice;
+exports.GasPrice = GasPrice
function calculateFee(gasLimit, gasPrice) {View on GitHub (pinned to 031785bb6d)
Solutions
- Normalize the string: trim whitespace and remove thousands/decimal separators, e.g. gasPrice.trim().replace(",", ".") then strip any space between amount and denom
- Use the plain <amount><denom> form: "0.025uatom"
- Validate against the pattern before passing it into client creation
Example fix
// before
const gasPrice = GasPrice.fromString("0.025 uatom") // throws
// after
const gasPrice = GasPrice.fromString("0.025uatom") // amount and denom joined, no separators Defensive patterns
Strategy: validation
Validate before calling
const GAS_PRICE_PATTERN = /^([0-9.]+)([a-zA-Z][a-zA-Z0-9/:._-]*)$/
const normalizeGasPrice = (input: string) =>
input.trim().replace(/\s+/g, "").replace(/,/g, ".") // "0.025 uatom" -> "0.025uatom"
const normalized = normalizeGasPrice(userInput)
if (!GAS_PRICE_PATTERN.test(normalized)) {
throw new Error(`Gas price must be <amount><denom>, got: ${userInput}`)
}
const gasPrice = GasPrice.fromString(normalized) Type guard
const isGasPriceString = (s: string): s is `${number}${string}` =>
/^[0-9.]+[a-zA-Z][a-zA-Z0-9/:._-]*$/.test(s) Try / catch
try {
const gasPrice = GasPrice.fromString(raw)
} catch (error) {
if ((error as Error).message === "Invalid gas price string") {
// strip whitespace/separators, reject negatives & scientific notation, then retry once
}
throw error
} Prevention
- Normalize user/config input (trim, no separators, no exponent notation) before parsing
- Test gas-price parsing in unit tests for comma/space/negative cases
- Prefer typed gas price objects over string round-trips where the API allows
When it happens
Trigger: Gas price strings like "0.5 uatom" (space), "1,5utoken" (comma separator), "-0.1uatom" (negative), "1e-6uatom" (scientific notation), "uatom" or "0.025" alone, or a denom starting with a digit or slash.
Common situations: User-entered gas prices from forms pasted with whitespace; locale-formatted decimals using commas; config files storing gas price with units separated; negative or exponent notation from calculators.
Related errors
- Denom must be between 2 and 128 characters
- Malformed response
- NOT IMPLEMENTED
- Invalid Sui signer: expected Ed25519Keypair
- Invalid Aptos transport
AI-assisted analysis of unionlabs/union@031785bb6d (2026-08-16).
Data as JSON: /api/errors/392751731dca4753.
Report an issue: GitHub.