2

Is there a way to find out the first transaction sent TO an account?

Hey everyone, I know that given an address, I can check if a given transaction is the first tx.from by checking its nonce. Nonce == 0 indicates that its the first tx.from sent by that account.

But given a transaction TO an account, is there a way for me to see if that is the first tx.to that account? Checking the nonce isn't valid here as you'd be checking the nonce of the sender.

Thanks all :D

2
  • Do you mean the first transaction received by an account ?
    – Ad-h0c
    Commented Sep 9, 2022 at 4:51
  • yup! not the first transaction from an account (can be checked via nonce). Basically want to check if a transaction received by an account is the first ever transaction received.
    – trademarc
    Commented Sep 9, 2022 at 5:25

2 Answers 2

3

The easiest way to do this is by using a service like Covalent, which provides you with an API to query the data as you would like.

I tried web3js and ethersjs and they don't have a function for you to get all the transactions sent to and from an address.

Another option is the run your own blockchain node and query it using geth client. But this one is not a simple thing.

Let's stick to a third party like Covalent for this example.

The following covalent endpoint will return all the transactions ever sent from and to the specified address.

https://api.covalenthq.com/v1/1/address/0x6827b8f6cc60497d9bf5210d602C0EcaFDF7C405/transactions_v2/

The last record in the array data.items is the first transaction. Which we can assume that it's the first transaction that sent ether to this address, because otherwise, it would not be able to send transactions itself, because it wouldn't have any balance yet.

In my Postman setup, it looks like this:

{{url}}/v1/{{chain_id}}/address/{{address}}/transactions_v2/

Where {{url}} is https://api.covalenthq.com

{{chain_id}} is 1 (which is Ethereum mainnet. Binance Smart Chain mainnet chain id is 56, etc).

{{address}} is 0x6827b8f6cc60497d9bf5210d602C0EcaFDF7C405

It returns a long json response, so I'll just put the last item in data.items:

{
                "block_signed_at": "2021-10-11T00:25:12Z",
                "block_height": 13394104,
                "tx_hash": "0xf60f0767c87e3f8eb751e1337e7e342e964aa15401db510ba93a62d73e58feb1",
                "tx_offset": 78,
                "successful": true,
                "from_address": "0xddfabcdc4d8ffc6d5beaf154f18b778f892a0740",
                "from_address_label": "Coinbase 3",
                "to_address": "0x6827b8f6cc60497d9bf5210d602c0ecafdf7c405",
                "to_address_label": null,
                "value": "5228110000000000",
                "value_quote": 17.574775518342285,
                "gas_offered": 21000,
                "gas_spent": 21000,
                "gas_price": 108792787426,
                "fees_paid": "2284648535946000",
                "gas_quote": 7.6800574505051165,
                "gas_quote_rate": 3361.592529296875,
                "log_events": []
            }

If you check the address 0x6827b8f6cc60497d9bf5210d602C0EcaFDF7C405 in etherscan, you can actually see that at that block was the first IN transaction for this address:

https://etherscan.io/address/0x6827b8f6cc60497d9bf5210d602c0ecafdf7c405

https://etherscan.io/tx/0xf60f0767c87e3f8eb751e1337e7e342e964aa15401db510ba93a62d73e58feb1

To call a Covalent API, you will need to get an api key from them. Simply register with them and get one.

Then you simply use the api key as the 'username' in the Authentication param in the header of the http request:

enter image description here

Read their docs: https://www.covalenthq.com/docs/api/#/0/0/USD/1

2
  • 1
    this is actually nice.
    – Ad-h0c
    Commented Sep 9, 2022 at 16:19
  • 1
    amazing as always, thanks Jeremy again!
    – trademarc
    Commented Sep 11, 2022 at 9:41
2

Within my knowledge I don't think it is possible unless you are tracking the state of an account.

Perhaps you might know that ethereum is an one big state machine which is composed of small states known as accounts.

Every time an account performs write operation the state of an account will be updated.

In your case, let us say, if an account is created and the state of this EOA(externally owned account) is zero because you don't own any balance. But when a transaction happens, the state of an account will change.

There are two types "IN" transactions in the ethereum. First one is when an account received "ETH" which directly maps to an account storage address and the second one is when an account receives any type of ERC tokens.

You can check if the receive is the first transaction or not in the first case; however, it is not possible in the second case, unless you are maintaining a huge database like etherscan.

In the first case, the state of an account changes from 0 -> xyz[depending on the ETH an account is received]

And for the second case, although your account receives the erc tokens, in reality, they exists in the ERC smart contract and map to your address

ERC20:

Look at this transaction.

https://goerli.etherscan.io/tx/0xe45479be8e4c6da11beeb7f54a103c23a1f11b962b89bb3f54456483092259ff

10,000 ERC20 tokens are transferred from account 1 to account 2.

tranx

state

In the second image, the state of the "received" account is zero and when the tokens are received the state changed.

I made another transaction but with a different erc20 token. Lets check the state of an account this time.

New transaction with different token

enter image description here

However the state of an account started from zero again because the token is different.

In simple words, you can check an accounts first "IN" if it is ETH transaction or if you are reading to an every event emitted by all smart contracts on the blockchain like ether scan does and query it.

Perhaps you can use etherscan API to read the all blocks from the time of account creation to now to find out the first transaction.

Tell me if it is useful!

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.