Skip to content

Instantly share code, notes, and snippets.

@Turupawn
Last active May 23, 2024 22:50
Show Gist options
  • Save Turupawn/de5a4adb468e3255b968c83918a1d5f5 to your computer and use it in GitHub Desktop.
Save Turupawn/de5a4adb468e3255b968c83918a1d5f5 to your computer and use it in GitHub Desktop.
// run with node etherscan_script.mjs
var ACCOUNT = "XXX"
var API_URL= "https://api-optimistic.etherscan.io/api"
var API_KEY = "YYY"
var MY_TX_COUNT = 0
var INTRACTIONS_WITH_MY_CONTRACTS_COUNT = 0
var ONBOARDED_COUNT = 0
var ONBOARDED_MULTI_TX_COUNT = 0
var ONBOARDED_MULTI_DAY_COUNT = 0
function sleep() {
return new Promise(resolve => setTimeout(resolve, 5000));
}
async function getInteractingAccounts(contractAddress, etherscanApiUrl, etherscanApiKey)
{
await sleep();
let apiCallURL = etherscanApiUrl +
"?module=account" +
"&action=txlist" +
"&address=" + contractAddress
"&startblock=0" +
"&endblock=99999999" +
"&page=0" +
"&offset=0" +
"&sort=asc" +
"&apikey=" + etherscanApiKey
return await fetch(apiCallURL)
.then(response => response.json())
.then(json =>
{
let result = json.result
var uniqueAddressesSet= new Set();
INTRACTIONS_WITH_MY_CONTRACTS_COUNT += result.length
for(let i=0;i<result.length;i++)
{
if (result[i]["from"]) {
uniqueAddressesSet.add(result[i]["from"]);
}
}
return uniqueAddressesSet
})
}
async function getContractsCreated(accountAddress, etherscanApiUrl, etherscanApiKey)
{
await sleep();
let apiCallURL = etherscanApiUrl +
"?module=account" +
"&action=txlist" +
"&address=" + accountAddress
"&startblock=0" +
"&endblock=99999999" +
"&page=0" +
"&offset=0" +
"&sort=asc" +
"&apikey=" + etherscanApiKey
return await fetch(apiCallURL)
.then(response => response.json())
.then(json =>
{
let result = json.result
var uniqueAddressesSet = new Set();
MY_TX_COUNT += result.length
for(let i=0;i<result.length;i++)
{
if(result[i]["methodId"] == "0x60806040")
{
uniqueAddressesSet.add(result[i]["contractAddress"]);
}
}
return uniqueAddressesSet
})
}
async function getSendEth(accountAddress, etherscanApiUrl, etherscanApiKey)
{
await sleep();
let apiCallURL = etherscanApiUrl +
"?module=account" +
"&action=txlist" +
"&address=" + accountAddress
"&startblock=0" +
"&endblock=99999999" +
"&page=0" +
"&offset=0" +
"&sort=asc" +
"&apikey=" + etherscanApiKey
return await fetch(apiCallURL)
.then(response => response.json())
.then(json =>
{
let result = json.result
var uniqueAddressesSet = new Set();
for(let i=0;i<result.length;i++)
{
if(result[i]["methodId"] == "0x"
&& result[i]["contractAddress"] == ""
&& result[i]["to"].toLowerCase() != ACCOUNT.toLowerCase())
{
uniqueAddressesSet.add(result[i]["to"]);
}
}
return uniqueAddressesSet
})
}
async function onboardingCounter(accountAddress, etherscanApiUrl, etherscanApiKey)
{
await sleep();
let apiCallURL = etherscanApiUrl +
"?module=account" +
"&action=txlist" +
"&address=" + accountAddress
"&startblock=0" +
"&endblock=99999999" +
"&page=0" +
"&offset=0" +
"&sort=asc" +
"&apikey=" + etherscanApiKey
return await fetch(apiCallURL)
.then(response => response.json())
.then(json =>
{
let result = json.result
if(result[0]["from"].toLowerCase() == ACCOUNT.toLowerCase())
{
console.log("Onboarded!")
ONBOARDED_COUNT += 1
if(result.length > 1)
{
console.log("Did multiple txs!")
ONBOARDED_MULTI_TX_COUNT += 1
}
console.log("Timespan between first and last tx "
+ (result[result.length-1].timeStamp - result[0].timeStamp) + "s")
if(result[result.length-1].timeStamp - result[0].timeStamp >= 86400)
{
console.log("Done a tx after a day")
ONBOARDED_MULTI_DAY_COUNT += 1
}
}
})
}
var sendEthTxs = Array.from(await getSendEth(
ACCOUNT,
API_URL,
API_KEY
))
var TODOS = new Set()
for(let j=0; j<sendEthTxs.length; j++)
{
console.log("Fetching: " + sendEthTxs[j])
await onboardingCounter(
sendEthTxs[j],
API_URL,
API_KEY
)
}
var contactsCreated = Array.from(await getContractsCreated(
ACCOUNT,
API_URL,
API_KEY
))
var TODOS = new Set()
for(let j=0; j<contactsCreated.length; j++)
{
let interacting = await getInteractingAccounts(
contactsCreated[j],
API_URL,
API_KEY
)
TODOS = new Set([...TODOS, ...interacting])
}
console.log("===================")
console.log("======Results======")
console.log("===================")
console.log("About onboarding:")
console.log("Accounts you onboarded: " + ONBOARDED_COUNT)
console.log("Onboarded accounts that did more than 1 tx: " + ONBOARDED_MULTI_TX_COUNT)
console.log("Onboarded accounts that did a tx after a day: " + ONBOARDED_MULTI_DAY_COUNT)
console.log("About smart contract dev:")
console.log("Contracts created: " + contactsCreated.length)
//console.log(contactsCreated)
console.log("Unique accounts that interacted with your contracts: " + TODOS.size)
//console.log(TODOS)
console.log("Your total transactions: " + MY_TX_COUNT)
console.log("Total transactions in your contracts: " + INTRACTIONS_WITH_MY_CONTRACTS_COUNT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment