Skip to main content

ERC6909Wrappable

Git Source

Inherits: ERC6909Metadata, IERC6909Wrappable, IERC6909TokenSupply

Title: ERC6909Wrappable

This abstract contract extends ERC6909 to allow for wrapping and unwrapping of the token to an ERC20 token. It extends the ERC6909Metadata contract, and additionally implements the IERC6909TokenSupply interface.

State Variables

_ERC20_IMPLEMENTATION

The address of the implementation of the ERC20 contract

address private immutable _ERC20_IMPLEMENTATION

_wrappableTokenIds

The set of all token IDs

EnumerableSet.UintSet internal _wrappableTokenIds

_totalSupplies

The total supply of each token

mapping(uint256 tokenId => uint256) private _totalSupplies

_tokenMetadataAdditional

Additional metadata for each token

mapping(uint256 tokenId => bytes) private _tokenMetadataAdditional

_wrappedTokens

The address of the wrapped ERC20 token for each token

mapping(uint256 tokenId => address) internal _wrappedTokens

Functions

constructor

constructor(address erc20Implementation_) ;

_getTokenData

Returns the clone initialisation data for a given token ID

function _getTokenData(uint256 tokenId_) internal view returns (bytes memory tokenData);

Parameters

NameTypeDescription
tokenId_uint256The token ID

Returns

NameTypeDescription
tokenDatabytesPacked bytes including the name, symbol, decimals and additional metadata

_getTokenAdditionalData

Returns the additional metadata for a token ID

function _getTokenAdditionalData(uint256 tokenId_) internal view returns (bytes memory additionalData);

Parameters

NameTypeDescription
tokenId_uint256The token ID

Returns

NameTypeDescription
additionalDatabytesThe additional metadata bytes

_mint

Mints the ERC6909 or ERC20 wrapped token to the recipient

function _mint(address onBehalfOf_, uint256 tokenId_, uint256 amount_, bool shouldWrap_)
internal
onlyValidTokenId(tokenId_);

Parameters

NameTypeDescription
onBehalfOf_addressThe address to mint the token to
tokenId_uint256The ID of the ERC6909 token
amount_uint256The amount of tokens to mint
shouldWrap_boolWhether to wrap the token to an ERC20 token

_burn

Burns the ERC6909 or ERC20 wrapped token from the recipient

This function reverts if:

  • amount_ is 0
  • onBehalfOf_ is 0
  • onBehalfOf_ is not the caller and has not approved the caller to spend the ERC6909 tokens (note: ERC6909 allowances govern both wrapped and unwrapped token burns)
  • ERC6909 token handling reverts
function _burn(address onBehalfOf_, uint256 tokenId_, uint256 amount_, bool wrapped_)
internal
onlyValidTokenId(tokenId_);

Parameters

NameTypeDescription
onBehalfOf_addressThe address to burn the token from
tokenId_uint256The ID of the ERC6909 token
amount_uint256The amount of tokens to burn
wrapped_boolWhether the token is wrapped

totalSupply

Returns the total supply of the token of type id.

function totalSupply(uint256 tokenId_) public view virtual override returns (uint256);

_update

Copied from draft-ERC6909TokenSupply.sol

function _update(address from, address to, uint256 id, uint256 amount) internal virtual override;

_getWrappedToken

Returns the address of the wrapped ERC20 token for a given token ID, or creates a new one if it does not exist

function _getWrappedToken(uint256 tokenId_) internal returns (IERC20BurnableMintable wrappedToken);

getWrappedToken

Returns the address of the wrapped ERC20 token for a given token ID

function getWrappedToken(uint256 tokenId_) public view returns (address wrappedToken);

Parameters

NameTypeDescription
tokenId_uint256The ID of the ERC6909 token

Returns

NameTypeDescription
wrappedTokenaddressThe address of the wrapped ERC20 token (or zero address)

wrap

Wraps an ERC6909 token to an ERC20 token

This function will burn the ERC6909 token from the caller and mint the wrapped ERC20 token to the same address. This function reverts if:

  • The token ID does not exist
  • The amount is zero
  • The caller has an insufficient balance of the token
function wrap(uint256 tokenId_, uint256 amount_) public returns (address wrappedToken);

Parameters

NameTypeDescription
tokenId_uint256The ID of the ERC6909 token
amount_uint256The amount of tokens to wrap

Returns

NameTypeDescription
wrappedTokenaddressThe address of the wrapped ERC20 token

unwrap

Unwraps an ERC20 token to an ERC6909 token

This function will burn the wrapped ERC20 token from the caller and mint the ERC6909 token to the same address. This function reverts if:

  • The token ID does not exist
  • The amount is zero
  • The caller has an insufficient balance of the wrapped token
function unwrap(uint256 tokenId_, uint256 amount_) public;

Parameters

NameTypeDescription
tokenId_uint256The ID of the ERC6909 token
amount_uint256The amount of tokens to unwrap

isValidTokenId

Returns whether a token ID is valid

function isValidTokenId(uint256 tokenId_) public view returns (bool);

Parameters

NameTypeDescription
tokenId_uint256The ID of the ERC6909 token

Returns

NameTypeDescription
<none>boolisValid Whether the token ID is valid

_onlyValidTokenId

function _onlyValidTokenId(uint256 tokenId_) internal view;

onlyValidTokenId

modifier onlyValidTokenId(uint256 tokenId_) ;

_createWrappableToken

Creates a new wrappable token

Reverts if the token ID already exists

function _createWrappableToken(
uint256 tokenId_,
string memory name_,
string memory symbol_,
uint8 decimals_,
bytes memory additionalMetadata_,
bool createWrappedToken_
) internal;

getWrappableTokens

Returns the token IDs and wrapped token addresses of all tokens

function getWrappableTokens()
public
view
override
returns (uint256[] memory tokenIds, address[] memory wrappedTokens);

Returns

NameTypeDescription
tokenIdsuint256[]The IDs of all tokens
wrappedTokensaddress[]The wrapped token addresses of all tokens

supportsInterface

function supportsInterface(bytes4 interfaceId_) public view virtual override(ERC6909, IERC165) returns (bool);