发送保障交易
使用 @stablechain/enterprise 通过预留的企业通道区块空间路由交易。guaranteedBlock 模块通过 Enterprise RPC 网关转发 GuaranteedTx (一种 0x3F 类型 CustomTx),使其落地在为企业工作负载预留的容量中。与免 Gas 费不同,签名者需要支付自己的 Gas 费,因此必须有足够的资金。
您可以将此功能与免 Gas 费结合使用,以获得保障转发交易:即免 Gas 费交易仍能在企业通道中落地。
先决条件
- Node.js 20 或更高版本,并安装了
@stablechain/enterprise和viem。请参阅 Enterprise SDK 参考。 - 一个 Enterprise RPC 网关 API 密钥。Enterprise SDK 在 Stable 主网和 Stable 测试网上运行,并且访问受限:请联系 Stable 获取网关端点。
- 一个资金充足的签名者,因为 GuaranteedTx 需要支付自己的 Gas 费。
1. 使用保障区块空间模块创建客户端
将 guaranteedBlock 和 enterpriseRpcEndpoints 传递给 createStableEnterprise。网关是唯一接受 0x3F GuaranteedTx 的端点,并且它持有您的 API 密钥。无效的 laneId 会立即被拒绝。
import { createStableEnterprise, stable } from "@stablechain/enterprise";
import { privateKeyToAccount } from "viem/accounts";
const enterprise = createStableEnterprise({
chain: stable,
enterpriseRpcEndpoints: [process.env.ENTERPRISE_RPC_URL], // the gateway URL Stable provisions
guaranteedBlock: {
account: privateKeyToAccount(process.env.SIGNER_PRIVATE_KEY as `0x${string}`), // must be funded
laneId: 0n, // Enterprise lane
},
});
const gb = enterprise.guaranteedBlock;StableEnterpriseClient { gasWaiver: undefined, guaranteedBlock, guaranteedWaiver: undefined }2. 发送单笔交易
使用签名者和可变字段调用 send。由于签名者支付 Gas 费,因此 1559 费用字段是必需的。chainId、企业 nonceKey 和 2D nonce(从网关发现)会为您处理。
import { createPublicClient, http } from "viem";
const publicClient = createPublicClient({ chain: stable, transport: http() });
const gasPrice = await publicClient.getGasPrice();
const { txHash } = await gb.send(signer, {
to: recipient,
gas: 21_000n,
gasFeeCap: gasPrice * 2n,
gasTipCap: gasPrice,
});
console.log("Guaranteed tx:", txHash);Guaranteed tx: 0xabcd...7890gasFeeCap 是每 Gas 的最大总费用(EIP-1559 maxFeePerGas),gasTipCap 是每 Gas 的最大优先级费用(maxPriorityFeePerGas)。将 gasTipCap 设置为当前 Gas 价格,并将 gasFeeCap 设置为它的两倍(如上所示)是一个安全的默认值。
签名者支付 Gas 费,因此在转发之前请确认它持有余额。来自零余额账户的 GuaranteedTx 会失败。
3. 发送批处理
调用 sendBatch 发送多笔交易。Nonce 会从发现的基础自动排序,并且您会得到每个输入的单个结果,按输入顺序排列。一个失败会使其后续交易受阻。
const results = await gb.sendBatch(signer, [
{ to: a, gas: 21_000n, gasFeeCap: gasPrice * 2n, gasTipCap: gasPrice },
{ to: b, gas: 21_000n, gasFeeCap: gasPrice * 2n, gasTipCap: gasPrice },
]);
for (const r of results) {
console.log(r.success ? `[${r.index}] ✔ ${r.txHash}` : `[${r.index}] ✖ ${r.error?.code}`);
}[0] ✔ 0xabcd...7890
[1] ✔ 0x1f2e...66a14. 发送预签名交易
对于非托管流程,使用 buildGuaranteedTx 在其他地方构建并签署 GuaranteedTx,然后只将签名后的十六进制字符串交给操作员。企业 Nonce 密钥来自 nonceKeyForLane。
import { buildGuaranteedTx, nonceKeyForLane, toSigner } from "@stablechain/enterprise";
const signed = await buildGuaranteedTx(toSigner(signer), stable.id, {
to: recipient,
gas: 21_000n,
gasFeeCap,
gasTipCap,
nonce, // the account's current 2D-lane nonce
nonceKey: nonceKeyForLane(0n), // Enterprise lane 0
});
const { txHash } = await gb.relay(signed);
console.log("Guaranteed tx:", txHash);Guaranteed tx: 0xabcd...7890对于多笔预签名交易,请使用 relayBatch。
与免 Gas 费结合
要免除用户的 Gas 费并仍能落地在企业通道中,请使用 guaranteedWaiver 模块组合这两个机制。用户无需余额和费用字段,并且交易会通过相同的网关进行路由。请参阅保障转发交易。
处理拒绝
send 和 relay 在被拒绝时会抛出 StableEnterpriseRelayError。网关特定的代码包括 GATEWAY_UNAUTHORIZED(API 密钥缺失或无效)和 QUOTA_EXCEEDED(网关 Gas 配额已用尽)。
import { StableEnterpriseRelayError } from "@stablechain/enterprise";
try {
await gb.send(signer, { to: recipient, gas: 21_000n, gasFeeCap, gasTipCap });
} catch (err) {
if (err instanceof StableEnterpriseRelayError && err.code === "GATEWAY_UNAUTHORIZED") {
// the Enterprise RPC gateway rejected the API key
}
throw err;
}StableEnterpriseRelayError: relay failed [GATEWAY_UNAUTHORIZED]: invalid api key接下来去哪里
- 保障转发交易:一键免除用户 Gas 费并落地在企业通道中。
- 使用免 Gas 费转发:为用户提供 Gas 赞助,以便他们在不持有 USDT0 的情况下进行交易。
- Enterprise SDK 参考:所有方法、配置选项和错误类的完整说明。

