1

Can anyone please help me to get list of transaction for ERC20 token similar to

https://api-ropsten.etherscan.io/api?module=account&action=txlist&address=0xd39bdb16d138e5219d013cba3d94c327bd246302&sort=asc

This is my ERC20 token address : 0xd39bdb16d138e5219d013cba3d94c327bd246302

I tried with this

http://api.etherscan.io/api?module=account&action=tokentx&address=0xd39bdb16d138e5219d013cba3d94c327bd246302&startblock=0&endblock=999999999&sort=asc&apikey=YourApiKeyToken

But am getting no transactions found

enter image description here

But i can see transactions in etherscan

enter image description here

3 Answers 3

3
  1. Change module=account to module=logs
  2. Change action=tokentx to action=getLogs
  3. Add topic0=0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef

The last one is the hash of an ERC20 Transfer event.

You can obtain it programmatically, for example, using web3.js v1.x:

const Web3 = require("web3");
const TRANSFER_EVENT = Web3.utils.keccak256("Transfer(address,address,uint256)");
console.log(TRANSFER_EVENT);
0

Through web3 code, when you query a particular transaction using web3.eth.getTransactionReceipt(id)

The response will contain a input parameter, check input.substring(0,10) == '0xa9059cbb' to ensure it is an erc20 transaction

Then you need to check input.substring(34,75) to get "to" address.

0

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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