So many changes

This commit is contained in:
Alex 2020-11-04 20:45:15 -05:00
parent 0dfe99ac1e
commit fcd2fe3d2a
8 changed files with 386 additions and 1 deletions

2
.gitignore vendored
View File

@ -1 +1,3 @@
/node_modules/
/manifest.txt
/calls.json

215
data/abi.js Normal file
View File

@ -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",
},
];

54
data/all-states.js Normal file
View File

@ -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",
];

View File

@ -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()
}

52
data/pull.js Normal file
View File

@ -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
}

View File

@ -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")))
})

5
package-lock.json generated
View File

@ -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",

View File

@ -10,6 +10,8 @@
"author": "",
"license": "ISC",
"dependencies": {
"lodash": "^4.17.20",
"web3": "^1.3.0"
}
},
"type": "module"
}