ERC20 DataManager
Data Managers are independent smart contracts that implement business logic for data management, serving as the primary entry point for user interactions and ensuring the underlying storage aligns with the required use-case logic.
There are various implementations of Data Managers, as they can integrate any logic, offering limitless possibilities. Having covered the core concepts, let's explore specific minimal implementations.
Data Managers can interact with each other. In this example, two Data Managers are deployed, with ERC1155 and ERC20 being essential for communication.
ERC20Fractions DataManager
Contract Explanation
The MinimalisticERC20FractionDataManager
contract manages ERC20 fractions of an ERC1155 token, exposing ERC20 functionalities and emitting Transfer events.
State Variables
Generic Data Manager
Name | Type | Description |
---|---|---|
_datapoint | DataPoint | DataPoint used in the fungibleFractions data object |
fungibleFractionsDO | IDataObject | Fungible Fractions Data Object contract |
dataIndex | IDataIndex | Data Index implementation |
ERC20 Data Manager Specific
Name | Type | Description |
---|---|---|
_name | string | ERC20 token name |
_symbol | string | ERC20 token symbol |
_allowances | mapping | Mapping of allowances from user to spender to amount |
ERC1155 Data Manager-Compatibility
Name | Type | Description |
---|---|---|
erc1155dm | address | ERC1155 data manager contract |
erc1155ID | uint256 | ERC1155 token ID |
In this contract, we use an alternative to a constructor for several reasons. Primarily, we need to link the ERC1155 Data Manager with the ERC20 Data Manager to create the ERC1155WithERC20FractionsDataManager. Since we don’t know all the required fields (e.g., the ERC1155 DataManager address) at deployment, this approach is necessary. Additionally, it supports upgradeability and uses the Clones pattern instead of factories. See the example below:
We observe ERC20 standard methods that read storage from the DataObject, making ERC1155 tokens compatible with the ERC20 interface. This method returns the total supply of ERC20 tokens in circulation. Unlike the 1155 Data Manager, which uses an ID to identify collections, here the entire ERC1155 collection is treated as an ERC20 token.
This contract also exposes the ERC20 approval()
method, which is not tied to token storage and follows the standard implementation. Therefore, we include the variable and the appropriate _allowance
check:
Here’s an interesting approach to ERC20 transfers:
This standard enables composability, allowing gating mechanisms on transfer methods via the _beforeTokenTransfer()
hook. This can be used for compliance checks when needed:
Finally, it forwards the write()
operation with the DataObject, DataPoint, ERC20 transfer selector, and necessary arguments. It also shows how the event is transmitted to the ERC1155 Data Manager.
Methods
1. initialize()
Description | Parameters |
---|---|
Initializes the ERC20 Fraction Data Manager | datapoint_ : DataPoint identifierdataIndex_ : Address of the Data IndexfungibleFractionsDO_ : Address of the Fungible Fractions Data Objecterc1155dm_ : Address of the ERC1155 data managererc1155ID_ : ERC1155 token IDname_ : ERC20 token namesymbol_ : ERC20 token symbol |
2. decimals()
Description | Returns |
---|---|
Returns the number of decimals for the token | Number of decimals |
3. name()
Description | Returns |
---|---|
Returns the name of the token | Token name |
4. symbol()
Description | Returns |
---|---|
Returns the symbol of the token | Token symbol |
5. totalSupply()
Description | Returns |
---|---|
Returns the total supply of the ERC20 token | Total supply |
6. balanceOf()
Description | Parameters | Returns |
---|---|---|
Returns the balance of an account | account : The account to check | Balance of the account |
7. fractionTransferredNotify()
Description | Parameters |
---|---|
Notifies a fraction transfer (only callable by ERC1155 data manager) | from : Sender addressto : Recipient addressamount : Amount transferred |
8. allowance()
Description | Parameters | Returns |
---|---|---|
Returns the allowance of a spender | owner_ : Token ownerspender : Spender address | Allowance amount |
9. approve()
Description | Parameters | Returns |
---|---|---|
Approves a spender to spend tokens | spender : Spender addressvalue : Amount to approve | True if successful |
10. transfer()
Description | Parameters | Returns |
---|---|---|
Transfers tokens to a recipient | to : Recipient addressamount : Amount to transfer | True if successful |
11. transferFrom()
Description | Parameters | Returns |
---|---|---|
Transfers tokens from one account to another | from : Sender addressto : Recipient addressamount : Amount to transfer | True if successful |
Internal Functions
1. _spendAllowance()
Description | Parameters |
---|---|
Spends allowance | owner_ : Token ownerspender : Spender addressamount : Amount to spend |
2. _writeTransfer()
Description | Parameters |
---|---|
Writes transfer to the fungible fractions data object | from : Sender addressto : Recipient addressamount : Amount to transfer |
3. _beforeTokenTransfer()
Description | Parameters |
---|---|
Hook that is called before any transfer of tokens | from : Sender addressto : Recipient addressamount : Amount to transfer |