GTokenTool全网最好的代币发行工具平台
当前位置:首页 >> 加密百科 >> 如何获取以太坊地址的交易历史记录

如何获取以太坊地址的交易历史记录

admin 加密百科 53

了解如何在单个请求中获取智能合约或用户地址的完整交易历史记录,包括外部、内部、代币、ERC-20、ERC-721 和 ERC-1155 代币转移

API Endpoint

This tutorial uses the alchemy_getAssetTransfers endpoint.

If you just need the script for this tutorial refer to the below Recipe or continue reading for more:

Script to Get Transaction History for an Address on Ethereum

A few reasons for why you'd want to get address transaction history by an address:

  • Displaying your a user’s full transaction history

  • Querying an address's transactions filtered by smart contract interactions

  • Analyzing a user's historical profit and loss

Regardless of the different types of transaction history, you want to look up, this process can be extremely burdensome for developers to stitch together without the Alchemy Transfers API

In this tutorial, we’ll be using Alchemy’s Transfers API to fetch all transactions sent from and sent to addresses you care about to create a complete picture of a user's transaction history.


How to query transaction history

When using the Transfers API for querying a user’s full on-chain history, it's important to have a few key parameters on hand.

  • fromAddress: the address we want to see transaction information originating from

  • toAddress: the address we want to see for recipient-based transactions

  • fromBlock: the starting time range we want to fetch transactions over (defaults tolatest)

  • toBlock : the ending time range we want to fetch transactions over (defaults tolatest)

  • category: the type of transfer events we care about, in our case we want to see all transactions so we can simply let the param use its default argument of ["external", "internal", "token"]

For transaction information that originates from your target sender address, use thefromAddress parameter within the Transfers API. For recipient-based transactions, use thetoAddress parameter.

📘

Specify from AND to address

If you want to get transactions that have a specificfrom andto address, you can specify thefromAddress andtoAddress in your request.


Example: Getting Transactions OriginatingFrom An Address

👍

No-Code Example

For a no-code view of the API request check out the composer tool

Alchemy SDK (Recommended)

Alchemy's SDK allows us to more efficiently interact with Alchemy's endpoints and make JSON-RPC requests.

"transfers_api_javascript_scripts"github.com"github.comtransfers_api_javascript_scripts/tx-history-from-alchemyweb3.js at main · alchemyplatform/transfers_api_javascript_scripts

Ensure you are inside your project folder and type the following command in the terminal:

npm install alchemy-sdk
1. Create a file.

In your current directory, create a new file calledtx-history-from-alchemy-sdk.js

Use your favorite file browser, code editor, or just directly in the terminal using thetouch command like this:

touch tx-history-from-alchemy-sdk.js
2. Write script!

Copy and paste the following code snippet into your new file:tx-history-from-alchemy-sdk.js

// Setup: npm install alchemy-sdk import { Alchemy, Network } from "alchemy-sdk"; const config = {   apiKey: "<-- ALCHEMY APP API KEY -->",   network: Network.ETH_MAINNET, }; const alchemy = new Alchemy(config); const data = await alchemy.core.getAssetTransfers({   fromBlock: "0x0",   fromAddress: "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",   category: ["external", "internal", "erc20", "erc721", "erc1155"], }); console.log(data);
3. Run script!

Now, on your command line, you can execute the script by calling:

node tx-history-from-alchemy-sdk.js

Node-Fetch

If you're usingnode-fetch a lightweight, common module that brings the Fetch API to Node.js and allows us to make our HTTP requests, here's a code snipper for the request you'd make!

github.comtransfers_api_javascript_scripts/tx-history-from-axios.js at main · alchemyplatform/transfers_api_javascript_scripts
1. Create a file.

In your current directory, create a new file calledtx-history-from-fetch.js using your favorite file browser, code editor, or just directly in the terminal using thetouch command like this:

touch tx-history-from-fetch.js
2. Write script!

Copy and paste in the following code snippet into your new file:tx-history-from-fetch.js

import fetch from 'node-fetch';   let data = JSON.stringify({   "jsonrpc": "2.0",   "id": 0,   "method": "alchemy_getAssetTransfers",   "params": [     {       "fromBlock": "0x0",       "fromAddress": "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",     }   ] });   var requestOptions = {     method: 'POST',     headers: { 'Content-Type': 'application/json' },     body: data,     redirect: 'follow'   };   const apiKey = "demo"   const baseURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;   const fetchURL = `${baseURL}`;   fetch(fetchURL, requestOptions)     .then(response => response.json())     .then(response => JSON.stringify(response, null, 2))     .then(result => console.log(result))     .catch(error => console.log('error', error));
3. Run script!
node tx-history-from-fetch.js

Axios

If you're using Javascriptaxios, a promise-based HTTP client for the browser and Node.js which allows us to make a raw request to the Alchemy API, here's a code snipper for the request you'd make!

github.comtransfers_api_javascript_scripts/tx-history-from-fetch.js at main · alchemyplatform/transfers_api_javascript_scripts
1. Create a file.

In your current directory, create a new file calledtx-history-from-axios.js using your favorite file browser, code editor, or just directly in the terminal using thetouch command.

touch tx-history-from-axios.js
2. Write script!

Copy and paste the following code snippet into your new file:tx-history-from-axios.js

import axios from 'axios';   let data = JSON.stringify({   "jsonrpc": "2.0",   "id": 0,   "method": "alchemy_getAssetTransfers",   "params": [     {       "fromBlock": "0x0",       "fromAddress": "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",     }   ] });   var requestOptions = {     method: 'post',     headers: { 'Content-Type': 'application/json' },     data: data,   };   const apiKey = "demo"   const baseURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;   const axiosURL = `${baseURL}`;   axios(axiosURL, requestOptions)     .then(response => console.log(JSON.stringify(response.data, null, 2)))     .catch(error => console.log(error));
3. Run script!

Now, on your command line, you can execute the script by calling:

node tx-history-from-axios.js

Example: Getting Recipient-based Transactions

👍

No-code Example

*For a no-code view of the API request check out the composer tool

Alchemy SDK (Recommended)

Alchemy's SDK allows us to more efficiently interact with Alchemy's endpoints and make JSON-RPC requests.

github.comtransfers_api_javascript_scripts/tx-history-to-alchemyweb3.js at main · alchemyplatform/transfers_api_javascript_scripts

Ensure you are inside your project folder and type the following command in the terminal:

npm install alchemy-sdk
1. Create a file

In your current directory, create a new file calledalchemy-sdk-transfers-to-script.js

Use your favorite file browser, code editor, or just directly in the terminal using thetouch command like this:

touch tx-history-from-alchemy-sdk.js1
2. Write script!

Copy and paste in the following code snippet into your new file:alchemy-sdk-transfers-to-script.js

// Setup: npm install alchemy-sdk import { Alchemy, Network } from "alchemy-sdk"; const config = {   apiKey: "<-- ALCHEMY APP API KEY -->",   network: Network.ETH_MAINNET, }; const alchemy = new Alchemy(config); const data = await alchemy.core.getAssetTransfers({   fromBlock: "0x0",   toAddress: "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",   category: ["external", "internal", "erc20", "erc721", "erc1155"], }); console.log(data);
3. Run script!

Now, on your command line, you can execute the script by calling:

touch tx-history-from-alchemy-sdk.js3

Node-Fetch

If you're usingnode-fetch a lightweight, common module that brings the Fetch API to Node.js and allows us to make our HTTP requests, here's a code snipper for the request you'd make!

github.comtransfers_api_javascript_scripts/tx-history-to-fetch.js at main · alchemyplatform/transfers_api_javascript_scripts
1. Create a file.

In your current directory, create a new file calledfetch-transfers-to-script.js using your favorite file browser, code editor, or just directly in the terminal using thetouch command like this:

touch tx-history-from-alchemy-sdk.js4
2. Write script!

Copy and paste in the following code snippet into your new file:fetch-transfers-to-script.js

touch tx-history-from-alchemy-sdk.js5
3. Run script!

Now, on your command line, you can execute the script by calling:

touch tx-history-from-alchemy-sdk.js6

Axios

If you're using Javascriptaxios, a promise-based HTTP client for the browser and Node.js which allows us to make a raw request to the Alchemy API, here's a code snipper for the request you'd make!

github.comtransfers_api_javascript_scripts/tx-history-to-axios.js at main · alchemyplatform/transfers_api_javascript_scripts
1. Create a file.

In your current directory, create a new file calledaxios-transfers-to-script.js using your favorite file browser, code editor, or just directly in the terminal using thetouch command.

touch tx-history-from-alchemy-sdk.js7
2. Write script!

Copy and paste the following code snippet into your new file:axios-transfers-to-script.js

touch tx-history-from-alchemy-sdk.js8
3. Run script!

Now, on your command line, you can execute the script by calling:

touch tx-history-from-alchemy-sdk.js9

How to process the API response

Now that we have made a query and can see the response, let's learn how to handle it.
If you feel like jumping ahead and grabbing some pre-built code, choose a repo that matches your preferred library.

Alchemy SDK (Recommended)

Parsing withAlchemy SDK Responses

"transfers_api_javascript_scripts"github.com"github.comtransfers_api_javascript_scripts/tx-history-parsed-alchemyweb3.js at main · alchemyplatform/transfers_api_javascript_scripts

Node-Fetch

Parsing withNode-Fetch Responses

"transfers_api_javascript_scripts"github.com"github.comtransfers_api_javascript_scripts/tx-history-parsed-fetch.js at main · alchemyplatform/transfers_api_javascript_scripts

Axios

Parsing withAxios Responses

"transfers_api_javascript_scripts"github.com"github.comtransfers_api_javascript_scripts/tx-history-parsed-axios.js at main · alchemyplatform/transfers_api_javascript_scripts

Raw API Response

Without parsing the response, we have a console log that looks as follows.

// Setup: npm install alchemy-sdk import { Alchemy, Network } from "alchemy-sdk"; const config = {   apiKey: "<-- ALCHEMY APP API KEY -->",   network: Network.ETH_MAINNET, }; const alchemy = new Alchemy(config); const data = await alchemy.core.getAssetTransfers({   fromBlock: "0x0",   fromAddress: "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",   category: ["external", "internal", "erc20", "erc721", "erc1155"], }); console.log(data);0

Understanding API Response

  • blockNum: the block number where a transaction event occurred, inhex

  • hash: the transaction hash of a transaction

  • from: where the transaction originated from

  • to: where ETH or another asset was transferred to

  • value:  the amount of ETH transferred

  • erc721TokenId: the ERC721 token ID.null if not an ERC721 token transfer.

  • erc1155Metadata: a list of objects containing the ERC1155tokenId  andvalue.null if not an ERC1155 transfer

  • tokenId: the token ID for ERC721 tokens or other NFT token standards

  • asset:ETH or the token's symbol.null if not defined in the contract and not available from other sources.

  • rawContract

    • value: raw transfer value denominated in the relevant Ethereum token

    • address: Ethereum token contract address

    • decimal:  contract decimal

Printing out theasset andvalue

Two of the many different response objects you may be interested in parsing are:asset andvalue.

Let's walk through an example that parses the returned JSON object.

Whether we're querying viaalchemy web3,axios, ornode-fetch, we'll need to save the queried response object into a constant.

Alchemy SDK (Recommended)

Saving response objects withAlchemy SDK

// Setup: npm install alchemy-sdk import { Alchemy, Network } from "alchemy-sdk"; const config = {   apiKey: "<-- ALCHEMY APP API KEY -->",   network: Network.ETH_MAINNET, }; const alchemy = new Alchemy(config); const data = await alchemy.core.getAssetTransfers({   fromBlock: "0x0",   fromAddress: "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",   category: ["external", "internal", "erc20", "erc721", "erc1155"], }); console.log(data);1

Node-Fetch

Saving response objects withNode-Fetch

// Setup: npm install alchemy-sdk import { Alchemy, Network } from "alchemy-sdk"; const config = {   apiKey: "<-- ALCHEMY APP API KEY -->",   network: Network.ETH_MAINNET, }; const alchemy = new Alchemy(config); const data = await alchemy.core.getAssetTransfers({   fromBlock: "0x0",   fromAddress: "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",   category: ["external", "internal", "erc20", "erc721", "erc1155"], }); console.log(data);2

Axios

Saving response objects withAxios

// Setup: npm install alchemy-sdk import { Alchemy, Network } from "alchemy-sdk"; const config = {   apiKey: "<-- ALCHEMY APP API KEY -->",   network: Network.ETH_MAINNET, }; const alchemy = new Alchemy(config); const data = await alchemy.core.getAssetTransfers({   fromBlock: "0x0",   fromAddress: "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",   category: ["external", "internal", "erc20", "erc721", "erc1155"], }); console.log(data);3

With our queried response object saved as a constant, we can now index through the transfers.
In particular, the steps we take are:

  1. Loop through all transfers in the result

  2. Print each element'svalue andasset field

// Setup: npm install alchemy-sdk import { Alchemy, Network } from "alchemy-sdk"; const config = {   apiKey: "<-- ALCHEMY APP API KEY -->",   network: Network.ETH_MAINNET, }; const alchemy = new Alchemy(config); const data = await alchemy.core.getAssetTransfers({   fromBlock: "0x0",   fromAddress: "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",   category: ["external", "internal", "erc20", "erc721", "erc1155"], }); console.log(data);4

If you followed along, your response should look like the following:

// Setup: npm install alchemy-sdk import { Alchemy, Network } from "alchemy-sdk"; const config = {   apiKey: "<-- ALCHEMY APP API KEY -->",   network: Network.ETH_MAINNET, }; const alchemy = new Alchemy(config); const data = await alchemy.core.getAssetTransfers({   fromBlock: "0x0",   fromAddress: "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",   category: ["external", "internal", "erc20", "erc721", "erc1155"], }); console.log(data);5

And that's it! You've now learned how to fetch transaction history for address on Ethereum. For more, check out the tutorial below:

"Integrating"docs.alchemy.com"docs.alchemy.comIntegrating Historical Transaction Data into your dApp

If you enjoyed this tutorial for getting address transaction history on Ethereum, give us a tweet @AlchemyPlatform! (Or give the author @crypt0zeke a shoutout!)

Don't forget to join our Discord server to meet other blockchain devs, builders, and entrepreneurs!

" class="rm-Markdown markdown-body rm-Markdown markdown-body ng-non-bindable" data-testid="RDMD" style="box-sizing: border-box; --font-size: 90%; color: var(--markdown-text); font-family: var(--markdown-font); line-height: var(--markdown-line-height); position: relative; text-size-adjust: 100%; overflow-wrap: break-word; font-size: var(--markdown-font-size, 14px); margin-left: 31px;">

📘

API 端点

本教程使用alchemy_getAssetTransfers端點。

如果您只需要本教程的脚本,请参阅以下指南或继续阅读以了解更多信息:

🔄获取以太坊地址交易历史记录的脚本打开菜谱

您想要通过地址获取地址交易历史记录的几个原因:

  • 显示用户的完整交易历史记录

  • 查询按智能合约交互过滤的地址交易

  • 分析用户的历史盈亏

无论你想查询哪种类型的交易历史记录,这个过程对于开发人员来说都是极其繁重的,如果没有Alchemy 传输 API

在本教程中,我们将使用 Alchemy 的转账 API获取您关心的所有已发送交易和已发送地址,以创建用户交易历史记录的完整信息。fromto


如何查询交易记录

使用时转账 API为了查询用户的完整链上历史记录,掌握一些关键参数非常重要。

  • fromAddress:我们想要查看交易信息的来源地址

  • toAddress:我们希望看到的基于收件人的交易的地址

  • fromBlock:我们要获取交易的起始时间范围(默认为latest

  • toBlock:我们要获取交易的结束时间范围(默认为latest

  • category:我们关心的转账事件类型,在我们的例子中,我们希望看到所有交易,因此我们可以简单地让 param 使用其默认参数 [" ", " ", " "]externalinternaltoken

对于来自目标发件人地址的交易信息,请使用fromAddress转账 API。对于基于收件人的交易,请使用参数。toAddress

📘

指定从和到地址

如果您想获取具有特定和地址的交易,您可以在请求中指定和。fromtofromAddresstoAddress


示例:获取来自某个地址的交易From

👍

无代码示例

如需查看 API 请求的无代码视图,请查看作曲工具

Alchemy SDK(推荐)

Alchemy 的 SDK允许我们更有效地与 Alchemy 的端点交互并发出 JSON-RPC 请求。

transfers_api_javascript_scripts/tx-history-from-alchemyweb3.js 在 main · alchemyplatform/transfers_api_javascript_scriptsgithub.comgithub.comtransfers_api_javascript_scripts/tx-history-from-alchemyweb3.js 在 main · alchemyplatform/transfers_api_javascript_scripts

确保您位于项目文件夹中,然后在终端中输入以下命令:

npm install alchemy-sdk
1.创建一个文件。

在当前目录中创建一个名为的新文件tx-history-from-alchemy-sdk.js

使用您最喜欢的文件浏览器、代码编辑器,或者直接在终端中使用以下命令:touch

touch tx-history-from-alchemy-sdk.js
2.编写脚本!

将以下代码片段复制并粘贴到新文件中:tx-history-from-alchemy-sdk.js

// Setup: npm install alchemy-sdk import { Alchemy, Network } from "alchemy-sdk"; const config = {   apiKey: "<-- ALCHEMY APP API KEY -->",   network: Network.ETH_MAINNET, }; const alchemy = new Alchemy(config); const data = await alchemy.core.getAssetTransfers({   fromBlock: "0x0",   fromAddress: "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",   category: ["external", "internal", "erc20", "erc721", "erc1155"], }); console.log(data);
3.运行脚本!

现在,在命令行上,您可以通过调用以下命令来执行脚本:

node tx-history-from-alchemy-sdk.js

节点获取

如果您正在使用轻量级、通用模块,将 Fetch API 引入 Node.js 并允许我们发出 HTTP 请求,下面是您要发出的请求的代码片段!node-fetch

transfers_api_javascript_scripts/tx-history-from-axios.js 在 main · alchemyplatform/transfers_api_javascript_scriptsgithub.comgithub.comtransfers_api_javascript_scripts/tx-history-from-axios.js 在 main · alchemyplatform/transfers_api_javascript_scripts
1.创建一个文件。

在当前目录中,使用您最喜欢的文件浏览器、代码编辑器或直接在终端中使用以下命令创建一个名为的新文件:tx-history-from-fetch.jstouch

touch tx-history-from-fetch.js
2.编写脚本!

将以下代码片段复制并粘贴到新文件中:tx-history-from-fetch.js

node tx-history-from-alchemy-sdk.js1
3.运行脚本!
node tx-history-from-fetch.js

Axios

如果您使用的是 Javascript (一种基于承诺的浏览器 HTTP 客户端)和 Node.js(允许我们向 Alchemy API 发出原始请求),下面是您所发出请求的代码片段!axios

transfers_api_javascript_scripts/tx-history-from-fetch.js 在 main · alchemyplatform/transfers_api_javascript_scriptsgithub.comgithub.comtransfers_api_javascript_scripts/tx-history-from-fetch.js 在 main · alchemyplatform/transfers_api_javascript_scripts
1.创建一个文件。

在当前目录中,使用您最喜欢的文件浏览器、代码编辑器或直接在终端中使用命令创建一个名为的新文件。tx-history-from-axios.jstouch

touch tx-history-from-axios.js
2.编写脚本!

将以下代码片段复制并粘贴到新文件中:tx-history-from-axios.js

node tx-history-from-alchemy-sdk.js4
3.运行脚本!

现在,在命令行上,您可以通过调用以下命令来执行脚本:

node tx-history-from-axios.js

示例:获取基于收件人的交易

👍

无代码示例

*如需查看 API 请求的无代码视图,请查看作曲工具

Alchemy SDK(推荐)

Alchemy 的 SDK允许我们更有效地与 Alchemy 的端点交互并发出 JSON-RPC 请求。

transfers_api_javascript_scripts/tx-history-to-alchemyweb3.js 在 main · alchemyplatform/transfers_api_javascript_scriptsgithub.comgithub.comtransfers_api_javascript_scripts/tx-history-to-alchemyweb3.js 在 main · alchemyplatform/transfers_api_javascript_scripts

确保您位于项目文件夹中,然后在终端中输入以下命令:

npm install alchemy-sdk
1. 创建文件

在当前目录中创建一个名为的新文件alchemy-sdk-transfers-to-script.js

使用您最喜欢的文件浏览器、代码编辑器,或者直接在终端中使用以下命令:touch

touch tx-history-from-alchemy-sdk.js1
2.编写脚本!

将以下代码片段复制并粘贴到新文件中:alchemy-sdk-transfers-to-script.js

// Setup: npm install alchemy-sdk import { Alchemy, Network } from "alchemy-sdk"; const config = {   apiKey: "<-- ALCHEMY APP API KEY -->",   network: Network.ETH_MAINNET, }; const alchemy = new Alchemy(config); const data = await alchemy.core.getAssetTransfers({   fromBlock: "0x0",   toAddress: "0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1",   category: ["external", "internal", "erc20", "erc721", "erc1155"], }); console.log(data);
3.运行脚本!

现在,在命令行上,您可以通过调用以下命令来执行脚本:

touch tx-history-from-alchemy-sdk.js3

节点获取

如果您正在使用轻量级、通用模块,将 Fetch API 引入 Node.js 并允许我们发出 HTTP 请求,下面是您要发出的请求的代码片段!node-fetch

transfers_api_javascript_scripts/tx-history-to-fetch.js 在 main · alchemyplatform/transfers_api_javascript_scriptsgithub.comgithub.comtransfers_api_javascript_scripts/tx-history-to-fetch.js 在 main · alchemyplatform/transfers_api_javascript_scripts
1.创建一个文件。

在当前目录中,使用您最喜欢的文件浏览器、代码编辑器或直接在终端中使用以下命令创建一个名为的新文件:fetch-transfers-to-script.jstouch

touch tx-history-from-alchemy-sdk.js4
2.编写脚本!

将以下代码片段复制并粘贴到新文件中:fetch-transfers-to-script.js

touch tx-history-from-fetch.js1
3.运行脚本!

现在,在命令行上,您可以通过调用以下命令来执行脚本:

touch tx-history-from-alchemy-sdk.js6

Axios

如果您使用的是 Javascript (一种基于承诺的浏览器 HTTP 客户端)和 Node.js(允许我们向 Alchemy API 发出原始请求),下面是您所发出请求的代码片段!axios

transfers_api_javascript_scripts/tx-history-to-axios.js 在 main · alchemyplatform/transfers_api_javascript_scriptsgithub.comgithub.comtransfers_api_javascript_scripts/tx-history-to-axios.js 在 main · alchemyplatform/transfers_api_javascript_scripts
1.创建一个文件。

在当前目录中,使用您最喜欢的文件浏览器、代码编辑器或直接在终端中使用命令创建一个名为的新文件。axios-transfers-to-script.jstouch

touch tx-history-from-alchemy-sdk.js7
2.编写脚本!

将以下代码片段复制并粘贴到新文件中:axios-transfers-to-script.js

touch tx-history-from-fetch.js4
3.运行脚本!

现在,在命令行上,您可以通过调用以下命令来执行脚本:

touch tx-history-from-alchemy-sdk.js9

如何处理 API 响应

现在我们已经进行了查询并可以看到响应,让我们学习如何处理它。
如果您想直接获取一些预构建的代码,请选择与您的首选库匹配的存储库。

Alchemy SDK(推荐)

解析响应Alchemy SDK

transfers_api_javascript_scripts/tx-history-parsed-alchemyweb3.js 在 main · alchemyplatform/transfers_api_javascript_scriptsgithub.comgithub.comtransfers_api_javascript_scripts/tx-history-parsed-alchemyweb3.js 在 main · alchemyplatform/transfers_api_javascript_scripts

节点获取

解析响应Node-Fetch

transfers_api_javascript_scripts/tx-history-parsed-fetch.js 在 main · alchemyplatform/transfers_api_javascript_scriptsgithub.comgithub.comtransfers_api_javascript_scripts/tx-history-parsed-fetch.js 在 main · alchemyplatform/transfers_api_javascript_scripts

Axios

解析响应Axios

transfers_api_javascript_scripts/tx-history-parsed-axios.js 在 main · alchemyplatform/transfers_api_javascript_scriptsgithub.comgithub.comtransfers_api_javascript_scripts/tx-history-parsed-axios.js 在 main · alchemyplatform/transfers_api_javascript_scripts

原始 API 响应

如果不解析响应,我们就会得到如下所示的控制台日志。

touch tx-history-from-fetch.js6

了解 API 响应

  • blockNum:交易事件发生的区块编号,单位为hex

  • hash:交易的交易哈希

  • from:交易源自哪里

  • to:ETH 或其他资产被转移到

  • value:转移的 ETH 数量

  • erc721TokenId:ERC721 代币 ID。如果不是 ERC721 代币转移。null

  • erc1155Metadata:包含 ERC1155的对象列表 ,如果不是 ERC1155 转账tokenIdvaluenull

  • tokenId:ERC721 代币或其他 NFT 代币标准的代币 ID

  • asset或代币的符号。如果未在合约中定义且无法从其他来源获得。ETHnull

  • rawContract

    • value:以相关以太坊代币计价的原始转移价值

    • address:以太坊代币合约地址

    • decimal:合约小数

打印出assetvalue

您可能有兴趣解析许多不同的响应对象中的两个:assetvalue

让我们看一个解析返回的 JSON 对象的示例。

无论我们通过、还是进行查询我们都需要将查询的响应对象保存到常量中。alchemy web3axiosnode-fetch

Alchemy SDK(推荐)

使用以下方法保存响应对象Alchemy SDK

touch tx-history-from-fetch.js7

节点获取

使用以下方法保存响应对象Node-Fetch

touch tx-history-from-fetch.js8

Axios

使用以下方法保存响应对象Axios

// Setup: npm install alchemy-sdk import { Alchemy, Network } from &quot;alchemy-sdk&quot;; const config = {   apiKey: &quot;&lt;-- ALCHEMY APP API KEY --&gt;&quot;,   network: Network.ETH_MAINNET, }; const alchemy = new Alchemy(config); const data = await alchemy.core.getAssetTransfers({   fromBlock: &quot;0x0&quot;,   fromAddress: &quot;0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1&quot;,   category: [&quot;external&quot;, &quot;internal&quot;, &quot;erc20&quot;, &quot;erc721&quot;, &quot;erc1155&quot;], }); console.log(data);3

将查询到的响应对象保存为常量后,我们现在可以索引传输。
具体来说,我们采取的步骤如下:

  1. 循环遍历结果中的所有传输

  2. 打印每个元素字段valueasset

import fetch from &#x27;node-fetch&#x27;;   let data = JSON.stringify({   &quot;jsonrpc&quot;: &quot;2.0&quot;,   &quot;id&quot;: 0,   &quot;method&quot;: &quot;alchemy_getAssetTransfers&quot;,   &quot;params&quot;: [     {       &quot;fromBlock&quot;: &quot;0x0&quot;,       &quot;fromAddress&quot;: &quot;0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1&quot;,     }   ] });   var requestOptions = {     method: &#x27;POST&#x27;,     headers: { &#x27;Content-Type&#x27;: &#x27;application/json&#x27; },     body: data,     redirect: &#x27;follow&#x27;   };   const apiKey = &quot;demo&quot;   const baseURL = `https://eth-mainnet.g.alchemy.com/v2/${apiKey}`;   const fetchURL = `${baseURL}`;   fetch(fetchURL, requestOptions)     .then(response =&gt; response.json())     .then(response =&gt; JSON.stringify(response, null, 2))     .then(result =&gt; console.log(result))     .catch(error =&gt; console.log(&#x27;error&#x27;, error));0

如果你继续往下看,你的回应应该如下所示:

// Setup: npm install alchemy-sdk import { Alchemy, Network } from &quot;alchemy-sdk&quot;; const config = {   apiKey: &quot;&lt;-- ALCHEMY APP API KEY --&gt;&quot;,   network: Network.ETH_MAINNET, }; const alchemy = new Alchemy(config); const data = await alchemy.core.getAssetTransfers({   fromBlock: &quot;0x0&quot;,   fromAddress: &quot;0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1&quot;,   category: [&quot;external&quot;, &quot;internal&quot;, &quot;erc20&quot;, &quot;erc721&quot;, &quot;erc1155&quot;], }); console.log(data);5

就是这样!现在您已经了解了如何获取以太坊地址的交易历史记录。有关更多信息,请查看以下教程:

将历史交易数据集成到你的 dApp 中docs.alchemy.comdocs.alchemy.com将历史交易数据集成到你的 dApp 中


作者:GTokenTool一键发币平台

交流群:https://t.me/+Kz4u3xoDpFo3ZWY1

同类推荐