3. Viewing account and transaction information using Web3 Data API

이전 튜토리얼을 통해 스마트 컨트랙트가 무엇인지 배우고 직접 이더리움 Holesky에서 트랜잭션을 실행해 스마트 컨트랙트를 배포하고 호출해 보았습니다. 이번 튜토리얼에서는 Nodit Web3 Data API를 이용해 배포를 위해 실행한 트랜잭션의 정보와 스마트 컨트랙트를 배포한 계정의 트랜잭션 정보를 확인해 보도록 하겠습니다.

📘

이번 튜토리얼을 진행하기 위해서는 Nodit API Key가 필요해요!

Nodit API Key는 Nodit 콘솔에 회원가입하여 획득할 수 있습니다. 아래 링크를 클릭하여 Nodit 콘솔에 회원가입하고 다양한 블록체인 노드를 이용해 보세요!


트랜잭션 조회

트랜잭션을 통해 setMessage 함수를 실행했을 때 다음과 같은 트랜잭션 정보를 확인할 수 있습니다.

{
  type: 2,
  chainId: 17000,
  nonce: 13,
  maxPriorityFeePerGas: BigNumber { _hex: '0x59682f00', _isBigNumber: true },
  maxFeePerGas: BigNumber { _hex: '0x59682f0e', _isBigNumber: true },
  gasPrice: null,
  gasLimit: BigNumber { _hex: '0x01303d', _isBigNumber: true },
  to: '0x4c12Da0413EA27D6Fe3957955940FFBA949C4cdB',
  value: BigNumber { _hex: '0x00', _isBigNumber: true },
  data: '0x368b87720000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003c4e6f646974206973206f6e65206f66207468652062657374204e6f64652050726f7669646572205365727669636520696e2074686520776f726c642100000000',
  accessList: [],
  hash: '0xd58046b4ca4558b90312b83b8e1f8bd1dbea870be516239855b93141463dafba',
  v: 0,
  r: '0xb38d85427e3c4558bbc8cb27b095d57ee828b80fa3345d78d6a1d864f40405f4',
  s: '0x43916907718ca24ed6474a7382f383ba338955c754437db2d6985a6a76635c4b',
  from: '0xEc801CFCF110675F32b593A7f25d09B8f7c6D291',
  confirmations: 0,
  wait: [Function (anonymous)]
}

이러한 트랜잭션 정보를 Nodit Web3 Data API를 이용해서 확인할 수 있습니다. Nodit Web3 Data API 중 Get Transaction By Hash API를 이용해 동일한 트랜잭션 정보를 확인해 보겠습니다.

타입스크립트 환경에서 간편하게 HTTP 요청을 전송하기 위해 axios라는 라이브러리를 설치합니다.

$ npm install axios

axios를 설치한 후 axios 인스턴스를 생성해 Nodit Web3 Data API를 호출합니다. aixos 인스턴스의 경우, baseURL과 headers를 미리 설정하여 편리하게 HTTP 요청을 전송할 수 있게 합니다.

// axiosRequest.ts

import axios from "axios";

const noditAPIKey = "your_nodit_api_key";
const axiosInstance = axios.create({
  baseURL: "https://web3.nodit.io/v1/ethereum/holesky",
  headers: {
    "Content-Type": "application/json",
    Accept: "application/json",
    "X-API-KEY": noditAPIKey,
  },
});

그리고 즉시실행함수를 이용해 axios 인스턴스가 Get Transaction By Hash API를 실행하는 코드를 작성합니다. Get Transaction By Hash API는 트랜잭션 해시를 필수 Body Params로 받습니다. withDecode를 true로 설정할 경우 해당 트랜잭션의 Data 필드에 입력된 데이터를 디코딩 된 값으로 확인할 수 있습니다.

(async () => {
  try {
    const txResult = await axiosInstance.post(
      "/blockchain/getTransactionByHash",
      {
        transactionHash:
          "0xd58046b4ca4558b90312b83b8e1f8bd1dbea870be516239855b93141463dafba",
        withDecode: true,
      }
    );
    console.log(txResult.data);
    console.log(txResult.data.decodedInput.args[0]);
  } catch (error) {
    console.error(error);
  }
})();


전체 코드는 다음과 같습니다. 파일을 실행하고 결과를 확인해 보세요!

// axiosRequest.ts

import axios from "axios";

const noditAPIKey = "your_nodit_api_key";
const axiosInstance = axios.create({
  baseURL: "https://web3.nodit.io/v1/ethereum/holesky",
  headers: {
    "Content-Type": "application/json",
    Accept: "application/json",
    "X-API-KEY": noditAPIKey,
  },
});

(async () => {
  try {
    const txResult = await axiosInstance.post(
      "/blockchain/getTransactionByHash",
      {
        transactionHash:
          "0xd58046b4ca4558b90312b83b8e1f8bd1dbea870be516239855b93141463dafba",
        withDecode: true,
      }
    );
    console.log(txResult.data);
    console.log(txResult.data.decodedInput.args[0]);
  } catch (error) {
    console.error(error);
  }
})();


아래와 같이 트랜잭션의 정보와 디코딩 된 트랜잭션의 Data 필드의 값을 확인할 수 있습니다.

{
  transactionHash: '0xd58046b4ca4558b90312b83b8e1f8bd1dbea870be516239855b93141463dafba',
  transactionIndex: '4',
  blockHash: '0x2710f36851312fbe2a20c8afd04b1f34b135e1cf59b0bf8881ee8fc1f91da5b0',
  blockNumber: '2319452',
  from: '0xEc801CFCF110675F32b593A7f25d09B8f7c6D291',
  to: '0x4c12Da0413EA27D6Fe3957955940FFBA949C4cdB',
  value: '0',
  input: '0x368b87720000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003c4e6f646974206973206f6e65206f66207468652062657374204e6f64652050726f7669646572205365727669636520696e2074686520776f726c642100000000',
  functionSelector: '0x368b8772',
  decodedInput: {
    type: 'function',
    name: 'setMessage',
    signature: 'setMessage(string)',
    args: [ [Object] ]
  },
  nonce: '13',
  gas: '77885',
  gasPrice: '1500000007',
  maxFeePerGas: '1500000014',
  maxPriorityFeePerGas: '1500000000',
  gasUsed: '76997',
  cumulativeGasUsed: '726811',
  effectiveGasPrice: '1500000007',
  contractAddress: null,
  type: '2',
  status: '1',
  logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
  accessList: [],
  timestamp: 1726125228
}

// console.log(txResult.data.decodedInput.args[0]);
{
  name: 'message',
  type: 'string',
  value: 'Nodit is one of the best Node Provider Service in the world!'
}

📘

이더리움 Explorer에서는 트랜잭션이 확인되는데 Web3 Data API로는 결과가 반환되지 않아요.

Web3 Data API의 경우, 데이터 무결성을 확보하기 위해 6블록이 지나 변경되지 않을 데이터를 제공하는 것을 정책으로 합니다. 따라서 블록이 생성된 직후 API로 조회할 경우 응답이 반환되지 않을 수 있으며 즉시 데이터를 반환받고 싶다면 Node API의 eth_getTransactionByHash API를 이용해 보세요!


계정을 이용한 트랜잭션 조회

이번에는 계정을 이용해 해당 계정이 실행한 트랜잭션을 조회해 보도록 하겠습니다. Nodit의 Web3 Data API 중 Get Transactions By Account API를 이용합니다. 즉시실행함수의 내용을 아래와 같이 수정합니다.

import axios from "axios";

const noditAPIKey = "your_nodit_api_key";
const axiosInstance = axios.create({
  baseURL: "https://web3.nodit.io/v1/ethereum/holesky",
  headers: {
    "Content-Type": "application/json",
    Accept: "application/json",
    "X-API-KEY": noditAPIKey,
  },
});

(async () => {
  try {
    const txResult = await axiosInstance.post(
      "/blockchain/getTransactionsByAccount",
      {
        accountAddress: "0xEc801CFCF110675F32b593A7f25d09B8f7c6D291",
        withDecode: true,
      }
    );
    for (const item of txResult.data.items) {
      console.log(item);
    }
  } catch (error) {
    console.error(error);
  }
})();

Get Transactions By Account API는 accountAddress를 인자로 받습니다. 그리고 withDecode를 true로 하여 디코딩 된 트랜잭션의 Data 필드의 값을 확인합니다. 실행하면 다음과 같이 결과를 확인할 수 있습니다.

{
  transactionHash: '0x269382830d9910b6c80399ef3af87b6217a41caee24a0d3c24e32d72cc56f866',
  transactionIndex: '2',
  blockHash: '0x2c1c0196f4ae18a7bd48e4c5c4e3328b8026f947839f3b92e137504122463a61',
  blockNumber: '2320187',
  from: '0xEc801CFCF110675F32b593A7f25d09B8f7c6D291',
  to: '0x4c12Da0413EA27D6Fe3957955940FFBA949C4cdB',
  value: '0',
  input: '0x368b87720000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000003d4e6f646974206973206f6e65206f66207468652062657374204e6f64652050726f7669646572205365727669636520696e2074686520776f726c642121000000',
  functionSelector: '0x368b8772',
  decodedInput: {
    type: 'function',
    name: 'setMessage',
    signature: 'setMessage(string)',
    args: [ [Object] ]
  },
  nonce: '15',
  gas: '46015',
  gasPrice: '1500000007',
  maxFeePerGas: '1500000014',
  maxPriorityFeePerGas: '1500000000',
  gasUsed: '41100',
  cumulativeGasUsed: '526530',
  effectiveGasPrice: '1500000007',
  contractAddress: null,
  type: '2',
  status: '1',
  logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
  accessList: [],
  timestamp: 1726134900
},
{
  transactionHash: '0x80846b0c169656f1bcb390b1286205f792d0ee9a456d320a7703ca8e168225e2',
  transactionIndex: '1',
  blockHash: '0x914d95b6ca009d9b198cd15beb0e279652fd38cc93ef8925ecbbff7320524087',
  blockNumber: '2318601',
  from: '0xEc801CFCF110675F32b593A7f25d09B8f7c6D291',
  to: '0x4c12Da0413EA27D6Fe3957955940FFBA949C4cdB',
  value: '0',
  input: '0x368b87720000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000f49742069732061206d6573736167650000000000000000000000000000000000',
  functionSelector: '0x368b8772',
  decodedInput: {
    type: 'function',
    name: 'setMessage',
    signature: 'setMessage(string)',
    args: [ [Object] ]
  },
  nonce: '4',
  gas: '31821',
  gasPrice: '1500000007',
  maxFeePerGas: '1500000014',
  maxPriorityFeePerGas: '1500000000',
  gasUsed: '31468',
  cumulativeGasUsed: '52468',
  effectiveGasPrice: '1500000007',
  contractAddress: null,
  type: '2',
  status: '1',
  logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
  accessList: [],
  timestamp: 1726114152
},
...
{
  transactionHash: '0x65fdeee8c624704070317475543d6b8a59ed0a157e925bc887cd1cb8a6d04470',
  transactionIndex: '5',
  blockHash: '0x2875c6a946e6b95d21370595739d921a07af4154c74d7f804ea1a6e535506f9d',
  blockNumber: '2312751',
  from: '0x007aB5199B6c57F7aA51bc3D0604a43505501a0C',
  to: '0xEc801CFCF110675F32b593A7f25d09B8f7c6D291',
  value: '10000000000000000',
  input: '0x',
  functionSelector: '0x',
  nonce: '88109',
  gas: '21000',
  gasPrice: '1500000007',
  maxFeePerGas: '1500000014',
  maxPriorityFeePerGas: '1500000000',
  gasUsed: '21000',
  cumulativeGasUsed: '753089',
  effectiveGasPrice: '1500000007',
  contractAddress: null,
  type: '2',
  status: '1',
  logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
  accessList: [],
  timestamp: 1726036116
}

Nodit은 계정이 실행한 트랜잭션 정보 확인 외에도 Internal 트랜잭션, 계정의 다음 Nonce 확인 등 계정과 관련된 다양한 API를 제공하고 있습니다. 아래 링크를 클릭해 Nodit이 제공하는 다양한 API를 확인해 보세요!

Nodit Web3 Data API 바로가기