|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import "@openzeppelin/contracts/access/Ownable.sol"; |
| 5 | +import "@openzeppelin/contracts/proxy/utils/Initializable.sol"; |
| 6 | +import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 7 | +import "./PropertyNFT.sol"; |
| 8 | + |
| 9 | +contract AssetListing is Initializable, Ownable { |
| 10 | + struct Asset { |
| 11 | + uint256 assetId; |
| 12 | + address owner; |
| 13 | + uint256 valuation; |
| 14 | + bool isListed; |
| 15 | + bool isSingleBuyer; |
| 16 | + uint256 collateralAmount; |
| 17 | + uint256 listingFee; |
| 18 | + uint256 tokenId; |
| 19 | + } |
| 20 | + |
| 21 | + mapping(uint256 => Asset) public assets; |
| 22 | + uint256 public nextAssetId; |
| 23 | + uint256 public collateralPercentage; // Example: 5% collateral |
| 24 | + uint256 public listingFeePercentage; // Example: 0.5% listing fee |
| 25 | + PropertyNFT public propertyNFT; |
| 26 | + IERC20 public usdc; |
| 27 | + |
| 28 | + event AssetRequested(uint256 indexed assetId, address indexed owner, uint256 valuation); |
| 29 | + event AssetListed(uint256 indexed assetId, address indexed owner, uint256 valuation); |
| 30 | + event CollateralReturned(uint256 indexed assetId, address indexed owner); |
| 31 | + event CollateralForfeited(uint256 indexed assetId, address indexed owner); |
| 32 | + |
| 33 | + function initialize( |
| 34 | + uint256 _collateralPercentage, |
| 35 | + uint256 _listingFeePercentage, |
| 36 | + address _usdc, |
| 37 | + address _propertyNFTAddress |
| 38 | + ) public initializer { |
| 39 | + collateralPercentage = _collateralPercentage; |
| 40 | + listingFeePercentage = _listingFeePercentage; |
| 41 | + usdc = IERC20(_usdc); |
| 42 | + propertyNFT = PropertyNFT(_propertyNFTAddress); |
| 43 | + nextAssetId = 1; |
| 44 | + } |
| 45 | + |
| 46 | + // Request to list an asset on the platform |
| 47 | + function requestAssetListing(uint256 valuation, bool isSingleBuyer, string memory tokenURI) external { |
| 48 | + uint256 collateral = (valuation * collateralPercentage) / 100; |
| 49 | + uint256 listingFee = (valuation * listingFeePercentage) / 100; |
| 50 | + |
| 51 | + require(usdc.transferFrom(msg.sender, address(this), collateral + listingFee), "Transfer failed"); |
| 52 | + |
| 53 | + uint256 assetId = nextAssetId++; |
| 54 | + uint256 tokenId = propertyNFT.mint(address(this), tokenURI); |
| 55 | + |
| 56 | + assets[assetId] = Asset({ |
| 57 | + assetId: assetId, |
| 58 | + owner: msg.sender, |
| 59 | + valuation: valuation, |
| 60 | + isListed: false, |
| 61 | + isSingleBuyer: isSingleBuyer, |
| 62 | + collateralAmount: collateral, |
| 63 | + listingFee: listingFee, |
| 64 | + tokenId: tokenId |
| 65 | + }); |
| 66 | + |
| 67 | + emit AssetRequested(assetId, msg.sender, valuation); |
| 68 | + } |
| 69 | + |
| 70 | + // Approve and list the asset for sale |
| 71 | + function listAsset(uint256 assetId) external onlyOwner { |
| 72 | + Asset storage asset = assets[assetId]; |
| 73 | + require(!asset.isListed, "Asset already listed"); |
| 74 | + |
| 75 | + if (asset.isSingleBuyer) { |
| 76 | + // Logic for single buyer scenario |
| 77 | + } else { |
| 78 | + // Logic for fractional ownership scenario |
| 79 | + // Typically, this would involve creating and selling shares or fractions |
| 80 | + } |
| 81 | + |
| 82 | + asset.isListed = true; |
| 83 | + |
| 84 | + emit AssetListed(assetId, asset.owner, asset.valuation); |
| 85 | + } |
| 86 | + |
| 87 | + // Return collateral after successful asset sale or full subscription |
| 88 | + function returnCollateral(uint256 assetId) external onlyOwner { |
| 89 | + Asset storage asset = assets[assetId]; |
| 90 | + require(asset.isListed, "Asset not listed"); |
| 91 | + require(asset.isSingleBuyer, "Asset not fully subscribed or sold"); |
| 92 | + |
| 93 | + uint256 collateralAmount = asset.collateralAmount; |
| 94 | + asset.collateralAmount = 0; |
| 95 | + |
| 96 | + usdc.transfer(asset.owner, collateralAmount); |
| 97 | + |
| 98 | + emit CollateralReturned(assetId, asset.owner); |
| 99 | + } |
| 100 | + |
| 101 | + // Forfeit collateral under certain conditions (e.g., listing violation or fraudulent behavior) |
| 102 | + function forfeitCollateral(uint256 assetId) external onlyOwner { |
| 103 | + Asset storage asset = assets[assetId]; |
| 104 | + require(asset.collateralAmount > 0, "No collateral to forfeit"); |
| 105 | + |
| 106 | + uint256 collateralAmount = asset.collateralAmount; |
| 107 | + asset.collateralAmount = 0; |
| 108 | + |
| 109 | + usdc.transfer(owner(), collateralAmount); |
| 110 | + |
| 111 | + emit CollateralForfeited(assetId, asset.owner); |
| 112 | + } |
| 113 | +} |
0 commit comments