-
Notifications
You must be signed in to change notification settings - Fork 16
/
forking.mjs
151 lines (128 loc) · 3.38 KB
/
forking.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
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
import inquirer from "inquirer";
import axios from "axios";
import { ethers } from "ethers";
import ora from "ora";
import { BB_BACKEND_URL, BB_API_KEY } from "./constants.mjs";
import { createNewDeployment } from "./helpers.mjs";
async function getBlockNumber(rpc) {
const provider = new ethers.providers.JsonRpcProvider(rpc);
const chainId = (await provider.getNetwork()).chainId;
function getLargestPossibleReorg() {
// mainnet
if (chainId === 1) {
return 5;
}
// Goerli
if (chainId === 5) {
return 5;
}
// Polygon
if (chainId === 137) {
return 50;
}
// Polygon Mumbai
if (chainId === 80001) {
return 50;
}
// BSC
if (chainId === 56) {
return 50;
}
return null;
}
const FALLBACK_MAX_REORG = 200;
const actualMaxReorg = getLargestPossibleReorg(chainId);
const maxReorg = actualMaxReorg || FALLBACK_MAX_REORG;
const latestBlock = await provider.getBlockNumber();
const lastSafeBlock = latestBlock - maxReorg;
return lastSafeBlock;
}
async function createFork() {
let latestBlockNumber = 0;
let blockNumber = 0;
let network;
let chainId;
let rpc;
const config1 = {
method: "get",
url: `${BB_BACKEND_URL}/chains`,
headers: {
Authorization: `Bearer ${BB_API_KEY}`,
"Content-Type": "application/json",
},
};
const response = await axios(config1);
const networkData = {};
Object.values(response.data).forEach((entry) => {
entry.options.forEach((option) => {
networkData[option.label] = [parseInt(option.value), option.networkRpc];
});
});
const networks = Object.keys(networkData);
await inquirer
.prompt([
{
type: "list",
name: "network",
message: "Choose forking network",
choices: networks,
},
])
.then(async (response) => {
network = response.network;
chainId = networkData[network][0];
rpc = networkData[network][1];
latestBlockNumber = await getBlockNumber(rpc);
await inquirer
.prompt([
{
type: "number",
name: "blockNumber",
message: "Fork from block number...",
default: latestBlockNumber,
},
])
.then((value) => {
blockNumber = value.blockNumber;
});
});
const createNodeSpinner = ora(
`Creating a new ${network} fork on Buildbear...`
).start();
const data = JSON.stringify({
chainId,
blockNumber,
});
const config = {
method: "post",
url: `${BB_BACKEND_URL}/v1/buildbear-sandbox`,
headers: {
Authorization: `Bearer ${BB_API_KEY}`,
"Content-Type": "application/json",
},
data,
};
let node;
try {
const response = await axios(config);
const resData = response.data;
if (resData.status === "success") {
node = {
testnetId: resData.sandboxId,
chainId: resData.chainId,
rpcUrl: resData.rpcUrl,
faucetUrl: resData.faucetUrl,
explorerUrl: resData.explorerUrl,
verificationUrl: resData.verificationUrl,
};
createNodeSpinner.succeed("Testnet created successfully");
console.log(node);
} else {
console.log("Error in creating Testnet, Error: ", resData);
}
} catch (err) {
console.log("Error in creating Testnet, Error: ", err);
}
if (node) createNewDeployment(node, chainId);
}
createFork();