Quickstart guide

1inch Transaction Traces API:

1inch's Transaction Traces API provides users with an efficient way to access transaction traces for different chains.

1. Check Synced Interval:

Before fetching any transaction traces, it's useful to know the synced interval. It gives the range of block numbers that have been synced and are available for query.

Endpoint:

Text
1
GET /v1.0/chain/{chain}/synced-interval

Parameters:

  • chain (required): Chain ID. For Ethereum mainnet, it would be 1.

Sample Request:

Text
1
https://api.1inch.com/traces/v1.0/chain/1/synced-interval

2. Get Block Trace by Number:

To retrieve the transaction traces of all transactions in a specific block.

Endpoint:

Text
1
GET /v1.0/chain/{chain}/block-trace/{blockNumber}

Parameters:

  • chain (required): Chain ID.
  • blockNumber (required): The block number you wish to retrieve traces from.

Sample Request:

Text
1
https://api.1inch.com/traces/v1.0/chain/1/block-trace/15000000

3. Get Block Trace by Transaction Hash:

If you are specifically interested in the trace of a particular transaction within a block, you can fetch it using the transaction hash.

Endpoint:

Text
1
GET /v1.0/chain/{chain}/block-trace/{blockNumber}/tx-hash/{txHash}

Parameters:

  • chain (required): Chain ID.
  • blockNumber (required): The block number containing the transaction.
  • txHash (required): The hash of the specific transaction you want to trace.

Sample Request:

Text
1
https://api.1inch.com/traces/v1.0/chain/1/block-trace/17378177/tx-hash/0x16897e492b2e023d8f07be9e925f2c15a91000ef11a01fc71e70f75050f1e03c

Prerequisites:

  • Node.js and npm installed.
  • A text editor or IDE.
  • API key for the Transaction Traces API.

Steps:

  1. Initialize a New Node.js Project:

    Bash
    1
    2
    3
    4
    mkdir traces_api_tutorial
    cd traces_api_tutorial
    npm init -y
    
  2. Install the Required Packages:

    We'll be using axios to make the HTTP requests:

    Bash
    1
    2
    npm install axios
    
  3. Set Up Your API Key:

    For the sake of security, store your API key in a .env file:

    Bash
    1
    2
    echo "API_KEY=YOUR-API-KEY" > .env
    

    Then, install dotenv to use this file:

    Bash
    1
    2
    npm install dotenv
    
  4. Write the Code:

    Create a new file named index.js and add the following:

JavaScript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
require("dotenv").config();
const axios = require("axios");

const BASE_URL = "https://api.1inch.com/traces/v1.0/chain";

async function getSyncedInterval(chain) {
  const url = `${BASE_URL}/${chain}/synced-interval`;
  const response = await axios.get(url, {
    headers: {
      Authorization: `Bearer ${process.env.API_KEY}`
    }
  });
  return response.data;
}

async function getBlockTraceByNumber(chain, blockNumber) {
  const url = `${BASE_URL}/${chain}/block-trace/${blockNumber}`;
  const response = await axios.get(url, {
    headers: {
      Authorization: `Bearer ${process.env.API_KEY}`
    }
  });
  return response.data;
}

async function getBlockTraceByNumberAndTxHash(chain, blockNumber, txHash) {
  const url = `${BASE_URL}/${chain}/block-trace/${blockNumber}/tx-hash/${txHash}`;
  const response = await axios.get(url, {
    headers: {
      Authorization: `Bearer ${process.env.API_KEY}`
    }
  });
  return response.data;
}

function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

(async function () {
  try {
    const chain = 1; // Example chain
    const blockNumber = 15000000; // Example block number
    const txHash = "0x16897e492b2e023d8f07be9e925f2c15a91000ef11a01fc71e70f75050f1e03c"; // Example transaction hash

    const syncedInterval = await getSyncedInterval(chain);
    console.log("Synced Interval:", syncedInterval);

    await sleep(1000); // Sleep for 1 second to avoid rate limit

    const blockTrace = await getBlockTraceByNumber(chain, blockNumber);
    console.log("Block Trace:", blockTrace);

    await sleep(1000); // Sleep for 1 second to avoid rate limit

    const txTrace = await getBlockTraceByNumberAndTxHash(chain, blockNumber, txHash);
    console.log("Transaction Trace:", txTrace);
  } catch (error) {
    console.error("Error fetching data:", error.response ? error.response.data : error.message);
  }
})();
  1. Run Your Script:

    Bash
    1
    2
    node index.js
    

Transaction traces are a powerful tool for anyone interacting with or developing on the blockchain. They provide invaluable insights into the workings of smart contracts, helping improve efficiency, security, and understanding. With 1inch's Transaction Traces API, accessing these insights becomes a seamless process, enabling deeper interaction with blockchain data.

With the steps above, you'll be able to interact with the Transaction Traces API using Node.js. Make sure you replace 'API-KEY' in the .env file with your actual API key.

Did you find what you need?