From 08bf4b7eb2ebe332d0c2f030b7b4181027b8c66c Mon Sep 17 00:00:00 2001 From: Jaco Date: Fri, 13 Jan 2023 15:54:40 +0200 Subject: [PATCH] Bugfix for formatBalance (forceUnit), result < 0 (#1706) * Bugfix for formatBalance (forceUnit), result < 0 * Deps up --- CHANGELOG.md | 7 ++ package.json | 2 +- packages/hw-ledger-transports/package.json | 4 +- packages/hw-ledger/package.json | 6 +- packages/keyring/package.json | 10 +-- packages/networks/package.json | 6 +- packages/util-crypto/package.json | 12 +-- packages/util/package.json | 12 +-- .../util/src/format/formatBalance.spec.ts | 7 ++ packages/util/src/format/formatBalance.ts | 5 +- packages/x-bigint/package.json | 4 +- packages/x-bundle/package.json | 4 +- packages/x-fetch/package.json | 4 +- packages/x-global/package.json | 2 +- packages/x-randomvalues/package.json | 6 +- packages/x-textdecoder/package.json | 4 +- packages/x-textencoder/package.json | 4 +- packages/x-ws/package.json | 4 +- yarn.lock | 82 +++++++++---------- 19 files changed, 101 insertions(+), 84 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4deff7c8ea..7ec674e506 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # CHANGELOG +## 10.2.5 Jan 13, 2023 + +Changes: + +- Bugfix for `formatBalance` (forceUnit) with `BN` inputs where output is less than 0 + + ## 10.2.4 Jan 13, 2023 Changes: diff --git a/package.json b/package.json index 478c2d9838..48c1bfce9c 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ }, "sideEffects": false, "type": "module", - "version": "10.2.4", + "version": "10.2.5", "versions": { "git": "10.2.4", "npm": "10.2.4" diff --git a/packages/hw-ledger-transports/package.json b/packages/hw-ledger-transports/package.json index b8d6de4424..e04384910e 100644 --- a/packages/hw-ledger-transports/package.json +++ b/packages/hw-ledger-transports/package.json @@ -16,7 +16,7 @@ }, "sideEffects": false, "type": "module", - "version": "10.2.4", + "version": "10.2.5", "browser": "browser.js", "main": "node.js", "react-native": "react-native.js", @@ -25,7 +25,7 @@ "@ledgerhq/hw-transport": "^6.27.9", "@ledgerhq/hw-transport-webhid": "^6.27.9", "@ledgerhq/hw-transport-webusb": "^6.27.9", - "@polkadot/util": "10.2.4" + "@polkadot/util": "10.2.5" }, "optionalDependencies": { "@ledgerhq/hw-transport-node-hid-singleton": "^6.28.6" diff --git a/packages/hw-ledger/package.json b/packages/hw-ledger/package.json index 649a98678d..099a487dfd 100644 --- a/packages/hw-ledger/package.json +++ b/packages/hw-ledger/package.json @@ -19,13 +19,13 @@ "./detectPackage.cjs" ], "type": "module", - "version": "10.2.4", + "version": "10.2.5", "main": "index.js", "dependencies": { "@babel/runtime": "^7.20.7", "@ledgerhq/hw-transport": "^6.27.9", - "@polkadot/hw-ledger-transports": "10.2.4", - "@polkadot/util": "10.2.4", + "@polkadot/hw-ledger-transports": "10.2.5", + "@polkadot/util": "10.2.5", "@zondax/ledger-substrate": "^0.40.3" } } diff --git a/packages/keyring/package.json b/packages/keyring/package.json index 3419503c0e..6ab6d8411d 100644 --- a/packages/keyring/package.json +++ b/packages/keyring/package.json @@ -20,15 +20,15 @@ "./detectPackage.cjs" ], "type": "module", - "version": "10.2.4", + "version": "10.2.5", "main": "index.js", "dependencies": { "@babel/runtime": "^7.20.7", - "@polkadot/util": "10.2.4", - "@polkadot/util-crypto": "10.2.4" + "@polkadot/util": "10.2.5", + "@polkadot/util-crypto": "10.2.5" }, "peerDependencies": { - "@polkadot/util": "10.2.4", - "@polkadot/util-crypto": "10.2.4" + "@polkadot/util": "10.2.5", + "@polkadot/util-crypto": "10.2.5" } } diff --git a/packages/networks/package.json b/packages/networks/package.json index 4b6564fa81..5464a7ffa9 100644 --- a/packages/networks/package.json +++ b/packages/networks/package.json @@ -17,14 +17,14 @@ }, "sideEffects": false, "type": "module", - "version": "10.2.4", + "version": "10.2.5", "main": "index.js", "dependencies": { "@babel/runtime": "^7.20.7", - "@polkadot/util": "10.2.4", + "@polkadot/util": "10.2.5", "@substrate/ss58-registry": "^1.37.0" }, "devDependencies": { - "@polkadot/hw-ledger": "10.2.4" + "@polkadot/hw-ledger": "10.2.5" } } diff --git a/packages/util-crypto/package.json b/packages/util-crypto/package.json index 2a569fd83c..3f62e07d83 100644 --- a/packages/util-crypto/package.json +++ b/packages/util-crypto/package.json @@ -22,7 +22,7 @@ "./detectPackage.cjs" ], "type": "module", - "version": "10.2.4", + "version": "10.2.5", "browser": { "crypto": false, "stream": false @@ -32,11 +32,11 @@ "@babel/runtime": "^7.20.7", "@noble/hashes": "1.1.5", "@noble/secp256k1": "1.7.1", - "@polkadot/networks": "10.2.4", - "@polkadot/util": "10.2.4", + "@polkadot/networks": "10.2.5", + "@polkadot/util": "10.2.5", "@polkadot/wasm-crypto": "^6.4.1", - "@polkadot/x-bigint": "10.2.4", - "@polkadot/x-randomvalues": "10.2.4", + "@polkadot/x-bigint": "10.2.5", + "@polkadot/x-randomvalues": "10.2.5", "@scure/base": "1.1.1", "ed2curve": "^0.3.0", "tweetnacl": "^1.0.3" @@ -45,6 +45,6 @@ "@types/ed2curve": "^0.2.2" }, "peerDependencies": { - "@polkadot/util": "10.2.4" + "@polkadot/util": "10.2.5" } } diff --git a/packages/util/package.json b/packages/util/package.json index 124f30a962..a6f8edc044 100644 --- a/packages/util/package.json +++ b/packages/util/package.json @@ -34,18 +34,18 @@ "./detectPackage.cjs" ], "type": "module", - "version": "10.2.4", + "version": "10.2.5", "main": "index.js", "dependencies": { "@babel/runtime": "^7.20.7", - "@polkadot/x-bigint": "10.2.4", - "@polkadot/x-global": "10.2.4", - "@polkadot/x-textdecoder": "10.2.4", - "@polkadot/x-textencoder": "10.2.4", + "@polkadot/x-bigint": "10.2.5", + "@polkadot/x-global": "10.2.5", + "@polkadot/x-textdecoder": "10.2.5", + "@polkadot/x-textencoder": "10.2.5", "@types/bn.js": "^5.1.1", "bn.js": "^5.2.1" }, "devDependencies": { - "@polkadot/x-randomvalues": "10.2.4" + "@polkadot/x-randomvalues": "10.2.5" } } diff --git a/packages/util/src/format/formatBalance.spec.ts b/packages/util/src/format/formatBalance.spec.ts index fc35d3171a..951b7fec34 100644 --- a/packages/util/src/format/formatBalance.spec.ts +++ b/packages/util/src/format/formatBalance.spec.ts @@ -13,6 +13,13 @@ describe('formatBalance', (): void => { expect(formatBalance('0')).toEqual('0'); }); + // this is after an issue/test from actual values with forceUnit + it('formats 1000 (BN) (decimals = 12, withAll, withZero = false)', (): void => { + expect( + formatBalance(new BN(1000), { decimals: 12, forceUnit: '-', withAll: true, withSi: false, withZero: true }) + ).toEqual('0.000000001000'); + }); + it('formats 123,456,789,000 (decimals=15)', (): void => { expect( formatBalance(TESTVAL, { decimals: 15, withSi: true }) diff --git a/packages/util/src/format/formatBalance.ts b/packages/util/src/format/formatBalance.ts index dd3c8e4dbb..b7c14778b1 100644 --- a/packages/util/src/format/formatBalance.ts +++ b/packages/util/src/format/formatBalance.ts @@ -93,11 +93,14 @@ function _formatBalance (input?: number | string | BN | b // get the post from the midpoint onward and then first add max decimals // before trimming to the correct (calculated) amount of decimals again let post = text - .padStart((mid < 0 ? 0 - mid : 0) + 1, '0') + .padStart(mid < 0 ? decimals : 1, '0') .substring(mid < 0 ? 0 : mid) .padEnd(withAll ? Math.max(decimals, 4) : 4, '0') .substring(0, withAll ? Math.max(4, decimals + si.power) : 4); + console.error(mid, pre, post, text + .padStart((mid < 0 ? 0 - mid : 0) + 1, '0')); + // remove all trailing 0's (if required via flag) if (!withZero) { let end = post.length - 1; diff --git a/packages/x-bigint/package.json b/packages/x-bigint/package.json index 5fa9b0f07b..ca7f9f44c3 100644 --- a/packages/x-bigint/package.json +++ b/packages/x-bigint/package.json @@ -20,9 +20,9 @@ "./shim.cjs" ], "type": "module", - "version": "10.2.4", + "version": "10.2.5", "dependencies": { "@babel/runtime": "^7.20.7", - "@polkadot/x-global": "10.2.4" + "@polkadot/x-global": "10.2.5" } } diff --git a/packages/x-bundle/package.json b/packages/x-bundle/package.json index 4c3566072f..086621b534 100644 --- a/packages/x-bundle/package.json +++ b/packages/x-bundle/package.json @@ -17,11 +17,11 @@ }, "sideEffects": false, "type": "module", - "version": "10.2.4", + "version": "10.2.5", "main": "index.js", "dependencies": { "@babel/runtime": "^7.20.7", - "@polkadot/util": "10.2.4", + "@polkadot/util": "10.2.5", "buffer-es6": "^4.9.3" } } diff --git a/packages/x-fetch/package.json b/packages/x-fetch/package.json index 283c04a0c5..c88f350c09 100644 --- a/packages/x-fetch/package.json +++ b/packages/x-fetch/package.json @@ -17,13 +17,13 @@ }, "sideEffects": false, "type": "module", - "version": "10.2.4", + "version": "10.2.5", "browser": "browser.js", "main": "node.js", "react-native": "react-native.js", "dependencies": { "@babel/runtime": "^7.20.7", - "@polkadot/x-global": "10.2.4", + "@polkadot/x-global": "10.2.5", "@types/node-fetch": "^2.6.2", "node-fetch": "^3.3.0" } diff --git a/packages/x-global/package.json b/packages/x-global/package.json index 2242129222..ff447d74d5 100644 --- a/packages/x-global/package.json +++ b/packages/x-global/package.json @@ -17,7 +17,7 @@ }, "sideEffects": false, "type": "module", - "version": "10.2.4", + "version": "10.2.5", "main": "index.js", "dependencies": { "@babel/runtime": "^7.20.7" diff --git a/packages/x-randomvalues/package.json b/packages/x-randomvalues/package.json index f28d701716..224f7e2b3c 100644 --- a/packages/x-randomvalues/package.json +++ b/packages/x-randomvalues/package.json @@ -17,16 +17,16 @@ }, "sideEffects": false, "type": "module", - "version": "10.2.4", + "version": "10.2.5", "browser": "browser.js", "main": "node.js", "react-native": "react-native.js", "dependencies": { "@babel/runtime": "^7.20.7", - "@polkadot/x-global": "10.2.4" + "@polkadot/x-global": "10.2.5" }, "devDependencies": { - "@types/react-native": "^0.70.8" + "@types/react-native": "^0.71.0" }, "denoDependencies": { "crypto": "std@0.161.0/node/crypto.ts" diff --git a/packages/x-textdecoder/package.json b/packages/x-textdecoder/package.json index 0fa3d3a11c..72fef6d543 100644 --- a/packages/x-textdecoder/package.json +++ b/packages/x-textdecoder/package.json @@ -17,13 +17,13 @@ }, "sideEffects": false, "type": "module", - "version": "10.2.4", + "version": "10.2.5", "browser": "browser.js", "main": "node.js", "react-native": "react-native.js", "dependencies": { "@babel/runtime": "^7.20.7", - "@polkadot/x-global": "10.2.4" + "@polkadot/x-global": "10.2.5" }, "denoDependencies": { "util": "std@0.161.0/node/util.ts" diff --git a/packages/x-textencoder/package.json b/packages/x-textencoder/package.json index 0babc2965a..6f671da63c 100644 --- a/packages/x-textencoder/package.json +++ b/packages/x-textencoder/package.json @@ -17,13 +17,13 @@ }, "sideEffects": false, "type": "module", - "version": "10.2.4", + "version": "10.2.5", "browser": "browser.js", "main": "node.js", "react-native": "react-native.js", "dependencies": { "@babel/runtime": "^7.20.7", - "@polkadot/x-global": "10.2.4" + "@polkadot/x-global": "10.2.5" }, "denoDependencies": { "util": "std@0.161.0/node/util.ts" diff --git a/packages/x-ws/package.json b/packages/x-ws/package.json index 7949f317c8..e1cb8bebbe 100644 --- a/packages/x-ws/package.json +++ b/packages/x-ws/package.json @@ -17,13 +17,13 @@ }, "sideEffects": false, "type": "module", - "version": "10.2.4", + "version": "10.2.5", "browser": "browser.js", "main": "node.js", "react-native": "react-native.js", "dependencies": { "@babel/runtime": "^7.20.7", - "@polkadot/x-global": "10.2.4", + "@polkadot/x-global": "10.2.5", "@types/websocket": "^1.0.5", "websocket": "^1.0.34" } diff --git a/yarn.lock b/yarn.lock index 344968e8cc..be30eb96f6 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2205,7 +2205,7 @@ __metadata: languageName: node linkType: hard -"@polkadot/hw-ledger-transports@10.2.4, @polkadot/hw-ledger-transports@workspace:packages/hw-ledger-transports": +"@polkadot/hw-ledger-transports@10.2.5, @polkadot/hw-ledger-transports@workspace:packages/hw-ledger-transports": version: 0.0.0-use.local resolution: "@polkadot/hw-ledger-transports@workspace:packages/hw-ledger-transports" dependencies: @@ -2214,21 +2214,21 @@ __metadata: "@ledgerhq/hw-transport-node-hid-singleton": ^6.28.6 "@ledgerhq/hw-transport-webhid": ^6.27.9 "@ledgerhq/hw-transport-webusb": ^6.27.9 - "@polkadot/util": 10.2.4 + "@polkadot/util": 10.2.5 dependenciesMeta: "@ledgerhq/hw-transport-node-hid-singleton": optional: true languageName: unknown linkType: soft -"@polkadot/hw-ledger@10.2.4, @polkadot/hw-ledger@workspace:packages/hw-ledger": +"@polkadot/hw-ledger@10.2.5, @polkadot/hw-ledger@workspace:packages/hw-ledger": version: 0.0.0-use.local resolution: "@polkadot/hw-ledger@workspace:packages/hw-ledger" dependencies: "@babel/runtime": ^7.20.7 "@ledgerhq/hw-transport": ^6.27.9 - "@polkadot/hw-ledger-transports": 10.2.4 - "@polkadot/util": 10.2.4 + "@polkadot/hw-ledger-transports": 10.2.5 + "@polkadot/util": 10.2.5 "@zondax/ledger-substrate": ^0.40.3 languageName: unknown linkType: soft @@ -2238,56 +2238,56 @@ __metadata: resolution: "@polkadot/keyring@workspace:packages/keyring" dependencies: "@babel/runtime": ^7.20.7 - "@polkadot/util": 10.2.4 - "@polkadot/util-crypto": 10.2.4 + "@polkadot/util": 10.2.5 + "@polkadot/util-crypto": 10.2.5 peerDependencies: - "@polkadot/util": 10.2.4 - "@polkadot/util-crypto": 10.2.4 + "@polkadot/util": 10.2.5 + "@polkadot/util-crypto": 10.2.5 languageName: unknown linkType: soft -"@polkadot/networks@10.2.4, @polkadot/networks@workspace:packages/networks": +"@polkadot/networks@10.2.5, @polkadot/networks@workspace:packages/networks": version: 0.0.0-use.local resolution: "@polkadot/networks@workspace:packages/networks" dependencies: "@babel/runtime": ^7.20.7 - "@polkadot/hw-ledger": 10.2.4 - "@polkadot/util": 10.2.4 + "@polkadot/hw-ledger": 10.2.5 + "@polkadot/util": 10.2.5 "@substrate/ss58-registry": ^1.37.0 languageName: unknown linkType: soft -"@polkadot/util-crypto@10.2.4, @polkadot/util-crypto@workspace:packages/util-crypto": +"@polkadot/util-crypto@10.2.5, @polkadot/util-crypto@workspace:packages/util-crypto": version: 0.0.0-use.local resolution: "@polkadot/util-crypto@workspace:packages/util-crypto" dependencies: "@babel/runtime": ^7.20.7 "@noble/hashes": 1.1.5 "@noble/secp256k1": 1.7.1 - "@polkadot/networks": 10.2.4 - "@polkadot/util": 10.2.4 + "@polkadot/networks": 10.2.5 + "@polkadot/util": 10.2.5 "@polkadot/wasm-crypto": ^6.4.1 - "@polkadot/x-bigint": 10.2.4 - "@polkadot/x-randomvalues": 10.2.4 + "@polkadot/x-bigint": 10.2.5 + "@polkadot/x-randomvalues": 10.2.5 "@scure/base": 1.1.1 "@types/ed2curve": ^0.2.2 ed2curve: ^0.3.0 tweetnacl: ^1.0.3 peerDependencies: - "@polkadot/util": 10.2.4 + "@polkadot/util": 10.2.5 languageName: unknown linkType: soft -"@polkadot/util@10.2.4, @polkadot/util@workspace:packages/util": +"@polkadot/util@10.2.5, @polkadot/util@workspace:packages/util": version: 0.0.0-use.local resolution: "@polkadot/util@workspace:packages/util" dependencies: "@babel/runtime": ^7.20.7 - "@polkadot/x-bigint": 10.2.4 - "@polkadot/x-global": 10.2.4 - "@polkadot/x-randomvalues": 10.2.4 - "@polkadot/x-textdecoder": 10.2.4 - "@polkadot/x-textencoder": 10.2.4 + "@polkadot/x-bigint": 10.2.5 + "@polkadot/x-global": 10.2.5 + "@polkadot/x-randomvalues": 10.2.5 + "@polkadot/x-textdecoder": 10.2.5 + "@polkadot/x-textencoder": 10.2.5 "@types/bn.js": ^5.1.1 bn.js: ^5.2.1 languageName: unknown @@ -2371,12 +2371,12 @@ __metadata: languageName: node linkType: hard -"@polkadot/x-bigint@10.2.4, @polkadot/x-bigint@workspace:packages/x-bigint": +"@polkadot/x-bigint@10.2.5, @polkadot/x-bigint@workspace:packages/x-bigint": version: 0.0.0-use.local resolution: "@polkadot/x-bigint@workspace:packages/x-bigint" dependencies: "@babel/runtime": ^7.20.7 - "@polkadot/x-global": 10.2.4 + "@polkadot/x-global": 10.2.5 languageName: unknown linkType: soft @@ -2385,7 +2385,7 @@ __metadata: resolution: "@polkadot/x-bundle@workspace:packages/x-bundle" dependencies: "@babel/runtime": ^7.20.7 - "@polkadot/util": 10.2.4 + "@polkadot/util": 10.2.5 buffer-es6: ^4.9.3 languageName: unknown linkType: soft @@ -2395,13 +2395,13 @@ __metadata: resolution: "@polkadot/x-fetch@workspace:packages/x-fetch" dependencies: "@babel/runtime": ^7.20.7 - "@polkadot/x-global": 10.2.4 + "@polkadot/x-global": 10.2.5 "@types/node-fetch": ^2.6.2 node-fetch: ^3.3.0 languageName: unknown linkType: soft -"@polkadot/x-global@10.2.4, @polkadot/x-global@workspace:packages/x-global": +"@polkadot/x-global@10.2.5, @polkadot/x-global@workspace:packages/x-global": version: 0.0.0-use.local resolution: "@polkadot/x-global@workspace:packages/x-global" dependencies: @@ -2409,31 +2409,31 @@ __metadata: languageName: unknown linkType: soft -"@polkadot/x-randomvalues@10.2.4, @polkadot/x-randomvalues@workspace:packages/x-randomvalues": +"@polkadot/x-randomvalues@10.2.5, @polkadot/x-randomvalues@workspace:packages/x-randomvalues": version: 0.0.0-use.local resolution: "@polkadot/x-randomvalues@workspace:packages/x-randomvalues" dependencies: "@babel/runtime": ^7.20.7 - "@polkadot/x-global": 10.2.4 - "@types/react-native": ^0.70.8 + "@polkadot/x-global": 10.2.5 + "@types/react-native": ^0.71.0 languageName: unknown linkType: soft -"@polkadot/x-textdecoder@10.2.4, @polkadot/x-textdecoder@workspace:packages/x-textdecoder": +"@polkadot/x-textdecoder@10.2.5, @polkadot/x-textdecoder@workspace:packages/x-textdecoder": version: 0.0.0-use.local resolution: "@polkadot/x-textdecoder@workspace:packages/x-textdecoder" dependencies: "@babel/runtime": ^7.20.7 - "@polkadot/x-global": 10.2.4 + "@polkadot/x-global": 10.2.5 languageName: unknown linkType: soft -"@polkadot/x-textencoder@10.2.4, @polkadot/x-textencoder@workspace:packages/x-textencoder": +"@polkadot/x-textencoder@10.2.5, @polkadot/x-textencoder@workspace:packages/x-textencoder": version: 0.0.0-use.local resolution: "@polkadot/x-textencoder@workspace:packages/x-textencoder" dependencies: "@babel/runtime": ^7.20.7 - "@polkadot/x-global": 10.2.4 + "@polkadot/x-global": 10.2.5 languageName: unknown linkType: soft @@ -2442,7 +2442,7 @@ __metadata: resolution: "@polkadot/x-ws@workspace:packages/x-ws" dependencies: "@babel/runtime": ^7.20.7 - "@polkadot/x-global": 10.2.4 + "@polkadot/x-global": 10.2.5 "@types/websocket": ^1.0.5 websocket: ^1.0.34 languageName: unknown @@ -2939,12 +2939,12 @@ __metadata: languageName: node linkType: hard -"@types/react-native@npm:^0.70.8": - version: 0.70.8 - resolution: "@types/react-native@npm:0.70.8" +"@types/react-native@npm:^0.71.0": + version: 0.71.0 + resolution: "@types/react-native@npm:0.71.0" dependencies: "@types/react": "*" - checksum: e951992178f0a6a2a5bc5312824ad67146594840c03ec51156022eacb23e8db7be7470d0c9ad61828dbae840e531bf0cf553205b9dc290b358091329746fd094 + checksum: 58332a3ab0bd239fa4211fd39d5143c8a6ae9c9b6217ebea1a8821eccfcc3b3805d9b69dd18eaf4917e9f656b9b59e2517591501b512ee7bd8cb12efa111a255 languageName: node linkType: hard