Default to hex-only when number bits > native Rust (#5611)

* Default to hex-only when number bits > native Rust

* CHANGELOG

* Add test for > u128 case
This commit is contained in:
Jaco
2023-04-23 15:53:32 +03:00
committed by GitHub
parent 3b3da09f33
commit 6b17bf5a7a
3 changed files with 12 additions and 4 deletions
+7
View File
@@ -1,5 +1,12 @@
# CHANGELOG
## master
Changes:
- Default to hex-only in `Int/Uint.toJSON` for > 128-bit values
## 10.4.1 Apr 22, 2023
Contributed:
+3 -3
View File
@@ -221,9 +221,9 @@ export abstract class AbstractInt extends BN implements INumber {
public override toJSON (onlyHex = false): any {
// FIXME this return type should by string | number, however BN returns string
// Options here are
// - super.bitLength() - the actual used bits
// - this.#bitLength - the type bits (this should be used, however contracts RPC is problematic)
return onlyHex || (super.bitLength() > MAX_NUMBER_BITS)
// - super.bitLength() - the actual used bits, use hex when close to MAX_SAFE_INTEGER
// - this.#bitLength - the max used bits, use hex when larger than native Rust type
return onlyHex || (this.#bitLength > 128) || (super.bitLength() > MAX_NUMBER_BITS)
? this.toHex()
: this.toNumber();
}
+2 -1
View File
@@ -123,8 +123,9 @@ describe('UInt', (): void => {
it('converts to JSON representation based on size', (): void => {
expect(new UInt(registry, '0x12345678', 32).toJSON()).toEqual(0x12345678);
expect(new UInt(registry, '0x1234567890', 64).toJSON()).toEqual(78187493520); // '0x0000001234567890');
expect(new UInt(registry, '0x1234567890', 64).toJSON()).toEqual(78187493520);
expect(new UInt(registry, '0x1234567890abcdef', 64).toJSON()).toEqual('0x1234567890abcdef');
expect(new UInt(registry, 1, 256).toJSON()).toEqual('0x0000000000000000000000000000000000000000000000000000000000000001');
});
it('has a sane inspect', (): void => {