|
// SPDX-License-Identifier: MIT |
|
pragma solidity ^0.8.9; |
|
|
|
import "@openzeppelin/contracts@4.9.3/token/ERC721/ERC721.sol"; |
|
import "@openzeppelin/contracts@4.9.3/token/ERC721/extensions/ERC721URIStorage.sol"; |
|
import "@openzeppelin/contracts@4.9.3/token/ERC721/extensions/ERC721Burnable.sol"; |
|
import "@openzeppelin/contracts@4.9.3/access/Ownable.sol"; |
|
import "@openzeppelin/contracts@4.9.3/utils/Counters.sol"; |
|
import "@openzeppelin/contracts@4.9.3/token/ERC20/IERC20.sol"; |
|
import "villa.sol"; |
|
|
|
contract SHARES { |
|
address private _shares_owner; |
|
uint256 public villaCount; |
|
address public USDT_ADDRESS; |
|
mapping(uint256 => address) public villa; |
|
constructor(address _usdt) { |
|
USDT_ADDRESS = _usdt; |
|
_shares_owner = msg.sender; |
|
} |
|
modifier onlyOwner() { |
|
require(msg.sender == _shares_owner, "owner only"); |
|
_; |
|
} |
|
//create new villa with nft |
|
function createVilla( |
|
string memory uid, |
|
string memory name, |
|
string memory uri, |
|
string memory symbol, |
|
address[] memory admins, |
|
uint256 buyPrice, |
|
uint256 sellPrice, |
|
uint256 max_nft_count) public onlyOwner returns (address) { |
|
uint256 villaId = villaCount++; |
|
//create new villa contract |
|
Villa villa_contract = new Villa(admins, name, symbol); |
|
villa[villaId] = address(villa_contract); |
|
//intialize villa data |
|
villa_contract.initializeVilla( |
|
uid, |
|
name, |
|
uri, |
|
buyPrice, |
|
sellPrice, |
|
max_nft_count |
|
); |
|
return address(villa_contract); |
|
} |
|
// function updateVillaBuyPrice(uint256 villaid, uint256 price) public onlyOwner { |
|
// require(villaid < villaCount, "Villa ID does not exist"); |
|
// require(price > 0, "Invalid Price"); |
|
// villa[villaid].buyPrice = price; |
|
// } |
|
// function updateVillaSellPrice(uint256 villaid, uint256 price) public onlyOwner { |
|
// require(villaid < villaCount, "Villa ID does not exist"); |
|
// require(price > 0, "Invalid Price"); |
|
// villa[villaid].sellPrice = price; |
|
// } |
|
// function buyShare(uint256 villaid, uint256 share_amount) public returns (uint256[] memory){ |
|
// require(villaid < villaCount, "Villa ID does not exist"); |
|
// require(villa[villaid].minted_nft + share_amount <= villa[villaid].max_nft_count, "Exceeds maximum NFT count for villa"); |
|
// IERC20 usdt_token = IERC20(USDT_ADDRESS); |
|
// uint256 buyPrice = villa[villaid].buyPrice; |
|
// uint256 totalCost = buyPrice * share_amount; |
|
// require(usdt_token.balanceOf(msg.sender) >= totalCost, "Insufficient USDT balance"); |
|
// require(usdt_token.allowance(msg.sender, address(this)) >= totalCost, "Allowance not sufficient"); |
|
// usdt_token.transferFrom(msg.sender, address(this), totalCost); |
|
// uint256[] memory mintedTokenIds = new uint256[](share_amount); |
|
// //mint nft |
|
// for (uint256 i = 0; i < share_amount; i++) { |
|
// villa[villaid].minted_nft++; |
|
// uint256 tokenId = _tokenIdCounter.current(); |
|
// _tokenIdCounter.increment(); |
|
// _safeMint(msg.sender, tokenId); |
|
// _setTokenURI(tokenId, villa[villaid].uri); |
|
// mintedTokenIds[i] = tokenId; |
|
// } |
|
// return mintedTokenIds; |
|
// } |
|
|
|
// function sellShare(uint256 villaid, uint256[] memory tokenids) public returns (uint256[] memory) { |
|
// require(villaid < villaCount, "Villa ID does not exist"); |
|
// require(tokenids.length > 0, "Invalid Token Ids"); |
|
// IERC20 usdt_token = IERC20(USDT_ADDRESS); |
|
// uint256 sellPrice = villa[villaid].sellPrice * tokenids.length; |
|
// usdt_token.approve(msg.sender, sellPrice); |
|
// //check all token if approce |
|
// for (uint256 i = 0; i < tokenids.length; i++) { |
|
// require(getApproved(tokenids[i]) == address(this), "Token Id not approve"); |
|
// } |
|
// //send the usdt |
|
// uint256[] memory sellTokenIds = new uint256[](tokenids.length); |
|
// usdt_token.transfer(msg.sender, sellPrice); |
|
// for (uint256 i = 0; i < tokenids.length; i++) { |
|
// //transfer nft |
|
// //todo save sell share |
|
// villa[villaid].minted_nft--; |
|
// transferFrom(msg.sender, address(this), tokenids[i]); |
|
// } |
|
// return sellTokenIds; |
|
// } |
|
function usdtBalance() public view onlyOwner returns (uint256) { |
|
IERC20 usdt_token = IERC20(USDT_ADDRESS); |
|
return usdt_token.balanceOf(address(this)); |
|
} |
|
function withdraw() public onlyOwner { |
|
IERC20 usdt_token = IERC20(USDT_ADDRESS); |
|
uint256 balance = usdt_token.balanceOf(address(this)); |
|
require(usdt_token.balanceOf(address(this)) > 0, "not enough usdt"); |
|
usdt_token.transfer(msg.sender, balance); |
|
} |
|
|
|
|
|
} |