-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathassetType.ts
48 lines (39 loc) · 1.28 KB
/
assetType.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
import { AssetName, AssetNameParams } from "./assetName";
import { ChainId, ChainIdParams } from "./chain";
import { CAIP } from "./spec";
import { IdentifierSpec } from "./types";
import { isValidId, joinParams, getParams } from "./utils";
export interface AssetTypeParams {
chainId: string | ChainIdParams;
assetName: string | AssetNameParams;
}
export class AssetType {
public static spec: IdentifierSpec = CAIP["19"].assetType;
public static parse(id: string): AssetTypeParams {
if (!isValidId(id, this.spec)) {
throw new Error(`Invalid ${this.spec.name} provided: ${id}`);
}
return new AssetType(getParams<AssetTypeParams>(id, this.spec)).toJSON();
}
public static format(params: AssetTypeParams): string {
return joinParams(params as any, this.spec);
}
public chainId: ChainId;
public assetName: AssetName;
constructor(params: AssetTypeParams | string) {
if (typeof params === "string") {
params = AssetType.parse(params);
}
this.chainId = new ChainId(params.chainId);
this.assetName = new AssetName(params.assetName);
}
public toString(): string {
return AssetType.format(this.toJSON());
}
public toJSON(): AssetTypeParams {
return {
chainId: this.chainId.toJSON(),
assetName: this.assetName,
};
}
}