|
| 1 | +# ERC-1155 Multi-Token Standard (ink! v6) |
| 2 | + |
| 3 | +This project implements the ERC-1155 standard using the ink! v6 framework for the PolkaVM RISC-V architecture. |
| 4 | + |
| 5 | +ERC-1155 is known as the "Multi-Token Standard." Unlike ERC-20 (which requires a new contract for every new currency) or ERC-721 (which tracks unique items individually), ERC-1155 can manage any number of fungible and non-fungible token types within a single smart contract. This makes it the industry standard for gaming and complex financial applications. |
| 6 | + |
| 7 | +## Project Structure |
| 8 | + |
| 9 | +- **Cargo.toml**: The configuration file. It targets `ink` version `6.0.0-beta` and includes the `parity-scale-codec` for data encoding. |
| 10 | +- **lib.rs**: The source code containing the hybrid storage logic and batch transfer mechanisms. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +To build this contract, you must have the ink! v6 toolchain installed: |
| 15 | + |
| 16 | +1. Add the Rust source code: |
| 17 | + ```bash |
| 18 | + rustup component add rust-src |
| 19 | + ``` |
| 20 | + |
| 21 | +2. Install the contract compiler (Beta): |
| 22 | + ```bash |
| 23 | + cargo install --force --locked --version 6.0.0-beta cargo-contract |
| 24 | + ``` |
| 25 | + |
| 26 | +## Building the Contract |
| 27 | + |
| 28 | +Compile the source code into a deployable package: |
| 29 | + |
| 30 | +```bash |
| 31 | +cargo contract build |
| 32 | +``` |
| 33 | + |
| 34 | +Artifacts generated in `target/ink/`: |
| 35 | +- **erc1155.contract**: The deployment bundle. |
| 36 | +- **erc1155.polkavm**: The raw RISC-V bytecode. |
| 37 | +- **erc1155.json**: The ABI metadata. |
| 38 | + |
| 39 | +## Architectural Concepts |
| 40 | + |
| 41 | +### 1. Hybrid Storage |
| 42 | +The core difference between this and previous standards is the Storage Key. |
| 43 | +- ERC-20 Maps: `User Address -> Balance` |
| 44 | +- ERC-721 Maps: `Token ID -> Owner Address` |
| 45 | +- **ERC-1155 Maps: `(User Address, Token ID) -> Balance`** |
| 46 | + |
| 47 | +This allows a single user to hold a balance of "100" for Token ID #1 (e.g., Gold Coins) and a balance of "1" for Token ID #2 (e.g., a Unique Sword) simultaneously in the same map. |
| 48 | + |
| 49 | +### 2. Batch Operations |
| 50 | +In older standards, if you wanted to send a sword, a shield, and some gold to a friend, you had to make 3 separate transactions. In ERC-1155, you can send all of them in a single transaction using "Batch Transfer." This significantly reduces gas costs and blockchain congestion. |
| 51 | + |
| 52 | +## Function Reference |
| 53 | + |
| 54 | +### Read-Only Functions |
| 55 | + |
| 56 | +#### `balance_of(owner: H160, id: TokenId) -> Balance` |
| 57 | +Returns the amount of tokens of a specific `id` owned by the `owner`. |
| 58 | +- If the token is Fungible (like currency), this returns the wallet balance. |
| 59 | +- If the token is an NFT, this returns `1` if they own it, or `0` if they don't. |
| 60 | + |
| 61 | +#### `balance_of_batch(owners: Vec<H160>, ids: Vec<TokenId>) -> Result<Vec<Balance>>` |
| 62 | +Allows querying multiple balances in a single call. |
| 63 | +- **Input**: A list of owners and a list of IDs. |
| 64 | +- **Output**: A list of balances corresponding to the inputs. |
| 65 | +- **Constraint**: The length of `owners` and `ids` must match. |
| 66 | + |
| 67 | +#### `is_approved_for_all(owner: H160, operator: H160) -> bool` |
| 68 | +Checks if an `operator` (e.g., a Marketplace contract or a Game server) is authorized to manage *all* assets belonging to `owner`. |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +### State-Changing Functions (Write) |
| 73 | + |
| 74 | +#### `mint(id: TokenId, amount: Balance)` |
| 75 | +Creates new tokens. |
| 76 | +- **id**: The identifier of the token type (e.g., `1` for Gold, `2` for Silver). |
| 77 | +- **amount**: How many to create. |
| 78 | +- **Effect**: Increases the balance of the caller and emits a `TransferSingle` event (from `None` to Caller). |
| 79 | + |
| 80 | +#### `set_approval_for_all(operator: H160, approved: bool)` |
| 81 | +Grants or revokes permission for a third-party `operator` to manage the caller's tokens. |
| 82 | +- **True**: Grants permission. The operator can now transfer any of the caller's tokens. |
| 83 | +- **False**: Revokes permission. |
| 84 | +- **Note**: Unlike ERC-20, ERC-1155 typically does not support "approving" a specific amount of a specific token. It uses a "trust the operator with everything" model, which is more efficient for games. |
| 85 | + |
| 86 | +#### `safe_transfer_from(from: H160, to: H160, id: TokenId, amount: Balance)` |
| 87 | +Transfers a specific amount of a specific token type. |
| 88 | +- **Checks**: |
| 89 | + 1. Is the caller the `from` account OR an approved operator? |
| 90 | + 2. Does the `from` account have enough balance? |
| 91 | +- **Effect**: Deducts balance from sender, adds to receiver, and emits `TransferSingle`. |
| 92 | + |
| 93 | +#### `safe_batch_transfer_from(from: H160, to: H160, ids: Vec<TokenId>, amounts: Vec<Balance>)` |
| 94 | +The signature feature of ERC-1155. Transfers multiple token types at once. |
| 95 | +- **ids**: A list of Token IDs to move (e.g., `[1, 2, 5]`). |
| 96 | +- **amounts**: A list of amounts matching the IDs (e.g., `[100, 1, 50]`). |
| 97 | +- **Constraint**: `ids.len()` must equal `amounts.len()`. |
| 98 | +- **Effect**: Updates all balances atomically. If any part of the transfer fails (e.g., insufficient balance for the 3rd item), the *entire* transaction fails. Emits `TransferBatch`. |
| 99 | + |
| 100 | +## Events |
| 101 | + |
| 102 | +### `TransferSingle` |
| 103 | +Emitted when a single token type is transferred. |
| 104 | +- Contains: `operator`, `from`, `to`, `id`, `value`. |
| 105 | + |
| 106 | +### `TransferBatch` |
| 107 | +Emitted when a batch transfer occurs. |
| 108 | +- Contains: `operator`, `from`, `to`, `ids` (list), `values` (list). |
| 109 | +- This is highly efficient for indexers, as they only need to process one event for multiple asset movements. |
| 110 | + |
| 111 | +### `ApprovalForAll` |
| 112 | +Emitted when an operator is approved or revoked. |
| 113 | + |
| 114 | +## Error Handling |
| 115 | + |
| 116 | +The contract returns a `Result` type with the following custom errors: |
| 117 | +- **NotOwner**: Caller tries to transfer tokens they don't control. |
| 118 | +- **NotApproved**: Caller is not the owner and not an authorized operator. |
| 119 | +- **InsufficientBalance**: Sender tries to send more than they have. |
| 120 | +- **BatchSizeMismatch**: Passed arrays (IDs and Amounts) have different lengths. |
| 121 | + |
| 122 | +## Testing |
| 123 | + |
| 124 | +Unit tests are included to verify the logic. |
| 125 | + |
| 126 | +```bash |
| 127 | +cargo test |
| 128 | +``` |
| 129 | + |
| 130 | +These tests confirm: |
| 131 | +- **Minting**: Correctly updates balances. |
| 132 | +- **Batch Transfers**: Successfully moves multiple assets and updates multiple storage entries in one go. |
0 commit comments