depreciate thegraph w/ relayer events
This commit is contained in:
parent
8d038c5869
commit
479b1e798b
@ -60,6 +60,22 @@ export const cachedEventsLength = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const corsConfig = (rpcUrl) => ({
|
||||||
|
headers: [
|
||||||
|
{
|
||||||
|
name: 'Access-Control-Allow-Origin',
|
||||||
|
value: rpcUrl
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: 'Access-Control-Allow-Methods',
|
||||||
|
value: 'POST, GET, OPTIONS'
|
||||||
|
}
|
||||||
|
],
|
||||||
|
withCredentials: false,
|
||||||
|
// buffer for tor connections
|
||||||
|
timeout: 30000
|
||||||
|
})
|
||||||
|
|
||||||
export const PROVIDERS = {
|
export const PROVIDERS = {
|
||||||
walletConnect: {
|
walletConnect: {
|
||||||
name: 'WalletConnect',
|
name: 'WalletConnect',
|
||||||
|
@ -4,7 +4,7 @@ import graph from '@/services/graph'
|
|||||||
import { download } from '@/store/snark'
|
import { download } from '@/store/snark'
|
||||||
import networkConfig from '@/networkConfig'
|
import networkConfig from '@/networkConfig'
|
||||||
import InstanceABI from '@/abis/Instance.abi.json'
|
import InstanceABI from '@/abis/Instance.abi.json'
|
||||||
import { CONTRACT_INSTANCES, eventsType } from '@/constants'
|
import { CONTRACT_INSTANCES, eventsType, corsConfig } from '@/constants'
|
||||||
import { sleep, flattenNArray, formatEvents, capitalizeFirstLetter } from '@/utils'
|
import { sleep, flattenNArray, formatEvents, capitalizeFirstLetter } from '@/utils'
|
||||||
|
|
||||||
const supportedCaches = ['1', '56', '100', '137']
|
const supportedCaches = ['1', '56', '100', '137']
|
||||||
@ -441,23 +441,9 @@ class EventsFactory {
|
|||||||
instances = new Map()
|
instances = new Map()
|
||||||
|
|
||||||
constructor(rpcUrl) {
|
constructor(rpcUrl) {
|
||||||
this.provider = new Web3(
|
const httpProvider = new Web3.providers.HttpProvider(rpcUrl, corsConfig(rpcUrl))
|
||||||
new Web3.providers.HttpProvider(rpcUrl, {
|
|
||||||
headers: [
|
this.provider = new Web3(httpProvider).eth
|
||||||
{
|
|
||||||
name: 'Access-Control-Allow-Origin',
|
|
||||||
value: rpcUrl
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: 'Access-Control-Allow-Methods',
|
|
||||||
value: 'POST, GET, OPTIONS'
|
|
||||||
}
|
|
||||||
],
|
|
||||||
withCredentials: false,
|
|
||||||
// buffer for tor connections
|
|
||||||
timeout: 30000
|
|
||||||
})
|
|
||||||
).eth
|
|
||||||
}
|
}
|
||||||
|
|
||||||
getBlockNumber = () => {
|
getBlockNumber = () => {
|
||||||
|
@ -1,10 +1,11 @@
|
|||||||
|
import Web3 from 'web3'
|
||||||
import namehash from 'eth-ens-namehash'
|
import namehash from 'eth-ens-namehash'
|
||||||
import { BigNumber as BN } from 'bignumber.js'
|
import { BigNumber as BN } from 'bignumber.js'
|
||||||
import { toChecksumAddress } from 'web3-utils'
|
import { toChecksumAddress, isAddress } from 'web3-utils'
|
||||||
|
|
||||||
import { graph } from '@/services'
|
|
||||||
import networkConfig from '@/networkConfig'
|
import networkConfig from '@/networkConfig'
|
||||||
import { REGISTRY_DEPLOYED_BLOCK } from '@/constants'
|
import { REGISTRY_DEPLOYED_BLOCK, corsConfig } from '@/constants'
|
||||||
|
import { sleep, flattenNArray } from '@/utils'
|
||||||
|
|
||||||
import AggregatorABI from '@/abis/Aggregator.abi.json'
|
import AggregatorABI from '@/abis/Aggregator.abi.json'
|
||||||
import RelayerRegistryABI from '@/abis/RelayerRegistry.abi.json'
|
import RelayerRegistryABI from '@/abis/RelayerRegistry.abi.json'
|
||||||
@ -24,28 +25,52 @@ class RelayerRegister {
|
|||||||
this.relayerRegistry = new this.provider.Contract(RelayerRegistryABI, registryContract)
|
this.relayerRegistry = new this.provider.Contract(RelayerRegistryABI, registryContract)
|
||||||
}
|
}
|
||||||
|
|
||||||
fetchEvents = async (fromBlock, toBlock) => {
|
fetchEvents = ({ fromBlock, toBlock }) => {
|
||||||
if (fromBlock <= toBlock) {
|
if (fromBlock <= toBlock) {
|
||||||
try {
|
return new Promise((resolve, reject) => {
|
||||||
const registeredEventsPart = await this.relayerRegistry.getPastEvents('RelayerRegistered', {
|
try {
|
||||||
fromBlock,
|
const registeredEventsPart = this.relayerRegistry.getPastEvents('RelayerRegistered', {
|
||||||
toBlock
|
fromBlock,
|
||||||
})
|
toBlock
|
||||||
|
})
|
||||||
|
|
||||||
return registeredEventsPart
|
resolve(registeredEventsPart)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const midBlock = (fromBlock + toBlock) >> 1
|
sleep(200)
|
||||||
|
|
||||||
if (midBlock - fromBlock < 2) {
|
const midBlock = (fromBlock + toBlock) >> 1
|
||||||
throw new Error(`error fetching events: ${error.message}`)
|
|
||||||
|
if (midBlock - fromBlock < 2) {
|
||||||
|
reject(new Error(`error fetching events: ${error.message}`))
|
||||||
|
}
|
||||||
|
|
||||||
|
const arr1 = this.fetchEvents({ fromBlock, toBlock: midBlock })
|
||||||
|
const arr2 = this.fetchEvents({ fromBlock: midBlock + 1, toBlock })
|
||||||
|
resolve([...arr1, ...arr2])
|
||||||
}
|
}
|
||||||
|
})
|
||||||
const arr1 = await this.fetchEvents(fromBlock, midBlock)
|
} else {
|
||||||
const arr2 = await this.fetchEvents(midBlock + 1, toBlock)
|
return []
|
||||||
return [...arr1, ...arr2]
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return []
|
}
|
||||||
|
|
||||||
|
batchFetchEvents = async ({ fromBlock, toBlock }) => {
|
||||||
|
const blockRange = 10000
|
||||||
|
const blockDifference = toBlock - fromBlock
|
||||||
|
const chunkCount = Math.ceil(blockDifference / blockRange)
|
||||||
|
const blockDenom = Math.ceil(blockDifference / chunkCount)
|
||||||
|
|
||||||
|
const promises = new Array(chunkCount).fill('').map((_, i) =>
|
||||||
|
this.fetchEvents({
|
||||||
|
fromBlock: i * blockDenom + fromBlock,
|
||||||
|
toBlock: (i + 1) * blockDenom + fromBlock
|
||||||
|
})
|
||||||
|
)
|
||||||
|
|
||||||
|
const batchEvents = flattenNArray(await Promise.all(promises))
|
||||||
|
const events = batchEvents.map((e) => ({ ...e.returnValues }))
|
||||||
|
|
||||||
|
return events
|
||||||
}
|
}
|
||||||
|
|
||||||
saveEvents = async ({ events, lastSyncBlock, storeName }) => {
|
saveEvents = async ({ events, lastSyncBlock, storeName }) => {
|
||||||
@ -96,51 +121,73 @@ class RelayerRegister {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getENSAddress = async (ensName) => {
|
||||||
|
const { url } = Object.values(networkConfig.netId1.rpcUrls)[0]
|
||||||
|
const httpProvider = new Web3.providers.HttpProvider(url, corsConfig(url))
|
||||||
|
const provider = new Web3(httpProvider)
|
||||||
|
|
||||||
|
const ensAddress = await provider.eth.ens.getAddress(ensName)
|
||||||
|
|
||||||
|
return ensAddress
|
||||||
|
}
|
||||||
|
|
||||||
fetchRelayers = async () => {
|
fetchRelayers = async () => {
|
||||||
|
const blockRange = 10000
|
||||||
// eslint-disable-next-line prefer-const
|
// eslint-disable-next-line prefer-const
|
||||||
let { blockFrom, blockTo, cachedEvents } = await this.getCachedData()
|
let { blockTo, cachedEvents } = await this.getCachedData()
|
||||||
let allRelayers = cachedEvents
|
let allRelayers = cachedEvents
|
||||||
|
|
||||||
if (blockFrom !== blockTo) {
|
const currentBlockNumber = await this.provider.getBlockNumber()
|
||||||
const registeredRelayersEvents = await graph.getAllRegisters(blockFrom)
|
const fromBlock = cachedEvents.length === 0 ? REGISTRY_DEPLOYED_BLOCK[1] : blockTo
|
||||||
|
const blockDifference = currentBlockNumber - fromBlock
|
||||||
|
|
||||||
let relayers = {
|
try {
|
||||||
lastSyncBlock: registeredRelayersEvents.lastSyncBlock,
|
let toBlock
|
||||||
events: registeredRelayersEvents.events.map((el) => ({
|
let registerRelayerEvents
|
||||||
ensName: el.ensName,
|
let lastBlock
|
||||||
relayerAddress: toChecksumAddress(el.address)
|
|
||||||
}))
|
if (cachedEvents.length > 0 || blockDifference === 0) {
|
||||||
|
return cachedEvents
|
||||||
|
} else if (blockDifference >= blockRange) {
|
||||||
|
toBlock = currentBlockNumber
|
||||||
|
registerRelayerEvents = await this.batchFetchEvents({ fromBlock, toBlock })
|
||||||
|
lastBlock = toBlock
|
||||||
|
} else {
|
||||||
|
toBlock = fromBlock + blockRange
|
||||||
|
registerRelayerEvents = await this.fetchEvents({ fromBlock, toBlock })
|
||||||
|
lastBlock = toBlock
|
||||||
}
|
}
|
||||||
|
|
||||||
const isGraphLate = relayers.lastSyncBlock && blockTo > Number(relayers.lastSyncBlock)
|
const lastIndex = registerRelayerEvents.length - 1
|
||||||
|
const lastSyncBlock = registerRelayerEvents[lastIndex]?.blockNumber || lastBlock
|
||||||
|
const relayerEvents = cachedEvents.concat(registerRelayerEvents || [])
|
||||||
|
|
||||||
if (isGraphLate) {
|
const events = []
|
||||||
blockFrom = relayers.lastSyncBlock
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!relayers.events.length || isGraphLate) {
|
for (let x = 0; x < relayerEvents.length; x++) {
|
||||||
const multicallEvents = await this.fetchEvents(blockFrom, blockTo)
|
const { ensName, relayerAddress } = relayerEvents[x]
|
||||||
const eventsRelayers = multicallEvents.map(({ returnValues }) => ({
|
let ensAddress
|
||||||
ensName: returnValues.ensName,
|
if (!isAddress(relayerAddress)) {
|
||||||
relayerAddress: returnValues.relayerAddress
|
ensAddress = await this.getENSAddress(ensName)
|
||||||
}))
|
ensAddress = toChecksumAddress(ensAddress)
|
||||||
|
} else {
|
||||||
relayers = {
|
ensAddress = relayerAddress
|
||||||
lastSyncBlock: blockTo,
|
|
||||||
events: relayers.events.concat(eventsRelayers)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
events.push({ ensName, relayerAddress: ensAddress })
|
||||||
}
|
}
|
||||||
|
|
||||||
await this.saveEvents({ storeName: 'register_events', ...relayers })
|
await this.saveEvents({ storeName: 'register_events', lastSyncBlock, events })
|
||||||
allRelayers = allRelayers.concat(relayers.events)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
allRelayers = allRelayers.concat(events)
|
||||||
|
} catch (e) {
|
||||||
|
return cachedEvents
|
||||||
|
}
|
||||||
return allRelayers
|
return allRelayers
|
||||||
}
|
}
|
||||||
|
|
||||||
filterRelayer = (acc, curr, ensSubdomainKey, relayer) => {
|
filterRelayer = (acc, curr, ensSubdomainKey, relayer) => {
|
||||||
const subdomainIndex = subdomains.indexOf(ensSubdomainKey)
|
const subdomainIndex = subdomains.indexOf(ensSubdomainKey)
|
||||||
|
|
||||||
const mainnetSubdomain = curr.records[0]
|
const mainnetSubdomain = curr.records[0]
|
||||||
const hostname = curr.records[subdomainIndex]
|
const hostname = curr.records[subdomainIndex]
|
||||||
const isHostWithProtocol = hostname.includes('http')
|
const isHostWithProtocol = hostname.includes('http')
|
||||||
@ -191,7 +238,6 @@ class RelayerRegister {
|
|||||||
|
|
||||||
getRelayers = async (ensSubdomainKey) => {
|
getRelayers = async (ensSubdomainKey) => {
|
||||||
const relayers = await this.fetchRelayers()
|
const relayers = await this.fetchRelayers()
|
||||||
|
|
||||||
const validRelayers = await this.getValidRelayers(relayers, ensSubdomainKey)
|
const validRelayers = await this.getValidRelayers(relayers, ensSubdomainKey)
|
||||||
|
|
||||||
return validRelayers
|
return validRelayers
|
||||||
|
Loading…
Reference in New Issue
Block a user