Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Created September 10, 2023 07:03
Show Gist options
  • Save miguelmota/2563482195c4b777c4d77af392a56614 to your computer and use it in GitHub Desktop.
Save miguelmota/2563482195c4b777c4d77af392a56614 to your computer and use it in GitHub Desktop.
JavaScript get etherscan logs by topic for contract address
async function getEtherscanLogs (chain: string, fromBlock: number, toBlock: number, address: string, topic0: string, topic1?: string) {
const baseUrl = 'https://api-goerli.etherscan.io'
let url = `${baseUrl}/api?module=logs&action=getLogs&address=${address}&fromBlock=${fromBlock}&toBlock=${toBlock}&topic0=${topic0}&page=1&offset=0&apikey=YourApiKeyToken`
if (topic1) {
url += `&topic0_1_opr=and&topic1=${topic1}`
}
const res = await fetch(url)
const json = await res.json()
const logs = json.result
return logs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment