Skip to content

Instantly share code, notes, and snippets.

@fzn0x
Forked from shov/fetchtransactionsbywallet.md
Last active January 6, 2022 17:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fzn0x/bdb15dc3dabf9929401664aa49fc33ee to your computer and use it in GitHub Desktop.
Save fzn0x/bdb15dc3dabf9929401664aa49fc33ee to your computer and use it in GitHub Desktop.
Etherscan Public API Fetch By Contract or Wallet Address Tips

Etherscan Public API Fetch By Contract or Wallet Address Tips (edited by fncolon)

Overview

There is no documentation at the moment, but there are some rumors. And experimental way I've made sure about few end-points are working good:

Common account info

GET: https://www.etherchain.org/api/account/0xAAAsomeADDR00000000000/ Will return something like that:

{
  "address": "0xAAAsomeADDR00000000000",
  "balance": "0",
  "name": null,
  "firstseen": "2018-02-13T08:25:30.000Z",
  "createdby": "d8e93673v4ghhshd7j9l61adf2b1d43742d8f8629586k6kfjmft98c112b16d6u9",
  "lastseen": "2018-02-20T10:29:08.000Z",
  "blocksmined": 0,
  "unclesmined": 0,
  "nameverified": null
}

Actions interface

But in general there is another end-point which use GET-params-quering approach:

To get balance call: GET: http://api.etherscan.io/api?module=account&action=balance&address=0xAAAsomeADDR00000000000

And you'll get:

{
  "status": "1",
  "message": "OK",
  "result": "0"
}

As we seen there are three important arguments:

  • module - it's just like controller or class or lib
  • action - action/method
  • address - here you should put a wallet

Transactions

To fetch all someone transaction history 'etherscan' offer just a one way: https://etherscan.io/address/0xAAAsomeADDR00000000000 and here you'll see web interface to comfortable read transaction list.. but if you looking for approach to fetch all them as json with your application there is one API:

GET: http://api.etherscan.io/api?module=account&action=txlist&address=0xAAAsomeADDR00000000000&sort=asc

Example of a response:

{
  "status": "1",
  "message": "OK",
  "result": [
    {
      "blockNumber": "------",
      "timeStamp": "-------",
      "hash": "0x-----",
      "nonce": "1",
      "blockHash": "0x--------",
      "transactionIndex": "70",
      "from": "0x----------",
      "to": "0x---------",
      "value": "87000",
      "gas": "636879",
      "gasPrice": "10000000000",
      "isError": "0",
      "txreceipt_status": "1",
      "input": ".. loooong value ...",
      "contractAddress": "0x--------",
      "cumulativeGasUsed": "3190544",
      "gasUsed": "636879",
      "confirmations": "-----"
    },
    ...
  ]
}

Pay attention a value gives in wei and you might convert it to eth before use it in your processes, you albe to do it by formula eth = wei / 10 ** <token.decimals> and validate your result here for example

Get Event/Method Name

  async getEventName(txHash) {
    const tx = await provider.getTransactionReceipt(txHash);

    const decodedInput = inter.parseLog({
      data: tx.logs[0].data,
      topics: tx.logs[0].topics,
    });

    // Decoded Transaction
    return decodedInput.name;
  }

Get Event/Method Name With Method ID Fallback like Etherscan

  // txHash, transaction input
  async getEventName(txHash, input) {
    const tx = await provider.getTransactionReceipt(txHash);

    if (tx.logs.length > 0) {
      const decodedInput = inter.parseLog({
        data: tx.logs[0].data,
        topics: tx.logs[0].topics,
      });

      // Decoded Transaction
      return decodedInput.name;
    } else {
      return input || "";
    }
  }

ฅ(≚ᄌ≚) Have a nice day!

@fzn0x
Copy link
Author

fzn0x commented Jan 6, 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment