Skip to content

Commit

Permalink
Bugfix for formatBalance (forceUnit), result < 0 (#1706)
Browse files Browse the repository at this point in the history
* Bugfix for formatBalance (forceUnit), result < 0

* Deps up
  • Loading branch information
jacogr authored Jan 13, 2023
1 parent 42daac5 commit 08bf4b7
Show file tree
Hide file tree
Showing 19 changed files with 101 additions and 84 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions packages/hw-ledger-transports/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions packages/hw-ledger/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
10 changes: 5 additions & 5 deletions packages/keyring/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
6 changes: 3 additions & 3 deletions packages/networks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
12 changes: 6 additions & 6 deletions packages/util-crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"./detectPackage.cjs"
],
"type": "module",
"version": "10.2.4",
"version": "10.2.5",
"browser": {
"crypto": false,
"stream": false
Expand All @@ -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"
Expand All @@ -45,6 +45,6 @@
"@types/ed2curve": "^0.2.2"
},
"peerDependencies": {
"@polkadot/util": "10.2.4"
"@polkadot/util": "10.2.5"
}
}
12 changes: 6 additions & 6 deletions packages/util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
7 changes: 7 additions & 0 deletions packages/util/src/format/formatBalance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down
5 changes: 4 additions & 1 deletion packages/util/src/format/formatBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,14 @@ function _formatBalance <ExtToBn extends ToBn> (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;
Expand Down
4 changes: 2 additions & 2 deletions packages/x-bigint/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
4 changes: 2 additions & 2 deletions packages/x-bundle/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
4 changes: 2 additions & 2 deletions packages/x-fetch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
2 changes: 1 addition & 1 deletion packages/x-global/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions packages/x-randomvalues/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "[email protected]/node/crypto.ts"
Expand Down
4 changes: 2 additions & 2 deletions packages/x-textdecoder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "[email protected]/node/util.ts"
Expand Down
4 changes: 2 additions & 2 deletions packages/x-textencoder/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "[email protected]/node/util.ts"
Expand Down
4 changes: 2 additions & 2 deletions packages/x-ws/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down
Loading

0 comments on commit 08bf4b7

Please sign in to comment.