Skip to content

Commit

Permalink
Merge pull request #115 from nation3/112-fix-unknown-block-number
Browse files Browse the repository at this point in the history
Fix "unknown block number" error (#112)
  • Loading branch information
aahna-ashina authored Dec 31, 2023
2 parents 74b0378 + dec5f70 commit cc05832
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 622 deletions.
18 changes: 10 additions & 8 deletions data-sources/citizens/generate-citizen-count-csv.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
const Web3 = require('web3')
import { ethers } from 'ethers'
const Passport = require('../abis/Passport.json')
const csvWriter = require('csv-writer')
const fs = require('fs')
const Papa = require('papaparse')

const web3 = new Web3('https://eth.llamarpc.com')
console.info('web3.version:', web3.version)
const ethersProvider = new ethers.JsonRpcProvider(
'https://rpc.ankr.com/eth'
)
console.info('ethersProvider:', ethersProvider)

const PassportContract = new web3.eth.Contract(
const passportContract = new ethers.Contract(
'0x3337dac9f251d4e403d6030e18e3cfb6a2cb1333',
Passport.abi,
'0x3337dac9f251d4e403d6030e18e3cfb6a2cb1333'
ethersProvider
)
console.info('PassportContract._address:', PassportContract._address)

loadPassportMintsByWeek()

Expand Down Expand Up @@ -77,13 +79,13 @@ async function loadPassportMintsByWeek() {

async function getNextId(): Promise<number> {
console.info('getNextId')
return await PassportContract.methods.getNextId().call()
return await passportContract.getNextId()
}

async function getTimestamp(id: number): Promise<number> {
console.info('getTimestamp, id:', id)
try {
return await PassportContract.methods.timestampOf(id).call()
return await passportContract.timestampOf(id)
} catch (err) {
console.error('err:', err)
return 0
Expand Down
41 changes: 18 additions & 23 deletions data-sources/citizens/generate-citizens-csv.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,24 @@
const Web3 = require('web3')
const Passport = require('../abis/Passport.json')
const VotingEscrow = require('../abis/VotingEscrow.json')
const csvWriter = require('csv-writer')
const fs = require('fs')
const ethers = require('ethers')
import { ethers } from 'ethers'

const web3 = new Web3('https://eth.llamarpc.com')
console.info('web3.version:', web3.version)

const ethersProvider = new ethers.providers.JsonRpcProvider(
'https://eth.llamarpc.com'
const ethersProvider = new ethers.JsonRpcProvider(
'https://rpc.ankr.com/eth'
)
console.info('ethersProvider:', ethersProvider)

const PassportContract = new web3.eth.Contract(
const passportContract = new ethers.Contract(
'0x3337dac9f251d4e403d6030e18e3cfb6a2cb1333',
Passport.abi,
'0x3337dac9f251d4e403d6030e18e3cfb6a2cb1333'
ethersProvider
)
console.info('PassportContract._address:', PassportContract._address)

const VotingEscrowContract = new web3.eth.Contract(
const votingEscrowContract = new ethers.Contract(
'0xf7def1d2fbda6b74bee7452fdf7894da9201065d',
VotingEscrow.abi,
'0xf7def1d2fbda6b74bee7452fdf7894da9201065d'
ethersProvider
)
console.info('VotingEscrowContract._address:', VotingEscrowContract._address)

loadCitizenData()

Expand Down Expand Up @@ -64,12 +59,12 @@ async function loadCitizenData() {
signerAddress = signerAddress.toLowerCase()
console.info('lowercase signerAddress:', signerAddress)

const ensName: string = await getEnsName(ownerAddress)
const ensName: string | null = await getEnsName(ownerAddress)
console.info('ensName:', ensName)

const votingPowerWei: number = await getVotingPower(ownerAddress)
console.info('votingPowerWei:', votingPowerWei)
const votingPowerEther: string = web3.utils.fromWei(votingPowerWei)
const votingPowerEther: string = ethers.formatUnits(votingPowerWei)
console.info('votingPowerEther:', votingPowerEther)
const votingPowerRounded: string = new Number(votingPowerEther).toFixed(2)
console.info('votingPowerRounded:', votingPowerRounded)
Expand All @@ -90,37 +85,37 @@ async function loadCitizenData() {

async function getNextId(): Promise<number> {
console.info('getNextId')
return await PassportContract.methods.getNextId().call()
return await passportContract.getNextId()
}

async function getOwner(passportId: number): Promise<string> {
console.info('getOwner')
try {
return await PassportContract.methods.ownerOf(passportId).call()
return await passportContract.ownerOf(passportId)
} catch (err) {
console.error('err:', err)
return ethers.constants.AddressZero
return ethers.ZeroAddress
}
}

async function getSigner(passportId: number): Promise<string> {
console.info('getSigner')
try {
return await PassportContract.methods.signerOf(passportId).call()
return await passportContract.signerOf(passportId)
} catch (err) {
console.error('err:', err)
return ethers.constants.AddressZero
return ethers.ZeroAddress
}
}

async function getEnsName(ethAddress: string): Promise<string> {
async function getEnsName(ethAddress: string): Promise<string | null> {
console.info('getEnsName')
return await ethersProvider.lookupAddress(ethAddress)
}

async function getVotingPower(ethAddress: string): Promise<number> {
console.info('getVotingPower')
return await VotingEscrowContract.methods.balanceOf(ethAddress).call()
return await votingEscrowContract.balanceOf(ethAddress)
}

export {}
27 changes: 13 additions & 14 deletions data-sources/citizens/generate-voting-power-datasets.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
/* eslint @typescript-eslint/no-var-requires: "off" */
const createObjectCsvWriter = require('csv-writer').createObjectCsvWriter

const Web3 = require('web3')
const VotingEscrow = require('../abis/VotingEscrow.json')
const csvWriter = require('csv-writer')
const fs = require('fs')
const ethers = require('ethers')
import { ethers } from 'ethers'
const EthDater = require('ethereum-block-by-date')

const web3 = new Web3('https://eth.llamarpc.com')
console.info('web3.version:', web3.version)
const ethersProvider = new ethers.JsonRpcProvider(
'https://rpc.ankr.com/eth'
)
console.info('ethersProvider:', ethersProvider)

const dater = new EthDater(web3)
const dater = new EthDater(ethersProvider)
console.info('dater:', dater)

const ethersProvider = new ethers.providers.JsonRpcProvider('https://eth.llamarpc.com')
console.info('ethersProvider:', ethersProvider)

const VotingEscrowContract = new web3.eth.Contract(VotingEscrow.abi, '0xf7def1d2fbda6b74bee7452fdf7894da9201065d')
console.info('VotingEscrowContract._address:', VotingEscrowContract._address)
const votingEscrowContract = new ethers.Contract(
'0xf7def1d2fbda6b74bee7452fdf7894da9201065d',
VotingEscrow.abi,
ethersProvider
)

loadVotingPowerData()

Expand Down Expand Up @@ -61,7 +60,7 @@ async function loadVotingPowerData() {
// Get Citizen's voting power at the current block
const votingPowerWei: number = await getVotingPowerAtBlock(ethAddress, blockByDate.block)
console.info('votingPowerWei:', votingPowerWei)
const votingPowerEther: number = web3.utils.fromWei(votingPowerWei)
const votingPowerEther: string = ethers.formatUnits(votingPowerWei)
console.info('votingPowerEther:', votingPowerEther)
const votingPowerRounded: string = new Number(votingPowerEther).toFixed(2)
console.info('votingPowerRounded:', votingPowerRounded)
Expand All @@ -84,5 +83,5 @@ async function loadVotingPowerData() {

async function getVotingPowerAtBlock(ethAddress: string, blockNumber: number): Promise<number> {
console.info('getVotingPowerAtBlock')
return await VotingEscrowContract.methods.balanceOfAt(ethAddress, blockNumber).call()
return await votingEscrowContract.balanceOfAt(ethAddress, blockNumber)
}
Loading

0 comments on commit cc05832

Please sign in to comment.