Perbill formatting with rawType equality (#2646)
* Perbill formatting with rawType equality * Balance toHuman() check * Formatter lookup * typing * Merge * Better Compact detection
This commit is contained in:
@@ -137,6 +137,7 @@ declare module '@polkadot/types/types/registry' {
|
||||
'Option<FixedI64>': Option<FixedI64>;
|
||||
'Vec<FixedI64>': Vec<FixedI64>;
|
||||
FixedU64: FixedU64;
|
||||
'Compact<FixedU64>': Compact<FixedU64>;
|
||||
'Option<FixedU64>': Option<FixedU64>;
|
||||
'Vec<FixedU64>': Vec<FixedU64>;
|
||||
Fixed128: Fixed128;
|
||||
@@ -146,12 +147,14 @@ declare module '@polkadot/types/types/registry' {
|
||||
'Option<FixedI128>': Option<FixedI128>;
|
||||
'Vec<FixedI128>': Vec<FixedI128>;
|
||||
FixedU128: FixedU128;
|
||||
'Compact<FixedU128>': Compact<FixedU128>;
|
||||
'Option<FixedU128>': Option<FixedU128>;
|
||||
'Vec<FixedU128>': Vec<FixedU128>;
|
||||
I32F32: I32F32;
|
||||
'Option<I32F32>': Option<I32F32>;
|
||||
'Vec<I32F32>': Vec<I32F32>;
|
||||
U32F32: U32F32;
|
||||
'Compact<U32F32>': Compact<U32F32>;
|
||||
'Option<U32F32>': Option<U32F32>;
|
||||
'Vec<U32F32>': Vec<U32F32>;
|
||||
PerU16: PerU16;
|
||||
@@ -181,6 +184,7 @@ declare module '@polkadot/types/types/registry' {
|
||||
'Option<AccountIdOf>': Option<AccountIdOf>;
|
||||
'Vec<AccountIdOf>': Vec<AccountIdOf>;
|
||||
AccountIndex: AccountIndex;
|
||||
'Compact<AccountIndex>': Compact<AccountIndex>;
|
||||
'Option<AccountIndex>': Option<AccountIndex>;
|
||||
'Vec<AccountIndex>': Vec<AccountIndex>;
|
||||
Address: Address;
|
||||
@@ -195,6 +199,7 @@ declare module '@polkadot/types/types/registry' {
|
||||
'Option<Balance>': Option<Balance>;
|
||||
'Vec<Balance>': Vec<Balance>;
|
||||
BalanceOf: BalanceOf;
|
||||
'Compact<BalanceOf>': Compact<BalanceOf>;
|
||||
'Option<BalanceOf>': Option<BalanceOf>;
|
||||
'Vec<BalanceOf>': Vec<BalanceOf>;
|
||||
Block: Block;
|
||||
@@ -217,6 +222,7 @@ declare module '@polkadot/types/types/registry' {
|
||||
'Option<ChangesTrieConfiguration>': Option<ChangesTrieConfiguration>;
|
||||
'Vec<ChangesTrieConfiguration>': Vec<ChangesTrieConfiguration>;
|
||||
ConsensusEngineId: ConsensusEngineId;
|
||||
'Compact<ConsensusEngineId>': Compact<ConsensusEngineId>;
|
||||
'Option<ConsensusEngineId>': Option<ConsensusEngineId>;
|
||||
'Vec<ConsensusEngineId>': Vec<ConsensusEngineId>;
|
||||
Digest: Digest;
|
||||
@@ -855,6 +861,7 @@ declare module '@polkadot/types/types/registry' {
|
||||
'Option<SchedulePeriod>': Option<SchedulePeriod>;
|
||||
'Vec<SchedulePeriod>': Vec<SchedulePeriod>;
|
||||
SchedulePriority: SchedulePriority;
|
||||
'Compact<SchedulePriority>': Compact<SchedulePriority>;
|
||||
'Option<SchedulePriority>': Option<SchedulePriority>;
|
||||
'Vec<SchedulePriority>': Vec<SchedulePriority>;
|
||||
Scheduled: Scheduled;
|
||||
@@ -980,6 +987,7 @@ declare module '@polkadot/types/types/registry' {
|
||||
'Option<KeyType>': Option<KeyType>;
|
||||
'Vec<KeyType>': Vec<KeyType>;
|
||||
MomentOf: MomentOf;
|
||||
'Compact<MomentOf>': Compact<MomentOf>;
|
||||
'Option<MomentOf>': Option<MomentOf>;
|
||||
'Vec<MomentOf>': Vec<MomentOf>;
|
||||
Nominations: Nominations;
|
||||
@@ -993,6 +1001,7 @@ declare module '@polkadot/types/types/registry' {
|
||||
'Option<NominatorIndexCompact>': Option<NominatorIndexCompact>;
|
||||
'Vec<NominatorIndexCompact>': Vec<NominatorIndexCompact>;
|
||||
OffchainAccuracy: OffchainAccuracy;
|
||||
'Compact<OffchainAccuracy>': Compact<OffchainAccuracy>;
|
||||
'Option<OffchainAccuracy>': Option<OffchainAccuracy>;
|
||||
'Vec<OffchainAccuracy>': Vec<OffchainAccuracy>;
|
||||
OffchainAccuracyCompact: OffchainAccuracyCompact;
|
||||
@@ -1301,9 +1310,11 @@ declare module '@polkadot/types/types/registry' {
|
||||
'Option<IncomingParachainDeploy>': Option<IncomingParachainDeploy>;
|
||||
'Vec<IncomingParachainDeploy>': Vec<IncomingParachainDeploy>;
|
||||
LeasePeriod: LeasePeriod;
|
||||
'Compact<LeasePeriod>': Compact<LeasePeriod>;
|
||||
'Option<LeasePeriod>': Option<LeasePeriod>;
|
||||
'Vec<LeasePeriod>': Vec<LeasePeriod>;
|
||||
LeasePeriodOf: LeasePeriodOf;
|
||||
'Compact<LeasePeriodOf>': Compact<LeasePeriodOf>;
|
||||
'Option<LeasePeriodOf>': Option<LeasePeriodOf>;
|
||||
'Vec<LeasePeriodOf>': Vec<LeasePeriodOf>;
|
||||
LocalValidationData: LocalValidationData;
|
||||
@@ -1329,6 +1340,7 @@ declare module '@polkadot/types/types/registry' {
|
||||
'Option<ParaScheduling>': Option<ParaScheduling>;
|
||||
'Vec<ParaScheduling>': Vec<ParaScheduling>;
|
||||
RelayChainBlockNumber: RelayChainBlockNumber;
|
||||
'Compact<RelayChainBlockNumber>': Compact<RelayChainBlockNumber>;
|
||||
'Option<RelayChainBlockNumber>': Option<RelayChainBlockNumber>;
|
||||
'Vec<RelayChainBlockNumber>': Vec<RelayChainBlockNumber>;
|
||||
Remark: Remark;
|
||||
|
||||
@@ -13,10 +13,15 @@ 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);
|
||||
|
||||
const FORMATTERS: [string, BN][] = [
|
||||
['Perquintill', new BN(1_000_000_000_000)],
|
||||
['Perbill', new BN(1_000_000_000)],
|
||||
['Permill', new BN(1_000_000)],
|
||||
['Percent', new BN(100)]
|
||||
];
|
||||
|
||||
function toPercentage (value: BN, divisor: BN): string {
|
||||
return `${(value.mul(MUL_P).div(divisor).toNumber() / 100).toFixed(2)}%`;
|
||||
}
|
||||
@@ -168,16 +173,19 @@ export default abstract class AbstractInt extends BN implements Codec {
|
||||
*/
|
||||
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
||||
public toHuman (isExpanded?: boolean): string {
|
||||
// FIXME we need proper expansion here
|
||||
return this instanceof this.registry.createClass('Balance')
|
||||
? this.isMax()
|
||||
const rawType = this.toRawType();
|
||||
|
||||
if (rawType === 'Balance') {
|
||||
return this.isMax()
|
||||
? 'everything'
|
||||
: formatBalance(this, { decimals: this.registry.chainDecimals, withSi: true, withUnit: this.registry.chainToken })
|
||||
: this instanceof this.registry.createClass('Perbill')
|
||||
? toPercentage(this, PER_B)
|
||||
: this instanceof this.registry.createClass('Permill')
|
||||
? toPercentage(this, PER_M)
|
||||
: formatNumber(this);
|
||||
: formatBalance(this, { decimals: this.registry.chainDecimals, withSi: true, withUnit: this.registry.chainToken });
|
||||
}
|
||||
|
||||
const [, divisor] = FORMATTERS.find(([type]) => type === rawType) || [];
|
||||
|
||||
return divisor
|
||||
? toPercentage(this, divisor)
|
||||
: formatNumber(this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -119,5 +119,12 @@ describe('UInt', (): void => {
|
||||
new (UInt.with(64, 'SomethingElse'))(registry).toRawType()
|
||||
).toEqual('SomethingElse');
|
||||
});
|
||||
|
||||
it('has proper toHuman() for PerMill/PerBill/Percent/Balance', (): void => {
|
||||
expect(registry.createType('Perbill', 12_340_000).toHuman()).toEqual('1.23%');
|
||||
expect(registry.createType('Percent', 12).toHuman()).toEqual('12.00%');
|
||||
expect(registry.createType('Permill', 16_900).toHuman()).toEqual('1.69%');
|
||||
expect(registry.createType('Balance', '123456789012345').toHuman()).toEqual('123.456 Unit');
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,11 +15,11 @@ const numberTypes = {
|
||||
FixedU128: 'UInt<128, FixedU128>',
|
||||
I32F32: 'Int<64, I32F32>',
|
||||
U32F32: 'UInt<64, U32F32>',
|
||||
PerU16: 'u16',
|
||||
Perbill: 'u32',
|
||||
Percent: 'u8',
|
||||
Permill: 'u32',
|
||||
Perquintill: 'u64'
|
||||
PerU16: 'UInt<16, PerU16>',
|
||||
Perbill: 'UInt<32, Perbill>',
|
||||
Percent: 'UInt<8, Percent>',
|
||||
Permill: 'UInt<32, Permill>',
|
||||
Perquintill: 'UInt<64, Perquintill>'
|
||||
};
|
||||
|
||||
export default {
|
||||
@@ -31,7 +31,7 @@ export default {
|
||||
AccountIndex: 'GenericAccountIndex',
|
||||
Address: 'GenericAddress',
|
||||
AssetId: 'u32',
|
||||
Balance: 'u128',
|
||||
Balance: 'UInt<128, Balance>',
|
||||
BalanceOf: 'Balance',
|
||||
Block: 'GenericBlock',
|
||||
BlockNumber: 'u32',
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
import { ITuple } from '@polkadot/types/types';
|
||||
import { Compact, Enum, Int, Struct, U8aFixed, UInt, Vec } from '@polkadot/types/codec';
|
||||
import { GenericAccountId, GenericAccountIndex, GenericBlock, GenericCall, GenericConsensusEngineId, GenericLookupSource } from '@polkadot/types/generic';
|
||||
import { Bytes, DoNotConstruct, Null, StorageKey, u128, u16, u32, u64, u8 } from '@polkadot/types/primitive';
|
||||
import { Bytes, DoNotConstruct, Null, StorageKey, u32, u64 } from '@polkadot/types/primitive';
|
||||
import { AuthorityId } from '@polkadot/types/interfaces/consensus';
|
||||
import { Signature } from '@polkadot/types/interfaces/extrinsics';
|
||||
|
||||
@@ -24,7 +24,7 @@ export interface Address extends GenericAddress {}
|
||||
export interface AssetId extends u32 {}
|
||||
|
||||
/** @name Balance */
|
||||
export interface Balance extends u128 {}
|
||||
export interface Balance extends UInt {}
|
||||
|
||||
/** @name BalanceOf */
|
||||
export interface BalanceOf extends Balance {}
|
||||
@@ -170,19 +170,19 @@ export interface Pays extends Enum {
|
||||
}
|
||||
|
||||
/** @name Perbill */
|
||||
export interface Perbill extends u32 {}
|
||||
export interface Perbill extends UInt {}
|
||||
|
||||
/** @name Percent */
|
||||
export interface Percent extends u8 {}
|
||||
export interface Percent extends UInt {}
|
||||
|
||||
/** @name Permill */
|
||||
export interface Permill extends u32 {}
|
||||
export interface Permill extends UInt {}
|
||||
|
||||
/** @name Perquintill */
|
||||
export interface Perquintill extends u64 {}
|
||||
export interface Perquintill extends UInt {}
|
||||
|
||||
/** @name PerU16 */
|
||||
export interface PerU16 extends u16 {}
|
||||
export interface PerU16 extends UInt {}
|
||||
|
||||
/** @name Phantom */
|
||||
export interface Phantom extends Null {}
|
||||
|
||||
Reference in New Issue
Block a user