> ## Documentation Index
> Fetch the complete documentation index at: https://docs.plasma.org/llms.txt
> Use this file to discover all available pages before exploring further.

# 搭建开发环境

> 配置工具并连接 Plasma 进行智能合约开发。

Plasma 完全兼容 EVM，让你可以使用标准的 Ethereum 工具，同时享受为稳定币原生应用优化的特性。要详细了解 Plasma 的架构，请参阅[架构概览](../../plasma-chain/architecture/system-overview.mdx)章节。

## 验证 RPC 连接

公共 RPC 端点对两个 Plasma 网络均可用：

```shell theme={null}
# Mainnet (recommended)
curl --location https://rpc.plasma.to --header 'Content-Type: application/json' --data '{"method":"eth_blockNumber","params":[],"id":1,"jsonrpc":"2.0"}'

# Testnet (optional)
curl --location https://testnet-rpc.plasma.to --header 'Content-Type: application/json' --data '{"method":"eth_blockNumber","params":[],"id":1,"jsonrpc":"2.0"}'
```

Mainnet 的响应类似：

```output theme={null}
{"jsonrpc":"2.0","id":1,"result":"0x11180f"}
```

<Tip>
  公共端点有速率限制。生产环境或高频请求请考虑使用[合作伙伴 RPC 提供商](../../plasma-chain/tools/rpc-providers)。
</Tip>

## 配置开发工具

以下示例分别针对 [Hardhat](#hardhat) 与 [Truffle](#truffle-configuration)：

### Hardhat

创建或修改你的 `hardhat.config.js` 文件，纳入两个 Plasma 网络：

```javascript theme={null}
require("dotenv").config();

module.exports = {
  networks: {
    plasmaMainnet: {
      url: process.env.MAINNET_RPC_ENDPOINT || "https://rpc.plasma.to",
      chainId: 9745,
      accounts: process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : []
    },
    // Optional: enable the testnet for staging.
    plasmaTestnet: {
      url: process.env.TESTNET_RPC_ENDPOINT || "https://testnet-rpc.plasma.to",
      chainId: 9746,
      accounts: process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : []
    }
  },
  solidity: {
    version: "0.8.28",
    settings: {
      optimizer: {
        enabled: true,
        runs: 200
      }
    }
  }
};
```

### Truffle 配置

如果使用 Truffle，请更新 `truffle-config.js`：

```javascript theme={null}
const HDWalletProvider = require('@truffle/hdwallet-provider');

module.exports = {
  networks: {
    plasmaMainnet: {
      provider: () => new HDWalletProvider(
        "<YOUR_MNEMONIC_HERE>",
        process.env.MAINNET_RPC_ENDPOINT || "https://rpc.plasma.to",
      ),
      network_id: 9745,
      gas: 5500000,
    },
    plasmaTestnet: {
      provider: () => new HDWalletProvider(
        "<YOUR_MNEMONIC_HERE>",
        process.env.TESTNET_RPC_ENDPOINT || "https://testnet-rpc.plasma.to",
      ),
      network_id: 9746,
      gas: 5500000,
    }
  },
  compilers: {
    solc: {
      version: "0.8.28",
      settings: {
        optimizer: {
          enabled: true,
          runs: 200
        }
      }
    }
  }
};
```

## 钱包集成

### Web3.js 集成

```javascript theme={null}
const Web3 = require('web3');
const web3 = new Web3(process.env.MAINNET_RPC_ENDPOINT || 'https://rpc.plasma.to');

// Check connection
web3.eth.getBlockNumber()
  .then(blockNumber => console.log('Connected to Plasma node. Current block:', blockNumber))
  .catch(error => console.error('Connection error:', error));
```

### Ethers.js 集成

```javascript theme={null}
const { ethers } = require('ethers');
const provider = new ethers.JsonRpcProvider('https://testnet-rpc.plasma.to');

// Check connection
provider.getBlockNumber()
  .then(blockNumber => console.log('Connected to Plasma node. Current block:', blockNumber))
  .catch(error => console.error('Connection error:', error));
```

## 验证设置

创建一个简单的脚本来验证你的开发环境配置是否正确：

```javascript theme={null}
const { ethers } = require('ethers');
require('dotenv').config();

async function testSetup() {
  // Connect to the Plasma node.
  const provider = new ethers.JsonRpcProvider('https://testnet-rpc.plasma.to');
  
  // Get network information.
  const network = await provider.getNetwork();
  console.log('Connected to network:', network);
  
  // Get current block number.
  const blockNumber = await provider.getBlockNumber();
  console.log('Current block number:', blockNumber);
  
  // Optionally, print address and balance if PRIVATE_KEY is set.
  if (process.env.PRIVATE_KEY) {
    const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider);
    const balance = await provider.getBalance(wallet.address);
    console.log('Account address:', wallet.address);
    console.log('Balance:', ethers.formatEther(balance), 'XPL');
  } else {
    console.log('Set PRIVATE_KEY to also print address and balance.');
  }
}

testSetup()
  .then(() => console.log('Setup test completed successfully'))
  .catch(error => console.error('Setup test failed:', error));
```

## 故障排查

| 问题                | 处理方法                     |
| :---------------- | :----------------------- |
| **连接被拒绝**         | 确认 RPC 端点正确且可访问          |
| **Gas 估算失败**      | 确保钱包有足够的 XPL             |
| **Solidity 版本错误** | 更新配置以匹配 Plasma 支持的编译器版本  |
| **交易失败**          | 校验 RPC URL 与正确的 chain ID |
