// SPDX-License-Identifier: UNLICENSED pragma solidity ^0.8.0; contract RP { uint256 soldAmount = 0; uint256 decimals = 18; uint256 checkPoint = 1000 * 10^decimals; // 1k uint256 nextCheckPoint = checkPoint * 10; // 10k uint256 price = 0.2 ether; function sellRp(uint256 _amount) public returns (uint256) { uint256 payout; uint256 left; // 0. balance check of RP amount // 0-0. check global limit on RP sellable // 0-1. check RP sellable limit on user // 1. calculate maticAmount for _amount if(checkPoint >= soldAmount + _amount) { payout = _amount * price / 10^decimals; soldAmount += _amount; } else { left = _amount; // need bounds to prevent exceeding block gas limit while(left > 0) { uint256 discrepancy = checkPoint - soldAmount; if(discrepancy > left) { payout += left * price / 10^decimals; soldAmount += left; left = 0; break; } payout += discrepancy * price / 10^decimals; left -= discrepancy; soldAmount += discrepancy; checkPoint = nextCheckPoint; nextCheckPoint = nextCheckPoint * 2; price = price / 2; } } // 3. withdraw RP from user to RP using safeTransferFrom() // 4. burn RP tokens // 5. transfer WMATIC to msg.sender return payout; } // onlyOwner modifier will be added function setPrice(uint256 _price) public { // checks if needed price = _price; } }
0.4.18