> For the complete documentation index, see [llms.txt](https://docs.decentraland.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/qu-kuai-lian/deploying-your-own-transactions-server.md). Agents can also search and read this documentation through its [MCP server](https://docs.decentraland.org/~gitbook/mcp).

# 部署你的交易服务器

为用户提供免成本交易

该 [transactions-server](https://github.com/decentraland/transactions-server/tree/1.8.0) 是一个代理服务器，用于将交易转发到 [Gelato](https://www.gelato.network/relay)。它会接收客户端签名的交易，并在幕后将其发送到相应的网络。这样，服务器所有者就能为用户提供免手续费的交易。

交易服务器旨在改善使用多个网络时的用户体验，并避免用户临时切换网络服务提供商。用户可以始终连接到 [Ethereum](https://ethereum.org/en/) ，并通过签署交易与 [Polygon](https://polygon.technology/) [交互](https://docs.decentraland.org/blockchain-integration/transactions-in-polygon/)

Decentraland DAO 已搭建一台供其 dApp 使用的服务器，并在一定限额内承担费用，但设有若干 [限制](#restrictions)。本文档将说明如何 [部署此服务器](#running-the-server) ，以便按照你所需的限制（如有）为用户转发交易。

## 限制

所有限制都针对用户尝试发送的每笔交易。实际上，这对应于向服务器发送一个 POST 请求。

服务器可配置的限制如下：

* 检查每日交易数量是否超过上限。更多信息请参阅 [交易部分](#transactions) 。
* 检查合约是否在白名单中；如果交易尝试与未获认可的合约交互，就会失败。为此，服务器会使用
  * 已部署的合约和集合。更多信息请参阅 [合约和集合部分](#contracts-and-collections) 。
* 限制销售价格低于指定阈值的交易。更多信息请参阅 [最低销售价值部分](#min-sale-value) 。

## 配置 Gelato

[Gelato](https://www.gelato.network/relay) 是一个多链中继协议。我们使用其基础设施来实现免手续费交易。也就是说，发送交易时，你实际上是签署一条消息并将其发送给 Gelato。该服务会代你发送交易，并将响应（交易哈希）返回给你。

它需要一个用于转发交易的合约，所幸我们可以复用 Decentraland 正在使用的合约（见下文）。

[Gelato](https://www.gelato.network/relay) 作为服务器的 API 使用。 [要进行配置，](#configuring-the-server) 你需要先获取一个 API KEY，之后 [我们会用到它](#gelato)。获取方法如下：

* [注册](https://app.gelato.network/relay) 该服务
* 为你打算使用的网络创建一个新的 dApp。要仿照 Decentraland 的配置：
  * 选择 `Mainnets` 选项
  * 为你的 dApp 设置合适的 `App name` ，
  * 选择 `Polygon` 作为……的网络 `智能合约`
  * 启用 `任意合约` 切换选项
* 从 `API 密钥` 部分复制 API KEY

最后，你需要为新创建的 dapp 充值。你可以在左侧边栏的 [1Balance](https://app.gelato.network/1balance) 部分连接钱包。连接后，你就可以存入 [USDC](https://polygonscan.com/token/0x3c499c542cef5e3811e1192ce70d8cc03d5c3359) 为用户将要发送的交易提供资金。如果需要获取 MATIC，请查看这篇 [文章](https://docs.decentraland.org/blockchain-integration/transactions-in-polygon/#where-can-i-get-matic-to-pay-for-transaction-fees).

### 测试网

如果你想在正式上线前测试应用，并且使用的是 Polygon，可以在 `Polygon Amoy`（Polygon 测试网）上进行测试。

要进行测试，只需重复 [此流程](#configuring-gelato) 但在网络字段中选择 `Matic Testnet (Amoy)` 即可。

你需要为 dapp 充值。只需从 [水龙头](https://sepoliafaucet.com/).

## 下载交易服务器

首先，你需要获取 Decentraland 的 transactions-server 代码副本。你可以在 [GitHub 上](https://github.com/decentraland/transactions-server/tree/v1)找到它。之后，你有两个选择：

1. **下载代码**：要下载代码，首先点击绿色的 `Code` 按钮，然后选择以下任一方式：

* 点击 `Download ZIP`
* 复制 `Clone` 标题下方的 URL，然后运行 `$ git clone THE_URL_HERE`

2. **复刻代码库**：你可以点击页面右上方的 `fork` 按钮。完成后，你就可以像第一种方式那样下载代码。你需要拥有 GitHub 账户。关于复刻代码库的更多信息，请参阅 [此处](https://docs.github.com/en/enterprise-server@3.5/get-started/quickstart/fork-a-repo)

## 配置服务器

交易服务器使用 [NodeJS](https://nodejs.org/en/) 和 [TypeScript](https://www.typescriptlang.org/)编写。运行之前，你需要配置一些环境变量。

操作步骤如下：

* 复制 `.env.defaults` 文件并粘贴，将其重命名为 `.env`
* 打开 `.env` 文件。你会看到一些变量已有默认值，例如 `HTTP_SERVER_PORT=5000` （服务器运行的端口）
* 大多数值可以保持不变，但有几个重要的值需要注意：
  * [Gelato](#gelato)
  * [交易](#transactions)
  * [合约和收藏集](#contracts-and-collections)
  * [最低销售价值](#min-sale-value)

### Gelato

使用 [配置 Gelato](#configuring-gelato).

```
GELATO_API_KEY=<YOUR_GELATO_API_KEY>
```

### 交易

收到新的交易请求时，系统会检查 **一个地址** 当天发送的交易数量。如果超过设定值，交易将失败。

```
MAX_TRANSACTIONS_PER_DAY=10
```

要完全移除此检查，可以在代码中删除

```ts
await checkQuota(components, transactionData)
```

方法，其所在函数为 `async function checkData(transactionData: TransactionData): Promise<void> {` ，文件位于 `src/ports/transaction/component.ts`

### 合约和收藏集

服务器会从合约地址 URL 获取地址并将其存储在本地，同时查询子图。收到新的交易请求时，服务器会检查该交易所交互的合约是否属于 URL 中已部署的合约或子图中已部署的收藏集。

如果要提供自己的合约，请更改 URL，并保持与当前 <https://contracts.decentraland.org/addresses.json> 相同的结构。使用的网络由 COLLECTIONS\_CHAIN\_ID 确定，重新获取缓存的间隔由 COLLECTIONS\_FETCH\_INTERVAL\_MS 确定。

如果你有自己的收藏集，也可以更改子图 URL。

要完全移除这些检查，可以在代码中删除

```ts
await checkContractAddress(components, transactionData)
```

方法，其所在函数为 `async function checkData(transactionData: TransactionData): Promise<void> {` ，文件位于 `src/ports/transaction/component.ts`

```
CONTRACT_ADDRESSES_URL=https://contracts.decentraland.org/addresses.json
COLLECTIONS_FETCH_INTERVAL_MS=3600000
COLLECTIONS_CHAIN_ID=80002

COLLECTIONS_SUBGRAPH_URL=https://subgraph.decentraland.org/decentraland/collections-matic-amoy
```

### 最低销售价值

收到新的交易请求时，系统会先解析请求中要中继的数据。如果检测到销售行为（如市场购买、出价等），系统会根据 MIN\_SALE\_VALUE\_IN\_WEI 检查其价值。如果低于该值，交易将失败。

```
MIN_SALE_VALUE_IN_WEI=1000000000000000000
```

要查看相关的销售方法，请参阅 `src/ports/transaction/validation/checkSalePrice.ts` ；要完全移除此检查，可以在代码中删除

```ts
await checkSalePrice(components, transactionData)
```

方法，其所在函数为 `async function checkData(transactionData: TransactionData): Promise<void> {` ，文件位于 `src/ports/transaction/component.ts`

## 运行服务器

现在所有配置都已完成，接下来就是运行服务器。你可以参照 [其 README](https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/qu-kuai-lian/deploying-your-own-transactions-server.md) ，简而言之，你需要：

* 确保 [NodeJS](https://nodejs.org/en/) 已安装
* 打开你喜欢的终端
* 运行以下命令：

```bash
npm install
npm run migrate # only the first run
npm run start
```

当然，你可能还想将其部署到你选择的服务上，例如 [AWS](https://aws.amazon.com/) 。你可以使用项目的 [Dockerfile](https://github.com/decentraland/transactions-server/blob/master/Dockerfile) 来完成部署。

## 使用服务器

现在一切都已设置并运行，是时候实际使用服务器了。

要发送交易，你需要向 `/transactions`发送 POST 请求。请求所需的结构定义在 `transactionSchema` 中，位于 `src/ports/transaction/types.ts`.

如果你想使用我们预先制作的库来简化操作，可以试试 [decentraland-transactions](https://github.com/decentraland/decentraland-transactions)。它通过 [decentraland-dapps](https://github.com/decentraland/decentraland-dapps) 在我们的 dapp 中使用，例如 [Marketplace](https://market.decentraland.org)，其中包含工具函数 [`sendTransaction`](https://github.com/decentraland/decentraland-dapps/blob/master/src/modules/wallet/utils.ts#L104)。请查看 [这段代码](https://github.com/decentraland/marketplace/blob/a2191515c6ae7ede54a685cc2dd9f9fafa35366b/webapp/src/modules/vendor/decentraland/OrderService.ts#L33) 了解示例。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.decentraland.org/creator/content-creator-zh/chang-jing-sdk7/qu-kuai-lian/deploying-your-own-transactions-server.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
