Skip to content

Commit

Permalink
Update index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
mirak-oracle authored Jul 15, 2024
1 parent 0d06298 commit 4962865
Showing 1 changed file with 77 additions and 2 deletions.
79 changes: 77 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@
<meta charset="UTF-8">
<title>CosmicCoin Mining Airdrop</title>


<script type="module">
// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/10.12.3/firebase-app.js";
import { getFirestore, doc, setDoc, getDoc } from "https://www.gstatic.com/firebasejs/10.12.3/firebase-firestore.js";
import { getAuth, signInAnonymously } from "https://www.gstatic.com/firebasejs/10.12.3/firebase-auth.js";
import { getFirestore, doc, setDoc, getDoc, updateDoc } from "https://www.gstatic.com/firebasejs/10.12.3/firebase-firestore.js";

// Your web app's Firebase configuration
const firebaseConfig = {
apiKey: "",
apiKey: "AIzaSyB_ALVZRrzoiCsO1PUbSDDNHClr-bjdTMA",
authDomain: "cosmiccoin-galaxy-airdrop.firebaseapp.com",
projectId: "cosmiccoin-galaxy-airdrop",
storageBucket: "cosmiccoin-galaxy-airdrop.appspot.com",
Expand All @@ -23,8 +25,81 @@

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const auth = getAuth();
const db = getFirestore(app);

// Sign in anonymously
signInAnonymously(auth)
.then(() => {
console.log('Signed in anonymously');
})
.catch((error) => {
console.error('Error signing in anonymously', error);
});

function generateUserID() {
const userID = 'user-' + Math.random().toString(36).substr(2, 9);
localStorage.setItem('userID', userID);
alert(`Your user ID: ${userID}`);
return userID;
}

function getUserID() {
let userID = localStorage.getItem('userID');
if (!userID) {
userID = generateUserID();
}
return userID;
}

async function connectWallet() {
if (window.ethereum) {
const accounts = await window.ethereum.request({ method: 'eth_requestAccounts' });
const userID = accounts[0];
localStorage.setItem('userID', userID);
alert('Wallet connected');
} else {
alert('Please install MetaMask');
}
}

async function transferBalanceToServer() {
const userID = getUserID();
const balance = localStorage.getItem('cosmicCoinBalance');
if (!balance || parseFloat(balance) <= 0) {
alert('No balance to transfer');
return;
}

const userDoc = doc(db, "users", userID);
const userSnapshot = await getDoc(userDoc);

if (userSnapshot.exists()) {
await updateDoc(userDoc, {
balance: parseFloat(userSnapshot.data().balance) + parseFloat(balance)
});
} else {
await setDoc(userDoc, {
balance: parseFloat(balance)
});
}

alert(`Balance of ${balance} CosmicCoin transferred to server`);
localStorage.setItem('cosmicCoinBalance', '0');
}

document.addEventListener('DOMContentLoaded', () => {
document.getElementById('connectWalletButton').addEventListener('click', connectWallet);
document.getElementById('transferToServerButton').addEventListener('click', transferBalanceToServer);
});
</script>
</head>
<body>
<button id="connectWalletButton">Connect Wallet</button>
<button id="transferToServerButton">Transfer Balance to Server</button>
</body>
</html>



<!-- Google tag (gtag.js) -->
Expand Down

0 comments on commit 4962865

Please sign in to comment.