From fcd2fe3d2a72d2b18c19977f39f0bfbf65322291 Mon Sep 17 00:00:00 2001 From: Alex Date: Wed, 4 Nov 2020 20:45:15 -0500 Subject: [PATCH] So many changes --- .gitignore | 2 + data/abi.js | 215 +++++++++++++++++++++++++++++++++++ data/all-states.js | 54 +++++++++ data/generateCallManifest.js | 37 ++++++ data/pull.js | 52 +++++++++ index.js | 18 +++ package-lock.json | 5 + package.json | 4 +- 8 files changed, 386 insertions(+), 1 deletion(-) create mode 100644 data/abi.js create mode 100644 data/all-states.js create mode 100644 data/generateCallManifest.js create mode 100644 data/pull.js diff --git a/.gitignore b/.gitignore index 2ccbe46..da5d98e 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /node_modules/ +/manifest.txt +/calls.json diff --git a/data/abi.js b/data/abi.js new file mode 100644 index 0000000..e65fc79 --- /dev/null +++ b/data/abi.js @@ -0,0 +1,215 @@ +export const abi = [ + { + inputs: [ + { + internalType: "bytes32", + name: "_requestId", + type: "bytes32", + }, + { + internalType: "uint256", + name: "_payment", + type: "uint256", + }, + { + internalType: "bytes4", + name: "_callbackFunctionId", + type: "bytes4", + }, + { + internalType: "uint256", + name: "_expiration", + type: "uint256", + }, + ], + name: "cancelRequest", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [], + stateMutability: "nonpayable", + type: "constructor", + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: "bytes32", + name: "id", + type: "bytes32", + }, + ], + name: "ChainlinkCancelled", + type: "event", + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: "bytes32", + name: "id", + type: "bytes32", + }, + ], + name: "ChainlinkFulfilled", + type: "event", + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: "bytes32", + name: "id", + type: "bytes32", + }, + ], + name: "ChainlinkRequested", + type: "event", + }, + { + inputs: [ + { + internalType: "string", + name: "_key", + type: "string", + }, + ], + name: "deleteMappingElement", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [ + { + internalType: "bytes32", + name: "_requestId", + type: "bytes32", + }, + { + internalType: "bytes32", + name: "_votes", + type: "bytes32", + }, + ], + name: "fulfillpresidentialWinners", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, + { + anonymous: false, + inputs: [ + { + indexed: true, + internalType: "address", + name: "previousOwner", + type: "address", + }, + { + indexed: true, + internalType: "address", + name: "newOwner", + type: "address", + }, + ], + name: "OwnershipTransferred", + type: "event", + }, + { + inputs: [ + { + internalType: "address", + name: "_oracle", + type: "address", + }, + { + internalType: "string", + name: "_jobId", + type: "string", + }, + { + internalType: "string", + name: "_state", + type: "string", + }, + ], + name: "requestPresidentialVotes", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [ + { + internalType: "address", + name: "newOwner", + type: "address", + }, + ], + name: "transferOwnership", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [], + name: "isOwner", + outputs: [ + { + internalType: "bool", + name: "", + type: "bool", + }, + ], + stateMutability: "view", + type: "function", + }, + { + inputs: [], + name: "owner", + outputs: [ + { + internalType: "address", + name: "", + type: "address", + }, + ], + stateMutability: "view", + type: "function", + }, + { + inputs: [ + { + internalType: "string", + name: "", + type: "string", + }, + ], + name: "presidentialWinners", + outputs: [ + { + internalType: "string", + name: "winner", + type: "string", + }, + { + internalType: "uint256", + name: "resultNow", + type: "uint256", + }, + { + internalType: "uint256", + name: "resultBlock", + type: "uint256", + }, + ], + stateMutability: "view", + type: "function", + }, +]; diff --git a/data/all-states.js b/data/all-states.js new file mode 100644 index 0000000..9efc087 --- /dev/null +++ b/data/all-states.js @@ -0,0 +1,54 @@ +export const allStates = [ + "US", + "AK", + "AL", + "AR", + "AZ", + "CA", + "CO", + "CT", + "DC", + "DE", + "FL", + "GA", + "HI", + "IA", + "ID", + "IL", + "IN", + "KS", + "KY", + "LA", + "MA", + "MD", + "ME", + "MI", + "MN", + "MO", + "MS", + "MT", + "NC", + "ND", + "NE", + "NH", + "NJ", + "NM", + "NV", + "NY", + "OH", + "OK", + "OR", + "PA", + "RI", + "SC", + "SD", + "TN", + "TX", + "UT", + "VA", + "VT", + "WA", + "WI", + "WV", + "WY", +]; diff --git a/data/generateCallManifest.js b/data/generateCallManifest.js new file mode 100644 index 0000000..50d38ea --- /dev/null +++ b/data/generateCallManifest.js @@ -0,0 +1,37 @@ +import fs from "fs"; + +export async function generateCallManifest( + data = { + // "NA": { + // "winner": "NODATA", + // "resultNow": "0", + // "resultBlock": "0" + // } + }, + location = "manifest.txt") { + let manifest = fs.createWriteStream(location,) + + let now = new Date(Date.now()) + manifest.write( + "TheElector: automated election call reporting system\n" + + "-----------\n" + + "MANIFEST DATA\n" + + "Generated: " + (now.getMonth() + 1) + "/" + now.getDay() + " " + now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds() + " Hours local\n" + + "-----------\n" + ) + + // Alphabetical list + manifest.write("BY STATE ALPHABETICALLY: \n") + for (const state in data) { + let time = new Date(data[state].resultNow * 1000) + + manifest.write("State: " + state + "\n"); + manifest.write("Call: " + data[state].winner + "\n"); + manifest.write("Time called: " + (time.getMonth() + 1) + "/" + time.getDay() + " " + + time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds() + "\n"); + manifest.write("-----------" + "\n"); + } + + + manifest.close() +} diff --git a/data/pull.js b/data/pull.js new file mode 100644 index 0000000..d596215 --- /dev/null +++ b/data/pull.js @@ -0,0 +1,52 @@ +import Web3 from "web3"; +import {abi} from "./abi.js"; +import {allStates} from "./all-states.js"; +import _ from "lodash" + +const web3 = new Web3( + new Web3.providers.HttpProvider( + "https://mainnet.infura.io/v3/c38d6812840a42d4a9284ca2afa504b6" + ) +); +const mainnetAddress = "0x0792724900B551d200D954a5Ed709d9514d73A9F"; +const mainnetContract = new web3.eth.Contract(abi, mainnetAddress); + +export async function fetchAllData(exclude = ["NA"]) { + + // Excludes areas from data fetching + let stateFetch = allStates; + if (exclude !== ["NA"]) + for (let toExclude = 0; toExclude <= exclude.length; toExclude++) { + stateFetch = _.remove(stateFetch, exclude[toExclude]) + } + + let allWins = {} + for (let i = 0; i <= stateFetch.length - 1; i++) { + let state = allStates[i]; + allWins[state] = await mainnetContract.methods + .presidentialWinners(state) + .call() + } + + // Removing "0" "1" "2" + for (const state in allWins) { + delete allWins[state][0] + delete allWins[state][1] + delete allWins[state][2] + } + return JSON.parse(JSON.stringify(allWins)) +} + + +export async function fetchCalls(exclude) { + let allWins = await fetchAllData(exclude) + + // Removing uncalled states + for (const state in allWins) { + if (allWins[state].winner === "") { + delete allWins[state] + } + } + return allWins +} + diff --git a/index.js b/index.js index e69de29..68f3516 100644 --- a/index.js +++ b/index.js @@ -0,0 +1,18 @@ +#!/bin/node +import fs from "fs"; +import {fetchCalls} from "./data/pull.js"; +import {generateCallManifest} from "./data/generateCallManifest.js"; + + +await fetchCalls().then((results) => { + const data = new Uint8Array(Buffer.from(JSON.stringify(results, null, 2))); + fs.writeFile("calls.json", data, () => { + console.log("Calls written to file") + }) + + generateCallManifest(results).then(() => (console.log("Manifest created"))) +}) + + + + diff --git a/package-lock.json b/package-lock.json index d4a437f..2a97a72 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1419,6 +1419,11 @@ "json-buffer": "3.0.0" } }, + "lodash": { + "version": "4.17.20", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.20.tgz", + "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==" + }, "lowercase-keys": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.1.tgz", diff --git a/package.json b/package.json index f5c17e3..51a6f1d 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,8 @@ "author": "", "license": "ISC", "dependencies": { + "lodash": "^4.17.20", "web3": "^1.3.0" - } + }, + "type": "module" }