-
Notifications
You must be signed in to change notification settings - Fork 2
/
factory.ts
644 lines (614 loc) · 20.8 KB
/
factory.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
import {
AbiRegistry,
Address,
AddressValue,
BigUIntType,
BigUIntValue,
BooleanValue,
ContractCallPayloadBuilder,
ContractFunction,
IAddress,
ResultsParser,
SmartContract,
StringValue,
TokenIdentifierValue,
Transaction,
U32Value
} from '@multiversx/sdk-core/out';
import { ApiNetworkProvider } from '@multiversx/sdk-network-providers/out';
import {
EnvironmentsEnum,
factoryContractAddress,
networkConfiguration
} from './config';
import factoryAbi from './abis/core-mx-minter-factory-sc.abi.json';
import { DeployedContract } from './interfaces';
import { checkVersionFormat } from './utils';
import { ErrContractQuery, ErrNetworkConfig } from './errors';
export class Factory {
readonly contract: SmartContract;
readonly chainID: string;
readonly networkProvider: ApiNetworkProvider;
readonly env: string;
/**
* Creates a new instance of the `Factory` class, which can be used to interact with the factory smart contract
* @param env 'devnet' | 'devnet2' | 'mainnet' | 'testnet' based on {@link EnvironmentsEnum}
* @param timeout Timeout for the network provider (DEFAULT = 10000ms)
*/
constructor(env: string, timeout = 10000) {
if (!(env in EnvironmentsEnum)) {
throw new ErrNetworkConfig(
`Invalid environment: ${env}, Expected: 'devnet' | 'devnet2' | 'mainnet' | 'testnet'`
);
}
this.env = env;
const networkConfig = networkConfiguration[env as EnvironmentsEnum];
this.chainID = networkConfig.chainID;
this.networkProvider = new ApiNetworkProvider(
networkConfig.networkProvider,
{
timeout: timeout
}
);
const contractAddress = factoryContractAddress[env as EnvironmentsEnum];
this.contract = new SmartContract({
address: new Address(contractAddress),
abi: AbiRegistry.create(factoryAbi)
});
}
/**
* Retrives the address of the factory smart contract
*/
getContractAddress(): IAddress {
return this.contract.getAddress();
}
/**
* Retrieves a boolean value indicating wheter the address is whitelisted or not
* @param address The address to check
* If address is whitelisted it can deploy minter contracts
*/
async viewAddressIsWhitelisted(address: IAddress): Promise<boolean> {
const interaction = this.contract.methodsExplicit.getIsWhitelisted([
new AddressValue(address)
]);
const query = interaction.buildQuery();
const queryResponse = await this.networkProvider.queryContract(query);
const endpointDefinition = interaction.getEndpoint();
const { firstValue, returnCode } = new ResultsParser().parseQueryResponse(
queryResponse,
endpointDefinition
);
if (returnCode.isSuccess()) {
const returnValue = firstValue?.valueOf();
return new BooleanValue(returnValue).valueOf();
} else {
throw new ErrContractQuery(
'viewAddressIsWhitelisted',
returnCode.toString()
);
}
}
/**
* Retrives the tax percentage used for the factory smart contract
*/
async viewTaxPercentage(): Promise<number> {
const interaction = this.contract.methodsExplicit.getTax();
const query = interaction.buildQuery();
const queryResponse = await this.networkProvider.queryContract(query);
const endpointDefinition = interaction.getEndpoint();
const { firstValue, returnCode } = new ResultsParser().parseQueryResponse(
queryResponse,
endpointDefinition
);
if (returnCode.isSuccess()) {
const returnValue = firstValue?.valueOf();
return returnValue.toNumber() / 100;
} else {
throw new ErrContractQuery('viewTaxPercentage', returnCode.toString());
}
}
/**
* Retrieves the factory treasury address
*/
async viewTreasuryAddress(): Promise<IAddress> {
const interaction = this.contract.methodsExplicit.getTreasuryAddress();
const query = interaction.buildQuery();
const queryResponse = await this.networkProvider.queryContract(query);
const endpointDefinition = interaction.getEndpoint();
const { firstValue, returnCode } = new ResultsParser().parseQueryResponse(
queryResponse,
endpointDefinition
);
if (returnCode.isSuccess()) {
const returnValue = firstValue?.valueOf();
return new Address(returnValue);
} else {
throw new ErrContractQuery('viewTreasuryAddress', returnCode.toString());
}
}
/**
* Retrieves a boolean value indicating wheter the factory smart contract requires whitelist or not
*/
async viewWhitelistState(): Promise<boolean> {
const interaction = this.contract.methodsExplicit.getWhitelistEnabled();
const query = interaction.buildQuery();
const queryResponse = await this.networkProvider.queryContract(query);
const endpointDefinition = interaction.getEndpoint();
const { firstValue, returnCode } = new ResultsParser().parseQueryResponse(
queryResponse,
endpointDefinition
);
if (returnCode.isSuccess()) {
const returnValue = firstValue?.valueOf();
return new BooleanValue(returnValue).valueOf();
} else {
throw new ErrContractQuery(
'viewWhitelistEnabledState',
returnCode.toString()
);
}
}
/**
* Retrives all deployed contracts of address parameter
* @param address The address to check
*/
async viewAddressContracts(address: IAddress): Promise<DeployedContract[]> {
const interaction = this.contract.methodsExplicit.getAddressContracts([
new AddressValue(address)
]);
const query = interaction.buildQuery();
const queryResponse = await this.networkProvider.queryContract(query);
const endpointDefinition = interaction.getEndpoint();
const { firstValue, returnCode } = new ResultsParser().parseQueryResponse(
queryResponse,
endpointDefinition
);
if (returnCode.isSuccess()) {
const returnValue = firstValue?.valueOf();
const results = returnValue?.map((contract: any) => {
return {
owner: contract.owner.toString(),
address: contract.address.toString(),
version: contract.version.toString()
};
});
return results;
} else {
throw new ErrContractQuery('viewAddressContracts', returnCode.toString());
}
}
/**
* Retrives all deployed contracts of the factory smart contract
*/
async viewContracts(from: number, to: number): Promise<DeployedContract[]> {
const interaction = this.contract.methodsExplicit.getChildContracts([
new U32Value(from),
new U32Value(to)
]);
const query = interaction.buildQuery();
const queryResponse = await this.networkProvider.queryContract(query);
const endpointDefinition = interaction.getEndpoint();
const { firstValue, returnCode } = new ResultsParser().parseQueryResponse(
queryResponse,
endpointDefinition
);
if (returnCode.isSuccess()) {
const returnValue = firstValue?.valueOf();
const results = returnValue?.map((contract: any) => {
return {
owner: contract.owner.toString(),
address: contract.address.toString(),
version: contract.version.toString()
};
});
return results;
} else {
throw new ErrContractQuery('viewContracts', returnCode.toString());
}
}
/**
* Retrives an array of versions available for deployment of the minter contract
*/
async viewVersions(): Promise<string[]> {
const interaction = this.contract.methodsExplicit.getVersions();
const query = interaction.buildQuery();
const queryResponse = await this.networkProvider.queryContract(query);
const endpointDefinition = interaction.getEndpoint();
const { firstValue, returnCode } = new ResultsParser().parseQueryResponse(
queryResponse,
endpointDefinition
);
if (returnCode.isSuccess()) {
const returnValue = firstValue?.valueOf();
const results = returnValue?.map((version: any) => {
return version.toString();
});
return results;
} else {
throw new ErrContractQuery('viewVersions', returnCode.toString());
}
}
/**
* Retrieves a boolean value indicating wheter the contract is paused or not
*/
async viewContractPauseState(): Promise<boolean> {
const interaction = this.contract.methodsExplicit.getPauseState();
const query = interaction.buildQuery();
const queryResponse = await this.networkProvider.queryContract(query);
const endpointDefinition = interaction.getEndpoint();
const { firstValue, returnCode } = new ResultsParser().parseQueryResponse(
queryResponse,
endpointDefinition
);
if (returnCode.isSuccess()) {
const returnValue = firstValue?.valueOf();
return new BooleanValue(returnValue).valueOf();
} else {
throw new ErrContractQuery(
'Error while retrieving the contract pause state'
);
}
}
/**
* Retrieves the code in hex encoding of minter smart contract based on version parameter
* @param version The version of the minter smart contract
*/
async viewContractCode(version: string): Promise<string> {
checkVersionFormat(version);
const interaction = this.contract.methodsExplicit.getVersionCode([
new StringValue(version)
]);
const query = interaction.buildQuery();
const queryResponse = await this.networkProvider.queryContract(query);
const endpointDefinition = interaction.getEndpoint();
const { firstValue, returnCode } = new ResultsParser().parseQueryResponse(
queryResponse,
endpointDefinition
);
if (returnCode.isSuccess()) {
const returnValue = firstValue?.valueOf().toString('hex');
return returnValue;
} else {
throw new ErrContractQuery('viewContractCode', returnCode.toString());
}
}
/**
* Retrieves the address of the claims contract used by the factory smart contract
*/
async viewClaimsContractAddress(): Promise<IAddress> {
const interaction =
this.contract.methodsExplicit.getClaimsContractAddress();
const query = interaction.buildQuery();
const queryResponse = await this.networkProvider.queryContract(query);
const endpointDefinition = interaction.getEndpoint();
const { firstValue, returnCode } = new ResultsParser().parseQueryResponse(
queryResponse,
endpointDefinition
);
if (returnCode.isSuccess()) {
const returnValue = firstValue?.valueOf();
return new Address(returnValue);
} else {
throw new ErrContractQuery(
'viewClaimsContractAddress',
returnCode.toString()
);
}
}
/**
* Retrieves the token identifier of the claims contract used by the factory smart contract
*/
async viewClaimsTokenIdentifier(): Promise<string> {
const interaction =
this.contract.methodsExplicit.getClaimsTokenIdentifier();
const query = interaction.buildQuery();
const queryResponse = await this.networkProvider.queryContract(query);
const endpointDefinition = interaction.getEndpoint();
const { firstValue, returnCode } = new ResultsParser().parseQueryResponse(
queryResponse,
endpointDefinition
);
if (returnCode.isSuccess()) {
const returnValue = firstValue?.valueOf();
return returnValue.toString();
} else {
throw new ErrContractQuery(
'viewClaimsTokenIdentifier',
returnCode.toString()
);
}
}
/**
*
* @param senderAddress The address of the sender, must be the owner of the factory contract
* @param require_whitelist A boolean value indicating if the factory will require whitelist
* @param treasuryAddress The address of the treasury where the tax will be sent
* @param tax The tax percentage to set in the contract ( minter contract will send the tax cut to treasury address)
* @param claimsContractAddress The address of the claims contract
* @param claimsTokenIdentifier The token identifier of the claims contract
*/
initializeContract(
senderAddress: IAddress,
require_whitelist: boolean,
treasuryAddress: IAddress,
tax: number,
claimsContractAddress: IAddress,
claimsTokenIdentifier: string
): Transaction {
const initializeContractTx = new Transaction({
value: 0,
data: new ContractCallPayloadBuilder()
.setFunction(new ContractFunction('initializeContract'))
.addArg(new BooleanValue(require_whitelist))
.addArg(new AddressValue(treasuryAddress))
.addArg(new BigUIntValue(tax))
.addArg(new AddressValue(claimsContractAddress))
.addArg(new TokenIdentifierValue(claimsTokenIdentifier))
.build(),
sender: senderAddress,
gasLimit: 20000000,
receiver: this.getContractAddress(),
chainID: this.chainID
});
return initializeContractTx;
}
/**
* @param senderAddress The address of the sender, must be the owner of the contract
* @param code The code of the minter contract
* @param version The version of the minter contract
*/
uploadCode(
senderAddress: IAddress,
code: string,
version: string
): Transaction {
checkVersionFormat(version);
const uploadCodeTx = new Transaction({
value: 0,
data: new ContractCallPayloadBuilder()
.setFunction(new ContractFunction('uploadCode'))
.addArg(new StringValue(code))
.addArg(new StringValue(version))
.build(),
sender: senderAddress,
gasLimit: 600000000,
receiver: this.getContractAddress(),
chainID: this.chainID
});
return uploadCodeTx;
}
/**
* @param senderAddress The address of the sender, must be the owner of the contract
* @param version The version of the minter contract
*/
removeCode(senderAddress: IAddress, version: string): Transaction {
checkVersionFormat(version);
const removeCodeTx = new Transaction({
value: 0,
data: new ContractCallPayloadBuilder()
.setFunction(new ContractFunction('removeCode'))
.addArg(new StringValue(version))
.build(),
sender: senderAddress,
gasLimit: 10000000,
receiver: this.getContractAddress(),
chainID: this.chainID
});
return removeCodeTx;
}
/**
*
* @param senderAddress The address of the sender, must be the owner of the contract
* @param address The address to whitelist
*/
whitelist(senderAddress: IAddress, address: IAddress): Transaction {
const whitelistTx = new Transaction({
value: 0,
data: new ContractCallPayloadBuilder()
.setFunction(new ContractFunction('whitelist'))
.addArg(new AddressValue(address))
.build(),
sender: senderAddress,
gasLimit: 10000000,
receiver: this.getContractAddress(),
chainID: this.chainID
});
return whitelistTx;
}
/**
*
* @param senderAddress The address of the sender, must be the owner of the contract
* @param address The address to remove from whitelist
*/
removeWhitelist(senderAddress: IAddress, address: IAddress): Transaction {
const delistTx = new Transaction({
value: 0,
data: new ContractCallPayloadBuilder()
.setFunction(new ContractFunction('removeWhitelist'))
.addArg(new AddressValue(address))
.build(),
sender: senderAddress,
gasLimit: 10000000,
receiver: this.getContractAddress(),
chainID: this.chainID
});
return delistTx;
}
/**
*
* @param senderAddress The address of the deployer of the minter contract
* @param version The version of the minter contract code
*/
deployContract(senderAddress: IAddress, version: string): Transaction {
checkVersionFormat(version);
const deployContractTx = new Transaction({
value: 0,
data: new ContractCallPayloadBuilder()
.setFunction(new ContractFunction('deployChildContract'))
.addArg(new StringValue(version))
.build(),
sender: senderAddress,
gasLimit: 50000000,
receiver: this.getContractAddress(),
chainID: this.chainID
});
return deployContractTx;
}
/**
*
* @param senderAddress The address of the sender, must be the owner of the factory or owner of the minter contract
* @param childContractAddress The address of the child contract
* @param upgradeVersion The version of the minter contract code to upgrade
*/
upgradeChildContract(
senderAddress: IAddress,
childContractAddress: IAddress,
upgradeVersion: string
): Transaction {
checkVersionFormat(upgradeVersion);
const upgradeChildContractTx = new Transaction({
value: 0,
data: new ContractCallPayloadBuilder()
.setFunction(new ContractFunction('upgradeChildContract'))
.addArg(new AddressValue(childContractAddress))
.addArg(new StringValue(upgradeVersion))
.build(),
sender: senderAddress,
gasLimit: 100000000,
receiver: this.getContractAddress(),
chainID: this.chainID
});
return upgradeChildContractTx;
}
/**
* Returns a transaction to change the ownership of the minter contract to the deployer of minter contract
* @param senderAddress The address of the sender, must be the owner of the contract
* Note: It change the ownership of the minter contract to the deployer of minter contract
*/
changeOwnership(
senderAddress: IAddress,
contractAddress: IAddress
): Transaction {
const changeOwnershipTx = new Transaction({
value: 0,
data: new ContractCallPayloadBuilder()
.setFunction(new ContractFunction('changeOwnership'))
.addArg(new AddressValue(contractAddress))
.build(),
sender: senderAddress,
gasLimit: 10000000,
receiver: this.getContractAddress(),
chainID: this.chainID
});
return changeOwnershipTx;
}
/**
*
* @param senderAddress The address of the sender, must be the owner of the contract
* @param taxPercentage The tax percentage to set in the contract ( minter contract will send the tax cut to treasury address)
*/
setTax(senderAddress: IAddress, taxPercentage: number): Transaction {
const setTaxTx = new Transaction({
value: 0,
data: new ContractCallPayloadBuilder()
.setFunction(new ContractFunction('setTax'))
.addArg(new BigUIntValue(taxPercentage))
.build(),
sender: senderAddress,
gasLimit: 10000000,
receiver: this.getContractAddress(),
chainID: this.chainID
});
return setTaxTx;
}
/**
*
* @param senderAddress The address of the sender, must be the owner of the contract
*/
enableWhitelist(senderAddress: IAddress): Transaction {
const enableWhitelistTx = new Transaction({
value: 0,
data: new ContractCallPayloadBuilder()
.setFunction(new ContractFunction('seWhitelistEnabled'))
.addArg(new BooleanValue(true))
.build(),
sender: senderAddress,
gasLimit: 10000000,
receiver: this.getContractAddress(),
chainID: this.chainID
});
return enableWhitelistTx;
}
/**
*
* @param senderAddress The address of the sender, must be the owner of the contract
*/
disableWhitelist(senderAddress: IAddress): Transaction {
const disableWhitelistTx = new Transaction({
value: 0,
data: new ContractCallPayloadBuilder()
.setFunction(new ContractFunction('seWhitelistEnabled'))
.addArg(new BooleanValue(false))
.build(),
sender: senderAddress,
gasLimit: 10000000,
receiver: this.getContractAddress(),
chainID: this.chainID
});
return disableWhitelistTx;
}
/**
* Pause contract transaction
*/
pause(senderAddress: IAddress): Transaction {
const pauseTx = new Transaction({
value: 0,
data: new ContractCallPayloadBuilder()
.setFunction(new ContractFunction('pause'))
.build(),
sender: senderAddress,
gasLimit: 10000000,
receiver: this.getContractAddress(),
chainID: this.chainID
});
return pauseTx;
}
/**
* Unpause contract transaction
*/
unpause(senderAddress: IAddress): Transaction {
const unpauseTx = new Transaction({
value: 0,
data: new ContractCallPayloadBuilder()
.setFunction(new ContractFunction('unpause'))
.build(),
sender: senderAddress,
gasLimit: 10000000,
receiver: this.getContractAddress(),
chainID: this.chainID
});
return unpauseTx;
}
/**
*
* @param senderAddress The address of the sender, must be the owner of the contract
* @param treasuryAddress The address of the treasury
*/
setTreasuryAddress(
senderAddress: IAddress,
treasuryAddress: IAddress
): Transaction {
const setTreasuryAddressTx = new Transaction({
value: 0,
data: new ContractCallPayloadBuilder()
.setFunction(new ContractFunction('setTreasuryAddress'))
.addArg(new AddressValue(treasuryAddress))
.build(),
sender: senderAddress,
gasLimit: 10000000,
receiver: this.getContractAddress(),
chainID: this.chainID
});
return setTreasuryAddressTx;
}
}