/
MosaicFlags.ts
117 lines (107 loc) · 4 KB
/
MosaicFlags.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* Copyright 2018 NEM
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { MosaicFlagsDto } from 'catbuffer-typescript';
/**
* Mosaic flags model
*/
export class MosaicFlags {
/**
* The creator can choose between a definition that allows a mosaic supply change at a later point or an immutable supply.
* Allowed values for the property are "true" and "false". The default value is "false".
*/
public readonly supplyMutable: boolean;
/**
* The creator can choose if the mosaic definition should allow for transfers of the mosaic among accounts other than the creator.
* If the property 'transferable' is set to "false", only transfer transactions
* having the creator as sender or as recipient can transfer mosaics of that type.
* If set to "true" the mosaics can be transferred to and from arbitrary accounts.
* Allowed values for the property are thus "true" and "false". The default value is "true".
*/
public readonly transferable: boolean;
/**
* Not all the mosaics of a given network will be subject to mosaic restrictions. The feature will only affect
* those to which the issuer adds the "restrictable" property explicitly at the moment of its creation. This
* property appears disabled by default, as it is undesirable for autonomous tokens like the public network currency.
*/
public readonly restrictable: boolean;
/**
* The creator can choose if he can revoke tokens after a transfer.
*/
public readonly revokable: boolean;
/**
* @param flags
*/
constructor(flags: number) {
this.supplyMutable = (flags & MosaicFlagsDto.SUPPLY_MUTABLE) !== 0;
this.transferable = (flags & MosaicFlagsDto.TRANSFERABLE) !== 0;
this.restrictable = (flags & MosaicFlagsDto.RESTRICTABLE) !== 0;
this.revokable = (flags & MosaicFlagsDto.REVOKABLE) !== 0;
}
/**
* Static constructor function with default parameters
* @returns {MosaicFlags}
* @param supplyMutable
* @param transferable
* @param restrictable
* @param revokable
*/
public static create(supplyMutable: boolean, transferable: boolean, restrictable = false, revokable = false): MosaicFlags {
const flags = this.toFlag({
supplyMutable: supplyMutable,
transferable: transferable,
restrictable: restrictable,
revokable: revokable,
});
return new MosaicFlags(flags);
}
/**
* Get mosaic flag value in number
* @returns {number}
*/
public getValue(): number {
return MosaicFlags.toFlag(this);
}
/**
* Create DTO object
*/
toDTO(): any {
return {
flags: this.getValue(),
};
}
/**
* It "adds up" individual flags into a bit wise number flag.
*
* @param supplyMutable - if the supply is mutable. First flag.
* @param transferable - if the balance can be transferred. Second flag.
* @param restrictable - if the transaction can be restricted. Third flag.
* @param revokable - if the balance can be revoked. Fourth flag.
* @private
*/
private static toFlag({
supplyMutable,
transferable,
restrictable,
revokable,
}: {
supplyMutable: boolean;
transferable: boolean;
restrictable: boolean;
revokable: boolean;
}): number {
return (supplyMutable ? 1 : 0) + (transferable ? 2 : 0) + (restrictable ? 4 : 0) + (revokable ? 8 : 0);
}
}