Add UInt .isMax() (#1791)

* Add UInt .isMax()

* Filter lockedBlance by max
This commit is contained in:
Jaco Greeff
2020-02-01 21:20:48 +01:00
committed by GitHub
parent 438c830366
commit 7221a3637c
4 changed files with 27 additions and 2 deletions
+1
View File
@@ -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
+4 -2
View File
@@ -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)));
}
}
+9
View File
@@ -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)
*/
+13
View File
@@ -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);
});
});
});