diff --git a/CHANGELOG.md b/CHANGELOG.md index 3adf116019..16252026ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # 1.16.0-beta.x +- Adjust `Perbill/Permill` `toHuman()` formatting to output percentage - Support for final Polkadot types & signed extensions - support for latest Substrate types - Update to latest TypeScript, eslint & typescript-eslint diff --git a/packages/types/src/codec/AbstractInt.ts b/packages/types/src/codec/AbstractInt.ts index 739b0a2feb..97d0b8c7cb 100644 --- a/packages/types/src/codec/AbstractInt.ts +++ b/packages/types/src/codec/AbstractInt.ts @@ -15,6 +15,14 @@ export type UIntBitLength = 8 | 16 | 32 | 64 | 128 | 256; export const DEFAULT_UINT_BITS = 64; +const PER_B = new BN(1_000_000_000); +const PER_M = new BN(1_000_000); +const MUL_P = new BN(1_00_00); + +function toPercentage (value: BN, divisor: BN): string { + return `${(value.mul(MUL_P).div(divisor).toNumber() / 100).toFixed(2)}%`; +} + /** @internal */ function decodeAbstracIntU8a (value: Uint8Array, bitLength: UIntBitLength, isNegative: boolean): string { if (!value.length) { @@ -159,7 +167,11 @@ export default abstract class AbstractInt extends BN implements Codec { ? this.isMax() ? 'everything' : formatBalance(this, { decimals: this.registry.chainDecimals, withSi: true, withUnit: this.registry.chainToken }) - : formatNumber(this); + : this instanceof this.registry.createClass('Perbill') + ? toPercentage(this, PER_B) + : this instanceof this.registry.createClass('Permill') + ? toPercentage(this, PER_M) + : formatNumber(this); } /** diff --git a/packages/types/src/codec/Linkage.spec.ts b/packages/types/src/codec/Linkage.spec.ts index 4e9661d0ff..cdbf534c71 100644 --- a/packages/types/src/codec/Linkage.spec.ts +++ b/packages/types/src/codec/Linkage.spec.ts @@ -10,7 +10,7 @@ const registry = new TypeRegistry(); describe('Linkage', (): void => { it('decodes with ValidatorPrefs', (): void => { const LINKA = { next: '5GznmRvdi5htUJKnMSWJgJUzSJJXSvWuHRSEdyUbHJZDNcwU', previous: null }; - const PREFS = { commission: '100,000,000' }; + const PREFS = { commission: '10.00%' }; // prefs sanity check expect(