generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Requirements.ts
212 lines (187 loc) · 6.07 KB
/
Requirements.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { Vec } from '@polkadot/types/codec';
import {
PolymeshPrimitivesComplianceManagerAssetCompliance,
PolymeshPrimitivesConditionTrustedIssuer,
} from '@polkadot/types/lookup';
import {
addAssetRequirement,
BaseAsset,
Context,
modifyComplianceRequirement,
Namespace,
removeAssetRequirement,
setAssetRequirements,
togglePauseRequirements,
} from '~/internal';
import {
AddAssetRequirementParams,
ComplianceRequirements,
ModifyComplianceRequirementParams,
NoArgsProcedureMethod,
ProcedureMethod,
RemoveAssetRequirementParams,
SetAssetRequirementsParams,
SubCallback,
UnsubCallback,
} from '~/types';
import {
boolToBoolean,
complianceRequirementToRequirement,
stringToTicker,
trustedIssuerToTrustedClaimIssuer,
} from '~/utils/conversion';
import { createProcedureMethod, requestMulti } from '~/utils/internal';
/**
* Handles all Asset Compliance Requirements related functionality
*/
export class Requirements extends Namespace<BaseAsset> {
/**
* @hidden
*/
constructor(parent: BaseAsset, context: Context) {
super(parent, context);
const { ticker } = parent;
this.add = createProcedureMethod(
{ getProcedureAndArgs: args => [addAssetRequirement, { ticker, ...args }] },
context
);
this.remove = createProcedureMethod(
{ getProcedureAndArgs: args => [removeAssetRequirement, { ticker, ...args }] },
context
);
this.set = createProcedureMethod(
{ getProcedureAndArgs: args => [setAssetRequirements, { ticker, ...args }] },
context
);
this.reset = createProcedureMethod(
{
getProcedureAndArgs: () => [setAssetRequirements, { ticker, requirements: [] }],
voidArgs: true,
},
context
);
this.pause = createProcedureMethod(
{
getProcedureAndArgs: () => [togglePauseRequirements, { ticker, pause: true }],
voidArgs: true,
},
context
);
this.unpause = createProcedureMethod(
{
getProcedureAndArgs: () => [togglePauseRequirements, { ticker, pause: false }],
voidArgs: true,
},
context
);
this.modify = createProcedureMethod(
{ getProcedureAndArgs: args => [modifyComplianceRequirement, { ticker, ...args }] },
context
);
}
/**
* Add a new compliance requirement to the the Asset. This doesn't modify existing requirements
*/
public add: ProcedureMethod<AddAssetRequirementParams, void>;
/**
* Remove an existing compliance requirement from the Asset
*/
public remove: ProcedureMethod<RemoveAssetRequirementParams, void>;
/**
* Configure compliance requirements for the Asset. This operation will replace all existing requirements with a new requirement set
*
* @example Say A, B, C, D and E are requirements and we arrange them as `[[A, B], [C, D], [E]]`.
* For a transfer to succeed, it must either comply with A AND B, C AND D, OR E.
*/
public set: ProcedureMethod<SetAssetRequirementsParams, void>;
/**
* Retrieve all of the Asset's compliance requirements, together with the Default Trusted Claim Issuers
*
* @note can be subscribed to, if connected to node using a web socket
*/
public get(): Promise<ComplianceRequirements>;
public get(callback: SubCallback<ComplianceRequirements>): Promise<UnsubCallback>;
// eslint-disable-next-line require-jsdoc
public async get(
callback?: SubCallback<ComplianceRequirements>
): Promise<ComplianceRequirements | UnsubCallback> {
const {
parent: { ticker },
context: {
polymeshApi: {
query: { complianceManager },
},
},
context,
} = this;
const rawTicker = stringToTicker(ticker, context);
const assembleResult = ([assetCompliance, claimIssuers]: [
PolymeshPrimitivesComplianceManagerAssetCompliance,
Vec<PolymeshPrimitivesConditionTrustedIssuer>
]): ComplianceRequirements => {
const requirements = assetCompliance.requirements.map(complianceRequirement =>
complianceRequirementToRequirement(complianceRequirement, context)
);
const defaultTrustedClaimIssuers = claimIssuers.map(issuer =>
trustedIssuerToTrustedClaimIssuer(issuer, context)
);
return { requirements, defaultTrustedClaimIssuers };
};
if (callback) {
context.assertSupportsSubscription();
return requestMulti<
[typeof complianceManager.assetCompliances, typeof complianceManager.trustedClaimIssuer]
>(
context,
[
[complianceManager.assetCompliances, rawTicker],
[complianceManager.trustedClaimIssuer, rawTicker],
],
res => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises -- callback errors should be handled by the caller
callback(assembleResult(res));
}
);
}
const result = await requestMulti<
[typeof complianceManager.assetCompliances, typeof complianceManager.trustedClaimIssuer]
>(context, [
[complianceManager.assetCompliances, rawTicker],
[complianceManager.trustedClaimIssuer, rawTicker],
]);
return assembleResult(result);
}
/**
* Delete all the current requirements for the Asset.
*/
public reset: NoArgsProcedureMethod<void>;
/**
* Pause all the Asset's requirements. This means that all transfers will be allowed until requirements are unpaused
*/
public pause: NoArgsProcedureMethod<void>;
/**
* Un-pause all the Asset's current requirements
*/
public unpause: NoArgsProcedureMethod<void>;
/**
* Check whether Asset compliance requirements are paused or not
*/
public async arePaused(): Promise<boolean> {
const {
parent: { ticker },
context: {
polymeshApi: {
query: { complianceManager },
},
},
context,
} = this;
const rawTicker = stringToTicker(ticker, context);
const { paused } = await complianceManager.assetCompliances(rawTicker);
return boolToBoolean(paused);
}
/**
* Modify a compliance requirement for the Asset
*/
public modify: ProcedureMethod<ModifyComplianceRequirementParams, void>;
}