theelector/data/pull.js
2020-11-05 16:22:14 -05:00

53 lines
1.2 KiB
JavaScript

import config from "../config.json"
import Web3 from "web3";
import {abi} from "./abi.js";
import {stateList} from "./stateData.js";
const web3 = new Web3(
new Web3.providers.HttpProvider(
config.url
)
);
const mainnetAddress = "0x0792724900B551d200D954a5Ed709d9514d73A9F";
const mainnetContract = new web3.eth.Contract(abi, mainnetAddress);
export async function fetchAllData(exclude = ["NA"]) {
// Excludes areas from data fetching
let stateFetch = stateList;
// 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 = stateList[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
}