Skip to content
This repository was archived by the owner on Aug 8, 2022. It is now read-only.

Commit 784ca48

Browse files
committed
feat(ScaleSet): add SscaleSet
1 parent 4fccdcb commit 784ca48

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

assembly/ScaleSet.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
import { CompactInt } from "./Int/CompactInt";
2+
import { BytesReader } from "./BytesReader";
3+
import { Codec } from "./interfaces/Codec";
4+
import { UnwrappableCodec } from "./interfaces/UnwrappableCodec";
5+
6+
/**
7+
* @description SCALE Codec support for native Map type
8+
*/
9+
export class ScaleSet<T extends Codec>
10+
extends Set<T>
11+
implements UnwrappableCodec<Set<T>> {
12+
/**
13+
* @description return underlying native type
14+
*/
15+
@inline
16+
unwrap(): Set<T> {
17+
// TODO: remove unwrap?
18+
return this;
19+
}
20+
21+
/**
22+
* The number of bytes this Map has
23+
*/
24+
@inline
25+
encodedLength(): i32 {
26+
return this.toU8a().length;
27+
}
28+
/**
29+
* Convert ScaleMap to u8[]
30+
* Length is encoded first, followed by all key and value encodings concatenated
31+
*/
32+
toU8a(): u8[] {
33+
const values: T[] = this.values();
34+
let len: CompactInt = new CompactInt(values.length);
35+
const result: Array<Array<u8>> = [len.toU8a()];
36+
for (let i = 0; i < values.length; i++) {
37+
result.push(values[i].toU8a());
38+
}
39+
return result.flat();
40+
}
41+
42+
/**
43+
* @description Non-static constructor
44+
* @param bytes
45+
* @param index
46+
*/
47+
populateFromBytes(bytes: u8[], index: i32 = 0): i32 {
48+
const bytesReader = new BytesReader(bytes, index);
49+
const lenComp = bytesReader.readInto<CompactInt>();
50+
for (let i: i32 = 0; i < lenComp.unwrap(); i++) {
51+
const value = bytesReader.readInto<T>();
52+
this.add(value);
53+
}
54+
55+
return bytesReader.currentIndex();
56+
}
57+
/**
58+
* @description Overloaded == operator
59+
* @param a instance of ExtrinsicData
60+
* @param b Instance of ExtrinsicData
61+
*/
62+
@operator("==")
63+
eq(other: ScaleSet<T>): bool {
64+
const aLen = this.size;
65+
const bLen = other.size;
66+
const aValues = this.values();
67+
const bValues = other.values();
68+
69+
if (aLen != bLen) {
70+
return false;
71+
}
72+
for (let i = 0; i < aValues.length; i++) {
73+
if (aValues[i] != bValues[i]) {
74+
return false;
75+
}
76+
}
77+
return true;
78+
}
79+
80+
/**
81+
* @description Overloaded != operator
82+
* @param a instance of ExtrinsicData
83+
* @param b Instance of ExtrinsicData
84+
*/
85+
@operator("!=")
86+
notEq(other: ScaleSet<T>): bool {
87+
return !this.eq(other);
88+
}
89+
90+
static fromU8a<T extends Codec>(input: u8[], index: i32 = 0): ScaleSet<T> {
91+
const set = new ScaleSet<T>();
92+
const bytesReader = new BytesReader(input, index);
93+
const len = bytesReader.readInto<CompactInt>().unwrap();
94+
95+
for (let i: i32 = 0; i < len; i++) {
96+
const val = bytesReader.readInto<T>();
97+
set.add(val);
98+
}
99+
return set;
100+
}
101+
}

0 commit comments

Comments
 (0)