-
Notifications
You must be signed in to change notification settings - Fork 4
/
deployUniversalProfile.ts
145 lines (132 loc) · 5.88 KB
/
deployUniversalProfile.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
import { AbiCoder, BytesLike, Signer, Wallet, randomBytes, toBeHex } from 'ethers';
import { ALL_PERMISSIONS, ERC725YDataKeys } from '@lukso/lsp-smart-contracts';
// constants
import {
UniversalProfileInitAddress,
LSP6KeyManagerInitAddress,
LSP23LinkedContractsFactoryAddress,
upPostDeploymentModuleAddress,
LSP6Controller,
} from '../..';
// typechain
import {
LSP23LinkedContractsFactory__factory,
LSP6KeyManagerInit__factory,
UniversalProfileInit__factory,
} from '../../typechain/lukso';
/**
* Deploy Universal Profile with Key Manager using LSP23 Linked Contracts Factory.
*
* @since v0.0.2
* @category LSP23
*
* @param signer The signer used to deploy the contracts, needs to have native token for gas fees.
* @param mainController The main controller for the Unviersal Profile.
* @param additionalDataKeys Data keys that you want to set on deployment.
* @param additionalDataValues Data values that you want to set on deployment.
* @param additionalControllers Additional controllers for the deployed Universal Profile.
*
* @returns The addresses of the Universal Profile & Key Manager.
*/
export const deployUniversalProfile = async (
signer: Signer | Wallet,
mainController: BytesLike,
additionalDataKeys: BytesLike[],
additionalDataValues: BytesLike[],
additionalControllers: LSP6Controller[],
salt?: BytesLike,
) => {
/// ------ Contract Instance for the `LSP23LinkedContractsFactory` ------
const LSP23LinkedContractsFactory = LSP23LinkedContractsFactory__factory.connect(
LSP23LinkedContractsFactoryAddress,
signer,
);
/// ---------------------------------------------------------------------
/// ------ Contract Instance for the `UniversalProfileInit` ------
const universalProfileInit = UniversalProfileInit__factory.connect(
UniversalProfileInitAddress,
signer,
);
/// ---------------------------------------------------------------------
/// ------ Contract Instance for the `LSP6KeyManagerInit` ------
const keyManagerInit = LSP6KeyManagerInit__factory.connect(LSP6KeyManagerInitAddress);
/// ---------------------------------------------------------------------
/// ------ Generate Random Salt for Universal Profile deployment (we can also use custom salt) ------
const deploymentSalt = salt ? salt : randomBytes(32);
/// -------------------------------------------------------------------------------------------------
/// ------ Data for Universal Profile deployment ------
const primaryContractDeploymentInit = {
salt: deploymentSalt,
fundingAmount: 0,
implementationContract: UniversalProfileInitAddress,
initializationCalldata: universalProfileInit.interface.encodeFunctionData('initialize', [
upPostDeploymentModuleAddress,
]),
};
/// ---------------------------------------------------
/// ------ Data for Key Manager deployment ------
const secondaryContractDeploymentInit = {
fundingAmount: 0,
implementationContract: LSP6KeyManagerInitAddress,
addPrimaryContractAddress: true,
initializationCalldata: keyManagerInit.interface.getFunction('initialize').selector,
extraInitializationParams: '0x',
};
/// ---------------------------------------------------
/// ------ Encode Data Keys & Values for updating permissions & LSP3Metadata ------
const encodedSetData = new AbiCoder().encode(
['bytes32[]', 'bytes[]'],
[
[
ERC725YDataKeys.LSP6['AddressPermissions[]'].length,
ERC725YDataKeys.LSP6['AddressPermissions[]'].index + '00'.repeat(16),
ERC725YDataKeys.LSP6['AddressPermissions:Permissions'] +
mainController.toString().substring(2),
...(additionalDataKeys ? additionalDataKeys : []),
...(additionalControllers
? additionalControllers.flatMap((controller, index) => [
ERC725YDataKeys.LSP6['AddressPermissions[]'].index +
toBeHex(index + 1, 16).substring(2),
ERC725YDataKeys.LSP6['AddressPermissions:Permissions'] +
controller.address.toString().substring(2),
])
: []),
],
[
toBeHex(1 + (additionalControllers ? additionalControllers.length : 0), 16),
mainController,
ALL_PERMISSIONS,
...(additionalDataValues ? additionalDataValues : []),
...(additionalControllers
? additionalControllers.flatMap((controller) => [
controller.address,
controller.permissions,
])
: []),
],
],
);
/// -------------------------------------------------------------------------------
/// ------ Pre-calculate the addresses for the Universal Profile & Key Manager ------
const [universalProfileAddress, keyManagerAddress] = await LSP23LinkedContractsFactory.connect(
signer,
).computeERC1167Addresses(
primaryContractDeploymentInit,
secondaryContractDeploymentInit,
upPostDeploymentModuleAddress,
encodedSetData,
);
/// ---------------------------------------------------------------------------------
/// ------ Deploy the Universal Profile & Key Manager ------
const deploymentTransaction = await LSP23LinkedContractsFactory.connect(
signer,
).deployERC1167Proxies(
primaryContractDeploymentInit,
secondaryContractDeploymentInit,
upPostDeploymentModuleAddress,
encodedSetData,
);
/// --------------------------------------------------------
await deploymentTransaction.wait(1);
return { universalProfileAddress, keyManagerAddress };
};