diff --git a/CHANGELOG.md b/CHANGELOG.md index ad82a96850..349da4044b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Inject and use SignedExtensions by name from V11 metadata (V4 extrinsics only) - Add typings for `api.tx.*.*` - the `polkadot-types-from-chain` now also generates these - Remove additional V1-only derives `(staking.recentlyOffline`, `balances.nominatorsFor`) +- Add `.isMax()` on int values, an opposite of `isZero()` (e.g. `u128`, as well as derived, e.g. `Balances`) # 1.0.1 diff --git a/packages/api-derive/src/balances/all.ts b/packages/api-derive/src/balances/all.ts index 9466f3a2bf..7afa764a47 100644 --- a/packages/api-derive/src/balances/all.ts +++ b/packages/api-derive/src/balances/all.ts @@ -25,9 +25,11 @@ function calcBalances (api: ApiInterfaceRx, [accountId, bestNumber, [freeBalance // only get the locks that are valid until passed the current block lockedBreakdown = locks.filter(({ until }): boolean => bestNumber && until.gt(bestNumber)); + const notAll = lockedBreakdown.filter(({ amount }): boolean => !amount.isMax()); + // get the maximum of the locks according to https://github.com/paritytech/substrate/blob/master/srml/balances/src/lib.rs#L699 - if (lockedBreakdown.length) { - lockedBalance = createType(api.registry, 'Balance', bnMax(...lockedBreakdown.map(({ amount }): Balance => amount))); + if (notAll.length) { + lockedBalance = createType(api.registry, 'Balance', bnMax(...notAll.map(({ amount }): Balance => amount))); } } diff --git a/packages/types/src/codec/AbstractInt.ts b/packages/types/src/codec/AbstractInt.ts index 93c47f6bb3..ffd420df65 100644 --- a/packages/types/src/codec/AbstractInt.ts +++ b/packages/types/src/codec/AbstractInt.ts @@ -111,6 +111,15 @@ export default abstract class AbstractInt extends BN implements Codec { ); } + /** + * @description True if this value is the max of the type + */ + public isMax (): boolean { + const u8a = this.toU8a().filter((byte): boolean => byte === 0xff); + + return u8a.length === (this._bitLength / 8); + } + /** * @description Returns the BN representation of the number. (Compatibility) */ diff --git a/packages/types/src/codec/UInt.spec.ts b/packages/types/src/codec/UInt.spec.ts index 60b6614283..37f13640c6 100644 --- a/packages/types/src/codec/UInt.spec.ts +++ b/packages/types/src/codec/UInt.spec.ts @@ -70,4 +70,17 @@ describe('UInt', (): void => { expect(test.eq('0x3039')).toBe(true); }); }); + + describe('isMax()', (): void => { + it('is false where not full', (): void => { + expect(new UInt(registry, '0x1234', 32).isMax()).toEqual(false); + expect(new UInt(registry, '0xffffff', 32).isMax()).toEqual(false); + expect(new UInt(registry, '0x12345678', 32).isMax()).toEqual(false); + expect(new UInt(registry, '0xfffffff0', 32).isMax()).toEqual(false); + }); + + it('is true when full', (): void => { + expect(new UInt(registry, '0xffffffff', 32).isMax()).toEqual(true); + }); + }); });