1

we have 2 contracts which is deployed on Bsc network. the first contract is NFT Contract and the second one is Gataway contract were we accept payment and currency swaps Please find below code Nft Contract

// SPDX-License-Identifier: Unli
pragma solidity ^0.8.16;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";


contract NftDemo is ERC721, ERC721Burnable, Ownable {

    uint256 public tokenIdCounter = 0;
    address payable public  platfromfeeAccount; // the account that receives fees
    uint public  platfromFee = 10; // the fee percentage on sales 
   
    
    mapping(uint256 => uint256) public mintPrice;
    mapping(address => bool) public whitelistContract;
    mapping(uint256 => bool) public retiredTokens;


    struct CarbonProject {
        uint project_id;
        uint tonnes;
        uint no_of_nft;
        uint price;
    }
    event publicMinting(
        uint order_id,
        uint indexed startTokenId,
        uint EndTokenId,
        uint price,
        address indexed cpo,
        address indexed buyer,
        uint platformfee,
        uint platformfeeAmount,
        uint cpoAmount,
        CarbonProject projectDetails
       
    );

    event privateMinting(
        string order_id,
        uint startTokenId,
        uint endTokenId,
        address indexed cpo,
        address indexed buyer,
        CarbonProject projectDetails
    );

    event mintAfterDelayEvent(
        string order_id,
        uint startTokenId,
        uint endTokenId,
        address indexed cpo,
        address indexed buyer,
        CarbonProject projectDetails
    );

    event ContractMinting(
        string order_id,
        uint startTokenId,
        uint endTokenId,
        address indexed cpo,
        address indexed buyer,
        CarbonProject projectDetails,
        address contractAddress
    );

    event withoutWalletMintEvent(
        string order_id,
        uint project_id,
        uint startTokenId,
        uint endTokenId,
        address indexed cpo,
        uint256 indexed userId,
        uint tonnes,
        address indexed contractAddress
    );

    constructor() ERC721("BC Carbon Credit", "BCCC") {}


    function addToWhitelist(address _address) external onlyOwner {
        whitelistContract[_address] = true;
    }

    function removeFromWhitelist(address _address) external onlyOwner {
        whitelistContract[_address] = false;
    }

    function isWhitelisted(address _address) public view returns (bool) {
        return whitelistContract[_address];
    }


        // editable platform Fee and platformFee Account 

    function setMintPrice(uint256 project_id,uint256 price) external onlyOwner{
        mintPrice[project_id] = price;
    }

    function setPlatformFeeAccount(address _platfromfeeAccount) external onlyOwner{
        platfromfeeAccount = payable(_platfromfeeAccount);

    }

    function setPlatformFeePercentage(uint _platfromFee) external onlyOwner{
        require(_platfromFee < 100, "Platform fee cannot be greater than 100%.");
        platfromFee = _platfromFee;

    }

    function _mint(address to, uint256 tokenId) internal override{
        require(isWhitelisted(msg.sender), "Only whitelisted addresses can transfer tokens");
        super._mint(to, tokenId);
    }

    function _safeMint(address to, uint256 tokenId) internal override {
        require(isWhitelisted(msg.sender), "Only whitelisted addresses can transfer tokens");
        super._safeMint(to, tokenId);
    }

    function safeTransferFrom(address from,address to,uint256 tokenId,bytes memory data) public override {
        require(isWhitelisted(msg.sender), "Only whitelisted addresses can transfer tokens");
        require(!retiredTokens[tokenId], "retired token can not transfer");
        super.safeTransferFrom(from, to, tokenId, data);
    }

    function safeTransferFrom(
    address from,
    address to,
    uint256 tokenId
    ) public override {
    require(isWhitelisted(msg.sender), "Only whitelisted addresses can transfer tokens");
    require(!retiredTokens[tokenId], "retired token can not transfer");

    super.safeTransferFrom(from, to, tokenId);
    }
    
    
    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) public override {
        require(isWhitelisted(msg.sender), "Only whitelisted addresses can transfer tokens");
        require(!retiredTokens[tokenId], "retired token can not transfer");
        super.transferFrom(from, to, tokenId);
    }

    function getMintPrice(uint256 project_id) view public returns(uint){
        return((mintPrice[project_id]*(100 + platfromFee ))/100);
    }


    function _baseURI() internal pure override returns (string memory) {
        return "https://api.beachcollective.io/nft/";
    }

    function mint(uint256 order_id,uint256 project_id,uint tonnes,uint no_of_nft,address to,address _cpo) public payable {
        uint price = getMintPrice(project_id);
        require(price != 0, "Carbon price should be greater then 0");
        uint total_price = no_of_nft * tonnes * price;
        require(msg.value == total_price, "Incorrect minting price");
        require(tonnes != 0, "Carbon Tonnes should be greater then 0");
        require(no_of_nft != 0, "Carbon Tonnes should be greater then 0");
        uint startTokenId = tokenIdCounter + 1;

        for (uint256 i = 0; i < no_of_nft; i++) {
            tokenIdCounter++;
            _safeMint(to, tokenIdCounter);
        }
        CarbonProject memory newProject = CarbonProject(project_id,tonnes,no_of_nft,price);
        if (platfromFee > 0){
            uint platfromFeeAmount = msg.value * platfromFee/100 ;
            uint cpoAmount = msg.value - platfromFeeAmount;
            payable(_cpo).transfer(cpoAmount);
            platfromfeeAccount.transfer(platfromFeeAmount);
            emit publicMinting(order_id,startTokenId,tokenIdCounter,price,_cpo,to,platfromFee,platfromFeeAmount,cpoAmount,newProject);
        }else{
            payable(_cpo).transfer(msg.value);
            emit publicMinting(order_id,startTokenId,tokenIdCounter,price,_cpo,to,platfromFee,0,msg.value,newProject);
        }
    }

    

    function privateMint(string memory order_id,address to,address _cpo,uint project_id,uint tonnes,uint no_of_nft,uint price) public onlyOwner {
    uint startTokenId = tokenIdCounter + 1;

    for (uint256 i = 0; i < no_of_nft; i++) {
        tokenIdCounter++;
        _safeMint(to, tokenIdCounter);
    }
    CarbonProject memory newProject = CarbonProject(project_id,tonnes,no_of_nft,price);

    emit privateMinting(
        order_id,
        startTokenId,
        tokenIdCounter,
        _cpo,
        to,newProject);
    }

   function ContractMint(string memory order_id,address to,address _cpo,uint project_id,uint tonnes,uint no_of_nft,uint price) public returns(uint) {
    require(isWhitelisted(msg.sender), "Only whitelisted addresses can mint.");
    uint startTokenId = tokenIdCounter + 1;
    for (uint256 i = 0; i < no_of_nft; i++) {
        tokenIdCounter++;
        _safeMint(to, tokenIdCounter);
    }
    CarbonProject memory newProject = CarbonProject(project_id,tonnes,no_of_nft,price);

    emit ContractMinting(
        order_id,
        startTokenId,
        tokenIdCounter,
        _cpo,
        to,newProject,
        address(msg.sender));

    return tokenIdCounter;

    }

    function withoutWalletMint(string memory order_id,uint256 user_id,address to,address _cpo,uint256 project_id,uint256 tonnes,uint256 no_of_nft) public returns(uint256[] memory) {
    require(isWhitelisted(msg.sender), "Only whitelisted addresses can mint.");
    uint startTokenId = tokenIdCounter + 1;
    uint256[] memory tokenIdList = new uint256[](no_of_nft);
    for (uint256 i = 0; i < no_of_nft; i++) {
        tokenIdCounter++;
        _safeMint(to, tokenIdCounter);
        tokenIdList[i] = tokenIdCounter;
    }

    emit withoutWalletMintEvent(
        order_id,
        project_id,
        startTokenId,
        tokenIdCounter,
        _cpo,
        user_id,
        tonnes,
        address(msg.sender));

    return tokenIdList;

    }




    function retire(uint256 tokenId) public {
        require(isWhitelisted(msg.sender), "Only whitelisted addresses can mint.");
        require(_exists(tokenId), "Token ID does not exist");
        // _burn(tokenId);
        retiredTokens[tokenId] = true;
    }

    function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
        require(_exists(tokenId), "Token ID does not exist");
        return string(abi.encodePacked(_baseURI(), Strings.toString(tokenId)));
    }



    function mintAfterDelay(string memory order_id,address to,address _cpo,uint project_id,uint tonnes,uint no_of_nft,uint price) public onlyOwner {
    uint startTokenId = tokenIdCounter + 1;
    for (uint256 i = 0; i < no_of_nft; i++) {
        tokenIdCounter++;

        _safeMint(to, tokenIdCounter);
    }
    CarbonProject memory newProject = CarbonProject(project_id,tonnes,no_of_nft,price);

    emit mintAfterDelayEvent(
        order_id,
        startTokenId,
        tokenIdCounter,
        _cpo,
        to,newProject);
    }

} 

** getaway contract **

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.11;
import "./Nft.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";

interface IBEP20 {
    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    function allowance(address owner, address spender) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
}


interface IPancakeRouter01 {
    function WETH() external pure returns (address);

    function swapExactTokensForTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts);
   
    function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
        external
        payable
        returns (uint[] memory amounts);
    
}


library SafeMath {
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a, "SafeMath: addition overflow");
        return c;
    }

    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        return sub(a, b, "SafeMath: subtraction overflow");
    }

    function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b <= a, errorMessage);
        uint256 c = a - b;
        return c;
    }

    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        if (a == 0) {
            return 0;
        }
        uint256 c = a * b;
        require(c / a == b, "SafeMath: multiplication overflow");
        return c;
    }

    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        return div(a, b, "SafeMath: division by zero");
    }

    function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
        require(b > 0, errorMessage);
        uint256 c = a / b;
        return c;
    }
        
}

contract GetawayContract is Context,Ownable,ReentrancyGuard{
   
    using SafeMath for uint256;

    // pancakeswap
     IPancakeRouter01 private pancakeV2Router;
    address public routerAddress = 0x10ED43C718714eb63d5aA57B78B54704E256024E;
    address public targetToken; 
    

    uint public platformFee = 15; 
    uint public carbonPlatformFee = 10;
    uint public  resale_platfromFee = 3; // the fee percentage on sales 
    uint256 public resale_royaltyFee = 5; // Percentage of sale price to be taken as royalty by the original creator of the NFT
    uint public itemCount;


    uint public totalPlatformFee; 
    bool public platformFeeFlag = true;
    
    address public platformFeeWallet;
    address public cccWallet;

    address public carbonAddress;


    mapping (address => bool) public Tokens;

    BCCarbonCredit CarbonCredit;

    
    mapping(address => bool) private _isBlacklisted;

    mapping (address => bool) public whitelistedNftContracts; 



    bool public _directTransferFlag = true ;
    bool public _orderPaymentFlag = true;


    // transaction details 
    struct OrderDetails { 
            uint product_id;
            address from_address;
            address to_address;
            uint amount;
            uint plantform_fee;
    }

    struct OrderCarbonDetails { 
            uint project_id;
            address cpo_address;
            address buyer_address;
            uint total_amount;
            uint platform_fee;
            uint platfrom_fee_amount;
            uint tonnes;
            uint no_of_nft;
            uint price;
            bool new_item;
            uint item_id;
            bool mintDelayFlag;
    }

    mapping(string => OrderCarbonDetails[]) private mintLaterOrderDetails;

    // mint without wallet 

    mapping(uint256 => address)  private userWallet ;

     // Function to set a user's wallet address
    function setUserWallet(uint256 userId, address walletAddress) public onlyOwner{
        userWallet[userId] = walletAddress;
    }

    // Function to get a user's wallet address
    function getUserWallet(uint256 userId) public view returns (address) {
        return userWallet[userId];
    }

    mapping(uint256 => uint256) private nftUser;

    // Function to associate a user with an NFT token ID
    function setUserOfNFT(uint256 tokenId, uint256 userId) public onlyOwner {
        nftUser[tokenId] = userId;
    }

    // Function to retrieve the user associated with an NFT token ID
    function getUserOfNFT(uint256 tokenId) public view returns (uint256) {
        return nftUser[tokenId];
    }


    // #########################################



    struct Item {
        uint itemId;
        IERC721 nft;
        uint tokenId;
        uint price;
        address payable seller;
        address payable owner;
        address payable cpo;
        bool sold;
    }

    // itemId -> Item
    mapping(uint => Item) public items;

    event Offered(uint itemId,address indexed nft,uint tokenId,uint price,address indexed seller);

    event RevokeOffered(uint itemId,address indexed nft,uint tokenId,uint price,address indexed seller);
 
    event Bought(uint itemId,address indexed nft,uint tokenId,uint price,address indexed seller,address indexed buyer);

    event OrderDetailsToWalletEvent(address indexed token,address indexed from,address indexed to,string order_id,uint256 value_after_convertion,uint256 platformfee_value,uint256 value_after_platformfee,OrderDetails order_details);
    event OrderDetailsToBagEvent(address indexed token,address indexed from,address indexed to,string order_id,uint256 value_after_convertion,uint256 platformfee_value,uint256 value_after_platformfee,OrderDetails order_details);

    event OrderCarbonDetailsToWalletEvent(address indexed token,address indexed from,address indexed to,string order_id,uint256 value_after_convertion,uint256 royalty_value,uint256 platformfee_value,uint256 value_after_platformfee,OrderCarbonDetails order_details);
    event OrderCarbonDetailsToBagEvent(address indexed token,address indexed from,address indexed to,string order_id,uint256 value_after_convertion,uint256 royalty_value,uint256 platformfee_value,uint256 value_after_platformfee,OrderCarbonDetails order_details);
    event RetairCarbonNftEvent(address indexed nft,uint tokenId,address indexed owner);



    // ################
    address public treasury = ""; // replace with user address


    
    
    
    
    event TransferAllTokenToTreasuryEvent(address indexed token,address indexed treasury, uint256 value,uint256 timestamp);

    event DirectTransferEvent(address indexed token,address indexed from, address indexed to,uint256 actual_value ,uint256 value_after_deduction,uint256 value_after_platformfee,uint256 platformfee_value);
    event DirectTransferEvent2(address indexed token,address indexed from, address indexed to,uint256 actual_value ,uint256 value_after_platformfee,uint256 platformfee_value);

    event MintNowEvent(string order_id,uint project_id);

    event addMintLaterEvent(string order_id,OrderCarbonDetails order);
    event MintNftEvent(uint256[] nftId);


  
    constructor(address token,address _platformFeeWallet, address _cccWallet,address _carbonAddress){
        targetToken = token;
        platformFeeWallet = _platformFeeWallet;
        cccWallet = _cccWallet;
        carbonAddress = _carbonAddress;
        CarbonCredit = BCCarbonCredit(_carbonAddress);
        Tokens[targetToken] = true;
        pancakeV2Router = IPancakeRouter01(routerAddress);

    }


      // whitlist nft contract address 
    function whitelistNftContract(address _contractAddress,bool _flag) public onlyOwner {
        whitelistedNftContracts[_contractAddress] = _flag;
    }

    function setRoyaltyFee(uint256 _fee) public onlyOwner {
        require(_fee < 100, "Royalty fee cannot be greater than 100%.");
        resale_royaltyFee = _fee;
    }

     function setResalePlatformFeePercentage(uint _platfromFee) external onlyOwner{
        require(_platfromFee < 100, "platform fee cannot be greater than 100%.");
        resale_platfromFee = _platfromFee;

    }


    function setPlatformFeeWalletAddress(address _platformFeeWallet) external onlyOwner {
        platformFeeWallet = _platformFeeWallet;
    }

    function setCccWalletAddress(address _cccWallet) external onlyOwner {
        cccWallet = _cccWallet;
    }


    function setCarbonAddress(address _carbonAddress) external onlyOwner {
        carbonAddress = _carbonAddress;
        CarbonCredit = BCCarbonCredit(_carbonAddress);

    }



    function setTargetAddress(address target_adr) external onlyOwner {
        targetToken = target_adr;
        Tokens[targetToken] = true;

    }

    

    modifier AllowedTokenCheck(IBEP20 _token){
        require(Tokens[address(_token)],'This Token is not allowed to deposit and withdraw.');
        _;
    }


    function setPlatformFee(uint _platformFee) external onlyOwner {
        platformFee = _platformFee;
    }



    function setPlatformFeeFlag(bool _platformFeeFlag) external onlyOwner {
        platformFeeFlag = _platformFeeFlag;
    }
    
    function setDirectTransferFlag(bool _bool) external onlyOwner {
        _directTransferFlag = _bool;
    }


    function setOrderPaymentFlagFlag(bool _bool) external onlyOwner {
        _orderPaymentFlag = _bool;
    }



    function setAddressIsBlackListed(address _address, bool _bool) external onlyOwner {
        _isBlacklisted[_address] = _bool;
    }

    function viewIsBlackListed(address _address) public view returns(bool) {
        return _isBlacklisted[_address];
    }

    function allowedTokens(address _token,bool _flag) public onlyOwner{
        Tokens[_token] = _flag;
    }

    
   
    function swapTokenfortargetToken(IBEP20 from_token,uint amountIn) private {
     
        address[] memory path;


        if (address(from_token) == pancakeV2Router.WETH()) {
            path = new address[](2);
            path[0] = address(from_token);
            path[1] = address(targetToken);
        
        pancakeV2Router.swapExactETHForTokens{value: amountIn}(0,path,address(this),block.timestamp);
        } else {
            path = new address[](3); 
            path[0] = address(from_token);
            path[1] = pancakeV2Router.WETH();
            path[2] = address(targetToken);

        from_token.transferFrom(msg.sender,address(this), amountIn);
        from_token.approve(address(pancakeV2Router), amountIn);
        pancakeV2Router.swapExactTokensForTokens(amountIn,1,path,address(this),block.timestamp);
        }
   
    }

    

    receive() external payable {}


   

   
    function RetireCarbonNft(uint tokenId) public{
    CarbonCredit.retire(tokenId);
    emit RetairCarbonNftEvent(carbonAddress,tokenId,msg.sender);


    }

    function withoutWalletOrder(uint256 user_id,string memory order_id,uint256 project_id,address to,address cpo_address , uint256 tonnes,uint256 no_of_nft)  external onlyOwner{

        uint256[] memory nftlist = CarbonCredit.withoutWalletMint(order_id,user_id,to,cpo_address, project_id, tonnes, no_of_nft);
        
        userWallet[user_id];
        for (uint i = 0; i < nftlist.length; i++) {
             nftUser[nftlist[i]] = user_id;            
        }
    }

    
    


 

}

"withoutWalletOrder" function from getaway contract which is minting nft by calling " withoutWalletMint" from the Nft contract. it works properly for wallet address in "to" argument of function. but when we are to mint nft for contract address it gives an error. we want to mint Nft for the contract address which old Nfts. please suggest.

1 Answer 1

1

Make sure your contract that is supposed to receive the ERC721 receiver interface. This might be an issue. Otherwise please share the error screenshot.

ERC721Receiver interface - https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC721/IERC721Receiver.sol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.