ERC-1178 Multi-class token standard
Stagnant · NFTs & multi-token · created 2018-06-22
A standard that allows multiple classes of fungible tokens, like preferred and common shares, to be issued together within a single contract, with ownership tracked, transferred, and exchanged.
Why it is needed
An ERC-20 contract can issue only one type of token, and ERC-721 only supports "classes" distinguished by an individual token_id per token, requiring tokens to be created one by one. So to handle multiple classes of fungible assets within a single contract, like a company's preferred, common, and restricted shares, the only option is to mimic ERC-721's token struct attributes. However, the original author's own testing found that even with the maximum gas sendable from MetaMask, only about 46 tokens could be created, making individual token creation impractical due to excessive gas costs. In this situation, a developer trying to handle multi-class assets has no choice but to use an inefficient data structure or deploy a separate contract for each class.
What to implement
function name() constant returns (string name): Returns the name of the entire collection of MCFTs managed by this contract (e.g. "My Company Tokens"). Optional.function className(uint256 classId) constant returns (string name): Returns the name of a specific classId class (e.g. "My Company Preferred Shares Token"). Optional.function symbol() constant returns (string symbol): Returns a short symbol representing the entire contract (e.g. "MUL"). Optional.function totalSupply() constant returns (uint256 totalSupply): Returns the total count of all MCFTs tracked by this contract.function individualSupply(uint256 _classId) constant returns (uint256 individualSupply): Returns the total count of MCFTs belonging to a specific _classId.function balanceOf(address _owner, uint256 _classId) constant returns (uint256 balance): Returns the quantity of _classId tokens held by owner _owner.function classesOwned(address _owner) constant returns (uint256[] classes): Returns an array of all classIds held by owner _owner.function approve(address _to, uint256 _classId, uint256 quantity): Approves _to to take up to quantity of _classId tokens. Fails on insufficient balance, an unregistered class, or specifying oneself.function transfer(address _to, uint256 _classId, uint256 quantity): Transfers the entire balance of msg.sender's _classId tokens to _to (only when quantity equals the balance).function approveForToken(uint256 classIdHeld, uint256 quantityHeld, uint256 classIdWanted, uint256 quantityWanted): Approves an exchange of your classIdHeld tokens for classIdWanted tokens at a specified ratio. Equivalent to placing a buy order on a marketplace.function exchange(address to, uint256 classIdPosted, uint256 quantityPosted, uint256 classIdWanted, uint256 quantityWanted): Settles an exchange order registered via approveForToken, swapping each party's tokens.transferFrom(address from, address to, uint256 classId): Lets an approved third party transfer from's classId tokens to to on their behalf.event Transfer(address indexed _from, address indexed _to, uint256 _classId): An event emitted whenever ownership is transferred in any way, including minting (_from=0) and burning (_to=0).event Approval(address indexed _owner, address indexed _approved, uint256 _classId): An event emitted when an approve call sets, changes, reconfirms, or revokes an approval.
In depth
ERC-20 compatible metadata functions
name, className, and symbol are all OPTIONAL and exist for readability in wallets and exchanges; other contracts or interfaces MUST NOT depend on these functions existing. name() returns the name of the entire collection of MCFTs managed by the contract (e.g. "My Company Tokens"), className(classId) returns the name of an individual class (e.g. "My Company Preferred Shares Token"), and symbol() returns a short symbol representing the whole (e.g. "MUL"). symbol SHOULD be 3 to 8 uppercase letters with no spaces or line breaks.
Supply lookup
totalSupply() returns the total count of all MCFTs tracked by this contract. individualSupply(_classId) returns only the total count of MCFTs belonging to a single specified class.
Ownership lookup and classesOwned
Unlike ERC-20's balanceOf, balanceOf(_owner, _classId) takes an additional class (classId) argument to look up holdings for a specific class only. classesOwned(_owner) returns an array of all classIds held by the owner; the original text notes that returning this array is supported via pragma experimental ABIEncoderV2.
State transition rules for approve
approve(_to, _classId, quantity) MUST throw if msg.sender's balance for that classId is less than quantity, if the classId is not registered with this contract, or if msg.sender==_to. Only one address can hold an approval for a given owner-classId pair at a time, so a new approve call overwrites the previous approval. Calling with _to as 0 revokes the approval. Setting, changing, reconfirming (calling approve again with the same address), or revoking an approval MUST all emit an Approval event, except when attempting to revoke an approval that does not already exist.
transfer allows only full transfers
transfer(_to, _classId, quantity) MUST succeed only when quantity exactly equals msg.sender's balance for that classId (quantity == balanceOf(msg.sender, _classId)), and MUST throw otherwise. In other words, partial transfers are not possible. A Transfer event is emitted on success. A "no-op transfer" to msg.sender itself is also allowed as a valid action that reconfirms ownership, and in this case too a Transfer event with the same from and to MUST be emitted.
Advanced exchange functions: approveForToken, exchange, transferFrom
approveForToken(classIdHeld, quantityHeld, classIdWanted, quantityWanted) is a function, equivalent to placing a buy order on a marketplace, that approves a third party (or the contract itself) to exchange your held tokens for another class of tokens at a specified ratio. exchange(to, classIdPosted, quantityPosted, classIdWanted, quantityWanted) is the function that settles that order; in the original sample implementation it is designed to succeed only when the counterparty has already approved the token transfer to the contract, but it also notes that an implementation could handle both approval and transfer in a single call. transferFrom(from, to, classId) is the function by which an approved third party performs the transfer on someone's behalf.
Events and minting/burning rules
The Transfer event MUST be emitted whenever ownership changes by any mechanism, including transfer and transferFrom. When minting new MCFTs, a Transfer event with _from set to 0 MUST be emitted for each token, and when burning, a Transfer event with _to set to 0 MUST be emitted. A Transfer event where _from and _to are the same is also valid for the no-op transfer case described in the transfer() documentation.
Compatibility limits with ERC-721, and the standard's status
This standard tries to follow ERC-721's semantics as closely as possible, but it is not fully compatible. Because of the fundamental difference between multi-class fungible tokens and non-fungible tokens, ERC-721's ownerOf, takeOwnership, and tokenOfOwnerByIndex cannot be implemented under this standard, and the function argument composition of balanceOf, approve, and transfer also differs. This proposal is currently Stagnant, with no further discussion ongoing, so the specification may change or may never be adopted.