使用您的链

专注于与您的应用链交互的指南

本指南涵盖了与您的应用链交互的四个关键方面:

  1. 交易提交:如何在您的应用链上发送交易。
  2. Gas 估算:如何估算和管理交易的 Gas。
  3. 错误处理:如何处理应用链上的错误。
  4. 恢复程序:如何从问题中恢复并使用应用链特定的恢复机制。

交易提交

在应用链上,一旦您的钱包设置并连接完成,提交交易的方式与其他 EVM 链类似,但可能包括应用链特定的交易类型或元数据。

// Example: Connect to your appchain's RPC
const provider = new ethers.JsonRpcProvider('https://your-appchain-rpc-url');
const chainId = 12345; // Your appchain's unique chain ID

// Make sure your wallet/signer is set up for the appchain
const signer = provider.getSigner();

// Submit a transaction (could be ETH, tokens, or custom logic)
const tx = await signer.sendTransaction({
  to: recipientAddress,
  value: ethers.utils.parseEther('0.1'),
  chainId // ensure correct chain
});
const receipt = await tx.wait();
console.log('Confirmed on appchain:', receipt.transactionHash);

Gas 估算

应用链可能使用不同的 Gas 定价模型,或相比公链有优化。一些应用链可能有较低的基础费用、固定的 Gas 价格,甚至提供交易补贴。

// Estimate gas using your appchain's provider
const gasEstimate = await provider.estimateGas({
  to: contractAddress,
  data: contract.interface.encodeFunctionData('method', [args])
});
// Appchains may recommend a different buffer or have a fixed gas limit
const gasLimit = gasEstimate.mul(120).div(100);

错误处理

应用链可能引入自定义的错误代码、回退原因或行为,这些在公链上不存在。您可能会遇到与应用链特定逻辑、治理或状态转换相关的错误。

try {
  const tx = await signer.sendTransaction(txData);
  const receipt = await tx.wait();
  if (receipt.status === 0) throw new Error('Transaction failed');
} catch (error) {
  // Appchain-specific error handling
  if (error.code === 'APPCHAIN_CUSTOM_ERROR') {
    // Handle custom error
  } else if (error.code === 'INSUFFICIENT_FUNDS') {
    // Not enough balance
  } else {
    throw error;
  }
}

**交易模拟:**交易模拟仅检查交易在当前链状态下是否会成功。然而,即使模拟成功,交易仍可能由于排序合约中的自定义逻辑而失败,这是标准模拟工具无法检测到的。

恢复程序

应用链可能提供独特的恢复机制,例如链上治理、管理员密钥或自定义恢复合约。这些机制可能与公链的恢复流程有显著不同。

  • **卡住的交易:**应用链可能允许通过治理或管理员干预来清除卡住的交易。
  • **账户恢复:**一些应用链支持社交恢复、多签或自定义账户抽象。
  • **紧急程序:**应用链可能具有断路器、可暂停合约或其他特定于应用的安全功能。
// Example: Using an appchain's emergency pause feature
await contract.pause(); // Only available if your appchain implements this