2020-11-05 16:22:14 -05:00
|
|
|
import config from "../config.json"
|
2020-11-04 20:45:15 -05:00
|
|
|
import Web3 from "web3";
|
|
|
|
import {abi} from "./abi.js";
|
2020-11-05 16:22:14 -05:00
|
|
|
import {stateList} from "./stateData.js";
|
|
|
|
|
2020-11-04 20:45:15 -05:00
|
|
|
const web3 = new Web3(
|
|
|
|
new Web3.providers.HttpProvider(
|
2020-11-04 20:52:47 -05:00
|
|
|
config.url
|
2020-11-04 20:45:15 -05:00
|
|
|
)
|
|
|
|
);
|
|
|
|
const mainnetAddress = "0x0792724900B551d200D954a5Ed709d9514d73A9F";
|
|
|
|
const mainnetContract = new web3.eth.Contract(abi, mainnetAddress);
|
|
|
|
|
|
|
|
export async function fetchAllData(exclude = ["NA"]) {
|
|
|
|
|
|
|
|
// Excludes areas from data fetching
|
2020-11-05 16:22:14 -05:00
|
|
|
let stateFetch = stateList;
|
|
|
|
// if (exclude !== ["NA"])
|
|
|
|
// for (let toExclude = 0; toExclude <= exclude.length; toExclude++) {
|
|
|
|
// stateFetch = _.remove(stateFetch, exclude[toExclude])
|
|
|
|
// }
|
2020-11-04 20:45:15 -05:00
|
|
|
|
|
|
|
let allWins = {}
|
|
|
|
for (let i = 0; i <= stateFetch.length - 1; i++) {
|
2020-11-05 16:22:14 -05:00
|
|
|
let state = stateList[i];
|
2020-11-04 20:45:15 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|