|
@@ -1,6 +1,6 @@
|
|
-import { Struct, Option, Text, bool, Vec, u16, u32, u64, Null, U8aFixed, BTreeSet, Compact } from '@polkadot/types'
|
|
|
|
-import { BlockNumber, H256, Moment } from '@polkadot/types/interfaces'
|
|
|
|
-import { Codec, Constructor, InterfaceTypes, Registry, RegistryTypes } from '@polkadot/types/types'
|
|
|
|
|
|
+import { Struct, Option, Text, bool, u16, u32, u64, Null, U8aFixed, BTreeSet, Compact, UInt } from '@polkadot/types'
|
|
|
|
+import { BlockNumber, Hash as PolkadotHash, Moment } from '@polkadot/types/interfaces'
|
|
|
|
+import { Codec, Constructor, RegistryTypes } from '@polkadot/types/types'
|
|
import { u8aConcat, u8aToHex } from '@polkadot/util'
|
|
import { u8aConcat, u8aToHex } from '@polkadot/util'
|
|
// we get 'moment' because it is a dependency of @polkadot/util, via @polkadot/keyring
|
|
// we get 'moment' because it is a dependency of @polkadot/util, via @polkadot/keyring
|
|
import moment from 'moment'
|
|
import moment from 'moment'
|
|
@@ -9,46 +9,41 @@ import { JoyEnum } from './JoyEnum'
|
|
|
|
|
|
export { JoyEnum, JoyStructCustom, JoyStructDecorated }
|
|
export { JoyEnum, JoyStructCustom, JoyStructDecorated }
|
|
|
|
|
|
-// Adds ".sort()" during BTreeSet toU8a encoding (required by the runtime)
|
|
|
|
|
|
+// Adds sorting during BTreeSet toU8a encoding (required by the runtime)
|
|
|
|
+// Currently only supports values that extend UInt
|
|
// FIXME: Will not cover cases where BTreeSet is part of extrinsic args metadata
|
|
// FIXME: Will not cover cases where BTreeSet is part of extrinsic args metadata
|
|
-export class JoyBTreeSet<V extends Codec> extends BTreeSet<V> {
|
|
|
|
- public static with<V extends Codec>(valType: Constructor<V> | keyof InterfaceTypes): Constructor<JoyBTreeSet<V>> {
|
|
|
|
- return class extends JoyBTreeSet<V> {
|
|
|
|
- constructor(registry: Registry, value?: Uint8Array | string | Set<any>) {
|
|
|
|
- super(registry, valType, value)
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
|
|
+export function JoyBTreeSet<V extends UInt>(valType: Constructor<V>): Constructor<BTreeSet<V>> {
|
|
|
|
+ return class extends BTreeSet.with(valType) {
|
|
|
|
+ public toU8a(isBare?: boolean): Uint8Array {
|
|
|
|
+ const encoded = new Array<Uint8Array>()
|
|
|
|
|
|
- public toU8a(isBare?: boolean): Uint8Array {
|
|
|
|
- const encoded = new Array<Uint8Array>()
|
|
|
|
|
|
+ if (!isBare) {
|
|
|
|
+ encoded.push(Compact.encodeU8a(this.size))
|
|
|
|
+ }
|
|
|
|
|
|
- if (!isBare) {
|
|
|
|
- encoded.push(Compact.encodeU8a(this.size))
|
|
|
|
- }
|
|
|
|
|
|
+ const sorted = Array.from(this).sort((a, b) => (a.lt(b) ? -1 : 1))
|
|
|
|
|
|
- Array.from(this)
|
|
|
|
- .sort()
|
|
|
|
- .forEach((v: V) => {
|
|
|
|
|
|
+ sorted.forEach((v: V) => {
|
|
encoded.push(v.toU8a(isBare))
|
|
encoded.push(v.toU8a(isBare))
|
|
})
|
|
})
|
|
|
|
|
|
- return u8aConcat(...encoded)
|
|
|
|
- }
|
|
|
|
|
|
+ return u8aConcat(...encoded)
|
|
|
|
+ }
|
|
|
|
|
|
- public toHex(): string {
|
|
|
|
- return u8aToHex(this.toU8a())
|
|
|
|
|
|
+ public toHex(): string {
|
|
|
|
+ return u8aToHex(this.toU8a())
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
export class Credential extends u64 {}
|
|
export class Credential extends u64 {}
|
|
-export class CredentialSet extends Vec.with(Credential) {} // BtreeSet ?
|
|
|
|
|
|
+export class CredentialSet extends JoyBTreeSet(Credential) {}
|
|
|
|
|
|
// common types between Forum and Proposal Discussions modules
|
|
// common types between Forum and Proposal Discussions modules
|
|
export class ThreadId extends u64 {}
|
|
export class ThreadId extends u64 {}
|
|
export class PostId extends u64 {}
|
|
export class PostId extends u64 {}
|
|
|
|
|
|
-export class Hash extends U8aFixed implements H256 {}
|
|
|
|
|
|
+export class Hash extends U8aFixed implements PolkadotHash {}
|
|
|
|
|
|
export type BlockAndTimeType = {
|
|
export type BlockAndTimeType = {
|
|
block: BlockNumber
|
|
block: BlockNumber
|