-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |