@0xobelisk/sui-cli 0.5.17 → 0.5.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,246 +1,246 @@
1
- import { Obelisk } from '@0xobelisk/sui-client';
2
- import { Transaction, UpgradePolicy } from '@mysten/sui/transactions';
3
- import { getFullnodeUrl, SuiClient } from '@mysten/sui/client';
4
- import { execSync } from 'child_process';
5
- import chalk from 'chalk';
6
- import { ObeliskCliError, UpgradeError } from './errors';
7
- import {
8
- updateVersionInFile,
9
- getOldPackageId,
10
- getVersion,
11
- getUpgradeCap,
12
- saveContractData,
13
- validatePrivateKey,
14
- } from './utils';
15
-
16
- type ObjectContent = {
17
- type: string;
18
- fields: Record<string, any>;
19
- hasPublicTransfer: boolean;
20
- dataType: string;
21
- };
22
-
23
- export async function upgradeHandler(
24
- name: string,
25
- network: 'mainnet' | 'testnet' | 'devnet' | 'localnet',
26
- schemaNames: string[]
27
- ) {
28
- const path = process.cwd();
29
- const projectPath = `${path}/contracts/${name}`;
30
- const privateKey = process.env.PRIVATE_KEY;
31
- if (!privateKey)
32
- throw new ObeliskCliError(
33
- `Missing PRIVATE_KEY environment variable.
34
- Run 'echo "PRIVATE_KEY=YOUR_PRIVATE_KEY" > .env'
35
- in your contracts directory to use the default sui private key.`
36
- );
37
-
38
- const privateKeyFormat = validatePrivateKey(privateKey);
39
- if (privateKeyFormat === false) {
40
- throw new ObeliskCliError(`Please check your privateKey.`);
41
- }
42
- const obelisk = new Obelisk({
43
- secretKey: privateKeyFormat,
44
- });
45
- const keypair = obelisk.getKeypair();
46
-
47
- const client = new SuiClient({
48
- url: getFullnodeUrl(network),
49
- });
50
-
51
- let oldVersion = Number(await getVersion(projectPath, network));
52
- let oldPackageId = await getOldPackageId(projectPath, network);
53
- let worldId = await getWorldId(projectPath, network);
54
- let upgradeCap = await getUpgradeCap(projectPath, network);
55
- let adminCap = await getAdminCap(projectPath, network);
56
-
57
- const newVersion = oldVersion + 1;
58
- await updateVersionInFile(projectPath, newVersion.toString());
59
-
60
- try {
61
- let modules: any, dependencies: any, digest: any;
62
- try {
63
- const {
64
- modules: extractedModules,
65
- dependencies: extractedDependencies,
66
- digest: extractedDigest,
67
- } = JSON.parse(
68
- execSync(
69
- `sui move build --dump-bytecode-as-base64 --path ${path}/contracts/${name}`,
70
- {
71
- encoding: 'utf-8',
72
- }
73
- )
74
- );
75
-
76
- modules = extractedModules;
77
- dependencies = extractedDependencies;
78
- digest = extractedDigest;
79
- } catch (error: any) {
80
- throw new UpgradeError(error.stdout);
81
- }
82
-
83
- const tx = new Transaction();
84
- const ticket = tx.moveCall({
85
- target: '0x2::package::authorize_upgrade',
86
- arguments: [
87
- tx.object(upgradeCap),
88
- tx.pure.u8(UpgradePolicy.COMPATIBLE),
89
- tx.pure.vector('u8', digest),
90
- ],
91
- });
92
-
93
- const receipt = tx.upgrade({
94
- modules,
95
- dependencies,
96
- package: oldPackageId,
97
- ticket,
98
- });
99
-
100
- tx.moveCall({
101
- target: '0x2::package::commit_upgrade',
102
- arguments: [tx.object(upgradeCap), receipt],
103
- });
104
-
105
- tx.transferObjects(
106
- [tx.object(upgradeCap)],
107
- keypair.toSuiAddress()
108
- );
109
-
110
- const result = await client.signAndExecuteTransaction({
111
- signer: keypair,
112
- transaction: tx,
113
- options: {
114
- showObjectChanges: true,
115
- },
116
- });
117
-
118
- console.log('');
119
- console.log(`${name} WorldId: ${worldId}`);
120
-
121
- let newPackageId = '';
122
- let newUpgradeCap = '';
123
- result.objectChanges!.map(object => {
124
- if (object.type === 'published') {
125
- console.log(
126
- chalk.blue(`${name} PackageId: ${object.packageId}`)
127
- );
128
- newPackageId = object.packageId;
129
- }
130
- if (
131
- object.type === 'mutated' &&
132
- object.objectType === '0x2::package::UpgradeCap'
133
- ) {
134
- console.log(
135
- chalk.blue(`${name} UpgradeCap: ${object.objectId}`)
136
- );
137
- newUpgradeCap = object.objectId;
138
- }
139
- });
140
-
141
- console.log(
142
- chalk.green(`Upgrade Transaction Digest: ${result.digest}`)
143
- );
144
-
145
- saveContractData(
146
- name,
147
- network,
148
- newPackageId,
149
- worldId,
150
- newUpgradeCap,
151
- adminCap,
152
- newVersion
153
- );
154
-
155
- oldPackageId = newPackageId;
156
- upgradeCap = newUpgradeCap;
157
- oldVersion = newVersion;
158
-
159
- console.log('\nExecuting the migrate: ');
160
- const delay = (ms: number) =>
161
- new Promise(resolve => setTimeout(resolve, ms));
162
- await delay(5000);
163
-
164
- const migrateTx = new Transaction();
165
- migrateTx.moveCall({
166
- target: `${newPackageId}::migrate::run`,
167
- arguments: [migrateTx.object(worldId), migrateTx.object(adminCap)],
168
- });
169
-
170
- let newWorldObject = await client.getObject({
171
- id: worldId,
172
- options: {
173
- showContent: true,
174
- showDisplay: true,
175
- showType: true,
176
- showOwner: true,
177
- },
178
- });
179
- let newObjectContent = newWorldObject.data!.content as ObjectContent;
180
-
181
- const uniqueSchema: string[] = schemaNames.filter(
182
- item => !newObjectContent.fields['schema_names'].includes(item)
183
- );
184
-
185
- console.log('new schema:', uniqueSchema);
186
- let needRegisterSchema = [];
187
- for (const newSchema of uniqueSchema) {
188
- migrateTx.moveCall({
189
- target: `${newPackageId}::${newSchema}_schema::register`,
190
- arguments: [
191
- migrateTx.object(worldId),
192
- migrateTx.object(adminCap),
193
- ],
194
- });
195
- needRegisterSchema.push(`${newSchema}_schema`);
196
- }
197
- const migrateResult = await client.signAndExecuteTransaction({
198
- signer: keypair,
199
- transaction: migrateTx,
200
- options: {
201
- showEffects: true,
202
- },
203
- });
204
-
205
- if (migrateResult.effects?.status.status === 'success') {
206
- console.log(
207
- chalk.green(
208
- `${name} migrate world success, new world version is: ${newObjectContent.fields['version']}, package version is ${newVersion}`
209
- )
210
- );
211
- if (needRegisterSchema.length !== 0) {
212
- console.log(
213
- chalk.green(
214
- `new schema: ${needRegisterSchema.toString()} register success.`
215
- )
216
- );
217
- }
218
-
219
- console.log(
220
- chalk.blue(
221
- `\n${name} world schemas is ${newObjectContent.fields['schema_names']}`
222
- )
223
- );
224
- } else {
225
- console.log(
226
- chalk.red(
227
- `${name} migrate world failed, world version is: ${newObjectContent.fields['version']}, package version is ${newVersion}`
228
- )
229
- );
230
- }
231
- } catch (error: any) {
232
- console.log(chalk.red('Upgrade failed!'));
233
- console.error(error.message);
234
-
235
- saveContractData(
236
- name,
237
- network,
238
- oldPackageId,
239
- worldId,
240
- upgradeCap,
241
- adminCap,
242
- oldVersion
243
- );
244
- await updateVersionInFile(projectPath, oldVersion.toString());
245
- }
246
- }
1
+ // import { Obelisk } from '@0xobelisk/sui-client';
2
+ // import { Transaction, UpgradePolicy } from '@mysten/sui/transactions';
3
+ // import { getFullnodeUrl, SuiClient } from '@mysten/sui/client';
4
+ // import { execSync } from 'child_process';
5
+ // import chalk from 'chalk';
6
+ // import { ObeliskCliError, UpgradeError } from './errors';
7
+ // import {
8
+ // updateVersionInFile,
9
+ // getOldPackageId,
10
+ // getVersion,
11
+ // getUpgradeCap,
12
+ // saveContractData,
13
+ // validatePrivateKey,
14
+ // } from './utils';
15
+
16
+ // type ObjectContent = {
17
+ // type: string;
18
+ // fields: Record<string, any>;
19
+ // hasPublicTransfer: boolean;
20
+ // dataType: string;
21
+ // };
22
+
23
+ // export async function upgradeHandler(
24
+ // name: string,
25
+ // network: 'mainnet' | 'testnet' | 'devnet' | 'localnet',
26
+ // schemaNames: string[]
27
+ // ) {
28
+ // const path = process.cwd();
29
+ // const projectPath = `${path}/contracts/${name}`;
30
+ // const privateKey = process.env.PRIVATE_KEY;
31
+ // if (!privateKey)
32
+ // throw new ObeliskCliError(
33
+ // `Missing PRIVATE_KEY environment variable.
34
+ // Run 'echo "PRIVATE_KEY=YOUR_PRIVATE_KEY" > .env'
35
+ // in your contracts directory to use the default sui private key.`
36
+ // );
37
+
38
+ // const privateKeyFormat = validatePrivateKey(privateKey);
39
+ // if (privateKeyFormat === false) {
40
+ // throw new ObeliskCliError(`Please check your privateKey.`);
41
+ // }
42
+ // const obelisk = new Obelisk({
43
+ // secretKey: privateKeyFormat,
44
+ // });
45
+ // const keypair = obelisk.getKeypair();
46
+
47
+ // const client = new SuiClient({
48
+ // url: getFullnodeUrl(network),
49
+ // });
50
+
51
+ // let oldVersion = Number(await getVersion(projectPath, network));
52
+ // let oldPackageId = await getOldPackageId(projectPath, network);
53
+ // let worldId = await getWorldId(projectPath, network);
54
+ // let upgradeCap = await getUpgradeCap(projectPath, network);
55
+ // let adminCap = await getAdminCap(projectPath, network);
56
+
57
+ // const newVersion = oldVersion + 1;
58
+ // await updateVersionInFile(projectPath, newVersion.toString());
59
+
60
+ // try {
61
+ // let modules: any, dependencies: any, digest: any;
62
+ // try {
63
+ // const {
64
+ // modules: extractedModules,
65
+ // dependencies: extractedDependencies,
66
+ // digest: extractedDigest,
67
+ // } = JSON.parse(
68
+ // execSync(
69
+ // `sui move build --dump-bytecode-as-base64 --path ${path}/contracts/${name}`,
70
+ // {
71
+ // encoding: 'utf-8',
72
+ // }
73
+ // )
74
+ // );
75
+
76
+ // modules = extractedModules;
77
+ // dependencies = extractedDependencies;
78
+ // digest = extractedDigest;
79
+ // } catch (error: any) {
80
+ // throw new UpgradeError(error.stdout);
81
+ // }
82
+
83
+ // const tx = new Transaction();
84
+ // const ticket = tx.moveCall({
85
+ // target: '0x2::package::authorize_upgrade',
86
+ // arguments: [
87
+ // tx.object(upgradeCap),
88
+ // tx.pure.u8(UpgradePolicy.COMPATIBLE),
89
+ // tx.pure.vector('u8', digest),
90
+ // ],
91
+ // });
92
+
93
+ // const receipt = tx.upgrade({
94
+ // modules,
95
+ // dependencies,
96
+ // package: oldPackageId,
97
+ // ticket,
98
+ // });
99
+
100
+ // tx.moveCall({
101
+ // target: '0x2::package::commit_upgrade',
102
+ // arguments: [tx.object(upgradeCap), receipt],
103
+ // });
104
+
105
+ // tx.transferObjects(
106
+ // [tx.object(upgradeCap)],
107
+ // keypair.toSuiAddress()
108
+ // );
109
+
110
+ // const result = await client.signAndExecuteTransaction({
111
+ // signer: keypair,
112
+ // transaction: tx,
113
+ // options: {
114
+ // showObjectChanges: true,
115
+ // },
116
+ // });
117
+
118
+ // console.log('');
119
+ // console.log(`${name} WorldId: ${worldId}`);
120
+
121
+ // let newPackageId = '';
122
+ // let newUpgradeCap = '';
123
+ // result.objectChanges!.map(object => {
124
+ // if (object.type === 'published') {
125
+ // console.log(
126
+ // chalk.blue(`${name} PackageId: ${object.packageId}`)
127
+ // );
128
+ // newPackageId = object.packageId;
129
+ // }
130
+ // if (
131
+ // object.type === 'mutated' &&
132
+ // object.objectType === '0x2::package::UpgradeCap'
133
+ // ) {
134
+ // console.log(
135
+ // chalk.blue(`${name} UpgradeCap: ${object.objectId}`)
136
+ // );
137
+ // newUpgradeCap = object.objectId;
138
+ // }
139
+ // });
140
+
141
+ // console.log(
142
+ // chalk.green(`Upgrade Transaction Digest: ${result.digest}`)
143
+ // );
144
+
145
+ // saveContractData(
146
+ // name,
147
+ // network,
148
+ // newPackageId,
149
+ // worldId,
150
+ // newUpgradeCap,
151
+ // adminCap,
152
+ // newVersion
153
+ // );
154
+
155
+ // oldPackageId = newPackageId;
156
+ // upgradeCap = newUpgradeCap;
157
+ // oldVersion = newVersion;
158
+
159
+ // console.log('\nExecuting the migrate: ');
160
+ // const delay = (ms: number) =>
161
+ // new Promise(resolve => setTimeout(resolve, ms));
162
+ // await delay(5000);
163
+
164
+ // const migrateTx = new Transaction();
165
+ // migrateTx.moveCall({
166
+ // target: `${newPackageId}::migrate::run`,
167
+ // arguments: [migrateTx.object(worldId), migrateTx.object(adminCap)],
168
+ // });
169
+
170
+ // let newWorldObject = await client.getObject({
171
+ // id: worldId,
172
+ // options: {
173
+ // showContent: true,
174
+ // showDisplay: true,
175
+ // showType: true,
176
+ // showOwner: true,
177
+ // },
178
+ // });
179
+ // let newObjectContent = newWorldObject.data!.content as ObjectContent;
180
+
181
+ // const uniqueSchema: string[] = schemaNames.filter(
182
+ // item => !newObjectContent.fields['schema_names'].includes(item)
183
+ // );
184
+
185
+ // console.log('new schema:', uniqueSchema);
186
+ // let needRegisterSchema = [];
187
+ // for (const newSchema of uniqueSchema) {
188
+ // migrateTx.moveCall({
189
+ // target: `${newPackageId}::${newSchema}_schema::register`,
190
+ // arguments: [
191
+ // migrateTx.object(worldId),
192
+ // migrateTx.object(adminCap),
193
+ // ],
194
+ // });
195
+ // needRegisterSchema.push(`${newSchema}_schema`);
196
+ // }
197
+ // const migrateResult = await client.signAndExecuteTransaction({
198
+ // signer: keypair,
199
+ // transaction: migrateTx,
200
+ // options: {
201
+ // showEffects: true,
202
+ // },
203
+ // });
204
+
205
+ // if (migrateResult.effects?.status.status === 'success') {
206
+ // console.log(
207
+ // chalk.green(
208
+ // `${name} migrate world success, new world version is: ${newObjectContent.fields['version']}, package version is ${newVersion}`
209
+ // )
210
+ // );
211
+ // if (needRegisterSchema.length !== 0) {
212
+ // console.log(
213
+ // chalk.green(
214
+ // `new schema: ${needRegisterSchema.toString()} register success.`
215
+ // )
216
+ // );
217
+ // }
218
+
219
+ // console.log(
220
+ // chalk.blue(
221
+ // `\n${name} world schemas is ${newObjectContent.fields['schema_names']}`
222
+ // )
223
+ // );
224
+ // } else {
225
+ // console.log(
226
+ // chalk.red(
227
+ // `${name} migrate world failed, world version is: ${newObjectContent.fields['version']}, package version is ${newVersion}`
228
+ // )
229
+ // );
230
+ // }
231
+ // } catch (error: any) {
232
+ // console.log(chalk.red('Upgrade failed!'));
233
+ // console.error(error.message);
234
+
235
+ // saveContractData(
236
+ // name,
237
+ // network,
238
+ // oldPackageId,
239
+ // worldId,
240
+ // upgradeCap,
241
+ // adminCap,
242
+ // oldVersion
243
+ // );
244
+ // await updateVersionInFile(projectPath, oldVersion.toString());
245
+ // }
246
+ // }
@@ -3,17 +3,18 @@ import { mkdirSync, writeFileSync } from 'fs';
3
3
  import { dirname } from 'path';
4
4
  import { SUI_PRIVATE_KEY_PREFIX } from '@mysten/sui/cryptography';
5
5
  import { FsIibError } from './errors';
6
+ export * from './localnode';
6
7
 
7
8
  export type schema = {
8
- name: string,
9
- objectId: string
10
- }
9
+ name: string;
10
+ objectId: string;
11
+ };
11
12
 
12
13
  export type DeploymentJsonType = {
13
14
  projectName: string;
14
15
  network: 'mainnet' | 'testnet' | 'devnet' | 'localnet';
15
16
  packageId: string;
16
- schemas: schema[]
17
+ schemas: schema[];
17
18
  upgradeCap: string;
18
19
  version: number;
19
20
  };
@@ -112,7 +113,8 @@ export async function getObjectIdBySchemaName(
112
113
  schemaName: string
113
114
  ): Promise<string | undefined> {
114
115
  const deployment = await getDeploymentJson(projectPath, network);
115
- return deployment.schemas.find((schema) => schema.name.includes(schemaName))?.objectId;
116
+ return deployment.schemas.find(schema => schema.name.includes(schemaName))
117
+ ?.objectId;
116
118
  }
117
119
 
118
120
  export function saveContractData(