doccomments_everywhere.sol 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. pragma solidity ^0.8.10;
  2. /// @author Max Campbell (https://github.com/maxall41), RafaCypherpunk (https://github.com/RafaCypherpunk)
  3. contract Property {
  4. using Counters for Counters.Counter;
  5. Counters.Counter private _tokenIds;
  6. mapping(uint256 => uint256) public pricePerShare_;
  7. mapping(address => uint256) public valueLocked_;
  8. mapping(uint256 => address) public tokenDeployers_;
  9. mapping(uint256 => uint256) public sellingTokens_;
  10. mapping(uint256 => uint256) public buyingTokens_;
  11. event MintProperty(uint256 id);
  12. constructor() ERC1155("https://api.vestana.io/api/product/get/{id}.json") {}
  13. receive() external payable {
  14. valueLocked_[msg.sender] = valueLocked_[msg.sender] + msg.value;
  15. }
  16. function mintProperty(
  17. uint256 _shares,
  18. uint256 _pricePerShare,
  19. uint256 _sharesForSale
  20. ) public {
  21. uint256 newPropertyId = _tokenIds.current();
  22. _tokenIds.increment();
  23. _mint(msg.sender, newPropertyId, _shares, "");
  24. pricePerShare_[newPropertyId] = _pricePerShare;
  25. tokenDeployers_[newPropertyId] = msg.sender;
  26. sellingTokens_[newPropertyId] = _sharesForSale;
  27. emit MintProperty(newPropertyId);
  28. }
  29. function getTokenOwner(uint256 _id) public view returns (address payable) {
  30. return payable(tokenDeployers_[_id]);
  31. }
  32. function getPricePerShare(uint256 _id) public view returns (uint256) {
  33. return pricePerShare_[_id];
  34. }
  35. /// @dev Used to purchase shares
  36. function purchaseShares(uint256 _shares, uint256 _id) public payable {
  37. /// @dev Get the owner of this token
  38. address payable owner = getTokenOwner(_id);
  39. /// @dev Get the price per share of this token
  40. uint256 _pricePerShare = getPricePerShare(_id);
  41. /// @dev Mae sure the contract can spend the owner's tokens
  42. require(
  43. this.isApprovedForAll(owner, address(this)) == true,
  44. "Owner has incorrect permissions"
  45. );
  46. /// @dev Make sure the purchaser has enough shares
  47. require(msg.value >= _pricePerShare * _shares, "Not enough");
  48. /// @dev Make sure there are shares available for purchase
  49. require(sellingTokens_[_id] >= _shares, "No more shares available");
  50. /// @dev Charges purchaser for shares
  51. owner.transfer(_pricePerShare * _shares);
  52. /// @dev Transfers purchased shares to purchaser
  53. /// @note This will fail if the owner has no more shares they want to sell
  54. this.safeTransferFrom(tokenDeployers_[_id], msg.sender, _id, _shares, "");
  55. }
  56. function setSellingShares(uint256 _newSharesToSell, uint256 _id) public {
  57. require(msg.sender == tokenDeployers_[_id], "You are not the owner");
  58. sellingTokens_[_id] = _newSharesToSell;
  59. }
  60. function setBuyingShares(uint256 _newSharesToSell, uint256 _id) public {
  61. require(msg.sender == tokenDeployers_[_id], "You are not the owner");
  62. buyingTokens_[_id] = _newSharesToSell;
  63. }
  64. function sellShares(uint256 shares_, uint256 _id) public {
  65. /// @dev Get the price per share
  66. uint256 _pricePerShare = getPricePerShare(_id);
  67. ///@dev Get the owner
  68. address _owner = getTokenOwner(_id);
  69. /// @dev Make sure the owner wants to sell these shares
  70. require(buyingTokens_[_id] >= shares_, "No buyback capacity");
  71. /// @dev Make sure the sender has enough shares
  72. require(
  73. this.balanceOf(msg.sender, _id) >= shares_,
  74. "Not enough shares to sell"
  75. );
  76. /// @dev Make sure the owner can afford this
  77. require(
  78. valueLocked_[_owner] >= shares_ * _pricePerShare,
  79. "Seller does not have enough assets"
  80. );
  81. /// @dev Charge purchaser shares
  82. this.safeTransferFrom(msg.sender, _owner, _id, shares_, "");
  83. /// @dev Send the purchaser the native token
  84. payable(msg.sender).transfer(shares_ * _pricePerShare);
  85. }
  86. }