Skip to content

Commit

Permalink
Improve Zod typings and type check all http get requests
Browse files Browse the repository at this point in the history
  • Loading branch information
grempe committed Nov 21, 2022
1 parent 0a19e49 commit ef5bc4a
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 246 deletions.
24 changes: 8 additions & 16 deletions src/commands/bitcoin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command, ensureDirSync, isTooManyTries, retryAsync } from "../deps.ts";
import { Command, isTooManyTries, retryAsync } from "../deps.ts";

import { fetchWithTimeout, writeCanonicalJSON } from "../utils.ts";
import { EntropyBitcoin } from "../types.ts";
import { get, writeCanonicalJSON } from "../utils.ts";

import { ENTROPY_DIR } from "../constants.ts";

Expand All @@ -10,23 +11,14 @@ export async function bitcoin() {
async () => {
console.log("bitcoin");

const resp = await fetchWithTimeout(
const resp: EntropyBitcoin = await get<EntropyBitcoin>(
"https://blockchain.info/latestblock",
);

if (resp.err) {
throw new Error(`failed to fetch : status code ${resp.err}`);
}

// extract just the data we want
const { height, hash, time, block_index: blockIndex } = resp;
ensureDirSync(ENTROPY_DIR);
await writeCanonicalJSON(`${ENTROPY_DIR}/bitcoin.json`, {
height,
hash,
time,
blockIndex,
});
await writeCanonicalJSON(
`${ENTROPY_DIR}/bitcoin.json`,
EntropyBitcoin.parse(resp),
);
},
{ delay: 1000, maxTry: 3 },
);
Expand Down
3 changes: 1 addition & 2 deletions src/commands/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ export async function cleanup() {

try {
await Deno.remove(ENTROPY_DIR, { recursive: true });
} catch (error) {
} catch (_error) {
// dir does not exist
console.error(error);
}
}

Expand Down
40 changes: 22 additions & 18 deletions src/commands/drandBeacon.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import {
Command,
DrandClient,
DrandHTTP,
ensureDirSync,
isTooManyTries,
retryAsync,
DrandHTTP, isTooManyTries,
retryAsync
} from "../deps.ts";

import { fetchWithTimeout, writeCanonicalJSON } from "../utils.ts";
import {
EntropyDrandBeacon,
EntropyDrandBeaconChainInfo,
EntropyDrandBeaconRandomness
} from "../types.ts";
import { get, writeCanonicalJSON } from "../utils.ts";

import { ENTROPY_DIR } from "../constants.ts";

Expand All @@ -21,32 +24,33 @@ export async function drandBeacon() {
console.log("drand-beacon");
const urls = ["https://drand.cloudflare.com"];

const resp = await fetchWithTimeout(
const resp: EntropyDrandBeaconChainInfo = await get<
EntropyDrandBeaconChainInfo
>(
"https://drand.cloudflare.com/info",
);

if (resp.err) {
throw new Error(`failed to fetch : status code ${resp.err}`);
}

const chainInfo = resp;

const options = { chainInfo };
const chainInfo: EntropyDrandBeaconChainInfo =
EntropyDrandBeaconChainInfo.parse(resp);

const client = await DrandClient.wrap(
DrandHTTP.forURLs(urls, chainInfo.hash),
options,
{ chainInfo },
);

const randomness = await client.get();

await client.close();

ensureDirSync(ENTROPY_DIR);
await writeCanonicalJSON(`${ENTROPY_DIR}/drand-beacon.json`, {
EntropyDrandBeaconRandomness.parse(randomness);

const beacon: EntropyDrandBeacon = {
chainInfo,
randomness,
});
};

EntropyDrandBeacon.parse(beacon);

await writeCanonicalJSON(`${ENTROPY_DIR}/drand-beacon.json`, beacon);
},
{ delay: 1000, maxTry: 3 },
);
Expand Down
22 changes: 8 additions & 14 deletions src/commands/ethereum.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command, ensureDirSync, isTooManyTries, retryAsync } from "../deps.ts";
import { Command, isTooManyTries, retryAsync } from "../deps.ts";

import { fetchWithTimeout, writeCanonicalJSON } from "../utils.ts";
import { EntropyEthereum } from "../types.ts";
import { get, writeCanonicalJSON } from "../utils.ts";

import { ENTROPY_DIR } from "../constants.ts";

Expand All @@ -10,21 +11,14 @@ export async function ethereum() {
async () => {
console.log("ethereum");

const resp = await fetchWithTimeout(
const resp: EntropyEthereum = await get<EntropyEthereum>(
"https://api.blockcypher.com/v1/eth/main",
);
if (resp.err) {
throw new Error(`failed to fetch : status code ${resp.err}`);
}

// extract just the data we want
const { height, hash, time } = resp;
ensureDirSync(ENTROPY_DIR);
await writeCanonicalJSON(`${ENTROPY_DIR}/ethereum.json`, {
height,
hash,
time,
});
await writeCanonicalJSON(
`${ENTROPY_DIR}/ethereum.json`,
EntropyEthereum.parse(resp),
);
},
{ delay: 1000, maxTry: 3 },
);
Expand Down
30 changes: 13 additions & 17 deletions src/commands/hackerNews.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command, ensureDirSync, isTooManyTries, retryAsync } from "../deps.ts";
import { Command, isTooManyTries, retryAsync } from "../deps.ts";

import { fetchWithTimeout, writeCanonicalJSON } from "../utils.ts";
import { EntropyHackerNews, EntropyHackerNewsStory } from "../types.ts";
import { get, writeCanonicalJSON } from "../utils.ts";

import { ENTROPY_DIR } from "../constants.ts";

Expand All @@ -13,33 +14,28 @@ export async function hackerNews() {
async () => {
console.log("hacker-news");

const newsStories = await fetchWithTimeout(
const newsStories = await get<number[]>(
"https://hacker-news.firebaseio.com/v0/newstories.json",
);

if (newsStories.err) {
throw new Error(`failed to fetch : status code ${newsStories.err}`);
}

const stories = [];

for (let i = 0; i < NUMBER_OF_STORIES; i++) {
const story = await fetchWithTimeout(
const story: EntropyHackerNewsStory = await get<
EntropyHackerNewsStory
>(
`https://hacker-news.firebaseio.com/v0/item/${newsStories[i]}.json`,
);
if (story.err) {
throw new Error(`failed to fetch : status code ${story.err}`);
}

const { by, id, time, title, url } = story;
EntropyHackerNewsStory.parse(story);

stories.push({ by, id, time, title, url });
stories.push(story);
}

ensureDirSync(ENTROPY_DIR);
await writeCanonicalJSON(`${ENTROPY_DIR}/hacker-news.json`, {
stories,
});
await writeCanonicalJSON(
`${ENTROPY_DIR}/hacker-news.json`,
EntropyHackerNews.parse({ stories }),
);
},
{ delay: 1000, maxTry: 3 },
);
Expand Down
24 changes: 7 additions & 17 deletions src/commands/nistBeacon.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command, ensureDirSync, isTooManyTries, retryAsync } from "../deps.ts";
import { Command, isTooManyTries, retryAsync } from "../deps.ts";

import { fetchWithTimeout, writeCanonicalJSON } from "../utils.ts";
import { EntropyNistBeacon } from "../types.ts";
import { get, writeCanonicalJSON } from "../utils.ts";

import { ENTROPY_DIR } from "../constants.ts";

Expand All @@ -10,25 +11,14 @@ export async function nistBeacon() {
async () => {
console.log("nist-beacon");

const beacon = await fetchWithTimeout(
const resp = await get<{ pulse: EntropyNistBeacon }>(
"https://beacon.nist.gov/beacon/2.0/pulse/last",
);

if (beacon.err) {
throw new Error(`failed to fetch : status code ${beacon.err}`);
}
const { pulse } = resp;
const beacon = EntropyNistBeacon.parse(pulse);

const { pulse } = beacon;
const { chainIndex, outputValue, pulseIndex, timeStamp, uri } = pulse;

ensureDirSync(ENTROPY_DIR);
await writeCanonicalJSON(`${ENTROPY_DIR}/nist-beacon.json`, {
chainIndex,
outputValue,
pulseIndex,
timeStamp,
uri,
});
await writeCanonicalJSON(`${ENTROPY_DIR}/nist-beacon.json`, beacon);
},
{ delay: 1000, maxTry: 3 },
);
Expand Down
17 changes: 9 additions & 8 deletions src/commands/previous.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command, ensureDirSync, isTooManyTries, retryAsync } from "../deps.ts";
import { Command, isTooManyTries, retryAsync } from "../deps.ts";

import { fetchWithTimeout, writeCanonicalJSON } from "../utils.ts";
import { EntropyPrevious, EntropyResponse } from "../types.ts";
import { get, writeCanonicalJSON } from "../utils.ts";

import { ENTROPY_DIR } from "../constants.ts";

Expand All @@ -12,20 +13,20 @@ export async function previous() {
async () => {
console.log("previous");

const resp = await fetchWithTimeout(
const resp: EntropyResponse = await get<EntropyResponse>(
`${OBSERVABLE_BASE_URL}/latest`,
);
if (resp.err) {
throw new Error(`failed to fetch : status code ${resp.err}`);
}

EntropyResponse.parse(resp);

const { hash } = resp;

ensureDirSync(ENTROPY_DIR);
await writeCanonicalJSON(`${ENTROPY_DIR}/previous.json`, {
const previous: EntropyPrevious = EntropyPrevious.parse({
hash,
uri: `${OBSERVABLE_BASE_URL}/${hash}`,
});

await writeCanonicalJSON(`${ENTROPY_DIR}/previous.json`, previous);
},
{ delay: 1000, maxTry: 3 },
);
Expand Down
31 changes: 9 additions & 22 deletions src/commands/stellar.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Command, ensureDirSync, isTooManyTries, retryAsync } from "../deps.ts";
import { Command, isTooManyTries, retryAsync } from "../deps.ts";

import { fetchWithTimeout, writeCanonicalJSON } from "../utils.ts";
import { EntropyStellar } from "../types.ts";
import { get, writeCanonicalJSON } from "../utils.ts";

import { ENTROPY_DIR } from "../constants.ts";

Expand All @@ -12,33 +13,19 @@ export async function stellar() {
// Retrieve the last ledger ID.
// curl -X GET "https://horizon.stellar.org/fee_stats" > stellar-fee-stats.json

const feeStats = await fetchWithTimeout(
const feeStats = await get<{ last_ledger: string }>(
"https://horizon.stellar.org/fee_stats",
);

if (feeStats.err) {
throw new Error(`failed to fetch : status code ${feeStats.err}`);
}

// const { data: feeStats } = respStats

// Read the ledger for last ledger ID
const latestLedger = await fetchWithTimeout(
const latestLedger: EntropyStellar = await get<EntropyStellar>(
`https://horizon.stellar.org/ledgers/${feeStats.last_ledger}`,
);

if (latestLedger.err) {
throw new Error(`failed to fetch : status code ${latestLedger.err}`);
}

// extract just the data we want
const { closed_at, hash, sequence } = latestLedger;
ensureDirSync(ENTROPY_DIR);
await writeCanonicalJSON(`${ENTROPY_DIR}/stellar.json`, {
closed_at,
hash,
sequence,
});
await writeCanonicalJSON(
`${ENTROPY_DIR}/stellar.json`,
EntropyStellar.parse(latestLedger),
);
},
{ delay: 1000, maxTry: 3 },
);
Expand Down
10 changes: 6 additions & 4 deletions src/commands/timestamp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Command, ensureDirSync, isTooManyTries, retryAsync } from "../deps.ts";
import { Command, isTooManyTries, retryAsync } from "../deps.ts";

import { EntropyTimestamp } from "../types.ts";
import { writeCanonicalJSON } from "../utils.ts";

import { ENTROPY_DIR } from "../constants.ts";
Expand All @@ -10,10 +11,11 @@ export async function timestamp() {
async () => {
console.log("timestamp");

ensureDirSync(ENTROPY_DIR);
await writeCanonicalJSON(`${ENTROPY_DIR}/timestamp.json`, {
const timestamp: EntropyTimestamp = {
capturedAt: new Date().toISOString(),
});
};

await writeCanonicalJSON(`${ENTROPY_DIR}/timestamp.json`, timestamp);
},
{ delay: 1000, maxTry: 3 },
);
Expand Down
Loading

0 comments on commit ef5bc4a

Please sign in to comment.