Solidity – How to Use ERC20 Token with ERC721 in Contract

ethereumjssoliditytokens

let’s consider in our application the users have ideas as NFT on which they will get funding. Each idea should be unique So people can send funds to NFT as ERC 20 tokens. Now i want to make the contract in which i can use both standards how is it possible kindly please tell?

Description: I want to create a contract in which user upload his idea as NFT and these NFT's can get fund from different users and users must send erc20 token as fund. and we know that there are some common functions in ERC20 and 721 so that we cannot use both in single cotract like i.e.

contract idea is ERC20,ERC721{
.
.
.
}
 


 So what i did for that purpose
    
    I have 3 contracts '1' is main.sol  where i'm importing my '2' other contracts Erc.sol and NFT.sol
    ------- main.sol -------

    SPDX-License-Identifier: Unlicense
    pragma solidity 'some version';
    import "./NFT.sol";
    import "./ERc.sol";
    contract main  {
    
          ERc erc =new ERc();
    
       constructor(uint256 _initialSupply)  {
          
     erc.addInitialSupply(_initialSupply);
        
    }
    
        
        NFT nft = new NFT();
      
     //by using nft instance i can call the NFT contract functions
    } 

——- NFT.sol ——-

pragma solidity 'some version';

import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";



// SPDX-License-Identifier: Unlicense
contract NFT is ERC721URIStorage{
  
//_minting will be here and constructor will overide ERC721

    
//get fund on nftIdea
function fund () public view returns(bool) {

bool res=erc.fundidea('address',value)
return res;
}
}

——- ERC20.sol ——-

// SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.2;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "./NFT.sol";

    contract ERc is ERC20 {
        
    constructor() ERC20('','') {
            
        }
    
    function addInitialSupply(uint256 _initialSupply)  public {
            _mint(msg.sender, _initialSupply * 10 );
        }
        function fundIdea(address _author, uint value) public returns(bool){
             transfer(address(_author), value);
             return true;
        }
    }

Best Answer

Use the ERC-1155 standard for this: https://eips.ethereum.org/EIPS/eip-1155

Core Idea: Aims to be a combination of the ERC721 and ERC20 standards. Enables batch transfers (to multiple parties!) to make stuff cheaper. Combination explanation here, but general idea is to have another mapping:

πŸ’© ERC721: mapping id β‡’ owner

🧠 ERC1155: mapping owner β‡’ id β‡’ balance

Ex: me β‡’ 0 β‡’ 200

OpenZeppelin has a ERC1155 contract that might be useful. In order to be compatible with ERC20 and ERC721 schemes, you might need to add some functions, but the basic structure is here

Related Topic