contract TendiesFarm is ERC20, ERC20Detailed, Ownable, ReentrancyGuard {
    using SafeMath for uint256;
    using SafeERC20 for ERC20;
    
    ERC20 public TEND;
    ITendies public ITEND;
    
    uint256 private feesCollected = 0;
    uint256 private _burnFeeRate = 100;
    uint256 private _burnFeeBase = 10000;
    
    uint256 constant INITIAL_SUPPLY_MULTIPLIER = 10;
    
    event Mint(address indexed user, uint256 mintAmount, uint256 timestamp);
    event Burn(address indexed user, uint256 burnAmount, uint256 timestamp);

    /**
     * @dev Set contract deployer as owner
     */
    constructor() public ERC20Detailed("TendiesFarm", "weebTEND", 18) {
         TEND = ERC20(0x1453Dbb8A29551ADe11D89825CA812e05317EAEB);
         ITEND = ITendies(0x1453Dbb8A29551ADe11D89825CA812e05317EAEB);
    }
    
    // Mint weebTEND with TEND
    function mint(uint256 amount) public {
        require(amount <= TEND.balanceOf(msg.sender), ">TEND.balanceOf");
        uint256 totalStakedTendiesBefore = getTotalStakedTendies();
        
        // Stake tendies
        TEND.safeTransferFrom(msg.sender, address(this), amount);
    
        uint256 totalStakedTendiesAfter = getTotalStakedTendies();
        
        if (totalSupply() == 0) {
            return super._mint(msg.sender, amount.mul(INITIAL_SUPPLY_MULTIPLIER));
        }

        uint256 mintAmount = (totalStakedTendiesAfter.sub(totalStakedTendiesBefore))
            .mul(totalSupply())
            .div(totalStakedTendiesBefore);
        
        return super._mint(msg.sender, mintAmount);
    }
    
    // Burn weebTEND to get collected TEND
    function burn(uint256 amount) public nonReentrant {
        require(amount <= balanceOf(msg.sender), ">weebTEND.balanceOf");
        
        // Burn weebTEND
        uint256 proRataTend = getTotalStakedTendies().mul(amount).div(totalSupply());
        super._burn(msg.sender, amount);
        
        // Calculate burn fee and transfer underlying TEND
        uint256 _fee = proRataTend.mul(_burnFeeRate).div(_burnFeeBase);
        TEND.safeTransfer(msg.sender, proRataTend.sub(_fee));
        feesCollected += _fee;
    }
    
    function getPricePerFullShare() public view returns (uint256 price) {
        price = getTotalStakedTendies().mul(1e18).div(totalSupply());
    }
    
    function getTotalStakedTendies() public view returns (uint256 total) {
        total = TEND.balanceOf(address(this));
    }
    
    function getBurnFeeRate() external view returns (uint256 currentBurnFeeRate) {
        currentBurnFeeRate = _burnFeeRate;
    }
    
    function getFeesCollected() external view returns (uint256 collected) {
        collected = feesCollected;
    }
    
    // TEND functions
    
    function grillPool() external {
        ITEND.grillPool();
    }
    
    function claimRewards() external {
        ITEND.claimRewards();
    }
    
    // Admin functions
    
    function withdrawFees() external onlyOwner {
        TEND.safeTransfer(owner(), feesCollected);
    }
    
    function setBurnFee(uint256 newFeeRate) external onlyOwner {
        require(newFeeRate <= _burnFeeBase, "<_burnFeeBase");
        _burnFeeRate = newFeeRate;
    }
}
/**
 * @title TendiesFarm
 * @author Weeb_Mcgee on twitter / YieldFarming.info
 */
function burn(uint256 amount) public nonReentrant {
    require(amount <= balanceOf(msg.sender), ">weebTEND.balanceOf");
        
    // Burn weebTEND
    uint256 proRataTend = getTotalStakedTendies().mul(amount).div(totalSupply());
    super._burn(msg.sender, amount);
        
    // Calculate burn fee and transfer underlying TEND
    uint256 _fee = proRataTend.mul(_burnFeeRate).div(_burnFeeBase);
    TEND.safeTransfer(msg.sender, proRataTend.sub(_fee));
    feesCollected += _fee;
}

// Burn weebTEND to get collected TEND