ERC1155
Last updated
Last updated
This guide provides a step-by-step approach for developers looking to implement the swap mechanism for ERC1155 tokens within the Swaplace project. ERC1155 is a multi-token standard allowing for the representation of both fungible and non-fungible tokens in a single contract. We'll focus on creating a swap structure that accommodates multiple ERC1155 tokens in a single transaction.
Check the testsuit for this case
A swap involves exchanging tokens between parties. For ERC1155 tokens, this could mean swapping multiple instances of fungible tokens or unique non-fungible tokens. Key parameters include:
Owner: The Ethereum address initiating the swap.
Allowed: The address permitted to accept the swap. Setting this to the zero address means anyone can accept.
Expiry: A timestamp indicating when the swap offer expires.
Recipient: Determines who receives any ETH involved in the swap. 0
indicates the acceptor, values 1
through 255
indicate the swap owner.
Value: Amount of ETH involved in the swap, with a precision of up to 6 decimals.
Assets: An array detailing the assets being offered in the swap, including token addresses, IDs, and amounts.
Asking: An array specifying what assets are requested in exchange.
Start by defining the parameters for your swap. This includes the addresses of the ERC1155 tokens involved, their IDs, amounts, and configuration details like expiry and recipient.
For each ERC1155 asset involved in the swap, encode the token ID and amount into a single value using encodeAsset
. This simplifies handling multiple assets.
Encode the allowed address, expiry timestamp, recipient type, and ETH value into a single uint256 value using encodeConfig
.
With parameters and configuration ready, compose the swap structure by calling the function calledcomposeSwap
, passing in the owner's address, encoded configuration, and arrays detailing the bid and ask components.
Finally, create the swap by calling the createSwap
function on the Swaplace
contract, passing in the composed swap structure. Ensure the transaction is sent from the owner's address.
You can check the full code