All Collections
Web3 API
How to Get the First Transaction of a Contract Address?
How to Get the First Transaction of a Contract Address?

This article show how to get the first transactions of a contract address using Moralis API

John Pilla avatar
Written by John Pilla
Updated over a week ago

Sample Code

To get the first transaction of a contract, you can utilize the Moralis getWalletActiveChains API. Here's a simple example of how to use it:

const Moralis = require("moralis").default;
const { EvmChain } = require("@moralisweb3/common-evm-utils");

const runApp = async () => {
await Moralis.start({
apiKey: "YOUR_API_KEY",
});

const address = "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d";
const chains = [EvmChain.ETHEREUM];

const response = await Moralis.EvmApi.wallets.getWalletActiveChains({
address,
chains,
});

console.log(response.toJSON());
}

runApp();

I used an NFT contract address for the example.

The response from the above code will provide details about the first transaction, including the date and block number:

{
"address": "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d",
"active_chains": [
{
"chain": "eth",
"chain_id": "0x1",
"first_transaction": {
"block_timestamp": "2021-04-22T23:13:40.000Z",
"block_number": "12292922",
"transaction_hash": "0xcfb197f62ec5c7f0e71a11ec0c4a0e394a3aa41db5386e85526f86c84b3f2796"
},
"last_transaction": {
"block_timestamp": "2023-09-13T09:14:11.000Z",
"block_number": "18126424",
"transaction_hash": "0x7414c1dcb63cf87e17a694419c13c231906571ce5d4d11c4f2aa76aa3a8cd8b6"
}
}
]
}

Use Cases

  1. Getting the Age of the Contract Address: By retrieving the first transaction's timestamp, you can easily determine the age of a contract. This can be useful for analytics, trust scoring, or other analytical purposes.

  2. Fetching Initial Transactions: With the block number of the first transaction, you can retrieve a set of subsequent initial transactions of the NFT contract using getNFTContractTransfers.

    For instance, to get the early transactions of a contract using the fromBlock and toBlock values:

const response = await await Moralis.EvmApi.nft.getNFTContractTransfers({
"chain": EvmChain.ETHEREUM,
"fromBlock": 12292922,
"toBlock": 12293022,
"address": "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d",
"format": "decimal"
});

This will fetch transactions from block 12292922 to 12293022, giving you a snapshot of the contract's early activities and making it easy to retrieve the initial transactions of a contract without having to paginate through all the transaction data.

Did this answer your question?