-1

Is there a way to send an 1155 or 731 nft in one transaction together with a token(erc-20) and if yes could someone provide an example

NoVa
  • 1
  • 1

1 Answers1

0

You can create a muticall contract that wraps both token transfers into one transaction.

Example:

pragma solidity ^0.8;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";

contract MyContract {
  IERC20 erc20Token = IERC20(address(0x123));
  IERC721 nftCollection = IERC721(address(0x456));


  function sendFromThisContract(address to, uint256 erc20Amount, uint256 nftId) external {
    erc20Token.transfer(to, erc20Amount);
    nftCollection.transferFrom(address(this), to, nftId);
  }

  function sendFromUser(address to, uint256 erc20Amount, uint256 nftId) external {
    erc20Token.transferFrom(msg.sender, to, erc20Amount);
    nftCollection.transferFrom(msg.sender, to, nftId);
  }
}

In case of sendFromUser(), the user needs to send additional 2 transactions to be able to perform this action:

  1. to the ERC20 token contract, approving MyContract to operate the user's tokens
  2. to the NFT collection contract, approving MyContract to operate the user's specific token ID (or all tokens)

It is not technically possible to transfer multiple tokens (with different contract addresses) in one transaction without an intermediary or without the approvals. This is because when you're sending a token, you're sending a transaction to the token/collection contract. And by design, a transaction can have only one recipient.

Petr Hejda
  • 40,554
  • 8
  • 72
  • 100