# ERC1155

### 想像一個遊戲裡，既有金幣（可互換）又有獨特武器（不可互換）。在以前，你需要兩個不同的合約分別處理。ERC1155 說：「一個合約搞定！」這就是為什麼它被稱為「多代幣標準」。

> ⚠️ **免責聲明**：本文僅供教育目的，不構成投資建議。加密貨幣投資有風險，請自行研究（DYOR）後再做決定。

***

## 什麼是 ERC1155？

| 項目   | 說明            |
| ---- | ------------- |
| 提案編號 | EIP-1155      |
| 提出者  | Enjin 團隊      |
| 類型   | 多代幣標準         |
| 特色   | 同時支援 FT 和 NFT |

***

## ERC1155 vs 其他標準

### 比較表

| 特性     | ERC20 | ERC721 | ERC1155 |
| ------ | ----- | ------ | ------- |
| 代幣類型   | 同質    | 非同質    | 兩者皆可    |
| 批量轉移   | ❌     | ❌      | ✅       |
| Gas 效率 | 一般    | 較高     | 最優      |
| 適用場景   | 貨幣    | 收藏品    | 遊戲      |

### 技術差異

```
三種標準的設計差異：

ERC20（同質代幣）：
- balanceOf(address) → 餘額
- transfer(to, amount)
- 每個代幣完全相同

ERC721（NFT）：
- ownerOf(tokenId) → 擁有者
- transferFrom(from, to, tokenId)
- 每個代幣唯一

ERC1155（多代幣）：
- balanceOf(address, id) → 特定代幣餘額
- safeBatchTransferFrom(from, to, ids[], amounts[])
- 可以是同質或非同質
- 支援批量操作
```

***

## 主要特點

### 批量操作

```
ERC1155 的批量能力：

場景：轉移 10 種不同物品

ERC721 做法：
- 呼叫 10 次 transferFrom
- 10 筆交易
- 10 倍 Gas 費

ERC1155 做法：
- 呼叫 1 次 safeBatchTransferFrom
- 1 筆交易
- 大幅節省 Gas

程式碼示例：
safeBatchTransferFrom(
  from,
  to,
  [id1, id2, id3, id4, id5],  // 多個物品 ID
  [10, 5, 1, 100, 1],         // 各自數量
  data
)
```

### 半同質代幣

| 類型    | 說明        | 例子     |
| ----- | --------- | ------ |
| 同質部分  | 可互換、可堆疊   | 遊戲金幣   |
| 非同質部分 | 每個獨特      | 傳奇裝備   |
| 半同質   | 同類可換、跨類不行 | 同款劍的多把 |

***

## 應用場景

### 遊戲產業

```
ERC1155 在遊戲中的應用：

一個 RPG 遊戲可能有：

同質資源：
- 金幣（每枚相同）
- 木材（可堆疊）
- 鑽石（通用貨幣）

非同質物品：
- 角色（每個獨特）
- 傳奇武器（限量唯一）
- 土地（位置唯一）

半同質物品：
- 普通劍（同款可換）
- 藥水（同類型相同）
- 箱子（同類別相同）

一個 ERC1155 合約全部搞定！
```

### 其他應用

| 應用   | 說明     |
| ---- | ------ |
| 活動票券 | 同類票券互換 |
| 會員卡  | 批量發行   |
| 數位收藏 | 限量版系列  |

***

## Metadata 結構

### OpenSea 範例

```json
{
    "name": "Edward Snowden (Supporting Image 2)",
    "description": "NFT 描述文字...",
    "external_link": null,
    "image": "https://lh3.googleusercontent.com/...",
    "animation_url": null
}
```

### Metadata 取得

```
取得 NFT Metadata 的方式：

API 呼叫：
GET /api/v1/metadata/{contract}/{tokenId}

回傳內容：
- name: NFT 名稱
- description: 描述
- image: 圖片 URL
- animation_url: 動畫 URL
- attributes: 屬性陣列

重要提醒：
- Metadata 通常存在鏈下
- 使用 IPFS 更去中心化
- 中心化伺服器有風險
```

***

## 開發考量

### 實作要點

| 要點     | 說明                   |
| ------ | -------------------- |
| 接收檢查   | 實作 onERC1155Received |
| 批量邏輯   | 正確處理陣列               |
| URI 管理 | 動態生成 Metadata        |
| 權限控制   | setApprovalForAll    |

```
ERC1155 合約範例結構：

contract MyGameItems is ERC1155 {
    // 代幣類型常數
    uint256 public constant GOLD = 0;
    uint256 public constant SILVER = 1;
    uint256 public constant SWORD = 2;
    uint256 public constant SHIELD = 3;

    constructor() ERC1155("https://game.example/api/item/{id}.json") {
        // 初始化：鑄造一些代幣
        _mint(msg.sender, GOLD, 10000, "");      // 10000 金幣
        _mint(msg.sender, SILVER, 5000, "");     // 5000 銀幣
        _mint(msg.sender, SWORD, 100, "");       // 100 把劍
        _mint(msg.sender, SHIELD, 50, "");       // 50 個盾
    }
}
```

{% hint style="info" %}
**URI 中的 {id}**

ERC1155 的 Metadata URI 可以使用 {id} 佔位符，合約會自動替換成實際的代幣 ID，實現動態 Metadata。
{% endhint %}

***

## 延伸閱讀

* [ERC721](/shu-wei-yi-shu-yu-shou-cang-pin/erc721.md) — NFT 標準
* [ERC20：代幣標準](/zhi-neng-he-yue-de-shi-jie/erc20-1.md) — 同質代幣標準
* [同質 vs 非同質](/shu-wei-yi-shu-yu-shou-cang-pin/tong-nfts.md) — 代幣類型比較
* [ERC 與 EIP](/zhi-neng-he-yue-de-shi-jie/smart-contract/smart-contract-developer/erc-eip.md) — 以太坊改進提案

***

#### 參考資料

* [EIP-1155 官方提案](https://eips.ethereum.org/EIPS/eip-1155)
* OpenSea 開發文件
* Enjin 技術文件


---

# 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://www.0x1.academy/shu-wei-yi-shu-yu-shou-cang-pin/erc1155.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.
