Skip to content

Latest commit

 

History

History
240 lines (144 loc) · 6.35 KB

token-bundler.md

File metadata and controls

240 lines (144 loc) · 6.35 KB

Token Bundler

1. Summary

The contract enables bundling ERC20, ERC721, and/or ERC1155 tokens into a single ERC1155 token (the Bundle token).

2. Important links

{% embed url="https://github.com/PWNFinance/TokenBundler/tree/master" %} GitHub {% endembed %}

{% file src="../../.gitbook/assets/TokenBundler.json" %} JSON ABI {% endfile %}

3. Contract Details

  • TokenBundler.sol contract is written in Solidity version 0.8.16

Features

The owner of the bundle token has rights to:

  • Transferring ownership of the bundle token to another address
  • Unwrap the entire bundle resulting in burning the bundle token and gaining ownership over the wrapped tokens

Inherited Contracts

Token Bundler inherits other contracts and implements certain interfaces. Please see their reference for a complete overview.

Functions

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

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

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);
}

View functions

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

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

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

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;
}

Events

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.

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 indexedid - Id of the bundle
  • address indexedcreator - Address of the bundle creator
BundleUnwrapped

BundleUnwrapped event is emitted when a bundle is unwrapped and burned.

This event has one parameter:

  • uint256 indexedid - Id of the unwrapped bundle.