Using an erc20 token inside ierc1155 contract

I'm using erc1155 and I have a contract fully working. I can mint, buy and sell NFTs using Wei. I then convert Wei to bnb and the user is requested to buy with bnb.

What I would like to do is to be able to trade NFTs with another erc20 token. How can I do that? I've tried to import the erc20 contract and call some functions to be able to trade in a different token without success.

Also, I've searched for a similar answer and haven't found any. Is there any tutorial you can point me to?

:computer: Environment

I'm using truffle..
Thanks in advance

Hi, welcome! :wave:

I think if you can buy the NFT with BNB, then it is easy to buy the NFT with an ERC20 token, you just need to make some slight adjustments, set this ERC20 token in your contract, and call function to receive this ERC20 token.

Hey, thanks for the input. Can you shed me some light on how to do that specifically? Import the contract erc20 and call those functions inside my buyItem function, for example?

Emmmm, what is your original function to buy NFT?

function addItemToInstantBuy(uint256 _tokenId, uint256 _amount, address _tokenAddress, uint256 _askingPrice) OnlyItemOwner(_tokenAddress,_tokenId) HasTransferApproval(_tokenAddress,msg.sender) external returns (uint256){
        require(_amount > 0, "No amount set");
        require(_amount <= IERC1155(_tokenAddress).balanceOf(msg.sender, _tokenId) - activeItems[msg.sender][_tokenId]);

        uint256 newItemId = itemsForSale.length;

        ItemForSale memory instantSale = ItemForSale({
            id: newItemId,
            tokenAddress: _tokenAddress,
            tokenId: _tokenId,
            amount: _amount,
            seller: payable(msg.sender) ,
            askingPrice: _askingPrice,
            isAuction: false,  
            duration: 0,
            maxBid: 0,
            maxBidUser: address(0),
            bidAmounts: new uint256[](0),
            users: new address[](0)
        }); 

        itemsForSale.push(instantSale);
        activeItems[msg.sender][instantSale.tokenId] += instantSale.amount;
        
        assert(itemsForSale[newItemId].id == newItemId);
        emit itemAdded(newItemId, _tokenId, _amount, _tokenAddress, _askingPrice, instantSale.isAuction, instantSale.duration, 
        instantSale.maxBid, instantSale.maxBidUser, instantSale.bidAmounts, instantSale.users);
        return newItemId; 
    }

@Skyge This is what I'm trying to do to buy an Item, what am I doing wrong?

function buyItem(uint256 id, address contract_) external ItemExists(id) IsForSale(id) HasTransferApproval(itemsForSale[id].tokenAddress,itemsForSale[id].seller){
        
        require(itemsForSale[id].isAuction == false, "Item is up for Auction");
        require(msg.sender != itemsForSale[id].seller);
        require(itemsForSale[id].amount > 0, "Item Sold Out");
        
        ERC20Interface = ERC20(contract_);  
        //if erc1155
        IERC1155(itemsForSale[id].tokenAddress).safeTransferFrom(itemsForSale[id].seller, msg.sender, itemsForSale[id].tokenId, 1, "");
            if(itemsForSale[id].askingPrice > ERC20Interface.allowance(msg.sender, address(this))) {  
            revert();  
        }  
        //else if erc20
        ERC20Interface.transferFrom(msg.sender, itemsForSale[id].seller, itemsForSale[id].askingPrice);
       

        itemsForSale[id].amount -= 1;

        activeItems[itemsForSale[id].seller][itemsForSale[id].tokenId] -= 1;
       


        emit itemSold(id, msg.sender,itemsForSale[id].askingPrice, itemsForSale[id].amount);
    }

Do you use the first function to buy NFT with BNB? Sorry, I do not find out how do you transfer the BNB.

In your second function buyItem, I am not sure how do you achieve your safeTransferFrom, do you use the library SafeERC20 in the OpenZeppelin repo? If so, maybe you mismatch the parameters.

@Skyge the addItemToInstantBuy is the function that puts the item for sale.
The buyItem is to buy the item. The safeTransferFrom is from IERC1155.

Is it correct to replace the safeTransferFrom to ERC20Interface.transferFrom() if I want to transfer using an erc20 token?

Ohhh, I see.

The code looks like ok, so is there a failed transaction, or what is your contract address?