ERC1155

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 here

Understanding Swap Parameters for ERC1155

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.

Step-by-Step Implementation

1. Define Swap Parameters

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.

const bidingAddr = [MockERC1155.address]; // Address of the ERC1155 token being offered
const tokenId1 = 4;
const amount1 = 69;
const tokenId2 = 5;
const amount2 = 69;
const tokenId3 = 6;
const amount3 = 69;

const askingAddr = [MockERC721.address]; // Address of the ERC721 token requested in exchange
const askingAmountOrId = [59, 79, 89];

2. Encode Asset Details

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.

3. Encode configuration

Encode the allowed address, expiry timestamp, recipient type, and ETH value into a single uint256 value using encodeConfig.

4. Compose the Swap

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.

5. Create the Swap

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 here

Last updated