1

I have an Alchemy API python script that checks the balance for an Ethereum address. You can set up the Alchemy API object like this:

from alchemy import Alchemy, Network

# create Alchemy object using your Alchemy api key, default is "demo"
api_key = "your_api_key"

# choose preferred network from Network, default is ETH_MAINNET
network = Network.ETH_MAINNET

# choose the maximum number of retries to perform, default is 5
max_retries = 3

# create Alchemy object
alchemy = Alchemy(api_key, network, max_retries=max_retries)

From here you can get the ether balance of an address like this:

eth_address_balance = alchemy.core.get_balance("ADDRESS", "BLOCK_NUMBER")

Notice that you can get the address balance at any block height by adding "BLOCK_NUMBER" to the get_balance function.

How would I get all transactions that send ether to a certain address between 2 block heights?

I know your first instinct is to tell me to check Etherscan but I've noticed that some exchanges send ether to my address and no transaction appears on Etherscan... That's why I'm trying to build this script.

1 Answer 1

0

Usually you'd find the native Ethereum transfer transactions in the Internal transactions tab on Etherscan.

internal-ethereum-transfers-uniswap


Here is a great Alchemy's explanation about what Internal transactions are: (https://docs.alchemy.com/docs/what-are-internal-transactions)


To get all the account's internal transactions with the Alchemy API, you can use the following API request:

{
  "jsonrpc": "2.0",
  "id": 0,
  "method": "alchemy_getAssetTransfers",
  "params": [
    {
      "fromBlock": "0xA97AB8",
      "toBlock": "0xA97CAC",
      "maxCount": "0x5",
      "excludeZeroValue": true,
      "category": [
        "internal"
      ]
    }
  ]
}
  • And you can also specify the "fromAddress" or "toAddress" properties to filter out only the outcoming OR the incoming transactions of an address, respectively

The following is a simple Javascript script example:

// Setup: npm install alchemy-sdk
import { Alchemy, Network } from "alchemy-sdk";

const config = {
  apiKey: "<-- ALCHEMY APP API KEY -->",
  network: Network.ETH_MAINNET,
};
const alchemy = new Alchemy(config);

const data = await alchemy.core.getAssetTransfers({
  fromBlock: "0x0",
  fromAddress: "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",
  category: ["internal"],
});

console.log(data);

With Python you can do something like that:

import logging
from alchemy import Alchemy, Network

# create Alchemy object using your Alchemy api key, default is "demo"
api_key = "[YOUR_VALID_ALCHEMY_API_KEY]"

# choose preferred network from Network, default is ETH_MAINNET
network = Network.ETH_MAINNET

# choose the maximum number of retries to perform, default is 5
max_retries = 3

# create Alchemy object
alchemy = Alchemy(api_key, network, max_retries=max_retries)

logging.basicConfig(level=logging.DEBUG, filename='sample.log')

internalTransferData = alchemy.core.get_asset_transfers(category=['internal'], to_address='0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045')

logging.debug(internalTransferData)

It will log all the internal Ethereum transfers that Vitalik's wallet received.

(Basically the same result as shown here: https://etherscan.io/address/vitalik.eth#internaltx)

You'll find the received data logs in sample.log.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.