// File: MasterDogePresale1.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./MasterDogeToken1.sol";

contract MasterDogePresale {
    MasterDogeToken public token;
    address payable public beneficiary;
    uint256 public conversionRate;

    event TokensPurchased(address indexed buyer, uint256 amount);

    constructor(address _tokenAddress, address payable _beneficiary, uint256 _conversionRate) {
        token = MasterDogeToken(_tokenAddress);
        beneficiary = _beneficiary;
        conversionRate = _conversionRate;
    }

    // Fallback function to receive Ether and send tokens
    receive() external payable {
        uint256 tokenAmount = msg.value * conversionRate;
        require(token.balanceOf(address(this)) >= tokenAmount, "Not enough tokens available");

        // Transfer tokens to the investor
        token.transfer(msg.sender, tokenAmount);
        emit TokensPurchased(msg.sender, tokenAmount);

        // Transfer received funds to the beneficiary address
        beneficiary.transfer(msg.value);
    }

    // Withdraw remaining funds in case anything fails
    function withdrawFunds() external {
        require(msg.sender == beneficiary, "Only the beneficiary can withdraw funds");
        beneficiary.transfer(address(this).balance);
    }

}

// File: MasterDogeToken1.sol



// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract MasterDogeToken {
    string public name = "Master Doge";
    string public symbol = "MASTERDOGE";
    uint8 public decimals = 18;
    uint256 public totalSupply;

    mapping(address => uint256) public balanceOf;

    event Transfer(address indexed from, address indexed to, uint256 value);

    constructor() {
        totalSupply = 2 * 10**12 * 10**18;
        balanceOf[msg.sender] = totalSupply;
        emit Transfer(address(0), msg.sender, totalSupply);
    }

    function transfer(address _to, uint256 _value) public returns (bool success) {
        require(balanceOf[msg.sender] >= _value, "Insufficient balance");
        require(_to != address(0), "Transfer to the zero address");

        balanceOf[msg.sender] -= _value;
        balanceOf[_to] += _value;
        emit Transfer(msg.sender, _to, _value);
        return true;
    }
}