theelector/data/pull.js

53 lines
1.3 KiB
JavaScript

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
}