Swaplace
  • About Swaplace
    • Manifesto
    • Overview
    • Roadmap
    • Tokenomics (🚧)
    • Start swaping with Swaplace
    • Marketplace
    • Call for contributors
      • ✧ How to start
      • ✧ Incentives
    • Community & Links
  • Developers
    • Guides
      • ✧ Overview
      • ✧ Setting up your local environment
      • ✧ Preparing
        • Encode config
        • Encode ERC1155 Asset
        • Make Swap
      • ✧ Creating
        • ERC20/ERC721
        • ERC1155
        • Native Ether
      • ✧ Accepting
      • ✧ Canceling
    • Technical Reference
      • ✧ Overview
      • ✧ Swaplace
      • ✧ SwapFactory
      • ✧ Interfaces
        • ✧ IERC165
        • ✧ IErrors
        • ✧ ISwap
        • ✧ ISwapFactory
        • ✧ ISwaplace
    • Deployments
Powered by GitBook
On this page
  • Understanding Swap Parameters for ERC1155
  • Step-by-Step Implementation
  • 1. Define Swap Parameters
  • 2. Encode Asset Details
  • 3. Encode configuration
  • 4. Compose the Swap
  • 5. Create the Swap
  1. Developers
  2. Guides
  3. ✧ Creating

ERC1155

PreviousERC20/ERC721NextNative Ether

Last updated 11 months ago

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

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.

const amountAndId1 = await Swaplace.encodeAsset(tokenId1, amount1);
const amountAndId2 = await Swaplace.encodeAsset(tokenId2, amount2);
const amountAndId3 = await Swaplace.encodeAsset(tokenId3, amount3);
const bidingAmountOrId = [amountAndId1, amountAndId2, amountAndId3];

3. Encode configuration

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

uconst currentTimestamp = (await blocktimestamp()) + 1000000; // Future timestamp for expiry
const config = await Swaplace.encodeConfig(allowed.address, currentTimestamp, 0, 0);

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.

const swap = await composeSwap(owner.address, config, bidingAddr, bidingAmountOrId, askingAddr, askingAmountOrId);

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.

await expect(await Swaplace.connect(owner).createSwap(swap))
  .to.emit(Swaplace, "SwapCreated")
  .withArgs(nextSwapId, owner.address, allowed.address);

You can check the full code

here
here