Token Bundler inherits other contracts and implements certain interfaces. Please see their reference for a complete overview.
create
Overview
This function mints a bundle token and transfers assets to the Bundler contract.
Make sure to approve all bundled assets towards the Token Bundler contract before calling this function.
This function takes one argument:
MultiToken.Asset[] memory
_assets
- List of assets to include in a bundle
See MultiToken for more information about the argument type.
The function returns the ID of the created bundle.
Implementation
Copy function create ( MultiToken . Asset [] memory _assets ) override external returns ( uint256 bundleId) {
uint256 length = _assets.length;
require (length > 0 , "Need to bundle at least one asset" );
require (length <= type( uint256 ).max - _nonce , "Bundler out of capacity" );
bundleId = ++ _id;
uint256 _bundleNonce = _nonce;
unchecked { _nonce += length; }
for ( uint i; i < length;) {
unchecked { ++ _bundleNonce; }
_tokens[_bundleNonce] = _assets[i];
_bundles[bundleId]. push (_bundleNonce);
_assets[i]. transferAssetFrom (msg.sender , address ( this ));
unchecked { ++ i; }
}
_mint (msg.sender , bundleId , 1 , "" );
emit BundleCreated (bundleId , msg.sender);
}
unwrap
Overview
This function burns the bundle token and transfers assets to the caller.
The caller has to be the bundle owner.
This function takes one argument:
uint256
_bundleId
- Bundle id to unwrap
Implementation
Copy function unwrap ( uint256 _bundleId ) override external {
require ( balanceOf (msg.sender , _bundleId) == 1 , "Sender is not bundle owner" );
uint256 [] memory tokenList = _bundles[_bundleId];
uint256 length = tokenList.length;
for ( uint i; i < length;) {
_tokens[tokenList[i]]. transferAsset (msg.sender);
delete _tokens[tokenList[i]];
unchecked { ++ i; }
}
delete _bundles[_bundleId];
_burn (msg.sender , _bundleId , 1 );
emit BundleUnwrapped (_bundleId);
}
token
Overview
Each token has its nonce. This function returns an Asset struct (see MultiToken ) for a provided token nonce.
This function takes one argument:
uint265
_tokenId
- Token nonce from the bundle asset list.
Implementation
Copy function token ( uint256 _tokenId ) override external view returns ( MultiToken . Asset memory ) {
return _tokens[_tokenId];
}
bundle
Overview
Returns an array of asset IDs in a bundle as a uint256[]
.
This function takes one argument:
uint256
_bundleId
- Bundle id
Implementation
Copy function bundle ( uint256 _bundleId ) override external view returns ( uint256 [] memory ) {
return _bundles[_bundleId];
}
tokensInBundle
Overview
Returns an array of assets in a bundle. Each asset is represented as an Asset struct (see MultiToken ).
This function takes one argument:
uint256
_bundleId
- Bundle id
Implementation
Copy function tokensInBundle ( uint256 _bundleId ) override external view returns ( MultiToken . Asset [] memory ) {
uint256 [] memory tokenList = _bundles[_bundleId];
uint256 length = tokenList.length;
MultiToken.Asset[] memory tokens = new MultiToken.Asset[](length);
for ( uint256 i; i < length;) {
tokens[i] = _tokens[tokenList[i]];
unchecked { ++ i; }
}
return tokens;
}
The Token Bundler contract doesn't define any events or custom errors and inherits events from ITokenBundler , ERC1155 , and Ownable . We will cover events inherited from the ITokenBunder interface. Please see the reference for ERC1155 and Ownable for a complete overview.
Copy event BundleCreated ( uint256 indexed id, address indexed creator);
event BundleUnwrapped ( uint256 indexed id);
BundleCreated
BundleCreated event is emitted when a new bundle is created.
This event has two parameters:
uint256 indexed
id
- Id of the bundle
address indexed
creator
- Address of the bundle creator
BundleUnwrapped
BundleUnwrapped event is emitted when a bundle is unwrapped and burned.
This event has one parameter:
uint256 indexed
id
- Id of the unwrapped bundle.