Created
August 2, 2023 00:26
-
-
Save dbeal-eth/9928c180139d1ea893e007143e9dd08a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const MAX_FAIL = parseInt(args[0], 16); | |
const chainMapping = { | |
'11155111': [ | |
`https://sepolia.infura.io/v3/${secrets.INFURA_API_KEY}`, | |
`https://rpc.sepolia.org`, | |
], | |
'10': [ | |
`https://mainnet.optimism.io`, | |
`https://optimism-mainnet.infura.io/v3/${secrets.INFURA_API_KEY}`, | |
], | |
'80001': [ | |
`https://polygon-mumbai.infura.io/${secrets.INFURA_API_KEY}` | |
] | |
}; | |
let responses = []; | |
for (let i = 1;i < args.length;i += 3) { | |
const chainId = parseInt(args[i], 16); | |
const callTo = args[i + 1] | |
const callHex = args[i + 2]; | |
const urls = chainMapping[chainId]; | |
const params = { jsonrpc: '2.0', id: 1, method: 'eth_call', params: [{ to: callTo, data: callHex }, 'latest'] }; | |
const results = (await Promise.allSettled( | |
urls.map(url => Functions.makeHttpRequest({ url, method: 'POST', data: params })) | |
)).map(r => r.value); | |
let ress = []; | |
for (const [i, result] of results.entries()) { | |
if (!result.data) { | |
console.log(`error on ${chainId}, API endpoint idx ${i}`, result.toString()); | |
} else { | |
ress.push(result.data.result); | |
} | |
} | |
if (urls.length - ress.length > MAX_FAIL) { | |
throw new Error('exceeded failure threshold'); | |
} | |
ress.sort(); | |
responses.push(ress[Math.floor(ress.length / 2)].slice(2)); | |
} | |
return Buffer.from(responses.join(''), 'hex'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment