Developer Advocate at Alchemy here :)
You can do this with the getAssetTransfers
endpoint from the Alchemy Transfers API, which the Alchemy SDK has support for.
For example, in javascript:
// Setup: npm install alchemy-sdk
const alchemySdk = require("alchemy-sdk");
const Alchemy = alchemySdk.Alchemy;
const Network = alchemySdk.Network;
(async function main() {
// Configure the Alchemy SDK instance with an https://www.alchemy.com API key.
const config = {
apiKey: "your api key",
network: Network.ETH_MAINNET,
};
const alchemy = new Alchemy(config);
// Define the address and ERC20 token contract I care about.
const USDCaddress = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
const thatguyintech = "thatguyintech.eth";
// Fetch transactions going into the desired address.
const usdcTransfersToAlbert = await alchemy.core.getAssetTransfers({
toAddress: thatguyintech,
contractAddresses: [USDCaddress],
category: ["erc20"],
});
console.log("transfers to me: ", usdcTransfersToAlbert);
// Fetch transactions leaving from the desired address.
const usdcTransfersFromAlbert = await alchemy.core.getAssetTransfers({
fromAddress: thatguyintech,
contractAddresses: [USDCaddress],
category: ["erc20"],
});
console.log("transfers from me: ", usdcTransfersFromAlbert);
})();
This would return all the USDC-related transactions for thatguyintech.eth
(there's ENS resolution too, which is super nice :D )
transfers to me: {
transfers: [
{
blockNum: '0xc59520',
uniqueId: '0xe1ccb0199953c1e2bdd23a40d272f9c8fab9b29cfe52c832569d8511ef689d5f:log:290',
hash: '0xe1ccb0199953c1e2bdd23a40d272f9c8fab9b29cfe52c832569d8511ef689d5f',
from: '0x88e6a0c2ddd26feeb64f039a2c41296fcb3f5640',
to: '0xf5fff32cf83a1a614e15f25ce55b0c0a6b5f8f2c',
value: 246.311717,
erc721TokenId: null,
erc1155Metadata: null,
tokenId: null,
asset: 'USDC',
category: 'erc20',
rawContract: [Object]
},
...
]
}
transfers from me: {
transfers: [
{
blockNum: '0xd2ad27',
uniqueId: '0x0da90c9c984f764008b298920ff8d0f1c5cfdabde52af4f6caf0f4c94fdccb0a:log:443',
hash: '0x0da90c9c984f764008b298920ff8d0f1c5cfdabde52af4f6caf0f4c94fdccb0a',
from: '0xf5fff32cf83a1a614e15f25ce55b0c0a6b5f8f2c',
to: '0x93fbe19d6fe3eb7618d9fd4318899c4f51b081c7',
value: 100,
erc721TokenId: null,
erc1155Metadata: null,
tokenId: null,
asset: 'USDC',
category: 'erc20',
rawContract: [Object]
},
...
]
}
You can switch across other supported networks by changing the network
attribute in the config
.
i.e. with these options:
// from node_modules/alchemy-sdk/dist/src/types/types.d.ts
export declare enum Network {
ETH_MAINNET = "eth-mainnet",
ETH_GOERLI = "eth-goerli",
OPT_MAINNET = "opt-mainnet",
OPT_GOERLI = "opt-goerli",
ARB_MAINNET = "arb-mainnet",
ARB_GOERLI = "arb-goerli",
MATIC_MAINNET = "polygon-mainnet",
MATIC_MUMBAI = "polygon-mumbai",
ASTAR_MAINNET = "astar-mainnet"
}
Hope that helps!