From 3d14a8a7ae83c96b04832058e82438f4d0ec72c8 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Tue, 17 Jun 2025 11:57:27 +0530 Subject: [PATCH] Update token denom to $Z --- environments/local.toml | 2 +- src/index.ts | 6 ++++-- src/utils.ts | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 src/utils.ts diff --git a/environments/local.toml b/environments/local.toml index b127cb0..6c6c40e 100644 --- a/environments/local.toml +++ b/environments/local.toml @@ -1,6 +1,6 @@ [upstream] rpcEndpoint = "http://localhost:26657" - denom = "znt" + denom = "$Z" prefix = "zenith" gasPrice = "1" faucetKey = "" diff --git a/src/index.ts b/src/index.ts index 5814e27..31f596d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,11 +6,13 @@ import toml from 'toml'; import cors from 'cors'; import { DirectSecp256k1Wallet } from '@cosmjs/proto-signing'; -import { GasPrice, SigningStargateClient } from '@cosmjs/stargate'; +import { SigningStargateClient } from '@cosmjs/stargate'; import { Comet38Client } from '@cosmjs/tendermint-rpc'; import { fromBech32 } from '@cosmjs/encoding'; import KeyvSqlite from '@keyv/sqlite'; +import { parseCustomGasPrice } from './utils'; + const CONFIG_PATH = 'environments/local.toml'; const FAUCET_DATA_FILE = 'faucet_data.sqlite'; const FAUCET_DATA_TTL = 86400000; // 24 hrs @@ -123,7 +125,7 @@ async function sendTokens (config: Config, recipientAddress: string, amount: str const client = await SigningStargateClient.createWithSigner( cometClient, wallet, - { gasPrice: GasPrice.fromString(`${config.upstream.gasPrice}${config.upstream.denom}`) } + { gasPrice: parseCustomGasPrice(`${config.upstream.gasPrice}${config.upstream.denom}`) } ); const result = await client.sendTokens( diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000..fddcf95 --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,34 @@ +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); +}