Skip to content

Commit

Permalink
Create WalletService.js
Browse files Browse the repository at this point in the history
  • Loading branch information
miladsoft committed Nov 10, 2024
1 parent 06450bd commit 528ccf1
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/services/WalletService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { TonConnectUI } from "@tonconnect/ui";

class WalletService {
constructor() {
this.tonConnect = null;
this.isConnected = false;

// Initialize only if localStorage and window are available
if (typeof window !== "undefined" && typeof localStorage !== "undefined") {
const storedConnectionStatus = JSON.parse(localStorage.getItem("isConnected"));
this.isConnected = storedConnectionStatus || false;

try {
this.tonConnect = new TonConnectUI({
manifestUrl: "https://seniorblockchain.io/tonconnect-manifest.json",
});
} catch (error) {
console.error("TonConnectUI initialization failed:", error);
}
}
}

async connectWallet() {
if (!this.tonConnect || this.isConnected) return;

try {
await this.tonConnect.connectWallet();
this.isConnected = true;
localStorage.setItem("isConnected", true);
} catch (error) {
console.error("Wallet connection failed:", error);
}
}

disconnectWallet() {
if (!this.tonConnect || !this.isConnected) return;

this.tonConnect.disconnect();
this.isConnected = false;
localStorage.setItem("isConnected", false);
}

getConnectionStatus() {
return this.isConnected;
}
}

export default new WalletService();

0 comments on commit 528ccf1

Please sign in to comment.