pragma solidity ^0.8.0;

interface IERC20 {
function mint(address account, uint amount) external;
function transfer(address to, uint256 amount) external returns (bool);
function transferFrom(address from, address to, uint256 amount) external returns (bool);
function balanceOf(address account) external view returns (uint256);
}

interface IERC721 {
function mint(address account, uint amount) external;
function transferFrom(address from, address to, uint256 tokenId) external;
}


contract Exchange is Ownable {
// This contract should be provided minting access on both the token and NFT contracts
// Token decimal assumed to be 18. Change if needed
// Check if the mint function signature matches with the one in your nft. Modify if needed

uint public tokenToNftExchangeRate = 1e6; // amount of tokens needed to get one nft
uint public nftToTokenExchangeRate = 1e6; // amount of tokens to receive in exchange of one nft
uint public swapFee = 5; // swap fee percentage

IERC20 token; // token to be swapped
IERC721 nft; // nft to be swapped

uint256[] private _tokenIds; // NFTs currently owned by this contract
address public devWallet;

constructor(address _token, address _nft,address dev) {
token = IERC20(_token);
nft = IERC721(_nft);
devWallet=dev;
}

// =============================================================
// SWAP
// =============================================================

/**
* @notice Swap NFT and receive tokens excluding swap fee
* @param nftId id of nft to swap
*
* Caller must have approved this contract to transfer `nftId` beforehand
* Transfers `nftToTokenExchangeRate` amount of tokens excluding fee to caller
* If contract has sufficient tokens available, then those are used for the swap
* Otherwise new tokens will be minted and sent to caller
*/
function swapNftForToken(uint nftId) external {
nft.transferFrom(msg.sender, address(this), nftId);
_tokenIds.push(nftId);

uint fee = ( nftToTokenExchangeRate * swapFee ) / 100;

if(token.balanceOf(address(this)) >= nftToTokenExchangeRate) {
token.transfer(msg.sender, nftToTokenExchangeRate - fee);
token.transfer(devWallet, fee);
} else {
token.mint(msg.sender, nftToTokenExchangeRate - fee);
token.mint(owner(), fee);
}

}

/**
* @notice Swap tokens and receive NFT
* @dev Transfers `tokenToNftExchangeRate` amount of tokens from caller to contract
*
* Caller must have approved this contract to transfer `tokenToNftExchangeRate` amount of tokens beforehand
* If contract has nfts available then those would be used for swap
* Otherwise new nft will be minted
*/
function swapTokenForNft() external {
uint fee = ( tokenToNftExchangeRate * swapFee ) / 100;

token.transferFrom(msg.sender, address(this), tokenToNftExchangeRate);
token.transfer(devWallet, fee);

uint len = _tokenIds.length;

if(len > 0) {
uint id = _tokenIds[len - 1];
_tokenIds.pop();
nft.transferFrom(address(this), msg.sender, id);
} else {
nft.mint(msg.sender, 1);
}

}

/// @notice Returns the nfts currently owned by this contract
function nftsOwned() external view returns (uint[] memory) {
return _tokenIds;
}


// =============================================================
// CONTRACT OWNER ONLY
// =============================================================


/**
* @notice Change current exchange rates
* @param _tokenToNftExchangeRate new token to nft exchange rate
* @param _nftToTokenExchangeRate new nft to token exchange rate
*/
function setExchangeRates(uint _tokenToNftExchangeRate, uint _nftToTokenExchangeRate) external onlyOwner {
tokenToNftExchangeRate = _tokenToNftExchangeRate;
nftToTokenExchangeRate = _nftToTokenExchangeRate;
}

/**
* @notice Change swap fee percentage
* @param _swapFee new swap fee
*/
function setSwapFee(uint _swapFee) external onlyOwner {
swapFee = _swapFee;
}

/* --- Functions to withdraw tokens and nfts stored in this contract if needed --- */

function withdrawTokens() external onlyOwner {
token.transfer(msg.sender, token.balanceOf(address(this)));
}

function withdrawNFT(uint id) external onlyOwner {
nft.transferFrom(address(this), msg.sender, id);
}

function WithdrawEth() external onlyOwner
{
payable(msg.sender).transfer(address(this).balance);
}

function changeDev (address newDev) external onlyOwner{
devWallet = newDev;
}


function onERC721Received(
address operator,
address from,
uint256 tokenId,
bytes calldata data
) external returns (bytes4) {
_tokenIds.push(tokenId);
operator;
from;
data;
return this.onERC721Received.selector;
}
}
[{"inputs":[{"internalType":"address","name":"_token","type":"address"},{"internalType":"address","name":"_nft","type":"address"},{"internalType":"address","name":"dev","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"inputs":[],"name":"WithdrawEth","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newDev","type":"address"}],"name":"changeDev","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"devWallet","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"nftToTokenExchangeRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"nftsOwned","outputs":[{"internalType":"uint256[]","name":"","type":"uint256[]"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"operator","type":"address"},{"internalType":"address","name":"from","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"onERC721Received","outputs":[{"internalType":"bytes4","name":"","type":"bytes4"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_tokenToNftExchangeRate","type":"uint256"},{"internalType":"uint256","name":"_nftToTokenExchangeRate","type":"uint256"}],"name":"setExchangeRates","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_swapFee","type":"uint256"}],"name":"setSwapFee","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"swapFee","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"nftId","type":"uint256"}],"name":"swapNftForToken","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"swapTokenForNft","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"tokenToNftExchangeRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"id","type":"uint256"}],"name":"withdrawNFT","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"withdrawTokens","outputs":[],"stateMutability":"nonpayable","type":"function"}]