@firmachain/firma-js 0.3.2 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -566,7 +566,7 @@ var FirmaStakingService = /** @class */ (function () {
566
566
  maxEntries: result.max_entries,
567
567
  historicalEntries: result.historical_entries,
568
568
  bondDenom: result.bond_denom,
569
- minCommissionRate: FirmaUtil_1.FirmaUtil.processCommissionRate(result.min_commission_rate)
569
+ minCommissionRate: result.min_commission_rate
570
570
  }];
571
571
  case 2:
572
572
  error_22 = _a.sent();
@@ -69,22 +69,14 @@ export declare class FirmaUtil {
69
69
  * @returns Duration object ready to use with protobuf
70
70
  */
71
71
  static createDurationFromString(durationStr: string): Duration;
72
- /**
73
- * Normalizes decimal string for Cosmos SDK usage.
74
- * Converts "0.000000000000000000" to empty string to avoid big.Int conversion errors.
75
- *
76
- * @param decimalStr - Decimal string that might cause big.Int conversion issues
77
- * @returns Normalized string safe for Cosmos SDK usage
78
- */
79
- static normalizeDecimalString(decimalStr: string): string;
80
72
  /**
81
73
  * Safely processes commission rate strings to prevent big.Int conversion errors.
82
- * This is specifically for handling commission rates that might be "0.000000000000000000".
74
+ * Converts decimal commission rates to Cosmos SDK atomics format (integer representation).
83
75
  *
84
76
  * @param commissionRate - Commission rate string from staking params
85
- * @returns Processed commission rate string safe for protobuf usage
77
+ * @returns Processed commission rate string safe for protobuf usage (atomics format or empty string)
86
78
  */
87
- static processCommissionRate(commissionRate: string): string;
79
+ static processCommissionRateAsDecimal(commissionRate: string): string;
88
80
  }
89
81
  export declare const DefaultTxMisc: {
90
82
  memo: string;
@@ -84,6 +84,7 @@ var proto_signing_1 = require("@cosmjs/proto-signing");
84
84
  var signingstargateclient_1 = require("./firmachain/common/signingstargateclient");
85
85
  var any_1 = require("./firmachain/google/protobuf/any");
86
86
  var long_1 = __importDefault(require("long"));
87
+ var bignumber_js_1 = __importDefault(require("bignumber.js"));
87
88
  var CommonTxClient_1 = require("./firmachain/common/CommonTxClient");
88
89
  var CryptoJS = require("crypto-js");
89
90
  var sha1 = require("crypto-js/sha1");
@@ -537,63 +538,33 @@ var FirmaUtil = /** @class */ (function () {
537
538
  nanos: nanos
538
539
  });
539
540
  };
540
- /**
541
- * Normalizes decimal string for Cosmos SDK usage.
542
- * Converts "0.000000000000000000" to empty string to avoid big.Int conversion errors.
543
- *
544
- * @param decimalStr - Decimal string that might cause big.Int conversion issues
545
- * @returns Normalized string safe for Cosmos SDK usage
546
- */
547
- FirmaUtil.normalizeDecimalString = function (decimalStr) {
548
- if (!decimalStr || decimalStr.trim() === "") {
549
- return "";
550
- }
551
- var trimmed = decimalStr.trim();
552
- // Check if it's a valid decimal number
553
- if (!/^-?\d*\.?\d*$/.test(trimmed)) {
554
- return trimmed; // Return as-is if not a valid decimal
555
- }
556
- try {
557
- var num = parseFloat(trimmed);
558
- // If the number is 0 or very close to 0, return empty string
559
- if (num === 0 || Math.abs(num) < 1e-18) {
560
- return "";
561
- }
562
- // For non-zero values, return the original string
563
- return trimmed;
564
- }
565
- catch (error) {
566
- // If parsing fails, return the original string
567
- return trimmed;
568
- }
569
- };
570
541
  /**
571
542
  * Safely processes commission rate strings to prevent big.Int conversion errors.
572
- * This is specifically for handling commission rates that might be "0.000000000000000000".
543
+ * Converts decimal commission rates to Cosmos SDK atomics format (integer representation).
573
544
  *
574
545
  * @param commissionRate - Commission rate string from staking params
575
- * @returns Processed commission rate string safe for protobuf usage
546
+ * @returns Processed commission rate string safe for protobuf usage (atomics format or empty string)
576
547
  */
577
- FirmaUtil.processCommissionRate = function (commissionRate) {
578
- if (!commissionRate || commissionRate.trim() === "") {
579
- return "";
580
- }
581
- var normalized = FirmaUtil.normalizeDecimalString(commissionRate);
582
- // For commission rates, if it's effectively zero, return empty string
583
- if (normalized === "") {
584
- return "";
585
- }
586
- // Ensure the value is within valid commission rate range (0-1)
587
- try {
588
- var rate = parseFloat(normalized);
589
- if (rate < 0 || rate > 1) {
590
- throw new Error("Invalid commission rate: " + commissionRate + ". Must be between 0 and 1");
591
- }
592
- return normalized;
548
+ FirmaUtil.processCommissionRateAsDecimal = function (commissionRate) {
549
+ var trimmed = commissionRate.trim();
550
+ if (!commissionRate || trimmed === "") {
551
+ throw new Error("Invalid commission rate format: " + commissionRate);
593
552
  }
594
- catch (error) {
553
+ if (!/^-?\d+\.?\d*$/.test(trimmed)) {
595
554
  throw new Error("Invalid commission rate format: " + commissionRate);
596
555
  }
556
+ // Validates input and creates BigNumber instance
557
+ var commissionRateBN = new bignumber_js_1.default(trimmed);
558
+ // Checks if it's a valid finite number
559
+ if (!commissionRateBN.isFinite())
560
+ throw new Error("Invalid commission rate format: " + commissionRate);
561
+ // Validates range (0 to 1 inclusive)
562
+ if (commissionRateBN.isNegative() || commissionRateBN.isGreaterThan(1))
563
+ throw new Error("Invalid commission rate range. Must be between 0 and 1 inclusive.");
564
+ // Converts to atomics format (multiply by 10^18)
565
+ var atomics = commissionRateBN.multipliedBy(new bignumber_js_1.default(10).pow(18));
566
+ // Returns integer string
567
+ return atomics.integerValue(bignumber_js_1.default.ROUND_DOWN).toString();
597
568
  };
598
569
  FirmaUtil.FctDecimal = 6;
599
570
  return FirmaUtil;
@@ -142,7 +142,7 @@ var LedgerSigningStargateClient = /** @class */ (function (_super) {
142
142
  });
143
143
  path = "/abci_query";
144
144
  params = {
145
- path: "/store/auth/key",
145
+ path: "/store/acc/key",
146
146
  data: hexAccAddress,
147
147
  };
148
148
  return [4 /*yield*/, axios.get(path, { params: params })];
@@ -50,6 +50,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
50
50
  var chai_1 = require("chai");
51
51
  var common_1 = require("../sdk/firmachain/common");
52
52
  var FirmaSDK_1 = require("../sdk/FirmaSDK");
53
+ var FirmaUtil_1 = require("../sdk/FirmaUtil");
53
54
  var config_test_1 = require("./config_test");
54
55
  // If test it, the properties of the chain change, so skip it.
55
56
  describe('[16. Gov Tx Test]', function () {
@@ -165,6 +166,7 @@ describe('[16. Gov Tx Test]', function () {
165
166
  params = _a.sent();
166
167
  params.maxValidators = 100;
167
168
  params.historicalEntries = 10000;
169
+ params.minCommissionRate = FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal(params.minCommissionRate);
168
170
  metadata = "";
169
171
  return [4 /*yield*/, firma.Gov.submitStakingParamsUpdateProposal(aliceWallet, title, summary, initialDepositFCT, params, metadata)];
170
172
  case 2:
@@ -201,7 +203,7 @@ describe('[16. Gov Tx Test]', function () {
201
203
  switch (_a.label) {
202
204
  case 0:
203
205
  initialDeposit = 5000;
204
- title = "CancelProposal test proposal";
206
+ title = "Software upgrade proposal";
205
207
  summary = "This is a Text & CancelProposal";
206
208
  plan = {
207
209
  name: 'v0.5.1',
@@ -38,12 +38,8 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  var chai_1 = require("chai");
40
40
  var FirmaUtil_1 = require("../sdk/FirmaUtil");
41
- var FirmaSDK_1 = require("../sdk/FirmaSDK");
42
- var config_test_1 = require("./config_test");
43
41
  describe('[18. util Test]', function () {
44
- var firma;
45
42
  beforeEach(function () {
46
- firma = new FirmaSDK_1.FirmaSDK(config_test_1.TestChainConfig);
47
43
  });
48
44
  // getHashFromString
49
45
  it('getSha1HashFromString test', function () { return __awaiter(void 0, void 0, void 0, function () {
@@ -269,56 +265,47 @@ describe('[18. util Test]', function () {
269
265
  return [2 /*return*/];
270
266
  });
271
267
  }); });
272
- it('normalizeDecimalString test - success case', function () { return __awaiter(void 0, void 0, void 0, function () {
268
+ it('processCommissionRateAsDecimal test - success case', function () { return __awaiter(void 0, void 0, void 0, function () {
273
269
  var result;
274
270
  return __generator(this, function (_a) {
275
- result = FirmaUtil_1.FirmaUtil.normalizeDecimalString("0.000000000000000000");
276
- chai_1.expect(result).to.equal("");
271
+ result = FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("0.000000000000000000");
272
+ chai_1.expect(result).to.equal("0");
273
+ result = FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("1");
274
+ chai_1.expect(result).to.equal("1000000000000000000");
275
+ result = FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("0.99999999999999999999999");
276
+ chai_1.expect(result).to.equal("999999999999999999");
277
+ result = FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("0.3719281729181018373290120300000831");
278
+ chai_1.expect(result).to.equal("371928172918101837");
279
+ result = FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("1.000000000000000000");
280
+ chai_1.expect(result).to.equal("1000000000000000000");
281
+ result = FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal(" 0.75 ");
282
+ chai_1.expect(result).to.equal("750000000000000000");
283
+ result = FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal(".23");
284
+ chai_1.expect(result).to.equal("230000000000000000");
285
+ result = FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal(".9999990000000");
286
+ chai_1.expect(result).to.equal("999999000000000000");
277
287
  return [2 /*return*/];
278
288
  });
279
289
  }); });
280
- it('normalizeDecimalString test - failure case', function () { return __awaiter(void 0, void 0, void 0, function () {
281
- var result;
282
- return __generator(this, function (_a) {
283
- result = FirmaUtil_1.FirmaUtil.normalizeDecimalString("0.000000000000000000");
284
- chai_1.expect(result).to.not.equal("0.000000000000000000");
285
- return [2 /*return*/];
286
- });
287
- }); });
288
- it('processCommissionRate test - success case', function () { return __awaiter(void 0, void 0, void 0, function () {
289
- var result;
290
- return __generator(this, function (_a) {
291
- result = FirmaUtil_1.FirmaUtil.processCommissionRate("0.000000000000000000");
292
- chai_1.expect(result).to.equal("");
293
- result = FirmaUtil_1.FirmaUtil.processCommissionRate("0.1");
294
- chai_1.expect(result).to.equal("0.1");
295
- result = FirmaUtil_1.FirmaUtil.processCommissionRate("1");
296
- chai_1.expect(result).to.equal("1");
297
- result = FirmaUtil_1.FirmaUtil.processCommissionRate(" 0.75 ");
298
- chai_1.expect(result).to.equal("0.75");
299
- result = FirmaUtil_1.FirmaUtil.processCommissionRate("");
300
- chai_1.expect(result).to.equal("");
301
- return [2 /*return*/];
302
- });
303
- }); });
304
- it('processCommissionRate - failure cases', function () { return __awaiter(void 0, void 0, void 0, function () {
305
- var testString;
290
+ it('processCommissionRateAsDecimal test - failure cases', function () { return __awaiter(void 0, void 0, void 0, function () {
306
291
  return __generator(this, function (_a) {
307
- testString = "1.01";
308
- try {
309
- FirmaUtil_1.FirmaUtil.processCommissionRate(testString);
310
- }
311
- catch (error) {
312
- chai_1.expect(error.message).to.equal("Invalid commission rate format: 1.01");
313
- }
314
- // Invalid commission rate: -0.1. Must be >= 0
315
- testString = "-0.1";
316
- try {
317
- FirmaUtil_1.FirmaUtil.processCommissionRate(testString);
318
- }
319
- catch (error) {
320
- chai_1.expect(error.message).to.equal("Invalid commission rate format: -0.1");
321
- }
292
+ chai_1.expect(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("."); }).to.throw("Invalid commission rate format: .");
293
+ chai_1.expect(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal(""); }).to.throw("Invalid commission rate format: ");
294
+ chai_1.expect(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal(" "); }).to.throw("Invalid commission rate format: ");
295
+ chai_1.expect(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("null"); }).to.throw("Invalid commission rate format: null");
296
+ chai_1.expect(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("0.1abc"); }).to.throw("Invalid commission rate format: 0.1abc");
297
+ chai_1.expect(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("0.1.2"); }).to.throw("Invalid commission rate format: 0.1.2");
298
+ chai_1.expect(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("--0.5"); }).to.throw("Invalid commission rate format: --0.5");
299
+ chai_1.expect(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("1.01"); }).to.throw("Invalid commission rate range. Must be between 0 and 1 inclusive.");
300
+ chai_1.expect(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("2"); }).to.throw("Invalid commission rate range. Must be between 0 and 1 inclusive.");
301
+ chai_1.expect(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("1.1"); }).to.throw("Invalid commission rate range. Must be between 0 and 1 inclusive.");
302
+ chai_1.expect(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("-0.1"); }).to.throw("Invalid commission rate range. Must be between 0 and 1 inclusive.");
303
+ chai_1.expect(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("-1"); }).to.throw("Invalid commission rate range. Must be between 0 and 1 inclusive.");
304
+ chai_1.expect(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("-0.01"); }).to.throw("Invalid commission rate range. Must be between 0 and 1 inclusive.");
305
+ chai_1.expect(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("-0.000000000000000001"); }).to.throw("Invalid commission rate range. Must be between 0 and 1 inclusive.");
306
+ chai_1.expect(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("1.00000000000000000001"); }).to.throw("Invalid commission rate range. Must be between 0 and 1 inclusive.");
307
+ chai_1.expect(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal("0.5%"); }).to.throw("Invalid commission rate format: 0.5%");
308
+ chai_1.expect(function () { return FirmaUtil_1.FirmaUtil.processCommissionRateAsDecimal(" 0.5 extra"); }).to.throw("Invalid commission rate format: 0.5 extra");
322
309
  return [2 /*return*/];
323
310
  });
324
311
  }); });
@@ -17,9 +17,17 @@ exports.TestChainConfig = {
17
17
  isShowLog: false,
18
18
  };
19
19
  exports.validatorMnemonic = "angry water bunker where iron absurd cruise deliver clutch unique creek pyramid arch express flush pill lens concert absent enemy boring mom nuclear rose";
20
+ // DEVNET
20
21
  exports.aliceMnemonic = "immune flavor record sphere foam planet faint grid disorder flag minute eternal beef sea camp surge extra scorpion pistol plastic happy siren juice found";
22
+ // TESTNET
23
+ // export const aliceMnemonic = "harvest galaxy sniff include record undo width oven tired sad month text museum curious firm mountain flash assault oval sand ribbon blouse consider lens";
24
+ // VALIDATOR
25
+ // export const aliceMnemonic = "clump erupt type lucky mask pig soup runway wrestle suspect element involve stamp civil auction resource blame same journey start unaware crush ten draw";
26
+ // export const aliceMnemonic = "owner pottery smile evolve pig base lady dismiss badge purchase divide royal medal buffalo miss carbon kiwi gate draft mouse yard reunion thank wage";
27
+ // export const aliceMnemonic = "main shallow liberty desk super palm remind throw track legal warrior client garbage type insect first token keen subway pony curtain pitch yellow arrive";
21
28
  // voting test
22
29
  // export const aliceMnemonic = "long shallow crumble clown truth book oval render seed canal buffalo assist sadness elbow afraid catalog brother trade food subject must luggage bread neither";
30
+ // export const aliceMnemonic = "clump erupt type lucky mask pig soup runway wrestle suspect element involve stamp civil auction resource blame same journey start unaware crush ten draw";
23
31
  // export const aliceMnemonic = "angry water bunker where iron absurd cruise deliver clutch unique creek pyramid arch express flush pill lens concert absent enemy boring mom nuclear rose";
24
32
  // export const aliceMnemonic = "stadium lonely midnight okay meat rib awesome wealth phone leisure turn prosper notable label fruit define little also father silver half drill bargain antique";
25
33
  // export const aliceMnemonic = "uncle banana theme relax oak prosper volcano glad industry bicycle tower thrive jelly curious luggage frame that defy reason jewel figure begin nice moon";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@firmachain/firma-js",
3
- "version": "0.3.2",
3
+ "version": "0.3.4",
4
4
  "description": "The Official FirmaChain Javascript SDK written in Typescript",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -41,6 +41,7 @@
41
41
  "@types/pako": "2.0.0",
42
42
  "axios": "0.27.2",
43
43
  "big-number": "2.0.0",
44
+ "bignumber.js": "^9.3.0",
44
45
  "cosmjs-types": "0.9.0",
45
46
  "crypto-js": "4.1.1",
46
47
  "fast-deep-equal": "^3.1.3",
@@ -50,6 +51,7 @@
50
51
  "readline-sync": "1.4.10"
51
52
  },
52
53
  "devDependencies": {
54
+ "@types/bignumber.js": "^4.0.0",
53
55
  "@types/chai": "4.2.21",
54
56
  "@types/mocha": "9.0.0",
55
57
  "@types/node": "16.4.3",