Dec-clutter UInt & Int (#1969)

This commit is contained in:
Jaco Greeff
2020-02-27 18:08:41 +01:00
committed by GitHub
parent a66b98fece
commit edff4231ff
3 changed files with 30 additions and 88 deletions
+30 -6
View File
@@ -6,7 +6,7 @@ import { H256 } from '../interfaces/runtime';
import { AnyNumber, Codec, Registry } from '../types';
import BN from 'bn.js';
import { assert, bnToBn, formatNumber, hexToBn, isHex, isString, isU8a, u8aToBn } from '@polkadot/util';
import { assert, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isHex, isString, isU8a, u8aToBn } from '@polkadot/util';
import { blake2AsU8a } from '@polkadot/util-crypto';
import Raw from './Raw';
@@ -140,14 +140,24 @@ export default abstract class AbstractInt extends BN implements Codec {
/**
* @description Returns a hex string representation of the value
*/
abstract toHex (): string;
public toHex (isLe = false): string {
// For display/JSON, this is BE, for compare, use isLe
return bnToHex(this, {
bitLength: this.bitLength(),
isLe,
isNegative: !this.isUnsigned
});
}
/**
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public toHuman (isExpanded?: boolean): string {
return formatNumber(this);
public toHuman (isExpanded?: boolean): any {
// FIXME we need proper expansion here
return this instanceof this.registry.createClass('Balance')
? formatBalance(this, { decimals: this.registry.chainDecimals, withSi: true, withUnit: this.registry.chainToken })
: formatNumber(this);
}
/**
@@ -165,7 +175,14 @@ export default abstract class AbstractInt extends BN implements Codec {
/**
* @description Returns the base runtime type name for this instance
*/
abstract toRawType (): string;
public toRawType (): string {
// NOTE In the case of balances, which have a special meaning on the UI
// and can be interpreted differently, return a specific value for it so
// underlying it always matches (no matter which length it actually is)
return this instanceof this.registry.createClass('Balance')
? 'Balance'
: `${this.isUnsigned ? 'u' : 'i'}${this.bitLength()}`;
}
/**
* @description Returns the string representation of the value
@@ -180,5 +197,12 @@ export default abstract class AbstractInt extends BN implements Codec {
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
abstract toU8a (isBare?: boolean): Uint8Array;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public toU8a (isBare?: boolean): Uint8Array {
return bnToU8a(this, {
bitLength: this.bitLength(),
isLe: true,
isNegative: !this.isUnsigned
});
}
}
-33
View File
@@ -4,8 +4,6 @@
import { AnyNumber, Constructor, Registry } from '../types';
import { bnToHex, bnToU8a } from '@polkadot/util';
import AbstractInt, { DEFAULT_UINT_BITS, UIntBitLength } from './AbstractInt';
/**
@@ -34,35 +32,4 @@ export default class Int extends AbstractInt {
}
};
}
/**
* @description Returns a hex string representation of the value
*/
public toHex (isLe = false): string {
return bnToHex(this, {
bitLength: this.bitLength(),
isLe,
isNegative: true
});
}
/**
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
return `i${this.bitLength()}`;
}
/**
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public toU8a (isBare?: boolean): Uint8Array {
return bnToU8a(this, {
bitLength: this.bitLength(),
isLe: true,
isNegative: true
});
}
}
-49
View File
@@ -4,8 +4,6 @@
import { AnyNumber, Constructor, Registry } from '../types';
import { bnToHex, bnToU8a, formatBalance } from '@polkadot/util';
import AbstractInt, { DEFAULT_UINT_BITS, UIntBitLength } from './AbstractInt';
/**
@@ -34,51 +32,4 @@ export default class UInt extends AbstractInt {
}
};
}
/**
* @description Returns a hex string representation of the value
*/
public toHex (isLe = false): string {
// For display/JSON, this is BE, for compare, use isLe
return bnToHex(this, {
bitLength: this.bitLength(),
isLe,
isNegative: false
});
}
/**
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
public toHuman (isExpanded?: boolean): any {
// FIXME we need proper expansion here
return this instanceof this.registry.createClass('Balance')
? formatBalance(this, { decimals: this.registry.chainDecimals, withSi: true, withUnit: this.registry.chainToken })
: super.toHuman(isExpanded);
}
/**
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
// NOTE In the case of balances, which have a special meaning on the UI
// and can be interpreted differently, return a specific value for it so
// underlying it always matches (no matter which length it actually is)
return this instanceof this.registry.createClass('Balance')
? 'Balance'
: `u${this.bitLength()}`;
}
/**
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public toU8a (isBare?: boolean): Uint8Array {
return bnToU8a(this, {
bitLength: this.bitLength(),
isLe: true,
isNegative: false
});
}
}