Add cosmjs-util package with custom gas parsing
This commit is contained in:
parent
9a23d3508d
commit
ae18f60378
41
.gitignore
vendored
Normal file
41
.gitignore
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
npm-debug.log*
|
||||
yarn-debug.log*
|
||||
yarn-error.log*
|
||||
lerna-debug.log*
|
||||
|
||||
# Dependency directories
|
||||
node_modules/
|
||||
|
||||
# TypeScript build output
|
||||
dist/
|
||||
|
||||
# Editor directories and files
|
||||
.vscode/
|
||||
.idea/
|
||||
*.sublime-project
|
||||
*.sublime-workspace
|
||||
|
||||
# Environment variables
|
||||
.env
|
||||
.env.local
|
||||
.env.development.local
|
||||
.env.test.local
|
||||
.env.production.local
|
||||
|
||||
# Crash reports
|
||||
*.crash
|
||||
|
||||
# Yarn
|
||||
.yarn/
|
||||
.pnp.*
|
||||
|
||||
# npm
|
||||
.npm/
|
||||
|
||||
# misc
|
||||
.nyc_output/
|
||||
.cache/
|
||||
.eslintcache
|
21
eslint.config.js
Normal file
21
eslint.config.js
Normal file
@ -0,0 +1,21 @@
|
||||
const eslint = require("@eslint/js");
|
||||
const tseslint = require("typescript-eslint");
|
||||
|
||||
module.exports = tseslint.config(
|
||||
eslint.configs.recommended,
|
||||
...tseslint.configs.recommended,
|
||||
{
|
||||
files: ["src/**/*.ts"],
|
||||
languageOptions: {
|
||||
parser: tseslint.parser,
|
||||
parserOptions: {
|
||||
ecmaVersion: 2020,
|
||||
sourceType: "module",
|
||||
project: "./tsconfig.json",
|
||||
},
|
||||
},
|
||||
rules: {
|
||||
// Add any specific rules here
|
||||
},
|
||||
}
|
||||
);
|
25
package.json
Normal file
25
package.json
Normal file
@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "@LaconicNetwork/cosmjs-util",
|
||||
"version": "1.0.0",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"lint": "eslint src/**/*.ts"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"description": "",
|
||||
"devDependencies": {
|
||||
"@typescript-eslint/eslint-plugin": "^8.34.1",
|
||||
"@typescript-eslint/parser": "^8.34.1",
|
||||
"eslint": "^9.29.0",
|
||||
"typescript": "^5.8.3",
|
||||
"typescript-eslint": "^8.34.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@cosmjs/stargate": "^0.33.1",
|
||||
"@cosmjs/math": "^0.33.1"
|
||||
}
|
||||
}
|
1
src/index.ts
Normal file
1
src/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export * from './utils';
|
35
src/utils.ts
Normal file
35
src/utils.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import { GasPrice } from '@cosmjs/stargate';
|
||||
import { Decimal } from '@cosmjs/math';
|
||||
|
||||
// Ref: https://github.com/cosmos/cosmjs/blob/v0.33.1/packages/stargate/src/fee.ts#L38
|
||||
/**
|
||||
* 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);
|
||||
}
|
15
tsconfig.json
Normal file
15
tsconfig.json
Normal file
@ -0,0 +1,15 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2016",
|
||||
"module": "commonjs",
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"declaration": true
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
Loading…
Reference in New Issue
Block a user