Use cosmjs-util package

This commit is contained in:
Prathamesh Musale 2025-06-17 18:49:12 +05:30
parent 3d14a8a7ae
commit 7a493ee4e9
3 changed files with 2 additions and 35 deletions

View File

@ -23,6 +23,7 @@
"@cosmjs/encoding": "^0.33.1",
"@cosmjs/proto-signing": "^0.33.1",
"@cosmjs/stargate": "^0.33.1",
"@LaconicNetwork/cosmjs-util": "^0.1.0",
"@keyv/sqlite": "^3.6.7",
"cors": "^2.8.5",
"express": "^4.19.2",

View File

@ -11,7 +11,7 @@ import { Comet38Client } from '@cosmjs/tendermint-rpc';
import { fromBech32 } from '@cosmjs/encoding';
import KeyvSqlite from '@keyv/sqlite';
import { parseCustomGasPrice } from './utils';
import { parseCustomGasPrice } from '@LaconicNetwork/cosmjs-util';
const CONFIG_PATH = 'environments/local.toml';
const FAUCET_DATA_FILE = 'faucet_data.sqlite';

View File

@ -1,34 +0,0 @@
import { GasPrice } from '@cosmjs/stargate';
import { Decimal } from '@cosmjs/math';
/**
* Parses a gas price string with a more flexible denom regex.
* This function is a custom implementation to allow for denoms that can start with
* numbers or other valid characters, while still supporting an optional dollar sign prefix.
*
* @param gasPrice The gas price string, e.g., '0.012utoken' or '0.001$mydenom'.
* @returns A GasPrice object.
* @throws If the gas price string is invalid.
*/
export function parseCustomGasPrice (gasPrice: string): GasPrice {
// This regex allows the denom to start with an optional '$' followed by any
// alphanumeric character or ':', '.', '_', '-'. It must have at least one such character.
const matchResult = gasPrice.match(/^([0-9.]+)(\$?[a-zA-Z][a-zA-Z0-9/:._-]*)$/);
if (!matchResult) {
throw new Error('Invalid gas price string');
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_, amount, denom] = matchResult;
// Basic denom length check (similar to checkDenom in @cosmjs/stargate)
if (denom.length < 2 || denom.length > 128) {
throw new Error('Denom must be between 2 and 128 characters');
}
const fractionalDigits = 18; // Standard for Cosmos SDK decimals
const decimalAmount = Decimal.fromUserInput(amount, fractionalDigits);
return new GasPrice(decimalAmount, denom);
}