Get Transaction Count (Gastracker API)


#1

I am building an app. I need to know two things about an address: 1) balance 2) is it unused. An address can have a zero balance and still be used. I am using this call to get the balance:

https://api.gastracker.io/addr/[address]

But, this call only returns the balance. If I want to know if the address has ever been used, I need to get the transactions. I can do that with this call:

https://api.gastracker.io/v1/addr/[address]/transactions

The problem is that this is massive overkill. I have to make two calls to find out if the address has a balance and is unused. And, I am going to get back a lot of data. I do not need to see all the transactions.

It would be really awesome if the addr json included a transaction count, or a total received so I can tell if the address is used or not. Is there another API other than gastracker that can do this?


#2

What you are looking for is standard eth_getTransactionCount RPC API, also for just balance you can use eth_getBalance. If you want to make one request instead of multiple, just make a batch request.

All these methods are standard Ethereum RPC API, and Gastracker provides an endpoint under https://web3.gastracker.io/

Links:


#3

Thanks, but I’m not using Json RPC. I’m just using Gastracker’s basic API. I plan to implement Json RPC down the track, but I’m just hoping to get the Gastracker API changed if possible. Most APIs support this.


#4

It may be provided by Gastracker API later, but at this moment it’s recommended to use standard RPC. Gastracker API is supposed to be used in addition to it, not replace.


#5

Thanks.

As a general point of feedback about Ethereum Classic in general is that it is lagging in the general area of API support. Gastracker is one of the only reliable ones around. I don’t want to have to implement the RPC just to get simple data that I can get with a GET call for other coins.


#6

Gastracker development was paused for more than a year now, but currently it’s getting restarted so API and other stuff may be improved soon.

It was expected that a standard JSON RPC is easier to use, because in most cases projects already have it as a standard protocol across multiple blockchains.


#7

It’s only a standard protocol across Ethereum and Ethereum Classic. Insight is a more standard protocol.

My app implements about ten coins and Ethereum based APIs are the hardest to deal with. I intend on implementing the Json RPC in my app but I’d prefer if the Ethereum based APIs just worked like other coins.


#8

@splix , I finally got around to trying the JSON RPC and it’s far better. It looked complicated, but I used my C# REST client library called RESTClient.Net, and it worked perfectly first time:

        var restClient = new CF.RESTClientDotNet.RESTClient(new CF.RESTClientDotNet.NewtonsoftSerializationAdapter(), new Uri("https://web3.gastracker.io/"));
        var result = await restClient.PostAsync<string, string>("{\"jsonrpc\":\"2.0\",\"method\":\"eth_getBalance\",\"params\":[\"0x407d73d8a49eeb85d32cf465507dd71d507100c1\", \"latest\"],\"id\":1}", null);

It gives me a Json result.


#9

@splix do you know of any other endpoints I can use for a backup?

Also, do you know of any other Ethereum endpoints I can use?


#10

Unfortunately, getTransactionCount only has transactions sent from the address.

“Returns the number of transactions sent from an address.”

I need it to list all transactions to/from


#11

This is my code that I think should be useful for other C# programmers. It batches up getting balances and transaction counts in one hit. It uses RESTClient.Net

    protected override Func<GetAddressesArgs, Task<IEnumerable<BlockChainAddressInformation>>> GetAddressesFunc { get; } = async getAddressesArgs =>
    {
        var retVal = new List<BlockChainAddressInformation>();

        var requests = new List<RequestBase>();

        var addresses = getAddressesArgs.Addresses.ToList();

        for (var i = 0; i < addresses.Count; i++)
        {
            var address = addresses[i];
            requests.Add(new RequestBase { Method = Method.eth_getBalance, Id = i * 2, Params = { address.ToLower(), DefaultBlock.latest.ToString() } });
            requests.Add(new RequestBase { Method = Method.eth_getTransactionCount, Id = i * 2 + 1, Params = { address.ToLower(), DefaultBlock.latest.ToString() } });
        }

        var balanceResults = await getAddressesArgs.RESTClient.PostAsync<List<ResultBase>, List<RequestBase>>(requests, null);

        balanceResults = balanceResults.OrderBy(br => br.Id).ToList();

        for (var i = 0; i < balanceResults.Count; i += 2)
        {
            var balanceResult = balanceResults[i];

            var request = requests[i];

            if (balanceResult.Error != null)
            {
                throw new Exception($"Error getting address: {request.Params[0]}.\r\nCode: {balanceResult.Error.Code} Message: {balanceResult.Error.Message}");
            }

            var balance = GetEthFromHex(balanceResult.Result);
            var transactionCount = GetLongFromHex(balanceResults[i + 1].Result);

            retVal.Add(new BlockChainAddressInformation(request.Params[0], balance, balance == 0 && transactionCount == 0));
        }

        return retVal;
    };

#12

There is actually a new method geth_getAddressTransactions that provides list of incoming transactions. It’s implemented in latest Classic Geth, and not supported by ETH clients, and not enabled on GasTracker RPC API endpoint yet.

It’s not the count of transactions though, but a list of them, so maybe not what you want. Basically it’s what you can already get through Gastracker REST API.

Can I ask what you’re trying to reach by it? Asking because it’s more complicated in Ethereum compared to cryptocurrencies such as Bitcoin. An address can get an incoming value even without direct transaction, but through a contract. For example if someone is using a multisig with addr 0xAAAA, which would be a smart contract in Ethereum, then by sending some amount to address 0xBBBB there will be only a transaction to smart contract itself. And the code of smart contract will do actual transfer. Balance of 0xBBBB will be increased, balance of 0xAAAA will be decreased, but there sill be only transaction user -> 0xAAAA


#13

This isn’t implemented in any of the APIs I’ve tested. I just get errors.

Basically, I’m just trying to test if the address is empty or not…

However, I’m now venturing in to smart contracts so this is a separate question. I’m now thinking that I could use this call to get all transactions, and then look at the transaction receipts to find the contracts involved. Once I have the contracts involved I could theoretically get the balances of any tokens at the address as well…


#14

@splix as a separate question, are you able to tell me if it is possible see if a given address has any contracts associated with it? I can now get the balance of tokens, but I need to know the token’s contract address before I can query…