了解如何在单个请求中获取智能合约或用户地址的完整交易历史记录,包括外部、内部、代币、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: 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 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. For transaction information that originates from your target sender address, use the Specify from AND to address If you want to get transactions that have a specific No-Code Example For a no-code view of the API request check out the composer tool Alchemy's SDK allows us to more efficiently interact with Alchemy's endpoints and make JSON-RPC requests. Ensure you are inside your project folder and type the following command in the terminal: In your current directory, create a new file called Use your favorite file browser, code editor, or just directly in the terminal using the Copy and paste the following code snippet into your new file: Now, on your command line, you can execute the script by calling: If you're using In your current directory, create a new file called Copy and paste in the following code snippet into your new file: If you're using Javascript In your current directory, create a new file called Copy and paste the following code snippet into your new file: Now, on your command line, you can execute the script by calling: No-code Example *For a no-code view of the API request check out the composer tool Alchemy's SDK allows us to more efficiently interact with Alchemy's endpoints and make JSON-RPC requests. Ensure you are inside your project folder and type the following command in the terminal: In your current directory, create a new file called Use your favorite file browser, code editor, or just directly in the terminal using the Copy and paste in the following code snippet into your new file: Now, on your command line, you can execute the script by calling: If you're using In your current directory, create a new file called Copy and paste in the following code snippet into your new file: Now, on your command line, you can execute the script by calling: If you're using Javascript In your current directory, create a new file called Copy and paste the following code snippet into your new file: Now, on your command line, you can execute the script by calling: Now that we have made a query and can see the response, let's learn how to handle it. Without parsing the response, we have a console log that looks as follows. Two of the many different response objects you may be interested in parsing are: Let's walk through an example that parses the returned JSON object. Whether we're querying via With our queried response object saved as a constant, we can now index through the transfers. Loop through all transfers in the result Print each element's If you followed along, your response should look like the following: And that's it! You've now learned how to fetch transaction history for address on Ethereum. For more, check out the tutorial below: 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! API 端点 本教程使用alchemy_getAssetTransfers端點。 如果您只需要本教程的脚本,请参阅以下指南或继续阅读以了解更多信息: 您想要通过地址获取地址交易历史记录的几个原因: 显示用户的完整交易历史记录 查询按智能合约交互过滤的地址交易 分析用户的历史盈亏 无论你想查询哪种类型的交易历史记录,这个过程对于开发人员来说都是极其繁重的,如果没有Alchemy 传输 API 在本教程中,我们将使用 Alchemy 的转账 API获取您关心的所有已发送交易和已发送地址,以创建用户交易历史记录的完整信息。 使用时转账 API为了查询用户的完整链上历史记录,掌握一些关键参数非常重要。 对于来自目标发件人地址的交易信息,请使用 指定从和到地址 如果您想获取具有特定和地址的交易,您可以在请求中指定和。 无代码示例 如需查看 API 请求的无代码视图,请查看作曲工具 Alchemy 的 SDK允许我们更有效地与 Alchemy 的端点交互并发出 JSON-RPC 请求。 确保您位于项目文件夹中,然后在终端中输入以下命令: 在当前目录中创建一个名为的新文件 使用您最喜欢的文件浏览器、代码编辑器,或者直接在终端中使用以下命令: 将以下代码片段复制并粘贴到新文件中: 现在,在命令行上,您可以通过调用以下命令来执行脚本: 如果您正在使用轻量级、通用模块,将 Fetch API 引入 Node.js 并允许我们发出 HTTP 请求,下面是您要发出的请求的代码片段! 在当前目录中,使用您最喜欢的文件浏览器、代码编辑器或直接在终端中使用以下命令创建一个名为的新文件: 将以下代码片段复制并粘贴到新文件中: 如果您使用的是 Javascript (一种基于承诺的浏览器 HTTP 客户端)和 Node.js(允许我们向 Alchemy API 发出原始请求),下面是您所发出请求的代码片段! 在当前目录中,使用您最喜欢的文件浏览器、代码编辑器或直接在终端中使用命令创建一个名为的新文件。 将以下代码片段复制并粘贴到新文件中: 现在,在命令行上,您可以通过调用以下命令来执行脚本: 无代码示例 *如需查看 API 请求的无代码视图,请查看作曲工具 Alchemy 的 SDK允许我们更有效地与 Alchemy 的端点交互并发出 JSON-RPC 请求。 确保您位于项目文件夹中,然后在终端中输入以下命令: 在当前目录中创建一个名为的新文件 使用您最喜欢的文件浏览器、代码编辑器,或者直接在终端中使用以下命令: 将以下代码片段复制并粘贴到新文件中: 现在,在命令行上,您可以通过调用以下命令来执行脚本: 如果您正在使用轻量级、通用模块,将 Fetch API 引入 Node.js 并允许我们发出 HTTP 请求,下面是您要发出的请求的代码片段! 在当前目录中,使用您最喜欢的文件浏览器、代码编辑器或直接在终端中使用以下命令创建一个名为的新文件: 将以下代码片段复制并粘贴到新文件中: 现在,在命令行上,您可以通过调用以下命令来执行脚本: 如果您使用的是 Javascript (一种基于承诺的浏览器 HTTP 客户端)和 Node.js(允许我们向 Alchemy API 发出原始请求),下面是您所发出请求的代码片段! 在当前目录中,使用您最喜欢的文件浏览器、代码编辑器或直接在终端中使用命令创建一个名为的新文件。 将以下代码片段复制并粘贴到新文件中: 现在,在命令行上,您可以通过调用以下命令来执行脚本: 现在我们已经进行了查询并可以看到响应,让我们学习如何处理它。 如果不解析响应,我们就会得到如下所示的控制台日志。 您可能有兴趣解析许多不同的响应对象中的两个:和。 让我们看一个解析返回的 JSON 对象的示例。 无论我们通过、还是进行查询,我们都需要将查询的响应对象保存到常量中。 将查询到的响应对象保存为常量后,我们现在可以索引传输。 循环遍历结果中的所有传输 打印每个元素和字段 如果你继续往下看,你的回应应该如下所示: 就是这样!现在您已经了解了如何获取以太坊地址的交易历史记录。有关更多信息,请查看以下教程:from
and sent to
addresses you care about to create a complete picture of a user's transaction history.How to query transaction history
fromAddress
: the address we want to see transaction information originating fromtoAddress
: the address we want to see for recipient-based transactionsfromBlock
: 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
"]fromAddress
parameter within the Transfers API. For recipient-based transactions, use thetoAddress
parameter.from
andto
address, you can specify thefromAddress
andtoAddress
in your request.Example: Getting Transactions Originating
From
An AddressAlchemy SDK (Recommended)
github.comtransfers_api_javascript_scripts/tx-history-from-alchemyweb3.js at main · alchemyplatform/transfers_api_javascript_scripts
npm install alchemy-sdk
1. Create a file.
tx-history-from-alchemy-sdk.js
touch
command like this:touch tx-history-from-alchemy-sdk.js
2. Write script!
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!
node tx-history-from-alchemy-sdk.js
Node-Fetch
node-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!1. Create a file.
tx-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!
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
axios
, 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!1. Create a file.
tx-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!
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!
node tx-history-from-axios.js
Example: Getting Recipient-based Transactions
Alchemy SDK (Recommended)
npm install alchemy-sdk
1. Create a file
alchemy-sdk-transfers-to-script.js
touch
command like this:touch tx-history-from-alchemy-sdk.js1
2. Write script!
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!
touch tx-history-from-alchemy-sdk.js3
Node-Fetch
node-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!1. Create a file.
fetch-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!
fetch-transfers-to-script.js
touch tx-history-from-alchemy-sdk.js5
3. Run script!
touch tx-history-from-alchemy-sdk.js6
Axios
axios
, 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!1. Create a file.
axios-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!
axios-transfers-to-script.js
touch tx-history-from-alchemy-sdk.js8
3. Run script!
touch tx-history-from-alchemy-sdk.js9
How to process the API response
If you feel like jumping ahead and grabbing some pre-built code, choose a repo that matches your preferred library.Alchemy SDK (Recommended)
Parsing with
Alchemy SDK
Responsesgithub.comtransfers_api_javascript_scripts/tx-history-parsed-alchemyweb3.js at main · alchemyplatform/transfers_api_javascript_scripts
Node-Fetch
Parsing with
Node-Fetch
Responsesgithub.comtransfers_api_javascript_scripts/tx-history-parsed-fetch.js at main · alchemyplatform/transfers_api_javascript_scripts
Axios
Parsing with
Axios
Responsesgithub.comtransfers_api_javascript_scripts/tx-history-parsed-axios.js at main · alchemyplatform/transfers_api_javascript_scripts
Raw API Response
// 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 transactionfrom
: where the transaction originated fromto
: where ETH or another asset was transferred tovalue
: the amount of ETH transferrederc721TokenId
: 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 transfertokenId
: the token ID for ERC721 tokens or other NFT token standardsasset
: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 tokenaddress
: Ethereum token contract addressdecimal
: contract decimalPrinting out the
asset
andvalue
asset
andvalue
.alchemy web3
,axios
, ornode-fetch
, we'll need to save the queried response object into a constant.Alchemy SDK (Recommended)
Saving response objects with
Alchemy 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 with
Node-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 with
Axios
// 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
In particular, the steps we take are:value
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
// 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
docs.alchemy.comIntegrating Historical Transaction Data into your dApp
📘
from
to
如何查询交易记录
fromAddress
:我们想要查看交易信息的来源地址toAddress
:我们希望看到的基于收件人的交易的地址fromBlock
:我们要获取交易的起始时间范围(默认为)latest
toBlock
:我们要获取交易的结束时间范围(默认为)latest
category
:我们关心的转账事件类型,在我们的例子中,我们希望看到所有交易,因此我们可以简单地让 param 使用其默认参数 [" ", " ", " "]external
internal
token
fromAddress
转账 API。对于基于收件人的交易,请使用参数。toAddress
📘
from
to
fromAddress
toAddress
示例:获取来自某个地址的交易
From
👍
Alchemy SDK(推荐)
github.comtransfers_api_javascript_scripts/tx-history-from-alchemyweb3.js 在 main · alchemyplatform/transfers_api_javascript_scripts
1.创建一个文件。
tx-history-from-alchemy-sdk.js
touch
2.编写脚本!
tx-history-from-alchemy-sdk.js
3.运行脚本!
节点获取
node-fetch
github.comtransfers_api_javascript_scripts/tx-history-from-axios.js 在 main · alchemyplatform/transfers_api_javascript_scripts
1.创建一个文件。
tx-history-from-fetch.js
touch
2.编写脚本!
tx-history-from-fetch.js
3.运行脚本!
Axios
axios
github.comtransfers_api_javascript_scripts/tx-history-from-fetch.js 在 main · alchemyplatform/transfers_api_javascript_scripts
1.创建一个文件。
tx-history-from-axios.js
touch
2.编写脚本!
tx-history-from-axios.js
3.运行脚本!
示例:获取基于收件人的交易
👍
Alchemy SDK(推荐)
github.comtransfers_api_javascript_scripts/tx-history-to-alchemyweb3.js 在 main · alchemyplatform/transfers_api_javascript_scripts
1. 创建文件
alchemy-sdk-transfers-to-script.js
touch
2.编写脚本!
alchemy-sdk-transfers-to-script.js
3.运行脚本!
节点获取
node-fetch
github.comtransfers_api_javascript_scripts/tx-history-to-fetch.js 在 main · alchemyplatform/transfers_api_javascript_scripts
1.创建一个文件。
fetch-transfers-to-script.js
touch
2.编写脚本!
fetch-transfers-to-script.js
3.运行脚本!
Axios
axios
github.comtransfers_api_javascript_scripts/tx-history-to-axios.js 在 main · alchemyplatform/transfers_api_javascript_scripts
1.创建一个文件。
axios-transfers-to-script.js
touch
2.编写脚本!
axios-transfers-to-script.js
3.运行脚本!
如何处理 API 响应
如果您想直接获取一些预构建的代码,请选择与您的首选库匹配的存储库。Alchemy SDK(推荐)
解析响应
Alchemy SDK
github.comtransfers_api_javascript_scripts/tx-history-parsed-alchemyweb3.js 在 main · alchemyplatform/transfers_api_javascript_scripts
节点获取
解析响应
Node-Fetch
github.comtransfers_api_javascript_scripts/tx-history-parsed-fetch.js 在 main · alchemyplatform/transfers_api_javascript_scripts
Axios
解析响应
Axios
github.comtransfers_api_javascript_scripts/tx-history-parsed-axios.js 在 main · alchemyplatform/transfers_api_javascript_scripts
原始 API 响应
了解 API 响应
blockNum
:交易事件发生的区块编号,单位为hex
hash
:交易的交易哈希from
:交易源自哪里to
:ETH 或其他资产被转移到value
:转移的 ETH 数量erc721TokenId
:ERC721 代币 ID。如果不是 ERC721 代币转移。null
erc1155Metadata
:包含 ERC1155的对象列表 ,如果不是 ERC1155 转账tokenId
value
null
tokenId
:ERC721 代币或其他 NFT 代币标准的代币 IDasset
:或代币的符号。如果未在合约中定义且无法从其他来源获得。ETH
null
rawContract
value
:以相关以太坊代币计价的原始转移价值address
:以太坊代币合约地址decimal
:合约小数打印出和
asset
value
asset
value
alchemy web3
axios
node-fetch
Alchemy SDK(推荐)
使用以下方法保存响应对象
Alchemy SDK
节点获取
使用以下方法保存响应对象
Node-Fetch
Axios
使用以下方法保存响应对象
Axios
具体来说,我们采取的步骤如下:value
asset
docs.alchemy.com将历史交易数据集成到你的 dApp 中