# Truffle

### 設置開發環境

#### 要求&#x20;

在我們開始之前，有一些技術要求。 請安裝以下內容： 要求：

* Windows, Linux or Mac OS X
* [Node.js v8.9.4 LTS or later](https://nodejs.org/en/)
* [Git](https://git-scm.com/)

**針對 Windows 的建議** 如果您在 Windows 上運行 Truffle，您可能會遇到一些可能阻止 Truffle 正確執行的命名衝突。 請參閱解決命名衝突部分的解決方案。

#### Installing Truffle <a href="#installing-truffle" id="installing-truffle"></a>

一旦我們安裝了這些，我們只需要一個命令來安裝 Truffle：

```
npm install -g truffle
```

要驗證 Truffle 是否已正確安裝，請在終端上鍵入 truffle version。 如果您看到錯誤，請確保您的 npm 模塊已添加到您的路徑中。

### 項目創建、編譯和配置

第一步是創建一個 Truffle 項目。 我們將使用 \*MegaCoin 作為示例，它創建了一個可以在賬戶之間轉移的代幣：

#### 為您的 Truffle 項目創建一個新目錄

```
mkdir MegaCoin
cd MegaCoin
```

#### 初始化你的項目：

要初始化您的項目，請使用以下命令

```
truffle init
```

完成此操作後，您現在將擁有一個包含以下項目的項目結構：&#x20;

* contract/: Solidity 合約目錄&#x20;
* migrations/：可編寫腳本的部署文件的目錄
* &#x20;test/：用於測試應用程序和合約的測試文件目錄&#x20;
* truffle-config.js：松露配置文件

#### 創建合同&#x20;

您可以編寫自己的智能合約或下載 BEP20 代幣智能合約模板。&#x20;

#### 編譯合約&#x20;

要編譯 Truffle 項目，請切換到項目所在目錄的根目錄，然後在終端中鍵入以下內容：

```
truffle compile
```

#### 為 PSC 配置Truffle

* &#x20;轉到 truffle-config.js&#x20;
* 使用 bsc-network-credentials 更新 truffle-config。

```
const HDWalletProvider = require('@truffle/hdwallet-provider');
const fs = require('fs');
const mnemonic = fs.readFileSync(".secret").toString().trim();

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",     // Localhost (default: none)
      port: 8545,            // Standard BSC port (default: none)
      network_id: "*",       // Any network (default: none)
    },
    testnet: {
      provider: () => new HDWalletProvider(mnemonic, `https://data-seed-prebsc-1-s1.binance.org:8545`),
      network_id: 97,
      confirmations: 10,
      timeoutBlocks: 200,
      skipDryRun: true
    },
    bsc: {
      provider: () => new HDWalletProvider(mnemonic, `https://bsc-dataseed1.binance.org`),
      network_id: 56,
      confirmations: 10,
      timeoutBlocks: 200,
      skipDryRun: true
    },
  },

  // Set default mocha options here, use special reporters etc.
  mocha: {
    // timeout: 100000
  },

  // Configure your compilers
  compilers: {
    solc: {
      version: "^0.6.12", // A version or constraint - Ex. "^0.5.0"
    }
  }
}
```

請注意，它需要為 Provider 傳遞助記詞，這是您要部署的帳戶的助記詞。 在根目錄中創建一個新的 .secret 文件並輸入您的 12 字助記詞助記詞以開始使用。 要從 metamask 錢包中獲取種子詞，您可以轉到 Metamask 設置，然後從菜單中選擇安全和隱私，您將在其中看到一個顯示種子詞的按鈕。

### 在 PSC 網絡上部署

在項目目錄的根目錄中運行此命令：

```
$ truffle migrate --network testnet
```

合約將部署在 Poly Chain Chapel 測試網上，如下所示：

```
1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
   > transaction hash:    0xaf4502198400bde2148eb4274b08d727a17080b685cd2dcd4aee13d8eb954adc
   > Blocks: 3            Seconds: 9
   > contract address:    0x81eCD10b61978D9160428943a0c0Fb31a5460466
   > block number:        3223948
   > block timestamp:     1604049862
   > account:             0x623ac9f6E62A8134bBD5Dc96D9B8b29b4B60e45F
   > balance:             6.24574114
   > gas used:            191943 (0x2edc7)
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00383886 ETH

   Pausing for 5 confirmations...
   ------------------------------
   > confirmation number: 2 (block: 3223952)
   > confirmation number: 3 (block: 3223953)
   > confirmation number: 4 (block: 3223954)
   > confirmation number: 6 (block: 3223956)

   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00383886 ETH


Summary
=======
> Total deployments:   1
> Final cost:          0.00383886 ETH
```

> 請記住您提供的地址、transaction\_hash 和其他詳細信息會有所不同，以上只是提供一個結構概念。

恭喜！ 您已成功部署 PRC20 智能合約。&#x20;

現在您可以與智能合約進行交互。 您可以在此處查看部署狀態： <https://scan.polysmartchain.com/>


---

# Agent Instructions: 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:

```
GET https://docs-cn.polysmartchain.com/kai-fa-zhe/truffle.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
