-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.ts
51 lines (44 loc) · 1.09 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { decodeAddress, encodeAddress } from "@polkadot/util-crypto/mod.ts";
import { ApiDecoration } from "@polkadot/api/types/index.ts";
export type Api = ApiDecoration<"promise">;
export class RateLogger {
constructor(
public itemsName = "items",
private lastCount = 0,
private start = performance.now(),
private count = 0,
private freq = 1000,
) {}
log() {
if (this.count - this.lastCount >= this.freq) {
const now = performance.now();
console.log(
`rate: ${this.count / ((now - this.start) / 1000)} ${this.itemsName}/s`,
);
this.lastCount = this.count;
}
}
public inc(count: number) {
this.count += count;
this.log();
}
}
export class Ss58AccountId {
constructor(public value: string) {
try {
decodeAddress(value);
} catch (_e) {
try {
this.value = encodeAddress(value);
} catch (_e) {
throw new Error(`invalid account id: ${value}`);
}
}
}
public static from(value: string) {
return new Ss58AccountId(value);
}
public toJson() {
return this.value;
}
}