📈
Everipedia
  • Home
  • Everipedia API
    • Everipedia.org REST API
  • Everipedia OraQles
    • About
    • Data Partners
      • Associated Press
  • Current OraQles Projects
    • January 5th, 2021 Senate Runoff
      • Ethers.js tutorial
      • Solidity Tutorial
  • Past OraQles Projects
    • 2020 US Presidential Election
      • 2020 US Presidential Election - Ethereum
        • Fetch election calls with Solidity
        • Fetch election calls with web3
        • Mainnet Oracle Data
        • Kovan Testnet Oracle Data
      • 2020 US Presidential Election - EOS
Powered by GitBook
On this page
  • 🚀 Want a shortcut? 🚀
  • Walkthrough

Was this helpful?

  1. Past OraQles Projects
  2. 2020 US Presidential Election
  3. 2020 US Presidential Election - Ethereum

Fetch election calls with web3

If you want to connect to the AP Election results in JavaScript using web3, learn how here.

PreviousFetch election calls with SolidityNextMainnet Oracle Data

Last updated 4 years ago

Was this helpful?

🚀 Want a shortcut? 🚀

. This shows how to connect to both mainnet and Kovan testnet using web3.

Walkthrough

  1. Begin by installing web3

npm install web3

2. Instantiate a web3 provider

const Web3 = require("web3");
export const web3 = new Web3(
  new Web3.providers.HttpProvider(
    "YOUR_INFURA_URL_OR_SIMILAR_PROVIDER"
  )
);

3. Connect to the contract (the "abi" variable below is a JavaScript array. Find and the )

// Mainnet

export const mainnetAddress = "0x0792724900B551d200D954a5Ed709d9514d73A9F";
export const mainnetContract = new web3.eth.Contract(abi, mainnetAddress);

// Kovan

export const kovanAddress = "0x12B7B8Dea45AF31b6303E00C735332A8b6752856";
export const kovanContract = new web3.eth.Contract(abi, kovanAddress);

4. Fetch the election results!

// Mainnet

const allWinners = await mainnetContract.methods
      .presidentialWinners(state)
      .call()

// Kovan

const allWinners = await kovanContract.methods
      .presidentialWinners(state)
      .call()

5. Make use of your verified election data!

/** 
This is the shape of the data
{
  [state]: {
    winner: "Biden" | "Trump" | ...,
    resultNow: "1604003431", // string unix time that winner was called
    resultBlock: "11153856", // string etherum block where call was mined in
  }
}

you can find an array of all states (52 = 50 states + D.C. + US) here:
https://github.com/EveripediaNetwork/everipedia-ap-2020/blob/master/src/all-states.js
*/

You may find this screenshot from Remix easier to understand. This is example data returned for the state "TEST" in production.

6. Still having issues?

Learn more about Chainlink Oracles at

Tweet to and any engineering questions (or send a DM on Twitter to )

Start with our boilerplate "Mission Control" App on GitHub
the mainnet abi here
kovan abi here
Refer to our example repo
https://docs.chain.link/docs
@dawsonbotsford
@everipedia
@dawsonbotsford
Function screenshot from remix.ethereum.org