forked from bnb-chain/opbnb-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_update.ts
43 lines (39 loc) · 1.49 KB
/
auto_update.ts
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
import Template from "https://deno.land/x/[email protected]/mod.ts";
// date format: YYYYMMDD, e.g. 20231102
function getSnapshotURL(network: "mainnet" | "testnet", date: Date): string {
const dateString = date.toISOString().substr(0, 10).replace(/-/g, "");
if (network === "mainnet") {
return `https://tf-bnbchain-prod-opbnb-mainnet-snapshot-s3-ap-northeast-1.s3.ap-northeast-1.amazonaws.com/geth-${dateString}.tar.gz`;
} else {
return `https://tf-nodereal-prod-opbnb-testnet-snapshot-s3-ap.s3.ap-northeast-1.amazonaws.com/geth-${dateString}.tar.gz`;
}
}
async function getLatestSnapshotURL(network: "mainnet" | "testnet") {
const date = new Date();
for (let i = 0; i < 10; i++) {
const url = getSnapshotURL(network, date);
const resp = await fetch(url);
console.log(url, resp.status);
if (resp.status === 200) {
return url;
}
date.setDate(date.getDate() - 1);
}
throw new Error("no snapshot found in latest 10 days");
}
async function main() {
const mainnetLatestSnapshotURL = await getLatestSnapshotURL("mainnet");
const testnetLatestSnapshotURL = await getLatestSnapshotURL("testnet");
const data = {
mainnet: mainnetLatestSnapshotURL,
testnet: testnetLatestSnapshotURL,
updatedAt: new Date().toISOString(),
};
console.log(data);
// Render a template
const tpl = new Template();
const result = await tpl.renderFile("./README.md.template", data);
console.log(result);
await Deno.writeTextFile("./README.md", result);
}
await main();