Support for F32 & F64 (#4958)
* Support for F32 & F64 * Additional * Adjust * Expand * Empty creation * Empty creation #2 (no params) * Comment around toJSON() * createdAtHash * Float tests * Comments * typo * Expand class description * Add floats to InterfaceTypes * Jiggle names * CHANGELOG
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { TypeRegistry } from '@polkadot/types';
|
||||
import { F32, F64 } from '@polkadot/types-codec';
|
||||
|
||||
describe('Float', (): void => {
|
||||
const registry = new TypeRegistry();
|
||||
|
||||
describe('F32', (): void => {
|
||||
it('has a sane toRawType()', (): void => {
|
||||
expect(
|
||||
new F32(registry).toRawType()
|
||||
).toEqual('f32');
|
||||
});
|
||||
|
||||
it('constructs from Uint8Array', (): void => {
|
||||
expect(
|
||||
new F32(registry, new Uint8Array([0, 0, 0, 128])).toNumber()
|
||||
).toEqual(-0.0);
|
||||
});
|
||||
|
||||
it('triggers isEmpty on 0', (): void => {
|
||||
expect(
|
||||
new F32(registry, 0).isEmpty
|
||||
).toEqual(true);
|
||||
});
|
||||
|
||||
it('constructs from a float value', (): void => {
|
||||
expect(
|
||||
new F32(registry, 123.456).toString()
|
||||
).toEqual('123.456');
|
||||
});
|
||||
});
|
||||
|
||||
describe('F64', (): void => {
|
||||
it('has a sane toRawType()', (): void => {
|
||||
expect(
|
||||
new F64(registry).toRawType()
|
||||
).toEqual('f64');
|
||||
});
|
||||
|
||||
it('constructs from hex', (): void => {
|
||||
expect(
|
||||
new F64(registry, '0x0000000000000080').toNumber()
|
||||
).toEqual(-0.0);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,128 @@
|
||||
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { AnyFloat, CodecClass, IFloat, Inspect, IU8a, Registry } from '../types';
|
||||
|
||||
import { floatToU8a, isHex, isU8a, u8aToFloat, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
|
||||
interface Options {
|
||||
bitLength?: 32 | 64;
|
||||
}
|
||||
|
||||
/**
|
||||
* @name Float
|
||||
* @description
|
||||
* A Codec wrapper for F32 & F64 values. You generally don't want to be using
|
||||
* f32/f64 in your runtime, operations on fixed points numbers are preferable. This class
|
||||
* was explicitly added since scale-codec has a flag that enables this and it is available
|
||||
* in some eth_* RPCs
|
||||
*/
|
||||
export class Float extends Number implements IFloat {
|
||||
readonly #bitLength: 32 | 64;
|
||||
|
||||
public createdAtHash?: IU8a;
|
||||
|
||||
readonly encodedLength: number;
|
||||
|
||||
readonly initialU8aLength?: number;
|
||||
|
||||
readonly registry: Registry;
|
||||
|
||||
constructor (registry: Registry, value?: AnyFloat, { bitLength = 32 }: Options = {}) {
|
||||
super(
|
||||
isU8a(value) || isHex(value)
|
||||
? value.length === 0
|
||||
? 0
|
||||
: u8aToFloat(u8aToU8a(value), { bitLength })
|
||||
: (value || 0)
|
||||
);
|
||||
|
||||
this.#bitLength = bitLength;
|
||||
this.encodedLength = bitLength / 8;
|
||||
this.initialU8aLength = this.encodedLength;
|
||||
this.registry = registry;
|
||||
}
|
||||
|
||||
public static with (bitLength: 32 | 64): CodecClass<Float> {
|
||||
return class extends Float {
|
||||
constructor (registry: Registry, value?: AnyFloat) {
|
||||
super(registry, value, { bitLength });
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
public get hash (): IU8a {
|
||||
return this.registry.hash(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns true if the type wraps an empty/default all-0 value
|
||||
*/
|
||||
get isEmpty (): boolean {
|
||||
return this.valueOf() === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Compares the value of the input to see if there is a match
|
||||
*/
|
||||
public eq (other?: unknown): boolean {
|
||||
return this.valueOf() === Number(other);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a breakdown of the hex encoding for this Codec
|
||||
*/
|
||||
public inspect (): Inspect {
|
||||
return {
|
||||
outer: [this.toU8a()]
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
public toHex (): HexString {
|
||||
return u8aToHex(this.toU8a());
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
||||
*/
|
||||
public toHuman (): string {
|
||||
return this.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Converts the Object to JSON, typically used for RPC transfers
|
||||
*/
|
||||
public toJSON (): string {
|
||||
// Not sure if this is actually a hex or a string value
|
||||
// (would need to check against RPCs to see the result here)
|
||||
return this.toHex();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the number representation (Same as valueOf)
|
||||
*/
|
||||
public toNumber (): number {
|
||||
return this.valueOf();
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns the base runtime type name for this instance
|
||||
*/
|
||||
public toRawType (): string {
|
||||
return `f${this.#bitLength}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
||||
*/
|
||||
public toU8a (): Uint8Array {
|
||||
return floatToU8a(this, { bitLength: this.#bitLength });
|
||||
}
|
||||
}
|
||||
@@ -64,6 +64,9 @@ export class Json extends Map<string, any> implements Codec {
|
||||
return compareMap(this, other);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a typed value from the internal map
|
||||
*/
|
||||
public getT <T> (key: string): T {
|
||||
return this.get(key) as unknown as T;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
export { bool, bool as Bool } from './Bool';
|
||||
export { CodecDate, CodecDate as Date } from './Date';
|
||||
export { Float } from './Float';
|
||||
export { Json } from './Json';
|
||||
export { Raw } from './Raw';
|
||||
export { CodecSet, CodecSet as Set } from './Set';
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { Float } from '../native/Float';
|
||||
|
||||
/**
|
||||
* @name f32
|
||||
* @description
|
||||
* A 32-bit float
|
||||
*/
|
||||
export class f32 extends Float.with(32) {
|
||||
// NOTE without this, we cannot properly determine extensions
|
||||
public readonly __FloatType = 'f32';
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import { Float } from '../native/Float';
|
||||
|
||||
/**
|
||||
* @name f64
|
||||
* @description
|
||||
* A 64-bit float
|
||||
*/
|
||||
export class f64 extends Float.with(64) {
|
||||
// NOTE without this, we cannot properly determine extensions
|
||||
public readonly __FloatType = 'f64';
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
export { f32, f32 as F32 } from './F32';
|
||||
export { f64, f64 as F64 } from './F64';
|
||||
export { i8, i8 as I8 } from './I8';
|
||||
export { i16, i16 as I16 } from './I16';
|
||||
export { i32, i32 as I32 } from './I32';
|
||||
|
||||
@@ -10,6 +10,9 @@ export type AnyFunction = (...args: any[]) => any;
|
||||
|
||||
export type AnyNumber = BN | bigint | Uint8Array | number | string;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
export type AnyFloat = Number | number | Uint8Array | string;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||
export type AnyString = String | string;
|
||||
|
||||
|
||||
@@ -33,6 +33,10 @@ export interface INumber extends Codec {
|
||||
toNumber (): number;
|
||||
}
|
||||
|
||||
export interface IFloat extends Codec {
|
||||
toNumber (): number;
|
||||
}
|
||||
|
||||
export interface IOption<T extends Codec = Codec> extends Codec {
|
||||
readonly isNone: boolean;
|
||||
readonly isSome: boolean;
|
||||
|
||||
Reference in New Issue
Block a user