Skip to content

Commit

Permalink
Detect if testnet is up / down
Browse files Browse the repository at this point in the history
  • Loading branch information
carletex committed Jan 14, 2022
1 parent 806e7e2 commit e4c26c1
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 2 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ETHERSCAN_API_KEY=
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ cd speedrun-grader
yarn install
```

you need a 12 word seed phrase as a deployer in a file called `mnemonic.txt`
Copy `.env.example` to `.env` and put in your [Etherscan API Key](https://etherscan.io/apis).

You also need a 12 word seed phrase as a deployer in a file called `mnemonic.txt`

(you could use this eth.build to create one: https://eth.build/build#6f1ab054a3e274128914fdfd3e0e0868245816059ee4cb0b2a4f608e57bddea8)

Expand Down
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const bodyParser = require("body-parser");
const app = express();
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const { isNetworkRunning } = require("./utils/network");

let challenges = JSON.parse(fs.readFileSync("challenges.json").toString())

Expand Down Expand Up @@ -101,7 +102,7 @@ app.get("/pretty/:challenge/:network/:address", async function(req, res) {

// Main API endpoint.
app.post('/', async function(req, res){
console.log("POST", req.body);
console.log("POST", req.body);
const challengeId = req.body.challenge;
const network = req.body.network;
const address = req.body.address;
Expand All @@ -111,6 +112,13 @@ app.post('/', async function(req, res){
return res.sendStatus(404);
}

if (!(await isNetworkRunning(network))) {
console.error(`❌📡❌ ${network} is down.`);
return res.status(503).json({ error: `${network} is down.` })
}

console.log(`📡 ${network} is UP.`);

const challenge = challenges[challengeId];
const result = await testChallenge({ challenge, network, address })

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"dependencies": {
"axios": "^0.24.0",
"body-parser": "^1.19.1",
"cors": "^2.8.5",
"dotenv": "^11.0.0",
"express": "^4.17.2",
"https": "^1.0.0",
"nodemon": "^2.0.15"
Expand Down
35 changes: 35 additions & 0 deletions utils/network.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require('dotenv').config();
const axios = require('axios');

const allowedNetworks = ["rinkeby", "ropsten", "kovan"];
const API_KEY = process.env.ETHERSCAN_API_KEY;

/**
* Checks if "network" is up.
* @param network a supported Ethereum network
* @returns {Promise<boolean>}
*/
const isNetworkRunning = async (network) => {
if (!allowedNetworks.includes(network)) return false;

const API_URL = `https://api-${network}.etherscan.io/api`;
const currentTimestamp = Math.round(Date.now() / 1000);
// We assume the network is Down that if not valid block in the last 2 minutes
const aliveFromTimestamp = currentTimestamp - 120;

const paramString = `?module=block&action=getblocknobytime&timestamp=${aliveFromTimestamp}&closest=after&apikey=${API_KEY}`;

try {
const response = await axios.get(API_URL + paramString);
// The Etherscan API returns OK / NOTOK
return response.data.message === "OK";
} catch (e) {
// Issue with the Request.
console.error(e);
return false;
}
}

module.exports = {
isNetworkRunning
}
17 changes: 17 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ [email protected]:
resolved "https://registry.yarnpkg.com/array-flatten/-/array-flatten-1.1.1.tgz#9a5f699051b1e7073328f2a008968b64ea2955d2"
integrity sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=

axios@^0.24.0:
version "0.24.0"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.24.0.tgz#804e6fa1e4b9c5288501dd9dff56a7a0940d20d6"
integrity sha512-Q6cWsys88HoPgAaFAVUb0WpPk0O8iTeisR9IMqy9G8AbO4NlpVknrnQS03zzF9PGAWgO3cgletO3VjV/P7VztA==
dependencies:
follow-redirects "^1.14.4"

balanced-match@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
Expand Down Expand Up @@ -289,6 +296,11 @@ dot-prop@^5.2.0:
dependencies:
is-obj "^2.0.0"

dotenv@^11.0.0:
version "11.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-11.0.0.tgz#ee37feddf8ada6d348a79e198312d4a8abfd1c1e"
integrity sha512-Fp/b504Y5W+e+FpCxTFMUZ7ZEQkQYF0rx+KZtmwixJxGQbLHrhCwo3FjZgNC8vIfrSi29PABNbMoCGD9YoiXbQ==

duplexer3@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/duplexer3/-/duplexer3-0.1.4.tgz#ee01dd1cac0ed3cbc7fdbea37dc0a8f1ce002ce2"
Expand Down Expand Up @@ -387,6 +399,11 @@ finalhandler@~1.1.2:
statuses "~1.5.0"
unpipe "~1.0.0"

follow-redirects@^1.14.4:
version "1.14.7"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.7.tgz#2004c02eb9436eee9a21446a6477debf17e81685"
integrity sha512-+hbxoLbFMbRKDwohX8GkTataGqO6Jb7jGwpAlwgy2bIz25XtRm7KEzJM76R1WiNT5SwZkX4Y75SwBolkpmE7iQ==

[email protected]:
version "0.2.0"
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.2.0.tgz#2269936428aad4c15c7ebe9779a84bf0b2a81811"
Expand Down

0 comments on commit e4c26c1

Please sign in to comment.