-
Notifications
You must be signed in to change notification settings - Fork 48
/
deploy.js
executable file
·382 lines (349 loc) · 10.6 KB
/
deploy.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
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
#!/usr/bin/env node
const fs = require('fs-extra');
const path = require('path');
const execa = require('execa');
const AWS = require('aws-sdk');
const chalk = require('chalk');
let credentials = {};
const setAWSCredentials = async () => {
try {
const credentialProviderChain = new AWS.CredentialProviderChain();
credentials = await credentialProviderChain.resolvePromise();
} catch (e) {
console.error('Failed to get AWS credentials');
throw e;
}
};
const SETTINGS = path.resolve(__dirname, 'deploy-settings.json');
const PATHS = {
contract: path.resolve(__dirname, './contract'),
provision: path.resolve(__dirname, './provision'),
marketplace: path.resolve(__dirname, './marketplace'),
stackOutputs: path.resolve(__dirname, './provision/stack-outputs.json'),
marketplaceEnv: path.resolve(__dirname, './marketplace/.env.local'),
lambdaContract: path.resolve(
__dirname,
'./provision/lambda/contracts/SimpleERC721.json'
),
compiledContract: path.resolve(
__dirname,
'./contract/artifacts/contracts/SimpleERC721.sol/SimpleERC721.json'
),
};
const logProgress = (description, complete = false) => {
let msg = '';
complete
? (msg = `🙌 ${description} Complete `)
: (msg = `⌛ ${description} \n${'-'.repeat(process.stdout.columns)}`);
console.log(chalk.bgGreen.bold.white(msg));
};
const keypress = async () => {
process.stdin.setRawMode(true);
return new Promise(resolve =>
process.stdin.once('data', () => {
process.stdin.setRawMode(false);
resolve();
})
);
};
/**
* @param {string} command
* @param {execa.Options<string>} options
*/
const commandWithPipe = (command, options = {}) => {
const cmd = execa.command(command, options);
cmd.stdout.pipe(process.stdout);
cmd.stderr.pipe(process.stderr);
return cmd;
};
const writeToSettings = async (key, value) => {
await fs.ensureFile(SETTINGS);
const settings = await fs.readJson(SETTINGS, { throws: false });
await fs.writeJSON(SETTINGS, { ...settings, [key]: value });
};
const getFromSettings = async key => {
try {
await fs.ensureFile(SETTINGS);
const settings = await fs.readJson(SETTINGS, { throws: false });
return settings && settings[key];
} catch (e) {
console.error('Failed to read settings file');
throw e;
}
};
const getFromStackOutput = async (stack, key) => {
try {
await fs.ensureFile(PATHS.stackOutputs);
const outputs = await fs.readJSON(PATHS.stackOutputs);
return outputs[stack][key];
} catch (e) {
console.error('Failed to read from stack outputs');
throw e;
}
};
const copyStackOutputToSettings = async (stack, outputKey, settingsKey) => {
const stackOutputValue = await getFromStackOutput(stack, outputKey);
await writeToSettings(settingsKey, stackOutputValue);
};
const markAsComplete = async name => {
await writeToSettings(name, true);
};
const checkBin = async ({ bin, install }) => {
console.log(`Checking if ${bin} is installed`);
try {
await execa.command(`${bin} --version`);
console.log(`${bin} found, skipping install`);
return true;
} catch (e) {
console.log(`${bin} not found, installing with "${install}"`);
await execa.command(install);
return false;
}
};
const checkDependencies = async () => {
logProgress('Checking dependencies');
await checkBin({ bin: 'cdk', install: 'npm install -g aws-cdk' });
logProgress('Checking dependencies', true);
};
const compileContract = async () => {
logProgress('Compile Contract');
await commandWithPipe('npx hardhat compile', {
cwd: PATHS.contract,
});
await fs.copy(PATHS.compiledContract, PATHS.lambdaContract, {
overwrite: true,
});
logProgress('Compile Contract', true);
};
const createAccount = async () => {
logProgress('Create Account');
const account = commandWithPipe('npx hardhat account', {
cwd: PATHS.contract,
});
const { stdout } = await account;
const [address] = stdout.match(/(0x[a-fA-F0-9]{40})/);
const [privateKey] = stdout.match(/(0x[a-fA-F0-9]{64})/);
if (!address) throw new Error('Unable to parse ethereum address');
if (!privateKey) throw new Error('Unable to parse ethereum private key');
await writeToSettings('address', address);
await writeToSettings('privateKey', privateKey);
logProgress('Create Account', true);
};
const deployAmbNode = async () => {
logProgress('Deploy Amazon Managed Blockchain Node');
console.log(
chalk.green.bold(
'Press any key to begin deploying AMB node. NOTE: This can take up to 30 minutes.'
)
);
await keypress();
await commandWithPipe('cdk bootstrap', {
cwd: PATHS.provision,
env: {
// Placeholder values to prevent synth from failing
AMB_HTTP_ENDPOINT: 'placeholder',
CONTRACT_ADDRESS: 'placeholder',
},
});
await commandWithPipe(
`cdk deploy SimpleNftMarketplaceBlockchainNode \
--require-approval never \
--outputs-file ${PATHS.stackOutputs} \
`,
{
cwd: PATHS.provision,
env: {
// Placeholder values to prevent synth from failing
AMB_HTTP_ENDPOINT: 'placeholder',
CONTRACT_ADDRESS: 'placeholder',
},
}
);
await copyStackOutputToSettings(
'SimpleNftMarketplaceBlockchainNode',
'AmbHttpEndpoint',
'ambEndpoint'
);
await copyStackOutputToSettings(
'SimpleNftMarketplaceBlockchainNode',
'DeployRegion',
'region'
);
logProgress('Deploy Amazon Managed Blockchain Node', true);
};
const deployApi = async () => {
logProgress('Deploy API');
const contractAddress = await getFromSettings('contractAddress');
const endpoint = await getFromSettings('ambEndpoint');
await commandWithPipe(
`cdk deploy SimpleNftMarketplaceStack \
--require-approval never \
--outputs-file ${PATHS.stackOutputs}`,
{
cwd: PATHS.provision,
env: {
AMB_HTTP_ENDPOINT: endpoint,
CONTRACT_ADDRESS: contractAddress,
},
}
);
await copyStackOutputToSettings(
'SimpleNftMarketplaceStack',
'UserPoolId',
'userPoolId'
);
await copyStackOutputToSettings(
'SimpleNftMarketplaceStack',
'UserPoolClientId',
'userPoolClientId'
);
await copyStackOutputToSettings(
'SimpleNftMarketplaceStack',
'NftApiEndpoint',
'nftApiEndpoint'
);
logProgress('Deploy API', true);
};
const promptForEther = async () => {
const address = await getFromSettings('address');
console.log(
chalk.green
.bold(`Navigate to https://goerlifaucet.com/ to add ETH to your address:
${address}
Then press any key to continue...`)
);
await keypress();
};
const waitForEther = async () => {
const address = await getFromSettings('address');
const endpoint = await getFromSettings('ambEndpoint');
const region = await getFromSettings('region');
await commandWithPipe('node scripts/wait-for-balance.js', {
cwd: PATHS.contract,
env: {
AMB_HTTP_ENDPOINT: endpoint,
AWS_ACCESS_KEY_ID: credentials.accessKeyId,
AWS_DEFAULT_REGION: region,
AWS_SECRET_ACCESS_KEY: credentials.secretAccessKey,
CONTRACT_ADDRESS: address,
},
});
};
const deployContract = async () => {
logProgress('Deploy Contract');
const privateKey = await getFromSettings('privateKey');
const endpoint = await getFromSettings('ambEndpoint');
const region = await getFromSettings('region');
const contract = commandWithPipe(
'npx hardhat run --network amb scripts/deploy-amb.js',
{
cwd: PATHS.contract,
env: {
AMB_HTTP_ENDPOINT: endpoint,
AWS_ACCESS_KEY_ID: credentials.accessKeyId,
AWS_DEFAULT_REGION: region,
AWS_SECRET_ACCESS_KEY: credentials.secretAccessKey,
PRIVATE_KEY: privateKey,
},
}
);
const { stdout } = await contract;
const [contractAddress] = stdout.match(/(0x[a-fA-F0-9]{40})/);
if (!contractAddress) throw new Error('Unable to parse contract address');
await writeToSettings('contractAddress', contractAddress);
logProgress('Deploy Contract', true);
};
const writeFrontEndVars = async () => {
logProgress('Write UI Configuration');
const region = await getFromSettings('region');
const apiEndpoint = await getFromSettings('nftApiEndpoint');
const userPoolId = await getFromSettings('userPoolId');
const webClientId = await getFromSettings('userPoolClientId');
const envLocal = `
VUE_APP_AWS_REGION=${region}
VUE_APP_API_ENDPOINT=${apiEndpoint}
VUE_APP_USER_POOL_ID=${userPoolId}
VUE_APP_USER_POOL_WEB_CLIENT_ID=${webClientId}
`;
await fs.writeFile(PATHS.marketplaceEnv, envLocal);
logProgress('Write UI Configuration', true);
};
const buildFrontEnd = async () => {
logProgress('Build UI');
await commandWithPipe('npm run build', {
cwd: PATHS.marketplace,
});
logProgress('Build UI', true);
};
const deployFrontEnd = async () => {
logProgress('Deploy UI');
const deploy = commandWithPipe(
`cdk deploy SimpleNftMarketplaceFrontendStack \
--require-approval never`,
{
cwd: PATHS.provision,
env: {
// Placeholder values to prevent synth from failing
AMB_HTTP_ENDPOINT: 'placeholder',
CONTRACT_ADDRESS: 'placeholder',
},
}
);
const { stderr } = await deploy;
const [cfnEndpoint] = stderr.match(/(https:\/\/[a-zA-Z0-9]+.cloudfront.net)/);
await writeToSettings('cfnEndpoint', cfnEndpoint);
logProgress('Deploy UI', true);
};
const runOnce = async fn => {
if (await getFromSettings(fn.name)) {
console.log(`Skipping ${fn.name} since it has already been completed`);
} else {
await fn();
await markAsComplete(fn.name);
}
};
const run = async () => {
// Set AWS Credentials
await setAWSCredentials();
// Check dependencies
await runOnce(checkDependencies);
// Create account
await runOnce(createAccount);
// Deploy AMB node
await deployAmbNode();
// Compile contract
await runOnce(compileContract);
// Prompt for Contract ETH
await promptForEther();
// Wait for ETH
await waitForEther();
// Deploy contract
await runOnce(deployContract);
// Deploy API
await deployApi();
// Write Front End Config
await writeFrontEndVars();
// Build Front End
await buildFrontEnd();
// Deploy Front End
await deployFrontEnd();
};
run()
.then(async () => {
console.log(
chalk.green(`
Success!
Your Simple NFT Marketplace has now been deployed and the UI can be accessed at
the following URL:
${await getFromSettings('cfnEndpoint')}
To mint and send your first NFT, you can start by creating an account on the UI
as listed in the docs starting from here:
https://github.com/aws-samples/simple-nft-marketplace/blob/main/docs/en/DOCS_04_FRONTEND.md#create-an-account
`)
);
process.exit();
})
.catch(e => {
console.error(e);
throw e;
});