1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 | 117× 117× 117× 117× 117× 117× 117× 117× 117× 117× 117× 117× 117× 117× 117× 117× 23× 22× 21× 20× 19× 18× 17× 16× 16× 16× 16× 16× 16× 16× 16× 16× 16× 15× 15× 1× 1× 1× 1× 1× 1× 16× 16× 15× 16× 16× 16× 16× 16× 5× 4× 4× 3× 3× 1× 3× 3× 2× 3× 3× 2× 3× 3× 3× 3× 3× 3× 3× 3× 3× 2× 2× 1× 2× 2× 2× 6× 5× 5× 5× 5× 5× 5× 5× 5× 5× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 1× 7× 7× 7× 7× 7× 7× 2× 1× 4× 3× 20× 20× 20× 1× 19× 19× 19× 2× 1× 20× 20× 20× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 2× 8× 7× 6× 5× 4× 4× 4× 4× 4× 4× 4× 4× 25× 25× 25× 25× 25× 1× 1× 1× 1× 1× 39× 39× 1× | // SPDX-License-Identifier: MIT pragma solidity 0.8.16; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; import "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol"; import "@openzeppelin/contracts/access/Ownable.sol"; import "@openzeppelin/contracts/token/ERC20/extensions/draft-ERC20Permit.sol"; import "@openzeppelin/contracts/utils/Context.sol"; import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol"; import "@uniswap/v3-periphery/contracts/libraries/TransferHelper.sol"; import "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol"; import "@uniswap/v3-periphery/contracts/interfaces/IQuoter.sol"; import "./interfaces/IKeyProtocolVariables.sol"; import "./interfaces/ILandXNFT.sol"; import "./interfaces/IxTokenRouter.sol"; import "./interfaces/IOraclePrices.sol"; import "./interfaces/IRentFoundation.sol"; import "./interfaces/IcToken.sol"; import "./interfaces/IxToken.sol"; import "./interfaces/ILNDX.sol"; //xToken NFT in = shards. xToken in = NFT //1 xToken = (tillableArea * cropShare) / 10000 contract XToken is Context, ERC20Permit, ERC20Burnable, Ownable, ERC1155Holder, IxToken { struct Stake { uint256 amount; uint256 startTime; } string public crop; address public xBasketContract; address public lndx; address public usdc; ILandXNFT public landXNFT; //address of landXNFT IxTokenRouter public xTokenRouter; // address of xTokenRouter IRentFoundation public rentFoundation; IOraclePrices public oraclePrices; IKeyProtocolVariables public keyProtocolValues; ISwapRouter public uniswapRouter; IQuoter public quoter; //only the initial owner of the NFT can redeem it mapping(uint256 => address) public initialOwner; mapping(address => uint256) public Claimed; mapping(address => Stake) public Staked; mapping(address => uint256) public Yield; mapping(uint256 => uint256) public SecurityDepositedAmount; Stake public TotalStaked; uint256 public TotalYield; Stake public NonStakedTokens; uint256 public NonDistributedYield; event Sharded(uint256 nftID, uint256 amount, string name); event BuyOut(uint256 nftID, uint256 amount, string name); event TokenStaked(address staker, uint256 amount); event Unstaked(address staker, uint256 amount); event YieldClaimed(address staker, uint256 amount); constructor( address _landXNFT, address _lndx, address _usdc, address _rentFoundation, address _xTokenRouter, address _keyProtocolValues, address _oraclePrices, string memory _crop// "SOY, CORN etc", ) ERC20Permit(string(abi.encodePacked("x", _crop))) ERC20("LandX xToken", string(abi.encodePacked("x", _crop))) { Erequire(_landXNFT != address(0), "zero address is not allowed"); Erequire(_lndx != address(0), "zero address is not allowed"); Erequire(_usdc != address(0), "zero address is not allowed"); Erequire(_rentFoundation != address(0), "zero address is not allowed"); Erequire(_xTokenRouter != address(0), "zero address is not allowed"); Erequire(_keyProtocolValues != address(0), "zero address is not allowed"); landXNFT = ILandXNFT(_landXNFT); crop = _crop; lndx = _lndx; usdc = _usdc; rentFoundation = IRentFoundation(_rentFoundation); xTokenRouter = IxTokenRouter(_xTokenRouter); keyProtocolValues = IKeyProtocolVariables(_keyProtocolValues); oraclePrices = IOraclePrices(_oraclePrices); quoter = IQuoter(0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6); uniswapRouter = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564); } //deposits an NFT to get shards equivalence function getShards(uint256 _id) external { require( landXNFT.tillableArea(_id) > 0, "this NFT has no land area set" ); require(landXNFT.cropShare(_id) > 0, "this NFT has no crop share set"); require( landXNFT.initialOwner(_id) == msg.sender, "only initial owner can shard" ); require( keccak256(abi.encodePacked(landXNFT.crop(_id))) == keccak256(abi.encodePacked(crop)), "wrong crop" ); require( xTokenRouter.getXToken(crop) == address(this), "tokens are not set for this crop" ); require( landXNFT.balanceOf(msg.sender, _id) > 0, "you must own this NFT" ); require( rentFoundation.initialRentApplied(_id) == false, "rent was already applied" ); initialOwner[_id] = msg.sender; //set the initial owner //transfers the nft. must have setApprovalForAll landXNFT.safeTransferFrom(msg.sender, address(this), _id, 1, ""); uint256 shards = landXNFT.tillableArea(_id) * (landXNFT.cropShare(_id)) * (1e6) / 10000; // xTokens has 6 decimal _mint(address(this), shards); uint256 fee = _calcFee(shards); uint256 validatorFee = _validatorFee(_id, shards); uint256 annualRent = getAnnualRentAmount(_id); uint256 xTokensAnnualRent = ((annualRent * oraclePrices.prices(crop)) / oraclePrices.getXTokenPrice(address(this))) * 1e3; // oraclePrices.prices returns price per megatonne, to to get value per KG we multiply by 10 ** 3 uint256 toSecurityDepositsAmount = (xTokensAnnualRent / 12) * keyProtocolValues.securityDepositMonths(); if (keyProtocolValues.preLaunch()) { _transfer( address(this), keyProtocolValues.landxOperationalWallet(), xTokensAnnualRent ); _transfer( address(this), keyProtocolValues.landxOperationalWallet(), fee ); } else { // transfer xTokens to securityDeposit uint256 usdcFee = _convertToUsdc(fee); uint256 usdcAnnualRent = _convertToUsdc(xTokensAnnualRent); uint256 toHedgeFund = (usdcAnnualRent * keyProtocolValues.hedgeFundAllocation()) / 10000; ERC20(usdc).transfer( keyProtocolValues.hedgeFundWallet(), toHedgeFund ); ERC20(usdc).transfer( address(rentFoundation), usdcAnnualRent - toHedgeFund ); _feeDistributor(usdcFee); } _transfer( address(this), keyProtocolValues.xTokensSecurityWallet(), toSecurityDepositsAmount ); if (landXNFT.validator(_id) != address(0)) { _transfer(address(this), landXNFT.validator(_id), validatorFee); } _transfer( address(this), msg.sender, shards - fee - xTokensAnnualRent - toSecurityDepositsAmount - validatorFee ); SecurityDepositedAmount[_id] = toSecurityDepositsAmount; rentFoundation.payInitialRent(_id, annualRent); _calculateNonDistributedYield(); emit Sharded(_id, shards, symbol()); } //returns the NFT after if caller has enough xTokens to burn. requires allowance! function getTheNFT(uint256 _id) external { require( initialOwner[_id] == msg.sender, "only initial owner can redeem the NFT" ); uint256 remainingRentUSDC; remainingRentUSDC = rentFoundation.buyOut(_id); uint256 securityDepositedAmount = SecurityDepositedAmount[_id]; if (rentFoundation.spentSecurityDeposit(_id)) { securityDepositedAmount = 0; } uint256 xTokens = 0; if (remainingRentUSDC > 0) { xTokens = _convertToXToken(remainingRentUSDC); } uint256 shards = landXNFT.tillableArea(_id) * landXNFT.cropShare(_id) * 1e6 / 10000; // xToken has 6 decimals //approval required for xTokensSecurityWalle if (securityDepositedAmount > 0) { _burn(keyProtocolValues.xTokensSecurityWallet(), securityDepositedAmount); } _burn(address(this), xTokens); //burns shards! burn(shards - securityDepositedAmount - xTokens); _calculateNonDistributedYield(); //transfer the NFTs landXNFT.safeTransferFrom(address(this), msg.sender, _id, 1, ""); emit BuyOut(_id, shards, symbol()); } function getTheNFTPreview(uint256 _id) public view returns(uint256) { bool allow; uint256 remainingRentUSDC; (allow, remainingRentUSDC) = rentFoundation.buyOutPreview(_id); require(allow, "there is a debt"); uint256 securityDepositedAmount = SecurityDepositedAmount[_id]; if (rentFoundation.spentSecurityDeposit(_id)) { securityDepositedAmount = 0; } uint256 xTokens = remainingRentUSDC * 1e6 / oraclePrices.getXTokenPrice(address(this)); //xToken has 6 decimals uint256 shards = landXNFT.tillableArea(_id) * landXNFT.cropShare(_id) * 1e6 / 10000; // xToken has 6 decimals return (shards - securityDepositedAmount - xTokens); } function stake(uint256 amount) public { _transfer(msg.sender, address(this), amount); uint256 yield = _calculateYield(Staked[msg.sender]); TotalYield += _calculateTotalYield(); Yield[msg.sender] += yield; Staked[msg.sender].startTime = block.timestamp; TotalStaked.startTime = block.timestamp; TotalStaked.amount += amount; Staked[msg.sender].amount += amount; _calculateNonDistributedYield(); emit TokenStaked(msg.sender, amount); } function unstake(uint256 amount) public { Erequire(Staked[msg.sender].amount >= amount); _transfer(address(this), msg.sender, amount); claim(); Staked[msg.sender].amount -= amount; TotalStaked.amount -= amount; _calculateNonDistributedYield(); emit Unstaked(msg.sender, amount); } function claim() public { address cToken = xTokenRouter.getCToken(crop); uint256 yield = _calculateYield(Staked[msg.sender]); TotalYield = TotalYield + _calculateTotalYield() - (Yield[msg.sender] + yield); IcToken(cToken).mint(msg.sender, (Yield[msg.sender] + yield)); Claimed[msg.sender] += (Yield[msg.sender] + yield); Staked[msg.sender].startTime = block.timestamp; TotalStaked.startTime = block.timestamp; Yield[msg.sender] = 0; emit YieldClaimed(msg.sender, yield); } function _feeDistributor(uint256 _fee) internal { uint256 lndxFee = (_fee * keyProtocolValues.lndxHoldersPercentage()) / 10000; uint256 operationalFee = (_fee * keyProtocolValues.landXOperationsPercentage()) / 10000; ERC20(usdc).transfer(lndx, lndxFee); ILNDX(lndx).feeToDistribute(lndxFee); ERC20(usdc).transfer( keyProtocolValues.landxOperationalWallet(), operationalFee ); ERC20(usdc).transfer( keyProtocolValues.landxChoiceWallet(), _fee - lndxFee - operationalFee ); } function availableToClaim(address account) public view returns (uint256) { return Yield[account] + _calculateYield(Staked[account]); } function totalAvailableToClaim() public view returns (uint256) { return TotalYield + _calculateTotalYield(); } // calculate cTokanes amount generated since amount was staked function _calculateYield(Stake storage s) internal view returns (uint256) { uint256 elapsedSeconds = block.timestamp - s.startTime; uint256 delimeter = 365 * 1 days; return (s.amount * elapsedSeconds) / delimeter; } function _calculateTotalYield() internal view returns (uint256) { uint256 elapsedSeconds = block.timestamp - TotalStaked.startTime; uint256 delimeter = 365 * 1 days; return (TotalStaked.amount * elapsedSeconds) / delimeter; } function xBasketTransfer(address _from, uint256 amount) external { require(msg.sender == xBasketContract, "not authorized"); _transfer(_from, xBasketContract, amount); } // change the address of xBasket. function changeXBasketAddress(address _newAddress) public onlyOwner { require(_newAddress != address(0), "zero address is not allowed"); xBasketContract = _newAddress; } function _calcFee(uint256 amount) internal view returns (uint256) { uint256 fee = keyProtocolValues.xTokenMintFee(); return (amount * fee) / 10000; } function _validatorFee(uint256 tokenID, uint256 amount) internal view returns(uint256) { if (landXNFT.validator(tokenID) == address(0)) { return 0; } uint256 validatorFee = landXNFT.validatorFee(tokenID); Iif (validatorFee == 0) { return 0; } return (amount* validatorFee) / 10000; } function setXTokenRouter(address _router) public onlyOwner { require(_router != address(0), "zero address is not allowed"); xTokenRouter = IxTokenRouter(_router); } // return annual rent amount in kg function getAnnualRentAmount(uint256 tokenID) public view returns (uint256) { uint256 rent = landXNFT.cropShare(tokenID); uint256 area = landXNFT.tillableArea(tokenID); return (rent * area) / 10000; } function quoteAmountOut(uint _amount) public returns (uint) { uint amountOut = quoter.quoteExactInputSingle(address(this), usdc, 3000, _amount, 0); return amountOut; } function _convertToXToken(uint256 amount) internal returns (uint256) { uint256 slippage = keyProtocolValues.buyXTokenSlippage(); uint256 predictedAmountOut = quoter.quoteExactInputSingle(usdc, address(this), 3000, amount, 0); uint256 minAmountOut = predictedAmountOut * 10000 / (10000 + slippage); TransferHelper.safeApprove(usdc, address(uniswapRouter), amount); ISwapRouter.ExactInputSingleParams memory params = ISwapRouter .ExactInputSingleParams( usdc, address(this), 3000, address(this), block.timestamp + 15, amount, minAmountOut, 0 ); return uniswapRouter.exactInputSingle(params); } function _convertToUsdc(uint256 amount) internal returns (uint256) { uint256 slippage = keyProtocolValues.sellXTokenSlippage(); uint256 predictedAmountOut = quoteAmountOut(amount); uint256 minAmountOut = predictedAmountOut * 10000 / (10000 + slippage); TransferHelper.safeApprove( address(this), address(uniswapRouter), amount ); ISwapRouter.ExactInputSingleParams memory params = ISwapRouter .ExactInputSingleParams( address(this), usdc, 3000, address(this), block.timestamp + 15, amount, minAmountOut, 0 ); return uniswapRouter.exactInputSingle(params); } function preview(uint256 id) public view returns ( uint256, uint256, uint256, uint256 ) { require(landXNFT.tillableArea(id) > 0, "this NFT has no land area set"); require(landXNFT.cropShare(id) > 0, "this NFT has no crop share set"); require( keccak256(abi.encodePacked(landXNFT.crop(id))) == keccak256(abi.encodePacked(crop)), "wrong crop" ); require( xTokenRouter.getXToken(landXNFT.crop(id)) == address(this), "Unable to shard this NFT" ); uint256 shards = (landXNFT.tillableArea(id) * landXNFT.cropShare(id) * 1e6) / 10000; uint256 annualRent = getAnnualRentAmount(id); uint256 xTokensAnnualRent = ((annualRent * oraclePrices.prices(crop)) / oraclePrices.getXTokenPrice(xTokenRouter.getXToken(crop))) * 1e3; uint256 toSecurityDepositsAmount = (xTokensAnnualRent / 12) * keyProtocolValues.securityDepositMonths(); uint256 fee = _calcFee(shards); uint256 validatorFee = _validatorFee(id, shards); uint256 toBeReceived = shards - fee - xTokensAnnualRent - toSecurityDepositsAmount - validatorFee; return ( shards, fee, xTokensAnnualRent + toSecurityDepositsAmount, toBeReceived ); } function _calculateNonDistributedYield() internal { uint256 elapsedSeconds = block.timestamp - NonStakedTokens.startTime; uint256 delimeter = 365 * 1 days; NonDistributedYield += NonStakedTokens.amount * elapsedSeconds / delimeter; NonStakedTokens.amount = totalSupply() - TotalStaked.amount; NonStakedTokens.startTime = block.timestamp; } function previewNonDistributedYield() external view returns(uint256) { uint256 elapsedSeconds = block.timestamp - NonStakedTokens.startTime; uint256 delimeter = 365 * 1 days; return NonDistributedYield + NonStakedTokens.amount * elapsedSeconds / delimeter; } // returns cTokens amount function getNonDistributedYield() external returns(uint256) { Irequire(msg.sender == address(rentFoundation), "only rentFoundation can take it"); _calculateNonDistributedYield(); uint256 amount = NonDistributedYield; NonDistributedYield = 0; return amount; } function renounceOwnership() public view override EonlyOwner { revert ("can 't renounceOwnership here"); } // for test only purposes function updateUniswapContracts(address _quoter, address _uniswapRouter) public EonlyOwner { quoter = IQuoter(_quoter); uniswapRouter = ISwapRouter(_uniswapRouter); } function decimals() public pure override returns (uint8) { return 6; } } |