2

I am trying to create a new wallet similar to Metamask. I have implemented few simple features and now I am looking to display transaction history for a particular wallet address. I have tried multiple ways to access the transaction history using Web3.js, Ethers and EtherScan library but it seems like there is no function called getHistory() or getTranansactionHistory().

There is a getTransaction() but for this function I have to pass in transaction hash code but I don't know how to access that.

Note: I have also connected and utilizing Infura.

3 Answers 3

2

Long story short, you can't do this with Ethereum's JSON RPC endpoints.

The reason is (based on my own understanding) Ethereum's data storage doesn't associate transactions with account addresses. The account address is stored in the state trie. Transaction trie and receipt trie are rooted in every block. It will be very expensive to retrieve historical transactions from an address with current data structure.

You can try other third-party services like etherscan API for tx history though.

1

You can do this with Etherscan API here is a function to get you started.

def getTokenTransactions(wallet_address, api_key):
url = f"https://api.etherscan.io/api?module=account&action=tokentx&address={wallet_address}&startblock=0&endblock=999999999&sort=asc&apikey={api_key}"
response = requests.get(url)
data = response.json()

if data["status"] == "1" and data["message"] == "OK":
    return data["result"]
else:
    return []
1

Ethers.js v6 does not implements getHistory in EtherscanProvider.

But you can extend it:


import { EtherscanProvider, Networkish, BlockTag } from 'ethers'; //^v6

export default class MyEtherscanProvider extends EtherscanProvider {

    constructor(networkish: Networkish, apiKey?: string) {
        super(networkish, apiKey);
    }

    async getHistory(address: string, startBlock?: BlockTag, endBlock?: BlockTag): Promise<Array<any>> {
        const params = {
            action: "txlist",
            address,
            startblock: ((startBlock == null) ? 0 : startBlock),
            endblock: ((endBlock == null) ? 99999999 : endBlock),
            sort: "asc"
        };

        return this.fetch("account", params);
    }
}

const myEtherScanInstance = new MyEtherscanProvider();
myEtherScanInstance.getHistory("0xSOME_ADDRESS_HERE").then(console.log);

The output is like:

[
       {
          blockNumber: '29203699',
          blockHash: '0x5603345265479915b16441b157d97f433f1f0fc9cd6579f44958539ec91dabd3',
          timeStamp: '1668722061',
          hash: '0xf7f03d30600fe58c41e1b611290622cf6d6f06f928101fcfb1185ec95042b956',
          nonce: '65',
          transactionIndex: '15',
          from: '0x7c125c1d515b8945841b3d5144a060115c58725f',
          to: '0xeb71ed911e4dfc35da80103a72fe983c8c709f33',
          value: '163225741820703280',
          gas: '21000',
          gasPrice: '1500000009',
          input: '0x',
          methodId: '0x',
          functionName: '',
          contractAddress: '',
          cumulativeGasUsed: '2396447',
          txreceipt_status: '1',
          gasUsed: '21000',
          confirmations: '7068649',
          isError: '0'
        },
        ... 9900 more items
      ]

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.