-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·244 lines (214 loc) · 9.49 KB
/
index.js
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
#!/usr/bin/env node
const { Command } = require('commander');
const inquirer = require('inquirer');
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const program = new Command();
const os = require('os');
program
.name('ston')
.description('A CLI tool for managing tokens')
.version('1.0.0');
program
.command('init')
.description('Initialize a new token operation')
.action(async () => {
console.log(`
d888888888 888888888888888 d888888888b d8888b 88b
888 888 888 888 888 88 888
888 888 888 888 888 88 888
"88888888b 888 888 888 888 88 888
888 888 888 888 888 88 888
888 888 888 888 888 88888
888888888" 888 "888888888" 888 "88P
💎 Welcome to STON CLI 💎
This tool helps you manage your token operations.\n
`);
const answers = await inquirer.prompt([
{
type: 'list',
name: 'operation',
message: 'Choose an operation:',
choices: [
'Create a new ERC721 token backed to WSTON',
'Create a new ERC1155 token backed to WSTON',
'Link an existing ERC721 token to WSTON',
],
},
]);
const cloneAndMove = (repoUrl) => {
const tempDir = path.join(os.tmpdir(), 'temp-repo');
try {
if (fs.existsSync(tempDir)) {
fs.rmdirSync(tempDir, { recursive: true });
}
execSync(`git clone ${repoUrl} ${tempDir}`, { stdio: 'inherit' });
fs.readdirSync(tempDir).forEach(file => {
const srcPath = path.join(tempDir, file);
const destPath = path.join(process.cwd(), file);
if (fs.existsSync(destPath)) {
console.log(`Skipping existing file or directory: ${file}`);
return;
}
fs.renameSync(srcPath, destPath);
});
fs.rmdirSync(tempDir, { recursive: true });
console.log('Environment imported successfully.');
} catch (error) {
console.error('Error importing the repository:', error.message);
}
};
const copyScripts = () => {
const scripts = ['1.updateERC721.js', '2.createTreasury.js'];
const scriptsDir = path.join(process.cwd(), 'scripts'); // Path to the "scripts" folder
// Create the "scripts" folder if it doesn't exist
if (!fs.existsSync(scriptsDir)) {
fs.mkdirSync(scriptsDir, { recursive: true });
console.log('Created "scripts" folder.');
}
scripts.forEach(script => {
const srcPath = path.join(__dirname, script); // Source path of the script
const destPath = path.join(scriptsDir, script); // Destination path in the "scripts" folder
if (!fs.existsSync(srcPath)) {
console.error(`Script ${script} not found in the current directory.`);
return;
}
if (fs.existsSync(destPath)) {
console.log(`Skipping existing script: ${script}`);
return;
}
fs.copyFileSync(srcPath, destPath); // Copy the script to the "scripts" folder
console.log(`Copied ${script} to the "scripts" folder.`);
});
console.log('Scripts copied successfully.');
};
switch (answers.operation) {
case 'Create a new ERC721 token backed to WSTON':
console.log('Creating a new ERC721 token backed to WSTON...');
cloneAndMove('https://github.com/tokamak-network/new-ERC721-template.git');
break;
case 'Create a new ERC1155 token backed to WSTON':
console.log('Creating a new ERC1155 token backed to WSTON...');
cloneAndMove('https://github.com/tokamak-network/new-ERC1155-template.git');
break;
case 'Link an existing ERC721 token to WSTON':
console.log('Linking an existing ERC721 token to WSTON...');
copyScripts();
break;
default:
console.log('Invalid operation');
}
});
program
.command('deploy')
.description('Deploy the smart contract')
.action(async () => {
try {
const networkConfig = {
optimism: {
bridgeScript: 'scripts/optimism/2.bridgeWstonToOptimism.js',
bridgeNetwork: 'l1', // Network for the bridge script
additionalScripts: [
{ script: 'scripts/optimism/3.deploySTON.js', network: 'l2' },
{ script: 'scripts/optimism/4.Initialization.js', network: 'l2' }
]
},
arbitrum: {
bridgeScript: 'scripts/arbitrum/2.bridgeWstonToArbitrum.js',
bridgeNetwork: 'l1', // Network for the bridge script
additionalScripts: [
{ script: 'scripts/arbitrum/3.deploySTON.js', network: 'l2' },
{ script: 'scripts/arbitrum/4.Initialization.js', network: 'l2' }
]
},
zkSync: {
bridgeScript: 'scripts/zksync/3.bridgeWstonToZkSync.js',
bridgeNetwork: 'l1', // Network for the bridge script
additionalScripts: [
{ script: 'scripts/zksync/4.deploySTON.js', network: 'l2' },
{ script: 'scripts/zksync/5.Initialization.js', network: 'l2' }
]
},
trh: {
bridgeScript: 'scripts/trh/2.bridgeWstonToTrh.js',
bridgeNetwork: 'l1', // Network for the bridge script
additionalScripts: [
{ script: 'scripts/trh/3.deploySTON.js', network: 'l2' },
{ script: 'scripts/trh/4.Initialization.js', network: 'l2' }
]
}
};
const { network } = await inquirer.prompt([
{
type: 'list',
name: 'network',
message: 'Select deployment network:',
choices: Object.keys(networkConfig),
},
]);
const config = networkConfig[network];
console.log(`
┌──────────────────────────────────────────────────────┐
│ 🚀 Deployment Checklist │
├──────────────────────────────────────────────────────┤
│ 1. .env file configured with correct parameters │
│ 2. L2 WSTON contract deployed on ${network.padEnd(10)} │
│ 3. Sufficient balance in deployment account │
└──────────────────────────────────────────────────────┘\n`);
// Check for existing node_modules
if (!fs.existsSync('node_modules')) {
console.log('Installing dependencies... 📦');
try {
execSync('yarn install --network-timeout 600000', {
stdio: 'inherit',
env: {
...process.env,
NODE_ENV: 'production'
}
});
} catch (error) {
console.error('\n⚠️ Dependency installation failed. Trying with npm...');
execSync('npm install --legacy-peer-deps', { stdio: 'inherit' });
}
}
if (!fs.existsSync('artifacts')) {
console.log('\nCompiling contracts... ⚙️');
execSync('npx hardhat compile', { stdio: 'inherit' });
}
console.log(`\nInitiating ${network} deployment... 🌐`);
execSync(`npx hardhat run ${config.bridgeScript} --network ${config.bridgeNetwork}`, {
stdio: 'inherit',
env: {
...process.env,
DEPLOYMENT_NETWORK: network.toUpperCase()
}
});
// Run additional scripts in their respective networks
if (config.additionalScripts && config.additionalScripts.length > 0) {
console.log('\nDEPLOYING STON ON THE TARGETED NETWORK... 🛠️');
for (const { script, network: scriptNetwork } of config.additionalScripts) {
console.log(`\nRunning script: ${script} on network: ${scriptNetwork}`);
execSync(`npx hardhat run ${script} --network ${scriptNetwork}`, {
stdio: 'inherit',
env: {
...process.env,
DEPLOYMENT_NETWORK: scriptNetwork.toUpperCase()
}
});
}
}
console.log(`
┌──────────────────────────────────────────────────────────────────┐
│ ✅ Successfully deployed to ${network.padEnd(10)} │
└──────────────────────────────────────────────────────────────────┘`);
} catch (error) {
console.error(`
┌──────────────────────────────────────────────────────────────────────────────┐
│ 🚨 Deployment failed: ${error.message.slice(0,40).padEnd(45)} │
└──────────────────────────────────────────────────────────────────────────────┘`);
process.exit(1);
}
});
program.parse(process.argv);