> 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).

# 部署你的交易服务器

为用户提供无成本交易

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

交易服务器用于改善使用多个网络时的用户体验，并防止用户在运行时切换网络提供商。用户可以保持连接到 [以太坊](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 请求。

服务器可配置的限制有：

* 检查每日最大交易次数配额。参见 [合集部分](#collections) 。
* 检查白名单中的合约，因此如果交易尝试与未识别的合约交互，就会失败。为此它使用
  * 已部署的合约和合集。参见 [合约和合集部分](#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 的设置：
  * 选择顶部导航栏上的 `主网` 选项
  * 设置合适的 `应用名称` 用于你的 dApp
  * 选择 `Polygon` 作为以下项的网络： `智能合约`
  * 启用 `任意合约` 切换选项
* 从以下位置复制 API KEY： `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 充值，但你可以轻松地从以下位置获取 Sepolia ETH 代币： [水龙头](https://sepoliafaucet.com/).

## 下载交易服务器

首先，你需要一份 Decentraland 的 transactions-server 代码副本。你可以在 [GitHub 上找到它](https://github.com/decentraland/transactions-server/tree/v1)。从那里开始，你有两个选择：

1. **下载代码**：要下载代码，你必须先点击绿色的 `代码` 按钮，然后执行以下任一操作

* 点击 `下载 ZIP`
* 复制以下位置下的 URL： `克隆` 标题，然后运行 `$ git clone THE_URL_HERE`

2. **Fork 代码**：你可以点击 `fork` 页面右上角的按钮。流程完成后，你就可以像第一种方法那样下载你的代码。为此你需要一个 GitHub 账户；有关 fork 仓库的更多信息，请参见 [这里](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

使用我们在以下过程中获取的 API 密钥： [配置 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](/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 # 仅首次运行时需要
npm run start
```

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

## 使用服务器

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

要真正发送交易，你需要向以下地址发送 POST 请求： `/transactions`。请求所需的 schema 由以下内容定义： `transactionSchema` 位于 `src/ports/transaction/types.ts`.

如果你想改用我们预制的库来让事情更简单，可以试试 [decentraland-transactions](https://github.com/decentraland/decentraland-transactions)。它通过以下方式使用： [decentraland-dapps](https://github.com/decentraland/decentraland-dapps) 在我们的 dapp 中，例如 [市场](https://market.decentraland.org)，配合 utils [`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 dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
