forked from BuildBearLabs/Hardhat-BuildBear
-
Notifications
You must be signed in to change notification settings - Fork 7
/
helpers.mjs
113 lines (93 loc) · 2.82 KB
/
helpers.mjs
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
import inquirer from "inquirer";
import axios from "axios";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { execSync } from "child_process";
import { BB_BACKEND_URL, BB_API_KEY } from "./constants.mjs";
import ora from "ora";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export function readNodes() {
let nodes = null;
try {
nodes = JSON.parse(
fs.readFileSync(path.join(__dirname, "./testnet.json")).toString().trim()
);
} catch (e) {}
return nodes;
}
export async function createNewDeployment(node, forkingChainId) {
await getUserDetails();
await waitForLiveNode(node);
// eslint-disable-next-line no-param-reassign
node.forkingChainId = forkingChainId;
fs.writeFileSync(
path.join(__dirname, "./testnet.json"),
JSON.stringify(node, null, 2)
);
console.log("Testnet details stored in testnet.json");
}
async function getUserDetails() {
const userResponse = await inquirer.prompt([
{
type: "confirm",
name: "confirm",
message: "Authorize Buildbear to read your Git username and email ?",
},
]);
const gitUsername = execSync("git config user.name").toString().trim();
const gitEmail = execSync("git config user.email").toString().trim();
var data = userResponse.confirm
? JSON.stringify({
username: gitUsername ? gitUsername : "Anonymous",
email: gitEmail ? gitEmail : undefined,
})
: JSON.stringify({ username: "Anonymous", email: undefined });
var config = {
method: "post",
url: `${BB_BACKEND_URL}/api/scaffold-eth`,
headers: {
Authorization: `Bearer ${BB_API_KEY}`,
"Content-Type": "application/json",
},
data: data,
};
await axios(config);
if (userResponse.confirm) console.log("Thanks from the Buildbear Team ❤️");
}
async function waitForLiveNode(node) {
const spinner = ora("Waiting for the Testnet to be live").start();
const config = {
method: "get",
url: `${BB_BACKEND_URL}/v1/buildbear-sandbox/${node.testnetId}`,
headers: {
Authorization: `Bearer ${BB_API_KEY}`,
"Content-Type": "application/json",
},
};
async function checkLoop() {
try {
const response = await axios(config);
const data = response.data;
if (data.status === "live") {
spinner.succeed("Testnet is live");
} else if (data.status === "started") {
await timeout(2000);
await checkLoop();
} else {
spinner.fail("Testnet has stopped");
}
return null;
} catch (err) {
if (err.response.data.error === "Node is started") {
await timeout(2000);
await checkLoop();
} else console.log(err);
}
}
await checkLoop();
}
function timeout(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}