@0xobelisk/sui-cli 1.1.5 ā 1.1.7
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.
- package/README.md +0 -2
- package/dist/dubhe.js.map +1 -1
- package/package.json +11 -7
- package/src/commands/build.ts +49 -49
- package/src/commands/call.ts +71 -71
- package/src/commands/checkBalance.ts +20 -20
- package/src/commands/configStore.ts +31 -35
- package/src/commands/faucet.ts +63 -71
- package/src/commands/generateKey.ts +25 -25
- package/src/commands/hello.ts +5 -5
- package/src/commands/index.ts +15 -15
- package/src/commands/indexer.ts +41 -46
- package/src/commands/localnode.ts +13 -13
- package/src/commands/publish.ts +35 -39
- package/src/commands/query.ts +70 -70
- package/src/commands/schemagen.ts +26 -26
- package/src/commands/test.ts +37 -37
- package/src/commands/upgrade.ts +29 -29
- package/src/commands/watch.ts +12 -12
- package/src/dubhe.ts +14 -12
- package/src/modules.d.ts +2 -2
- package/src/utils/callHandler.ts +117 -122
- package/src/utils/checkBalance.ts +25 -33
- package/src/utils/errors.ts +28 -28
- package/src/utils/generateAccount.ts +76 -91
- package/src/utils/indexerHandler.ts +183 -151
- package/src/utils/printDubhe.ts +1 -1
- package/src/utils/publishHandler.ts +322 -371
- package/src/utils/queryStorage.ts +126 -126
- package/src/utils/startNode.ts +99 -111
- package/src/utils/storeConfig.ts +33 -45
- package/src/utils/upgradeHandler.ts +208 -234
- package/src/utils/utils.ts +150 -176
|
@@ -5,259 +5,233 @@ import { execSync } from 'child_process';
|
|
|
5
5
|
import chalk from 'chalk';
|
|
6
6
|
import { DubheCliError, UpgradeError } from './errors';
|
|
7
7
|
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
8
|
+
getOldPackageId,
|
|
9
|
+
getVersion,
|
|
10
|
+
getUpgradeCap,
|
|
11
|
+
saveContractData,
|
|
12
|
+
validatePrivateKey,
|
|
13
|
+
getOnchainSchemas,
|
|
14
|
+
switchEnv,
|
|
15
|
+
getSchemaId
|
|
15
16
|
} from './utils';
|
|
16
17
|
import * as fs from 'fs';
|
|
17
18
|
import * as path from 'path';
|
|
18
19
|
import { DubheConfig } from '@0xobelisk/sui-common';
|
|
19
20
|
|
|
20
21
|
type Migration = {
|
|
21
|
-
|
|
22
|
-
|
|
22
|
+
schemaName: string;
|
|
23
|
+
fields: string;
|
|
23
24
|
};
|
|
24
25
|
|
|
25
|
-
function updateMigrateMethod(
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
`public fun migrate\\(_schema: &mut Schema, _cap: &UpgradeCap, _ctx: &mut TxContext\\) {[^}]*}`
|
|
33
|
-
);
|
|
34
|
-
const newMigrateMethod = `
|
|
26
|
+
function updateMigrateMethod(projectPath: string, migrations: Migration[]): void {
|
|
27
|
+
let filePath = `${projectPath}/sources/codegen/schema.move`;
|
|
28
|
+
const fileContent = fs.readFileSync(filePath, 'utf-8');
|
|
29
|
+
const migrateMethodRegex = new RegExp(
|
|
30
|
+
`public fun migrate\\(_schema: &mut Schema, _cap: &UpgradeCap, _ctx: &mut TxContext\\) {[^}]*}`
|
|
31
|
+
);
|
|
32
|
+
const newMigrateMethod = `
|
|
35
33
|
public fun migrate(_schema: &mut Schema, _cap: &UpgradeCap, _ctx: &mut TxContext) {
|
|
36
|
-
${migrations
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
34
|
+
${migrations
|
|
35
|
+
.map((migration) => {
|
|
36
|
+
let storage_type = '';
|
|
37
|
+
if (migration.fields.includes('StorageValue')) {
|
|
38
|
+
storage_type = `storage_value::new(b"${migration.schemaName}", _ctx)`;
|
|
39
|
+
} else if (migration.fields.includes('StorageMap')) {
|
|
40
|
+
storage_type = `storage_map::new(b"${migration.schemaName}", _ctx)`;
|
|
41
|
+
} else if (migration.fields.includes('StorageDoubleMap')) {
|
|
42
|
+
storage_type = `storage_double_map::new(b"${migration.schemaName}", _ctx)`;
|
|
43
|
+
}
|
|
44
|
+
return `storage::add_field<${migration.fields}>(&mut _schema.id, b"${migration.schemaName}", ${storage_type});`;
|
|
45
|
+
})
|
|
46
|
+
.join('')}
|
|
48
47
|
}
|
|
49
48
|
`;
|
|
50
49
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
newMigrateMethod
|
|
54
|
-
);
|
|
55
|
-
fs.writeFileSync(filePath, updatedContent, 'utf-8');
|
|
50
|
+
const updatedContent = fileContent.replace(migrateMethodRegex, newMigrateMethod);
|
|
51
|
+
fs.writeFileSync(filePath, updatedContent, 'utf-8');
|
|
56
52
|
}
|
|
57
53
|
|
|
58
54
|
function replaceEnvField(
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
| 'latest-published-id'
|
|
64
|
-
| 'published-version',
|
|
65
|
-
newValue: string
|
|
55
|
+
filePath: string,
|
|
56
|
+
networkType: 'mainnet' | 'testnet' | 'devnet' | 'localnet',
|
|
57
|
+
field: 'original-published-id' | 'latest-published-id' | 'published-version',
|
|
58
|
+
newValue: string
|
|
66
59
|
): string {
|
|
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
|
-
return previousValue;
|
|
60
|
+
const envFilePath = path.resolve(filePath);
|
|
61
|
+
const envContent = fs.readFileSync(envFilePath, 'utf-8');
|
|
62
|
+
const envLines = envContent.split('\n');
|
|
63
|
+
|
|
64
|
+
const networkSectionIndex = envLines.findIndex((line) => line.trim() === `[env.${networkType}]`);
|
|
65
|
+
if (networkSectionIndex === -1) {
|
|
66
|
+
console.log(`Network type [env.${networkType}] not found in the file.`);
|
|
67
|
+
return '';
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let fieldIndex = -1;
|
|
71
|
+
let previousValue: string = '';
|
|
72
|
+
for (let i = networkSectionIndex + 1; i < envLines.length; i++) {
|
|
73
|
+
const line = envLines[i].trim();
|
|
74
|
+
if (line.startsWith('[')) break; // End of the current network section
|
|
75
|
+
|
|
76
|
+
if (line.startsWith(field)) {
|
|
77
|
+
fieldIndex = i;
|
|
78
|
+
previousValue = line.split('=')[1].trim().replace(/"/g, '');
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (fieldIndex !== -1) {
|
|
84
|
+
envLines[fieldIndex] = `${field} = "${newValue}"`;
|
|
85
|
+
const newEnvContent = envLines.join('\n');
|
|
86
|
+
fs.writeFileSync(envFilePath, newEnvContent, 'utf-8');
|
|
87
|
+
} else {
|
|
88
|
+
console.log(`${field} not found for [env.${networkType}].`);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return previousValue;
|
|
101
92
|
}
|
|
102
93
|
export async function upgradeHandler(
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
94
|
+
config: DubheConfig,
|
|
95
|
+
name: string,
|
|
96
|
+
network: 'mainnet' | 'testnet' | 'devnet' | 'localnet'
|
|
106
97
|
) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
98
|
+
await switchEnv(network);
|
|
99
|
+
|
|
100
|
+
const path = process.cwd();
|
|
101
|
+
const projectPath = `${path}/contracts/${name}`;
|
|
102
|
+
const privateKey = process.env.PRIVATE_KEY;
|
|
103
|
+
if (!privateKey)
|
|
104
|
+
throw new DubheCliError(
|
|
105
|
+
`Missing PRIVATE_KEY environment variable.
|
|
115
106
|
Run 'echo "PRIVATE_KEY=YOUR_PRIVATE_KEY" > .env'
|
|
116
107
|
in your contracts directory to use the default sui private key.`
|
|
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
|
-
console.log(
|
|
247
|
-
chalk.green(`Upgrade Transaction Digest: ${result.digest}`)
|
|
248
|
-
);
|
|
249
|
-
|
|
250
|
-
saveContractData(
|
|
251
|
-
name,
|
|
252
|
-
network,
|
|
253
|
-
newPackageId,
|
|
254
|
-
schemaId,
|
|
255
|
-
upgradeCap,
|
|
256
|
-
oldVersion + 1,
|
|
257
|
-
config.schemas
|
|
258
|
-
);
|
|
259
|
-
} catch (error: any) {
|
|
260
|
-
console.log(chalk.red('Upgrade failed!'));
|
|
261
|
-
console.error(error.message);
|
|
262
|
-
}
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
const privateKeyFormat = validatePrivateKey(privateKey);
|
|
111
|
+
if (privateKeyFormat === false) {
|
|
112
|
+
throw new DubheCliError(`Please check your privateKey.`);
|
|
113
|
+
}
|
|
114
|
+
const dubhe = new Dubhe({
|
|
115
|
+
secretKey: privateKeyFormat
|
|
116
|
+
});
|
|
117
|
+
const keypair = dubhe.getSigner();
|
|
118
|
+
|
|
119
|
+
const client = new SuiClient({
|
|
120
|
+
url: getFullnodeUrl(network)
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
let oldVersion = Number(await getVersion(projectPath, network));
|
|
124
|
+
let oldPackageId = await getOldPackageId(projectPath, network);
|
|
125
|
+
let upgradeCap = await getUpgradeCap(projectPath, network);
|
|
126
|
+
let schemaId = await getSchemaId(projectPath, network);
|
|
127
|
+
|
|
128
|
+
const original_published_id = replaceEnvField(
|
|
129
|
+
`${projectPath}/Move.lock`,
|
|
130
|
+
network,
|
|
131
|
+
'original-published-id',
|
|
132
|
+
'0x0000000000000000000000000000000000000000000000000000000000000000'
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
let pendingMigration: Migration[] = [];
|
|
136
|
+
let schemas = await getOnchainSchemas(projectPath, network);
|
|
137
|
+
Object.entries(config.schemas).forEach(([key, value]) => {
|
|
138
|
+
if (!schemas.hasOwnProperty(key)) {
|
|
139
|
+
pendingMigration.push({ schemaName: key, fields: value });
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
pendingMigration.forEach((migration) => {
|
|
144
|
+
console.log(`\nš Starting Migration for ${migration.schemaName}...`);
|
|
145
|
+
console.log('š Migration Fields:', migration.fields);
|
|
146
|
+
});
|
|
147
|
+
updateMigrateMethod(projectPath, pendingMigration);
|
|
148
|
+
|
|
149
|
+
try {
|
|
150
|
+
let modules: any, dependencies: any, digest: any;
|
|
151
|
+
try {
|
|
152
|
+
const {
|
|
153
|
+
modules: extractedModules,
|
|
154
|
+
dependencies: extractedDependencies,
|
|
155
|
+
digest: extractedDigest
|
|
156
|
+
} = JSON.parse(
|
|
157
|
+
execSync(`sui move build --dump-bytecode-as-base64 --path ${path}/contracts/${name}`, {
|
|
158
|
+
encoding: 'utf-8'
|
|
159
|
+
})
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
modules = extractedModules;
|
|
163
|
+
dependencies = extractedDependencies;
|
|
164
|
+
digest = extractedDigest;
|
|
165
|
+
} catch (error: any) {
|
|
166
|
+
throw new UpgradeError(error.stdout);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
console.log('\nš Starting Upgrade Process...');
|
|
170
|
+
console.log('š OldPackageId:', oldPackageId);
|
|
171
|
+
console.log('š UpgradeCap Object Id:', upgradeCap);
|
|
172
|
+
console.log('š OldVersion:', oldVersion);
|
|
173
|
+
|
|
174
|
+
const tx = new Transaction();
|
|
175
|
+
const ticket = tx.moveCall({
|
|
176
|
+
target: '0x2::package::authorize_upgrade',
|
|
177
|
+
arguments: [
|
|
178
|
+
tx.object(upgradeCap),
|
|
179
|
+
tx.pure.u8(UpgradePolicy.COMPATIBLE),
|
|
180
|
+
tx.pure.vector('u8', digest)
|
|
181
|
+
]
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
const receipt = tx.upgrade({
|
|
185
|
+
modules,
|
|
186
|
+
dependencies,
|
|
187
|
+
package: oldPackageId,
|
|
188
|
+
ticket
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
tx.moveCall({
|
|
192
|
+
target: '0x2::package::commit_upgrade',
|
|
193
|
+
arguments: [tx.object(upgradeCap), receipt]
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
const result = await client.signAndExecuteTransaction({
|
|
197
|
+
signer: keypair,
|
|
198
|
+
transaction: tx,
|
|
199
|
+
options: {
|
|
200
|
+
showObjectChanges: true
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
let newPackageId = '';
|
|
205
|
+
result.objectChanges!.map((object) => {
|
|
206
|
+
if (object.type === 'published') {
|
|
207
|
+
console.log(chalk.blue(`${name} PackageId: ${object.packageId}`));
|
|
208
|
+
console.log(chalk.blue(`${name} Version: ${oldVersion + 1}`));
|
|
209
|
+
newPackageId = object.packageId;
|
|
210
|
+
}
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
replaceEnvField(
|
|
214
|
+
`${projectPath}/Move.lock`,
|
|
215
|
+
network,
|
|
216
|
+
'original-published-id',
|
|
217
|
+
original_published_id
|
|
218
|
+
);
|
|
219
|
+
replaceEnvField(`${projectPath}/Move.lock`, network, 'latest-published-id', newPackageId);
|
|
220
|
+
replaceEnvField(`${projectPath}/Move.lock`, network, 'published-version', oldVersion + 1 + '');
|
|
221
|
+
|
|
222
|
+
console.log(chalk.green(`Upgrade Transaction Digest: ${result.digest}`));
|
|
223
|
+
|
|
224
|
+
saveContractData(
|
|
225
|
+
name,
|
|
226
|
+
network,
|
|
227
|
+
newPackageId,
|
|
228
|
+
schemaId,
|
|
229
|
+
upgradeCap,
|
|
230
|
+
oldVersion + 1,
|
|
231
|
+
config.schemas
|
|
232
|
+
);
|
|
233
|
+
} catch (error: any) {
|
|
234
|
+
console.log(chalk.red('Upgrade failed!'));
|
|
235
|
+
console.error(error.message);
|
|
236
|
+
}
|
|
263
237
|
}
|