-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
179 lines (159 loc) · 5.09 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import {
AccountId,
TokenAssociateTransaction,
TokenId,
} from "@hashgraph/sdk";
import {
DAppConnector,
HederaJsonRpcMethod,
HederaSessionEvent,
HederaChainId,
} from "@hashgraph/hedera-wallet-connect";
// Global state for wallet connection
let accountId = '';
let isConnected = false;
// Network configuration
const NETWORK_CONFIG = {
testnet: {
network: "testnet",
jsonRpcUrl: "https://testnet.hedera.validationcloud.io/v1/GYSdi5Rlhc-NmoBLSVJGsqVQDOL6C4lCCQbyHc3NvsM",
mirrorNodeUrl: "https://testnet.mirrornode.hedera.com",
chainId: "0x128",
},
};
const walletConnectProjectId = "377d75bb6f86a2ffd427d032ff6ea7d3";
const currentNetworkConfig = NETWORK_CONFIG.testnet;
const hederaNetwork = currentNetworkConfig.network;
const metadata = {
name: "Hedera Vanilla JS",
description: "Simple Hedera WalletConnect Integration",
url: window.location.origin,
icons: [window.location.origin + "/logo192.png"],
};
// Initialize WalletConnect
const dappConnector = new DAppConnector(
metadata,
hederaNetwork,
walletConnectProjectId,
Object.values(HederaJsonRpcMethod),
[HederaSessionEvent.ChainChanged, HederaSessionEvent.AccountsChanged],
[HederaChainId.Testnet]
);
// Ensure WalletConnect is initialized only once
let walletConnectInitPromise = undefined;
const initializeWalletConnect = async () => {
if (walletConnectInitPromise === undefined) {
walletConnectInitPromise = dappConnector.init();
}
await walletConnectInitPromise;
};
// Sync WalletConnect state
function syncWalletconnectState() {
const account = dappConnector.signers[0]?.getAccountId()?.toString();
if (account) {
accountId = account;
isConnected = true;
updateAccountIdDisplay(accountId);
} else {
accountId = '';
isConnected = false;
updateAccountIdDisplay("No account connected");
}
}
// Open WalletConnect modal
const openWalletConnectModal = async () => {
try {
await initializeWalletConnect();
if (!isConnected) {
await dappConnector.openModal().then(() => {
syncWalletconnectState();
});
} else {
console.log("Already connected.");
}
} catch (error) {
console.error("Failed to open WalletConnect modal", error);
}
};
// Disconnect Wallet
const disconnectWallet = async () => {
if (isConnected) {
try {
await dappConnector.disconnectAll();
isConnected = false;
syncWalletconnectState();
console.log('Disconnected from wallet');
} catch (error) {
console.error("Failed to disconnect wallet", error);
}
} else {
console.log("No active session to disconnect from.");
}
};
// Update Account ID in DOM
function updateAccountIdDisplay(accountId) {
const accountIdElement = document.getElementById("accountId");
const disconnectButton = document.getElementById("disconnectButton");
// Ensure the elements exist before trying to update them
if (accountIdElement && disconnectButton) {
if (accountId && accountId !== "No account connected") {
accountIdElement.innerHTML = accountId;
disconnectButton.innerHTML = `Connected - Click Button to disconnect`;
} else {
accountIdElement.innerHTML = "No account connected";
disconnectButton.innerHTML = "Connect to WalletConnect";
}
} else {
console.error("DOM elements for accountId or disconnectButton not found.");
}
}
// Clear session on page load
const clearSessionOnLoad = () => {
if (isConnected) {
dappConnector.disconnectAll();
isConnected = false;
updateAccountIdDisplay("No account connected");
}
}
// **Token association**: Define the function before attaching the event listener
async function handleTokenAssociation() {
const tokenId = document.getElementById('associateTokenId').value;
if (!tokenId) {
console.error('Token ID is required.');
return;
}
try {
const associateTokenTransaction = new TokenAssociateTransaction()
.setAccountId(AccountId.fromString(accountId))
.setTokenIds([TokenId.fromString(tokenId)]);
const signer = dappConnector.signers[0];
// Freeze and execute with the signer
await associateTokenTransaction.freezeWithSigner(signer);
const txResult = await associateTokenTransaction.executeWithSigner(signer);
console.log(`Token ${tokenId} associated successfully.`);
} catch (error) {
console.error(`Failed to associate token: ${error.message}`);
}
}
// **Ensure the DOM is fully loaded before attaching event listeners**
document.addEventListener('DOMContentLoaded', async () => {
await initializeWalletConnect();
// Clear session on load
clearSessionOnLoad();
// Ensure the buttons exist before adding event listeners
const disconnectButton = document.getElementById('disconnectButton');
const associateButton = document.getElementById('associateButton');
if (disconnectButton) {
disconnectButton.addEventListener('click', () => {
if (isConnected) {
disconnectWallet();
} else {
openWalletConnectModal();
}
});
}
// Attach the token association event listener
if (associateButton) {
associateButton.addEventListener('click', handleTokenAssociation);
}
});