0% found this document useful (0 votes)
80 views1 page

Frontend Crypto Presale

Frontend Crypto Presale

Uploaded by

Manu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views1 page

Frontend Crypto Presale

Frontend Crypto Presale

Uploaded by

Manu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

<!

DOCTYPE html>
<html>
<head>
<title>Token Sale</title>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<h1>Token Sale</h1>

<label for="numberOfTokens">Number of Tokens:</label>


<input type="number" id="numberOfTokens" min="1" value="1" />
<button onclick="buyTokens()">Buy Tokens</button>

<script>
// Contract and account variables
const contractAddress = 'CONTRACT_ADDRESS';
const tokenPrice = 1; // Set the token price in Ether
const adminAddress = 'ADMIN_ADDRESS';
const provider = 'https://bsc-dataseed.binance.org/'; // BSC RPC Provider

// Function to buy tokens


async function buyTokens() {
const numberOfTokens = document.getElementById('numberOfTokens').value;
const amount = numberOfTokens * tokenPrice;

// Check if the user's wallet supports the required methods


if (window.ethereum && window.ethereum.isMetaMask) {
try {
// Request permission to access the user's accounts
await ethereum.request({ method: 'eth_requestAccounts' });

// Get the current provider


const web3Provider = new
ethers.providers.JsonRpcProvider(provider);

// Connect to the contract using the contract address and ABI


const tokenContract = new ethers.Contract(contractAddress,
TOKEN_ABI, web3Provider);
const signer = web3Provider.getSigner();

// Perform the token purchase


const transaction = await
tokenContract.connect(signer).buyTokens(numberOfTokens, { value: amount });
await transaction.wait();

// Display success message


alert('Tokens purchased successfully!');
} catch (error) {
console.error(error);
alert('An error occurred while purchasing tokens.');
}
} else {
alert('Please install MetaMask and connect to Binance Smart
Chain.');
}
}
</script>
</body>
</html>

You might also like