# 環境建置
1. VSCode
3. Ganache
4. Metamask
5. Truffle
6. Infura(待測試,參考的教學要貼助記詞,太危險)
7. [額外補充 - 實務情況](/L2I5WkwfRW2mw-50-b9ORQ)
## 1. VSCode
安裝 solidity 套件(讓VSCode 可辨識 .sol 檔)

vscode plugins:
- solidity
- Solidity Contract Flattener
- Solidity Debugger
- Solidity Metrics
- Solidity Visual Developer
## 2. Ganache 安裝
> 介紹:提供快速的本地開發測試環境,啟動時自動創建十組帳號,資料僅暫時在記憶體中,程式關掉後資料就會消失。
MacOs 可以直接到官網下載安裝

點擊 quickstart
Windows 安裝參考
https://steemit.com/blockchain/@oneleo/windows-ethereum-ganache
## 3. MetaMask 設定
### 設定本地的鏈:
新的RPC URL -> 抓自 Ganache 的 RPC Server
鏈 ID -> 1337 這個基本上是固定值,代表本地的port

### 導入Ganache錢包
點擊錢包列表右側的鑰匙符號,會有 private key 可以複製

到 Metamask 導入帳戶,貼上剛剛複製的私鑰

導入成功之後會長的像這樣

## 5. Truffle 安裝設定
> PS. 本次 Node version v16.13.0,可以使用 nvm 做 node 多版本管理
```bash=
npm install -g truffle # or yarn global add truffle@
truffle version # 查看版本
mkdir project_name
cd project_name
truffle init # 建立 contracts, migrations, truffle-config.js 等檔案
```
### 設定 truffle-config.js 的 networks, compiles
[參考此處設定](/qmC0cAFESIOrqBnQoNWjkQ)
```bash=
truffle compile # 編譯
truffle migrate # 執行部屬合約用,他會執行在此專案內migrations資料夾內的檔案
truffle migrate --reset # add --reset, to update the contract which has deployed
# 執行完可以打開 ganache 發現第一個錢包本來有100eth,現在變成99.99eth
# 恭喜到這裡就成功在本地部屬了你的智能合約
# 另外補充
truffle console # 命令行,類似資料庫命令行(mongo之類的)
```
**1. truffle.config.js 的 IP 與port 要與 RPC SERVER 中的 networks 一致**


**2. compilers 的 version 要與 contract 內的 solidity 版本一致**

## 6. Infura 設定
> 之前的設定都是關於本地架RPC server及測試鏈(Ganache),Infura提供一個快速可使用的RPC server,我們可以透過它連到測試鏈或者公鏈
Infura 官網連結:https://infura.io/
註冊流程可參考:https://ithelp.ithome.com.tw/articles/10202794
建立完專案後可獲得:`project id`, `project secret`, `endpoints`
可選擇哪一種鏈:這裡可以用ropsten
先安裝
```
yarn add truffle-hdwallet-provider
```
truffle-config.js
```javascript=
networks: {
ropsten: {
provider: function () {
return new HDWalletProvider("MetaMask助記詞<-這個不要用好了,感覺很危險", "拿到的endpoint");
},
network_id: '3',
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 7545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
},
},
```
部屬指定 ropsten
```
truffle migrate --network ropsten
```