# NFT開發環境與架構
---
## 智能合約的開發流程
1. 撰寫 智能合約
2. 編譯 智能合約
3. 部署 智能合約
---
## 以太坊為核心的開發平台
* 以太坊虛擬機 [EVM](https://cypherpunks-core.github.io/ethereumbook_zh/14.html)
* Ethereum
* Polygon
* BNB Chain (BSC)
---
## 撰寫智能合約程式碼
* Solidity
* 編譯型語言:編譯後獲得 bytecode 與 ABI
* 針對EVM平台設計的語言
* 例子:[BAYC](https://opensea.io/collection/boredapeyachtclub)
* 好用的Library:[OpenZeppelin](https://openzeppelin.com/contracts/)
---
### 開發工具
* 線上版IDE
* [Remix](https://remix.ethereum.org/#optimize=false&runs=200&evmVersion=null)
* 開發框架
* [Truffle](https://trufflesuite.com/)
* 影片:[Ganache & Truffle 環境安裝](https://www.youtube.com/watch?v=BSnH_b8I6tc)
* [Hardhat](https://hardhat.org/)
* 影片:[Hardhat 環境安裝](https://www.youtube.com/watch?v=S0DFNvJu4TQ)
---
## 部署智能合約:將bytecode佈署到EVM
* 測試網
* Testnet Rinkeby
* Testnet Ropsten
* 水龍頭:[Chainlink Faucets](https://faucets.chain.link/)
* [Metamask](https://metamask.io/) 非託管式錢包
* 影片:[Metamask安裝申請](https://www.youtube.com/watch?v=WoUNXIlRQ5c)
* 節點供應商:[Infura](https://infura.io/) , [Alchemy](https://www.alchemy.com/)
---
## 檢視與驗證智能合約
* Ethereum瀏覽器
* [Etherscan](https://etherscan.io/)
* 例子:[BAYC](https://etherscan.io/address/0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d#contracts)
* [Etherscan Testnet Rinkeby](https://rinkeby.etherscan.io/?__cf_chl_tk=UwawmqETyNSLR.zJl1Gy6OT1i8DDuJj3xYHT.nwiDMM-1646833722-0-gaNycGzNCGU)
* 使用框架驗證合約
* [Etherscan API](https://etherscan.io/myaccount)
---
## Metadata
* [Opensea Metadata](https://docs.opensea.io/docs/metadata-standards) 範例
```json=
{
"description": "Friendly OpenSea Creature that enjoys long swims in the ocean.",
"external_url": "https://openseacreatures.io/3",
"image": "https://storage.googleapis.com/opensea-prod.appspot.com/puffs/3.png",
"name": "Dave Starbelly",
"attributes": [ ... ],
}
```
---
### Attributes 範例
```json=
...
{
"attributes": [
{
"trait_type": "Base",
"value": "Starfish"
},
{
"trait_type": "Eyes",
"value": "Big"
},
{
"trait_type": "Mouth",
"value": "Surprised"
},
{
"trait_type": "Level",
"value": 5
},
{
"trait_type": "Stamina",
"value": 1.4
},
{
"trait_type": "Personality",
"value": "Sad"
},
{
"display_type": "boost_number",
"trait_type": "Aqua Power",
"value": 40
},
{
"display_type": "boost_percentage",
"trait_type": "Stamina Increase",
"value": 10
},
{
"display_type": "number",
"trait_type": "Generation",
"value": 2
}
]
}
```
---
### [IPFS](https://ipfs.io/) 技術
* 分散式的 P2P 網路儲存服務,每一個節點都掌握部份的分割檔案,一旦上傳成功,檔案將被紀錄在 [Merkle Tree](https://ithelp.ithome.com.tw/articles/10215108) 上而且無法修改與刪除,每個檔案都有唯一的 HASH 值,允許使用者利用該唯一的 HASH 值訪問並下載檔案。
* 參考資料:
[一分鐘了解星際檔案系統 IPFS 原理與應用
](https://blockcast.it/2019/10/21/mica-research-1-min-intro-of-ipfs/#:~:text=IPFS%20%E5%8D%94%E8%AD%B0%E9%81%8B%E7%94%A8%E4%BA%86%E5%8D%80,%E5%8F%AA%E8%83%BD%E6%8C%81%E6%9C%89%E5%B0%8D%E6%87%89)
---
### 實際例子:[BAYC](https://opensea.io/collection/boredapeyachtclub)
```json=
{
"image":"ipfs://QmcJYkCKK7QPmYWjp4FD2e3Lv5WCGFuHNUByvGKBaytif4",
"attributes":
[
{"trait_type":"Eyes","value":"3d"},
{"trait_type":"Mouth","value":"Bored Cigarette"},
{"trait_type":"Fur","value":"Robot"},
{"trait_type":"Hat","value":"Sea Captain's Hat"},
{"trait_type":"Background","value":"Aquamarine"}
]
}
```
---
## 牛刀小試
---
### 部署一份最簡單的ERC721
* 使用 [OpenZeppelin](https://openzeppelin.com/contracts/) Libraby
* 使用 [Remix](https://remix.ethereum.org/#optimize=false&runs=200&evmVersion=null) 編譯並部署
* 使用 [Etherscan](https://etherscan.io/) 驗證並檢視
```solidity==
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract MyToken is ERC721 {
constructor() ERC721("MyToken", "MTK") {}
}
```
{"metaMigratedAt":"2023-06-16T20:51:08.433Z","metaMigratedFrom":"Content","title":"NFT開發環境與架構","breaks":true,"contributors":"[{\"id\":\"11079d34-d747-4fac-96df-c6f4e4f2721f\",\"add\":7114,\"del\":3111}]"}