generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.ts
214 lines (188 loc) · 5.3 KB
/
index.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
213
214
import BigNumber from 'bignumber.js';
import {
Account,
Context,
Entity,
modifyAllowance,
ModifyAllowanceParams,
PolymeshError,
quitSubsidy,
} from '~/internal';
import {
AllowanceOperation,
DecreaseAllowanceParams,
ErrorCode,
IncreaseAllowanceParams,
NoArgsProcedureMethod,
ProcedureMethod,
SetAllowanceParams,
} from '~/types';
import { createProcedureMethod, toHumanReadable } from '~/utils/internal';
export interface UniqueIdentifiers {
/**
* beneficiary address
*/
beneficiary: string;
/**
* subsidizer address
*/
subsidizer: string;
}
type HumanReadable = UniqueIdentifiers;
/**
* Represents a Subsidy relationship on chain
*/
export class Subsidy extends Entity<UniqueIdentifiers, HumanReadable> {
/**
* @hidden
* Check if a value is of type {@link UniqueIdentifiers}
*/
public static override isUniqueIdentifiers(identifier: unknown): identifier is UniqueIdentifiers {
const { beneficiary, subsidizer } = identifier as UniqueIdentifiers;
return typeof beneficiary === 'string' && typeof subsidizer === 'string';
}
/**
* Account whose transactions are being paid for
*/
public beneficiary: Account;
/**
* Account that is paying for the transactions
*/
public subsidizer: Account;
/**
* @hidden
*/
public constructor(identifiers: UniqueIdentifiers, context: Context) {
const { beneficiary, subsidizer } = identifiers;
super(identifiers, context);
this.beneficiary = new Account({ address: beneficiary }, context);
this.subsidizer = new Account({ address: subsidizer }, context);
this.quit = createProcedureMethod(
{ getProcedureAndArgs: () => [quitSubsidy, { subsidy: this }], voidArgs: true },
context
);
this.setAllowance = createProcedureMethod<
Pick<ModifyAllowanceParams, 'allowance'>,
ModifyAllowanceParams,
void
>(
{
getProcedureAndArgs: args => [
modifyAllowance,
{
...args,
subsidy: this,
operation: AllowanceOperation.Set,
} as ModifyAllowanceParams,
],
},
context
);
this.increaseAllowance = createProcedureMethod<
Pick<ModifyAllowanceParams, 'allowance'>,
ModifyAllowanceParams,
void
>(
{
getProcedureAndArgs: args => [
modifyAllowance,
{
...args,
subsidy: this,
operation: AllowanceOperation.Increase,
} as ModifyAllowanceParams,
],
},
context
);
this.decreaseAllowance = createProcedureMethod<
Pick<ModifyAllowanceParams, 'allowance'>,
ModifyAllowanceParams,
void
>(
{
getProcedureAndArgs: args => [
modifyAllowance,
{
...args,
subsidy: this,
operation: AllowanceOperation.Decrease,
} as ModifyAllowanceParams,
],
},
context
);
}
/**
* Terminate this Subsidy relationship. The beneficiary Account will be forced to pay for their own transactions
*
* @note both the beneficiary and the subsidizer are allowed to unilaterally quit the Subsidy
*/
public quit: NoArgsProcedureMethod<void>;
/**
* Set allowance for this Subsidy relationship
*
* @note Only the subsidizer is allowed to set the allowance
*
* @throws if the allowance to set is equal to the current allowance
*/
public setAllowance: ProcedureMethod<Pick<SetAllowanceParams, 'allowance'>, void>;
/**
* Increase allowance for this Subsidy relationship
*
* @note Only the subsidizer is allowed to increase the allowance
*/
public increaseAllowance: ProcedureMethod<Pick<IncreaseAllowanceParams, 'allowance'>, void>;
/**
* Decrease allowance for this Subsidy relationship
*
* @note Only the subsidizer is allowed to decrease the allowance
*
* @throws if the amount to decrease by is more than the existing allowance
*/
public decreaseAllowance: ProcedureMethod<Pick<DecreaseAllowanceParams, 'allowance'>, void>;
/**
* Determine whether this Subsidy relationship exists on chain
*/
public async exists(): Promise<boolean> {
const {
beneficiary: { address: beneficiaryAddress },
subsidizer,
context,
} = this;
const subsidyWithAllowance = await context.accountSubsidy(beneficiaryAddress);
return (
subsidyWithAllowance !== null && subsidyWithAllowance.subsidy.subsidizer.isEqual(subsidizer)
);
}
/**
* Get amount of POLYX subsidized for this Subsidy relationship
*
* @throws if the Subsidy does not exist
*/
public async getAllowance(): Promise<BigNumber> {
const {
beneficiary: { address: beneficiaryAddress },
subsidizer,
context,
} = this;
const subsidyWithAllowance = await context.accountSubsidy(beneficiaryAddress);
if (!subsidyWithAllowance || !subsidyWithAllowance.subsidy.subsidizer.isEqual(subsidizer)) {
throw new PolymeshError({
code: ErrorCode.DataUnavailable,
message: 'The Subsidy no longer exists',
});
}
return subsidyWithAllowance.allowance;
}
/**
* Return the Subsidy's static data
*/
public toHuman(): HumanReadable {
const { beneficiary, subsidizer } = this;
return toHumanReadable({
beneficiary,
subsidizer,
});
}
}