> For the complete documentation index, see [llms.txt](https://docs.aftermath.finance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aftermath.finance/zh/yong-xu-he-yue/architecture/account/gasless-trading.md).

# 免 Gas 交易（GasPool）

从中央资金池为代理钱包代付 Gas

在应用中，GasPool 称为 Gas Credit。[一键交易](https://docs.aftermath.finance/zh/yong-xu-he-yue/architecture/account/one-click-trading.md)会为您完成设置并使用它。

如果您运行机器人或程序化策略，无需为每个代理钱包充值 SUI 作为 Gas。**GasPool** 可以从一个预付余额为代理钱包的交易代付 Gas。

GasPool 就像一张预付 Gas 卡。您从主钱包向其中充入 SUI（或 USDC），此后代理钱包每次执行交易，协议都会从池中扣除 Gas 费用。代理钱包只需持有 USDC 保证金资产。

## 工作原理

主钱包创建一个 GasPool，向其中存入资金，并将代理钱包加入白名单。此后，代理钱包调用受支持的交易接口时，会带上一个 `sponsor` 字段，其中包含主钱包及其可复用的条款签名。API 构建交易，从池中提取 Gas，并返回 `txKind` 和 `sponsorSignature`。代理钱包对交易签名后，连同两个签名一起提交。

代理钱包完全不用碰 SUI。余额不足时，从主钱包给 GasPool 充值即可。

## 设置

### 第 1 步：创建 GasPool

从主钱包出发，在一笔交易内完成 GasPool 的创建和注资：

**接口：** `POST /api/gas-pool/transactions/create`

```json
{
  "walletAddress": "<your-primary-wallet>",
  "deferShare": false,
  "initialDepositAmount": "1000000000n"
}
```

`initialDepositAmount` 以 MIST 计（1 SUI = 1,000,000,000 MIST）。也可以之后再存入。

### 第 2 步：存入（可选充值）

**接口：** `POST /api/gas-pool/transactions/deposit`

直接存入 SUI：

```json
{
  "walletAddress": "<your-primary-wallet>",
  "amount": "500000000n"
}
```

或存入 USDC（或任何其他代币）。该接口会先通过 Aftermath 路由自动将其兑换为 SUI，再存入池中：

```json
{
  "walletAddress": "<your-primary-wallet>",
  "coinType": "0xdba34672e30cb065b1f93e3ab55318768fd6fef66c15942c9f7cb846e2f900e7::usdc::USDC",
  "amount": "5000000n",
  "slippage": 0.01
}
```

您无需持有 SUI，用手头已有的任何代币即可为 GasPool 注资。

### 第 3 步：为代理钱包授权

**接口：** `POST /api/gas-pool/transactions/grant`

```json
{
  "walletAddress": "<your-primary-wallet>",
  "targetWalletAddress": "<your-agent-wallet>"
}
```

### 第 4 步：执行代付交易

代理钱包调用任何 `/api/perpetuals/account/transactions/...` 接口时，加入 `sponsor` 字段：

```json
{
  "walletAddress": "<your-agent-wallet>",
  "accountId": "424n",
  "sponsor": {
    "walletAddress": "<your-primary-wallet>",
    "bytes": "<base64 of Aftermath Terms and Conditions>",
    "signature": "<primary-wallet signature>",
    "gasBudget": 50000000
  }
}
```

`gasBudget` 为可选项，以 MIST 计。若省略，则由服务决定预算。对确切的 UTF-8 消息 `Aftermath Terms and Conditions` 签名，在 `bytes` 中发送其 base64 表示，并在该钱包连接中复用此签名。

API 返回：

* `txKind`：base64 编码的交易，供代理钱包签名
* `sponsorSignature`：Gas 代付方的签名

代理钱包对 `txKind` 签名后，连同两个签名一起提交交易。

## 管理您的 GasPool

| 操作       | 接口                                         |
| -------- | ------------------------------------------ |
| 查询余额和白名单 | `POST /api/gas-pool/pool`                  |
| 追加存入资金   | `POST /api/gas-pool/transactions/deposit`  |
| 提取 SUI   | `POST /api/gas-pool/transactions/withdraw` |
| 授予访问权限   | `POST /api/gas-pool/transactions/grant`    |
| 撤销访问权限   | `POST /api/gas-pool/transactions/revoke`   |
| 共享延迟共享的池 | `POST /api/gas-pool/transactions/share`    |
| 直接代付交易   | `POST /api/gas-pool/transactions/sponsor`  |

### PTB 组合

创建池时设置 `deferShare: true`，即可获得 `gasPoolArg` 和 `sharePolicyArg`，随后在同一笔组合交易中将这些参数传给 `/api/gas-pool/transactions/share`。存入可以使用已有的 `txKind` 和 `coinArg`。提取可以设置 `deferTransfer: true`，以获得 `withdrawnCoinArg` 供其他 PTB 命令使用。

直接代付接口接受可复用的条款签名、可选的 `txKind` 和可选的 `gasBudget`（以 MIST 计）。它返回 `transaction`、`sponsorSignature` 和 `digest`；请以发送方身份对 `transaction` 签名，并提交两个签名。

GasPool 失败使用固定的错误码：`2030` 表示资金不足，`2031` 表示服务暂时不可用，`2032` 表示授权失败，`2033` 表示交易无法被代付。签名验证失败使用 `2034`。

## 替代方案：本地代付

如果不想使用 GasPool，也可以自行代付：在提交前，由主钱包作为 Gas 代付方对交易载荷进行共同签名。这样无需 GasPool，即可完全掌控代付。
