Add cosmjs-util package with custom gas parsing #1

Merged
nabarun merged 3 commits from pm-add-util into main 2025-06-17 15:40:25 +00:00
10 changed files with 1693 additions and 0 deletions

27
.gitea/workflows/lint.yml Normal file
View File

@ -0,0 +1,27 @@
name: Lint & Build
on:
pull_request:
push:
branches: [ main ]
jobs:
lint_and_build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]
steps:
- uses: actions/checkout@v3
- name: Download yarn
run: |
curl -fsSL -o /usr/local/bin/yarn https://github.com/yarnpkg/yarn/releases/download/v1.22.21/yarn-1.22.21.js
chmod +x /usr/local/bin/yarn
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
registry-url: 'https://npm.pkg.github.com'
- run: yarn
- name: Run lint
run: yarn lint
- name: Run build
run: yarn build

View File

@ -0,0 +1,36 @@
name: Publish npm package to gitea
on:
release:
types: [published]
jobs:
npm_publish:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ 20.x ]
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Download yarn
run: |
curl -fsSL -o /usr/local/bin/yarn https://github.com/yarnpkg/yarn/releases/download/v1.22.21/yarn-1.22.21.js
chmod +x /usr/local/bin/yarn
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: yarn
- name: Run yarn build
run: |
yarn build
- name: Configure git.vdb.to npm registry
run: |
npm config set registry https://git.vdb.to/api/packages/LaconicNetwork/npm/
- name: Authenticate to git.vdb.to registry
run: |
npm config set -- '//git.vdb.to/api/packages/LaconicNetwork/npm/:_authToken' "${{ secrets.CICD_PUBLISH_TOKEN }}"
- name: npm publish
run: |
npm publish

41
.gitignore vendored Normal file
View 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

4
.husky/pre-commit Executable file
View File

@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"
yarn lint

21
eslint.config.js Normal file
View 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
},
}
);

31
package.json Normal file
View File

@ -0,0 +1,31 @@
{
"name": "@LaconicNetwork/cosmjs-util",
"version": "0.1.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"lint": "eslint src/**/*.ts",
"prepare": "husky"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^8.34.1",
"@typescript-eslint/parser": "^8.34.1",
"eslint": "^9.29.0",
"husky": "^9.1.7",
"lint-staged": "^16.1.2",
"typescript": "^5.8.3",
"typescript-eslint": "^8.34.1"
},
"dependencies": {
"@cosmjs/math": "^0.33.1",
"@cosmjs/stargate": "^0.33.1"
},
"lint-staged": {
"*.ts": "yarn lint"
}
}

1
src/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './utils';

35
src/utils.ts Normal file
View 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
View 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"]
}

1482
yarn.lock Normal file

File diff suppressed because it is too large Load Diff