Skip to content

Instantly share code, notes, and snippets.

@samsonajulor
Last active August 5, 2023 17:02
Show Gist options
  • Save samsonajulor/23cb467676e3a991d22458b1f4c9dd40 to your computer and use it in GitHub Desktop.
Save samsonajulor/23cb467676e3a991d22458b1f4c9dd40 to your computer and use it in GitHub Desktop.
1. Write a script to interact with an Ethereum account on mainnet , return its balance and transaction count.
import { ethers } from 'ethers';
async function getBalanceAndTransactionCount(address: string) {
try {
// Create a default provider for mainnet
const provider = ethers.getDefaultProvider('mainnet');
// Get the balance of the Ethereum address
const balance = await provider.getBalance(address);
console.log('Balance:', ethers.utils.formatEther(balance), 'ETH');
// Get the transaction count (number of transactions) of the Ethereum address
const transactionCount = await provider.getTransactionCount(address);
console.log('Transaction Count:', transactionCount);
} catch (error) {
console.error('Error:', error);
}
}
// Replace 'YOUR_ETHEREUM_ADDRESS' with the actual Ethereum address you want to query
const ethereumAddress = 'YOUR_ETHEREUM_ADDRESS';
getBalanceAndTransactionCount(ethereumAddress);
2. Given the addresses below:
0x57f1887a8BF19b14fC0dF6Fd9B2acc9Af147eA85
0xf4bfaf916a68b0fC859D63a319034C0f72A88a5C
0x3b138FC7eC06B2A44565994CfDe5134A75915995
write a script to determine the account type of the address.
function checkAccountType(address: string) {
const provider = ethers.getDefaultProvider('mainnet');
console.log(provider);
const code = await provider.getCode(address);
if (code === '0x') {
console.log(`${address} is an Externally Owned Account (EOA).`);
} else {
console.log(`${address} is a Contract Account.`);
}
}
const addressesToCheck = [
'0x57f1887a8BF19b14fC0dF6Fd9B2acc9Af147eA85',
'0xf4bfaf916a68b0fC859D63a319034C0f72A88a5C',
'0x3b138FC7eC06B2A44565994CfDe5134A75915995'
];
addressesToCheck.forEach(address => {
checkAccountType(address);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment